View Javadoc
1   /*
2    * Copyright (C) 2011, Chris Aniszczyk <caniszczyk@gmail.com>
3    * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.internal.storage.file;
13  
14  import java.io.Serializable;
15  
16  import org.eclipse.jgit.internal.JGitText;
17  import org.eclipse.jgit.lib.CheckoutEntry;
18  import org.eclipse.jgit.lib.Constants;
19  import org.eclipse.jgit.lib.ObjectId;
20  import org.eclipse.jgit.lib.PersonIdent;
21  import org.eclipse.jgit.lib.ReflogEntry;
22  import org.eclipse.jgit.util.RawParseUtils;
23  
24  /**
25   * Parsed reflog entry
26   */
27  public class ReflogEntryImpl implements Serializable, ReflogEntry {
28  	private static final long serialVersionUID = 1L;
29  
30  	private ObjectId oldId;
31  
32  	private ObjectId newId;
33  
34  	private PersonIdent who;
35  
36  	private String comment;
37  
38  	ReflogEntryImpl(byte[] raw, int pos) {
39  		oldId = ObjectId.fromString(raw, pos);
40  		pos += Constants.OBJECT_ID_STRING_LENGTH;
41  		if (raw[pos++] != ' ')
42  			throw new IllegalArgumentException(
43  					JGitText.get().rawLogMessageDoesNotParseAsLogEntry);
44  		newId = ObjectId.fromString(raw, pos);
45  		pos += Constants.OBJECT_ID_STRING_LENGTH;
46  		if (raw[pos++] != ' ') {
47  			throw new IllegalArgumentException(
48  					JGitText.get().rawLogMessageDoesNotParseAsLogEntry);
49  		}
50  		who = RawParseUtils.parsePersonIdentOnly(raw, pos);
51  		int p0 = RawParseUtils.next(raw, pos, '\t');
52  		if (p0 >= raw.length)
53  			comment = ""; // personident has no \t, no comment present //$NON-NLS-1$
54  		else {
55  			int p1 = RawParseUtils.nextLF(raw, p0);
56  			comment = p1 > p0 ? RawParseUtils.decode(raw, p0, p1 - 1) : ""; //$NON-NLS-1$
57  		}
58  	}
59  
60  	/* (non-Javadoc)
61  	 * @see org.eclipse.jgit.internal.storage.file.ReflogEntry#getOldId()
62  	 */
63  	/** {@inheritDoc} */
64  	@Override
65  	public ObjectId getOldId() {
66  		return oldId;
67  	}
68  
69  	/* (non-Javadoc)
70  	 * @see org.eclipse.jgit.internal.storage.file.ReflogEntry#getNewId()
71  	 */
72  	/** {@inheritDoc} */
73  	@Override
74  	public ObjectId getNewId() {
75  		return newId;
76  	}
77  
78  	/* (non-Javadoc)
79  	 * @see org.eclipse.jgit.internal.storage.file.ReflogEntry#getWho()
80  	 */
81  	/** {@inheritDoc} */
82  	@Override
83  	public PersonIdent getWho() {
84  		return who;
85  	}
86  
87  	/* (non-Javadoc)
88  	 * @see org.eclipse.jgit.internal.storage.file.ReflogEntry#getComment()
89  	 */
90  	/** {@inheritDoc} */
91  	@Override
92  	public String getComment() {
93  		return comment;
94  	}
95  
96  	/** {@inheritDoc} */
97  	@SuppressWarnings("nls")
98  	@Override
99  	public String toString() {
100 		return "Entry[" + oldId.name() + ", " + newId.name() + ", " + getWho()
101 				+ ", " + getComment() + "]";
102 	}
103 
104 	/* (non-Javadoc)
105 	 * @see org.eclipse.jgit.internal.storage.file.ReflogEntry#parseCheckout()
106 	 */
107 	/** {@inheritDoc} */
108 	@Override
109 	public CheckoutEntry parseCheckout() {
110 		if (getComment().startsWith(CheckoutEntryImpl.CHECKOUT_MOVING_FROM)) {
111 			return new CheckoutEntryImpl(this);
112 		}
113 		return null;
114 	}
115 }