View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.revwalk;
45  
46  import java.nio.charset.Charset;
47  
48  import org.eclipse.jgit.util.RawParseUtils;
49  
50  /**
51   * Single line at the end of a message, such as a "Signed-off-by: someone".
52   * <p>
53   * These footer lines tend to be used to represent additional information about
54   * a commit, like the path it followed through reviewers before finally being
55   * accepted into the project's main repository as an immutable commit.
56   *
57   * @see RevCommit#getFooterLines()
58   */
59  public final class FooterLine {
60  	private final byte[] buffer;
61  
62  	private final Charset enc;
63  
64  	private final int keyStart;
65  
66  	private final int keyEnd;
67  
68  	private final int valStart;
69  
70  	private final int valEnd;
71  
72  	FooterLine(final byte[] b, final Charset e, final int ks, final int ke,
73  			final int vs, final int ve) {
74  		buffer = b;
75  		enc = e;
76  		keyStart = ks;
77  		keyEnd = ke;
78  		valStart = vs;
79  		valEnd = ve;
80  	}
81  
82  	/**
83  	 * Whether keys match
84  	 *
85  	 * @param key
86  	 *            key to test this line's key name against.
87  	 * @return true if {@code key.getName().equalsIgnorecase(getKey())}.
88  	 */
89  	public boolean matches(FooterKey key) {
90  		final byte[] kRaw = key.raw;
91  		final int len = kRaw.length;
92  		int bPtr = keyStart;
93  		if (keyEnd - bPtr != len)
94  			return false;
95  		for (int kPtr = 0; kPtr < len;) {
96  			byte b = buffer[bPtr++];
97  			if ('A' <= b && b <= 'Z')
98  				b += 'a' - 'A';
99  			if (b != kRaw[kPtr++])
100 				return false;
101 		}
102 		return true;
103 	}
104 
105 	/**
106 	 * Get key name of this footer.
107 	 *
108 	 * @return key name of this footer; that is the text before the ":" on the
109 	 *         line footer's line. The text is decoded according to the commit's
110 	 *         specified (or assumed) character encoding.
111 	 */
112 	public String getKey() {
113 		return RawParseUtils.decode(enc, buffer, keyStart, keyEnd);
114 	}
115 
116 	/**
117 	 * Get value of this footer.
118 	 *
119 	 * @return value of this footer; that is the text after the ":" and any
120 	 *         leading whitespace has been skipped. May be the empty string if
121 	 *         the footer has no value (line ended with ":"). The text is
122 	 *         decoded according to the commit's specified (or assumed)
123 	 *         character encoding.
124 	 */
125 	public String getValue() {
126 		return RawParseUtils.decode(enc, buffer, valStart, valEnd);
127 	}
128 
129 	/**
130 	 * Extract the email address (if present) from the footer.
131 	 * <p>
132 	 * If there is an email address looking string inside of angle brackets
133 	 * (e.g. "&lt;a@b&gt;"), the return value is the part extracted from inside the
134 	 * brackets. If no brackets are found, then {@link #getValue()} is returned
135 	 * if the value contains an '@' sign. Otherwise, null.
136 	 *
137 	 * @return email address appearing in the value of this footer, or null.
138 	 */
139 	public String getEmailAddress() {
140 		final int lt = RawParseUtils.nextLF(buffer, valStart, '<');
141 		if (valEnd <= lt) {
142 			final int at = RawParseUtils.nextLF(buffer, valStart, '@');
143 			if (valStart < at && at < valEnd)
144 				return getValue();
145 			return null;
146 		}
147 		final int gt = RawParseUtils.nextLF(buffer, lt, '>');
148 		if (valEnd < gt)
149 			return null;
150 		return RawParseUtils.decode(enc, buffer, lt, gt - 1);
151 	}
152 
153 	/** {@inheritDoc} */
154 	@Override
155 	public String toString() {
156 		return getKey() + ": " + getValue(); //$NON-NLS-1$
157 	}
158 }