You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FooterLine.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. package org.eclipse.jgit.revwalk;
  44. import java.nio.charset.Charset;
  45. import org.eclipse.jgit.util.RawParseUtils;
  46. /**
  47. * Single line at the end of a message, such as a "Signed-off-by: someone".
  48. * <p>
  49. * These footer lines tend to be used to represent additional information about
  50. * a commit, like the path it followed through reviewers before finally being
  51. * accepted into the project's main repository as an immutable commit.
  52. *
  53. * @see RevCommit#getFooterLines()
  54. */
  55. public final class FooterLine {
  56. private final byte[] buffer;
  57. private final Charset enc;
  58. private final int keyStart;
  59. private final int keyEnd;
  60. private final int valStart;
  61. private final int valEnd;
  62. FooterLine(final byte[] b, final Charset e, final int ks, final int ke,
  63. final int vs, final int ve) {
  64. buffer = b;
  65. enc = e;
  66. keyStart = ks;
  67. keyEnd = ke;
  68. valStart = vs;
  69. valEnd = ve;
  70. }
  71. /**
  72. * @param key
  73. * key to test this line's key name against.
  74. * @return true if {@code key.getName().equalsIgnorecase(getKey())}.
  75. */
  76. public boolean matches(final FooterKey key) {
  77. final byte[] kRaw = key.raw;
  78. final int len = kRaw.length;
  79. int bPtr = keyStart;
  80. if (keyEnd - bPtr != len)
  81. return false;
  82. for (int kPtr = 0; kPtr < len;) {
  83. byte b = buffer[bPtr++];
  84. if ('A' <= b && b <= 'Z')
  85. b += 'a' - 'A';
  86. if (b != kRaw[kPtr++])
  87. return false;
  88. }
  89. return true;
  90. }
  91. /**
  92. * @return key name of this footer; that is the text before the ":" on the
  93. * line footer's line. The text is decoded according to the commit's
  94. * specified (or assumed) character encoding.
  95. */
  96. public String getKey() {
  97. return RawParseUtils.decode(enc, buffer, keyStart, keyEnd);
  98. }
  99. /**
  100. * @return value of this footer; that is the text after the ":" and any
  101. * leading whitespace has been skipped. May be the empty string if
  102. * the footer has no value (line ended with ":"). The text is
  103. * decoded according to the commit's specified (or assumed)
  104. * character encoding.
  105. */
  106. public String getValue() {
  107. return RawParseUtils.decode(enc, buffer, valStart, valEnd);
  108. }
  109. /**
  110. * Extract the email address (if present) from the footer.
  111. * <p>
  112. * If there is an email address looking string inside of angle brackets
  113. * (e.g. "&lt;a@b&gt;"), the return value is the part extracted from inside the
  114. * brackets. If no brackets are found, then {@link #getValue()} is returned
  115. * if the value contains an '@' sign. Otherwise, null.
  116. *
  117. * @return email address appearing in the value of this footer, or null.
  118. */
  119. public String getEmailAddress() {
  120. final int lt = RawParseUtils.nextLF(buffer, valStart, '<');
  121. if (valEnd <= lt) {
  122. final int at = RawParseUtils.nextLF(buffer, valStart, '@');
  123. if (valStart < at && at < valEnd)
  124. return getValue();
  125. return null;
  126. }
  127. final int gt = RawParseUtils.nextLF(buffer, lt, '>');
  128. if (valEnd < gt)
  129. return null;
  130. return RawParseUtils.decode(enc, buffer, lt, gt - 1);
  131. }
  132. @Override
  133. public String toString() {
  134. return getKey() + ": " + getValue(); //$NON-NLS-1$
  135. }
  136. }