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 6.8KB

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