1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.lib;
12
13 import java.text.MessageFormat;
14
15 import org.eclipse.jgit.errors.IllegalTodoFileModification;
16 import org.eclipse.jgit.internal.JGitText;
17
18
19
20
21
22
23 public class RebaseTodoLine {
24
25
26
27 @SuppressWarnings("nls")
28 public enum Action {
29
30 PICK("pick", "p"),
31
32
33 REWORD("reword", "r"),
34
35
36 EDIT("edit", "e"),
37
38
39 SQUASH("squash", "s"),
40
41
42 FIXUP("fixup", "f"),
43
44
45
46
47
48 COMMENT("comment", "#");
49
50 private final String token;
51
52 private final String shortToken;
53
54 private Action(String token, String shortToken) {
55 this.token = token;
56 this.shortToken = shortToken;
57 }
58
59
60
61
62 public String toToken() {
63 return this.token;
64 }
65
66 @Override
67 public String toString() {
68 return "Action[" + token + "]";
69 }
70
71
72
73
74
75 public static Action parse(String token) {
76 for (Action action : Action.values()) {
77 if (action.token.equals(token)
78 || action.shortToken.equals(token))
79 return action;
80 }
81 throw new IllegalArgumentException(MessageFormat.format(
82 JGitText.get().unknownOrUnsupportedCommand, token,
83 Action.values()));
84 }
85 }
86
87 Action action;
88
89 final AbbreviatedObjectId commit;
90
91 String shortMessage;
92
93 String comment;
94
95
96
97
98
99
100
101 public RebaseTodoLine(String newComment) {
102 this.action = Action.COMMENT;
103 setComment(newComment);
104 this.commit = null;
105 this.shortMessage = null;
106 }
107
108
109
110
111
112
113
114
115
116
117
118 public RebaseTodoLine(Action action, AbbreviatedObjectId commit,
119 String shortMessage) {
120 this.action = action;
121 this.commit = commit;
122 this.shortMessage = shortMessage;
123 this.comment = null;
124 }
125
126
127
128
129
130
131 public Action getAction() {
132 return action;
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146
147 public void setAction(Action newAction) throws IllegalTodoFileModification {
148 if (!Action.COMMENT.equals(action) && Action.COMMENT.equals(newAction)) {
149
150 if (comment == null)
151
152
153 comment = "# " + action.token + " "
154 + ((commit == null) ? "null" : commit.name()) + " "
155 + ((shortMessage == null) ? "null" : shortMessage);
156 } else if (Action.COMMENT.equals(action) && !Action.COMMENT.equals(newAction)) {
157
158 if (commit == null)
159 throw new IllegalTodoFileModification(MessageFormat.format(
160 JGitText.get().cannotChangeActionOnComment, action,
161 newAction));
162 }
163 this.action = newAction;
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181 public void setComment(String newComment) {
182 if (newComment == null) {
183 this.comment = null;
184 return;
185 }
186
187 if (newComment.contains("\n") || newComment.contains("\r"))
188 throw createInvalidCommentException(newComment);
189
190 if (newComment.trim().length() == 0 || newComment.startsWith("#")) {
191 this.comment = newComment;
192 return;
193 }
194
195 throw createInvalidCommentException(newComment);
196 }
197
198 private static IllegalArgumentException createInvalidCommentException(
199 String newComment) {
200 return new IllegalArgumentException(
201 MessageFormat.format(
202 JGitText.get().argumentIsNotAValidCommentString, newComment));
203 }
204
205
206
207
208
209
210
211 public AbbreviatedObjectId getCommit() {
212 return commit;
213 }
214
215
216
217
218
219
220
221
222 public String getShortMessage() {
223 return shortMessage;
224 }
225
226
227
228
229
230
231
232 public void setShortMessage(String shortMessage) {
233 this.shortMessage = shortMessage;
234 }
235
236
237
238
239
240
241
242
243
244 public String getComment() {
245 return comment;
246 }
247
248
249 @SuppressWarnings("nls")
250 @Override
251 public String toString() {
252 return "Step["
253 + action
254 + ", "
255 + ((commit == null) ? "null" : commit)
256 + ", "
257 + ((shortMessage == null) ? "null" : shortMessage)
258 + ", "
259 + ((comment == null) ? "" : comment) + "]";
260 }
261 }