View Javadoc
1   /*
2    * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.internal.storage.file;
12  
13  import java.io.File;
14  import java.io.FileNotFoundException;
15  import java.io.IOException;
16  import java.util.ArrayList;
17  import java.util.Collections;
18  import java.util.List;
19  
20  import org.eclipse.jgit.lib.Constants;
21  import org.eclipse.jgit.lib.ReflogEntry;
22  import org.eclipse.jgit.lib.ReflogReader;
23  import org.eclipse.jgit.lib.Repository;
24  import org.eclipse.jgit.util.IO;
25  import org.eclipse.jgit.util.RawParseUtils;
26  
27  /**
28   * Utility for reading reflog entries
29   */
30  class ReflogReaderImpl implements ReflogReader {
31  	private File logName;
32  
33  	/**
34  	 * @param db
35  	 * @param refname
36  	 */
37  	ReflogReaderImpl(Repository db, String refname) {
38  		logName = new File(db.getDirectory(), Constants.LOGS + '/' + refname);
39  	}
40  
41  	/* (non-Javadoc)
42  	 * @see org.eclipse.jgit.internal.storage.file.ReflogReaader#getLastEntry()
43  	 */
44  	/** {@inheritDoc} */
45  	@Override
46  	public ReflogEntry getLastEntry() throws IOException {
47  		return getReverseEntry(0);
48  	}
49  
50  	/* (non-Javadoc)
51  	 * @see org.eclipse.jgit.internal.storage.file.ReflogReaader#getReverseEntries()
52  	 */
53  	/** {@inheritDoc} */
54  	@Override
55  	public List<ReflogEntry> getReverseEntries() throws IOException {
56  		return getReverseEntries(Integer.MAX_VALUE);
57  	}
58  
59  	/* (non-Javadoc)
60  	 * @see org.eclipse.jgit.internal.storage.file.ReflogReaader#getReverseEntry(int)
61  	 */
62  	/** {@inheritDoc} */
63  	@Override
64  	public ReflogEntry getReverseEntry(int number) throws IOException {
65  		if (number < 0)
66  			throw new IllegalArgumentException();
67  
68  		final byte[] log;
69  		try {
70  			log = IO.readFully(logName);
71  		} catch (FileNotFoundException e) {
72  			if (logName.exists()) {
73  				throw e;
74  			}
75  			return null;
76  		}
77  
78  		int rs = RawParseUtils.prevLF(log, log.length);
79  		int current = 0;
80  		while (rs >= 0) {
81  			rs = RawParseUtils.prevLF(log, rs);
82  			if (number == current)
83  				return new ReflogEntryImpl(log, rs < 0 ? 0 : rs + 2);
84  			current++;
85  		}
86  		return null;
87  	}
88  
89  	/* (non-Javadoc)
90  	 * @see org.eclipse.jgit.internal.storage.file.ReflogReaader#getReverseEntries(int)
91  	 */
92  	/** {@inheritDoc} */
93  	@Override
94  	public List<ReflogEntry> getReverseEntries(int max) throws IOException {
95  		final byte[] log;
96  		try {
97  			log = IO.readFully(logName);
98  		} catch (FileNotFoundException e) {
99  			if (logName.exists()) {
100 				throw e;
101 			}
102 			return Collections.emptyList();
103 		}
104 
105 		int rs = RawParseUtils.prevLF(log, log.length);
106 		List<ReflogEntry> ret = new ArrayList<>();
107 		while (rs >= 0 && max-- > 0) {
108 			rs = RawParseUtils.prevLF(log, rs);
109 			ReflogEntry entry = new ReflogEntryImpl(log, rs < 0 ? 0 : rs + 2);
110 			ret.add(entry);
111 		}
112 		return ret;
113 	}
114 }