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.

RebaseTodoLine.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright (C) 2013, Christian Halstrick <christian.halstrick@sap.com>
  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.lib;
  44. import java.text.MessageFormat;
  45. import org.eclipse.jgit.api.errors.JGitInternalException;
  46. import org.eclipse.jgit.internal.JGitText;
  47. /**
  48. * Describes a single line in a file formatted like the git-rebase-todo file.
  49. *
  50. * @since 3.2
  51. */
  52. public class RebaseTodoLine {
  53. /**
  54. * Describes rebase actions
  55. */
  56. @SuppressWarnings("nls")
  57. public static enum Action {
  58. /** Use commit */
  59. PICK("pick", "p"),
  60. /** Use commit, but edit the commit message */
  61. REWORD("reword", "r"),
  62. /** Use commit, but stop for amending */
  63. EDIT("edit", "e"),
  64. /** Use commit, but meld into previous commit */
  65. SQUASH("squash", "s"),
  66. /** like "squash", but discard this commit's log message */
  67. FIXUP("fixup", "f"),
  68. /**
  69. * A comment in the file. Also blank lines (or lines containing only
  70. * whitespaces) are reported as comments
  71. */
  72. COMMENT("comment", "#");
  73. private final String token;
  74. private final String shortToken;
  75. private Action(String token, String shortToken) {
  76. this.token = token;
  77. this.shortToken = shortToken;
  78. }
  79. /**
  80. * @return full action token name
  81. */
  82. public String toToken() {
  83. return this.token;
  84. }
  85. @Override
  86. public String toString() {
  87. return "Action[" + token + "]";
  88. }
  89. /**
  90. * @param token
  91. * @return the Action
  92. */
  93. static public Action parse(String token) {
  94. for (Action action : Action.values()) {
  95. if (action.token.equals(token)
  96. || action.shortToken.equals(token))
  97. return action;
  98. }
  99. throw new JGitInternalException(MessageFormat.format(
  100. JGitText.get().unknownOrUnsupportedCommand, token,
  101. Action.values()));
  102. }
  103. }
  104. Action action;
  105. final AbbreviatedObjectId commit;
  106. String shortMessage;
  107. String comment;
  108. /**
  109. * Create a new comment line
  110. *
  111. * @param newComment
  112. * the new comment
  113. */
  114. public RebaseTodoLine(String newComment) {
  115. this.action = Action.COMMENT;
  116. setComment(newComment);
  117. this.commit = null;
  118. this.shortMessage = null;
  119. }
  120. /**
  121. * Create a new non-comment line
  122. *
  123. * @param action
  124. * @param commit
  125. * @param shortMessage
  126. */
  127. public RebaseTodoLine(Action action, AbbreviatedObjectId commit,
  128. String shortMessage) {
  129. this.action = action;
  130. this.commit = commit;
  131. this.shortMessage = shortMessage;
  132. this.comment = null;
  133. }
  134. /**
  135. * @return rebase action type
  136. */
  137. public Action getAction() {
  138. return action;
  139. }
  140. /**
  141. * Set the action. It's not allowed to set a non-comment action on a line
  142. * which was a comment line before. But you are allowed to set the comment
  143. * action on a non-comment line and afterwards change the action back to
  144. * non-comment.
  145. *
  146. * @param newAction
  147. */
  148. public void setAction(Action newAction) {
  149. if (!Action.COMMENT.equals(action) && Action.COMMENT.equals(newAction)) {
  150. // transforming from non-comment to comment
  151. if (comment == null)
  152. // no comment was explicitly set. Take the old line as comment
  153. // text
  154. comment = "# " + action.token + " " //$NON-NLS-1$ //$NON-NLS-2$
  155. + ((commit == null) ? "null" : commit.name()) + " " //$NON-NLS-1$ //$NON-NLS-2$
  156. + ((shortMessage == null) ? "null" : shortMessage); //$NON-NLS-1$
  157. } else if (Action.COMMENT.equals(action) && !Action.COMMENT.equals(newAction)) {
  158. // transforming from comment to non-comment
  159. if (commit == null)
  160. throw new JGitInternalException(MessageFormat.format(
  161. JGitText.get().cannotChangeActionOnComment, action,
  162. newAction));
  163. }
  164. this.action = newAction;
  165. }
  166. /**
  167. * <p>
  168. * Set a comment for this line that is used if this line's
  169. * {@link RebaseTodoLine#action} is a {@link Action#COMMENT}
  170. * </p>
  171. * It's allowed to unset the comment by calling
  172. * <code>setComment(null)</code> <br>
  173. * A valid comment either starts with a hash (i.e. <code>'#'</code>), is an
  174. * empty string, or consists of only spaces and tabs.<br>
  175. * If the argument <code>newComment</code> doesn't match these requirements
  176. * an Exception is thrown.
  177. *
  178. * @param newComment
  179. * the comment
  180. */
  181. public void setComment(String newComment) {
  182. if (newComment == null) {
  183. this.comment = null;
  184. return;
  185. }
  186. if (newComment.contains("\n") || newComment.contains("\r")) //$NON-NLS-1$ //$NON-NLS-2$
  187. throw createInvalidCommentException(newComment);
  188. if (newComment.trim().length() == 0 || newComment.startsWith("#")) { //$NON-NLS-1$
  189. this.comment = newComment;
  190. return;
  191. }
  192. throw createInvalidCommentException(newComment);
  193. }
  194. private static JGitInternalException createInvalidCommentException(
  195. String newComment) {
  196. IllegalArgumentException iae = new IllegalArgumentException(
  197. MessageFormat.format(
  198. JGitText.get().argumentIsNotAValidCommentString, newComment));
  199. return new JGitInternalException(iae.getMessage(), iae);
  200. }
  201. /**
  202. * @return abbreviated commit SHA-1 of commit that action will be performed
  203. * on
  204. */
  205. public AbbreviatedObjectId getCommit() {
  206. return commit;
  207. }
  208. /**
  209. * @return the first line of the commit message of the commit the action
  210. * will be performed on.
  211. */
  212. public String getShortMessage() {
  213. return shortMessage;
  214. }
  215. /**
  216. * @param shortMessage
  217. */
  218. public void setShortMessage(String shortMessage) {
  219. this.shortMessage = shortMessage;
  220. }
  221. /**
  222. * @return a comment. If the line is a comment line then the comment is
  223. * returned. Lines starting with # or blank lines or lines
  224. * containing only spaces and tabs are considered as comment lines.
  225. * The complete line is returned (e.g. including the '#')
  226. */
  227. public String getComment() {
  228. return comment;
  229. }
  230. @SuppressWarnings("nls")
  231. @Override
  232. public String toString() {
  233. return "Step["
  234. + action
  235. + ", "
  236. + ((commit == null) ? "null" : commit)
  237. + ", "
  238. + ((shortMessage == null) ? "null" : shortMessage)
  239. + ", "
  240. + ((comment == null) ? "" : comment) + "]";
  241. }
  242. }