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 * @param key
84 * key to test this line's key name against.
85 * @return true if {@code key.getName().equalsIgnorecase(getKey())}.
86 */
87 public boolean matches(final FooterKey key) {
88 final byte[] kRaw = key.raw;
89 final int len = kRaw.length;
90 int bPtr = keyStart;
91 if (keyEnd - bPtr != len)
92 return false;
93 for (int kPtr = 0; kPtr < len;) {
94 byte b = buffer[bPtr++];
95 if ('A' <= b && b <= 'Z')
96 b += 'a' - 'A';
97 if (b != kRaw[kPtr++])
98 return false;
99 }
100 return true;
101 }
102
103 /**
104 * @return key name of this footer; that is the text before the ":" on the
105 * line footer's line. The text is decoded according to the commit's
106 * specified (or assumed) character encoding.
107 */
108 public String getKey() {
109 return RawParseUtils.decode(enc, buffer, keyStart, keyEnd);
110 }
111
112 /**
113 * @return value of this footer; that is the text after the ":" and any
114 * leading whitespace has been skipped. May be the empty string if
115 * the footer has no value (line ended with ":"). The text is
116 * decoded according to the commit's specified (or assumed)
117 * character encoding.
118 */
119 public String getValue() {
120 return RawParseUtils.decode(enc, buffer, valStart, valEnd);
121 }
122
123 /**
124 * Extract the email address (if present) from the footer.
125 * <p>
126 * If there is an email address looking string inside of angle brackets
127 * (e.g. "<a@b>"), the return value is the part extracted from inside the
128 * brackets. If no brackets are found, then {@link #getValue()} is returned
129 * if the value contains an '@' sign. Otherwise, null.
130 *
131 * @return email address appearing in the value of this footer, or null.
132 */
133 public String getEmailAddress() {
134 final int lt = RawParseUtils.nextLF(buffer, valStart, '<');
135 if (valEnd <= lt) {
136 final int at = RawParseUtils.nextLF(buffer, valStart, '@');
137 if (valStart < at && at < valEnd)
138 return getValue();
139 return null;
140 }
141 final int gt = RawParseUtils.nextLF(buffer, lt, '>');
142 if (valEnd < gt)
143 return null;
144 return RawParseUtils.decode(enc, buffer, lt, gt - 1);
145 }
146
147 @Override
148 public String toString() {
149 return getKey() + ": " + getValue(); //$NON-NLS-1$
150 }
151 }