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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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.errors.IllegalTodoFileModification;
  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 IllegalArgumentException(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. * @throws IllegalTodoFileModification
  148. * on attempt to set a non-comment action on a line which was a
  149. * comment line before.
  150. */
  151. public void setAction(Action newAction) throws IllegalTodoFileModification {
  152. if (!Action.COMMENT.equals(action) && Action.COMMENT.equals(newAction)) {
  153. // transforming from non-comment to comment
  154. if (comment == null)
  155. // no comment was explicitly set. Take the old line as comment
  156. // text
  157. comment = "# " + action.token + " " //$NON-NLS-1$ //$NON-NLS-2$
  158. + ((commit == null) ? "null" : commit.name()) + " " //$NON-NLS-1$ //$NON-NLS-2$
  159. + ((shortMessage == null) ? "null" : shortMessage); //$NON-NLS-1$
  160. } else if (Action.COMMENT.equals(action) && !Action.COMMENT.equals(newAction)) {
  161. // transforming from comment to non-comment
  162. if (commit == null)
  163. throw new IllegalTodoFileModification(MessageFormat.format(
  164. JGitText.get().cannotChangeActionOnComment, action,
  165. newAction));
  166. }
  167. this.action = newAction;
  168. }
  169. /**
  170. * <p>
  171. * Set a comment for this line that is used if this line's
  172. * {@link RebaseTodoLine#action} is a {@link Action#COMMENT}
  173. * </p>
  174. * It's allowed to unset the comment by calling
  175. * <code>setComment(null)</code> <br>
  176. * A valid comment either starts with a hash (i.e. <code>'#'</code>), is an
  177. * empty string, or consists of only spaces and tabs.<br>
  178. * If the argument <code>newComment</code> doesn't match these requirements
  179. * an Exception is thrown.
  180. *
  181. * @param newComment
  182. * the comment
  183. */
  184. public void setComment(String newComment) {
  185. if (newComment == null) {
  186. this.comment = null;
  187. return;
  188. }
  189. if (newComment.contains("\n") || newComment.contains("\r")) //$NON-NLS-1$ //$NON-NLS-2$
  190. throw createInvalidCommentException(newComment);
  191. if (newComment.trim().length() == 0 || newComment.startsWith("#")) { //$NON-NLS-1$
  192. this.comment = newComment;
  193. return;
  194. }
  195. throw createInvalidCommentException(newComment);
  196. }
  197. private static IllegalArgumentException createInvalidCommentException(
  198. String newComment) {
  199. return new IllegalArgumentException(
  200. MessageFormat.format(
  201. JGitText.get().argumentIsNotAValidCommentString, newComment));
  202. }
  203. /**
  204. * @return abbreviated commit SHA-1 of commit that action will be performed
  205. * on
  206. */
  207. public AbbreviatedObjectId getCommit() {
  208. return commit;
  209. }
  210. /**
  211. * @return the first line of the commit message of the commit the action
  212. * will be performed on.
  213. */
  214. public String getShortMessage() {
  215. return shortMessage;
  216. }
  217. /**
  218. * @param shortMessage
  219. */
  220. public void setShortMessage(String shortMessage) {
  221. this.shortMessage = shortMessage;
  222. }
  223. /**
  224. * @return a comment. If the line is a comment line then the comment is
  225. * returned. Lines starting with # or blank lines or lines
  226. * containing only spaces and tabs are considered as comment lines.
  227. * The complete line is returned (e.g. including the '#')
  228. */
  229. public String getComment() {
  230. return comment;
  231. }
  232. @SuppressWarnings("nls")
  233. @Override
  234. public String toString() {
  235. return "Step["
  236. + action
  237. + ", "
  238. + ((commit == null) ? "null" : commit)
  239. + ", "
  240. + ((shortMessage == null) ? "null" : shortMessage)
  241. + ", "
  242. + ((comment == null) ? "" : comment) + "]";
  243. }
  244. }