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