diff options
author | Tobias Pfeifer <to.pfeifer@web.de> | 2013-08-05 12:54:23 +0200 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2013-10-31 14:25:29 +0100 |
commit | baab84836ab8ed0ebd6a57dae991607b0c24039b (patch) | |
tree | 0adae3a8a20ec4abc395247ad74df67fbafac23e /org.eclipse.jgit | |
parent | c3873b584f81bc5be3fdea53e9e0e76271a9a854 (diff) | |
download | jgit-baab84836ab8ed0ebd6a57dae991607b0c24039b.tar.gz jgit-baab84836ab8ed0ebd6a57dae991607b0c24039b.zip |
Parse commitId and shortMessage of a commented out line in rebase todo
A rebase todo file consists of regular non-comment action lines and
comment lines. In case that a regular action line has been commented out
(i.e. prefixed with a hash '#'), the RebaseTodoLine that is representing
this line should hold the values for commitId and shortMessage even
though it's a comment line. This allows to switch between comment and
non-comment easily even after the file has been persisted and reread.
Change-Id: I56ec2ba08eaf3772e2d74d937dd496209a744d4b
Signed-off-by: Tobias Pfeifer <to.pfeifer@web.de>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/lib/RebaseTodoFile.java | 139 |
1 files changed, 95 insertions, 44 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RebaseTodoFile.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RebaseTodoFile.java index a386fff0e1..8db75662a2 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RebaseTodoFile.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RebaseTodoFile.java @@ -51,6 +51,7 @@ import java.io.OutputStreamWriter; import java.util.LinkedList; import java.util.List; +import org.eclipse.jgit.lib.RebaseTodoLine.Action; import org.eclipse.jgit.util.IO; import org.eclipse.jgit.util.RawParseUtils; @@ -88,8 +89,6 @@ public class RebaseTodoFile { int tokenBegin = 0; List<RebaseTodoLine> r = new LinkedList<RebaseTodoLine>(); while (ptr < buf.length) { - RebaseTodoLine.Action action = null; - AbbreviatedObjectId commit = null; tokenBegin = ptr; ptr = RawParseUtils.nextLF(buf, ptr); int lineStart = tokenBegin; @@ -99,54 +98,106 @@ public class RebaseTodoFile { // Handle comments if (buf[tokenBegin] == '#') { if (includeComments) - r.add(new RebaseTodoLine(RawParseUtils.decode(buf, - tokenBegin, lineEnd + 1))); - continue; - } - // skip leading spaces, tabs, CR - while (tokenBegin <= lineEnd - && (buf[tokenBegin] == ' ' || buf[tokenBegin] == '\t' || buf[tokenBegin] == '\r')) - tokenBegin++; - // Handle empty lines (maybe empty after skipping leading - // whitespace) - if (tokenBegin > lineEnd) { - if (includeComments) - r.add(new RebaseTodoLine(RawParseUtils.decode(buf, - lineStart, 1 + lineEnd))); - continue; - } - int nextSpace = RawParseUtils.next(buf, tokenBegin, ' '); - int tokenCount = 0; - while (tokenCount < 3 && nextSpace < ptr) { - switch (tokenCount) { - case 0: - String actionToken = new String(buf, tokenBegin, nextSpace - - tokenBegin - 1); - tokenBegin = nextSpace; - action = RebaseTodoLine.Action.parse(actionToken); - break; - case 1: - if (action == null) - break; - nextSpace = RawParseUtils.next(buf, tokenBegin, ' '); - String commitToken = new String(buf, tokenBegin, nextSpace - - tokenBegin - 1); - tokenBegin = nextSpace; - commit = AbbreviatedObjectId.fromString(commitToken); - break; - case 2: - if (action == null) - break; - r.add(new RebaseTodoLine(action, commit, RawParseUtils - .decode(buf, tokenBegin, 1 + lineEnd))); - break; + parseComments(buf, tokenBegin, r, lineEnd); + } else { + // skip leading spaces+tabs+cr + tokenBegin = nextParsableToken(buf, tokenBegin, lineEnd); + // Handle empty lines (maybe empty after skipping leading + // whitespace) + if (tokenBegin == -1) { + if (includeComments) + r.add(new RebaseTodoLine(RawParseUtils.decode(buf, + lineStart, 1 + lineEnd))); + continue; } - tokenCount++; + RebaseTodoLine line = parseLine(buf, tokenBegin, lineEnd); + if (line == null) + continue; + r.add(line); } } return r; } + private static void parseComments(byte[] buf, int tokenBegin, + List<RebaseTodoLine> r, int lineEnd) { + RebaseTodoLine line = null; + String commentString = RawParseUtils.decode(buf, + tokenBegin, lineEnd + 1); + try { + int skip = tokenBegin + 1; // skip '#' + skip = nextParsableToken(buf, skip, lineEnd); + if (skip != -1) { + // try to parse the line as non-comment + line = parseLine(buf, skip, lineEnd); + // successfully parsed as non-comment line + // mark this line as a comment explicitly + line.setAction(Action.COMMENT); + // use the read line as comment string + line.setComment(commentString); + } + } catch (Exception e) { + // parsing as non-comment line failed + line = null; + } finally { + if (line == null) + line = new RebaseTodoLine(commentString); + r.add(line); + } + } + + /** + * Skip leading space, tab, CR and LF characters + * + * @param buf + * @param tokenBegin + * @param lineEnd + * @return the token within the range of the given {@code buf} that doesn't + * need to be skipped, {@code -1} if no such token found within the + * range (i.e. empty line) + */ + private static int nextParsableToken(byte[] buf, int tokenBegin, int lineEnd) { + while (tokenBegin <= lineEnd + && (buf[tokenBegin] == ' ' || buf[tokenBegin] == '\t' || buf[tokenBegin] == '\r')) + tokenBegin++; + if (tokenBegin > lineEnd) + return -1; + return tokenBegin; + } + + private static RebaseTodoLine parseLine(byte[] buf, int tokenBegin, + int lineEnd) { + RebaseTodoLine.Action action = null; + AbbreviatedObjectId commit = null; + + int nextSpace = RawParseUtils.next(buf, tokenBegin, ' '); + int tokenCount = 0; + while (tokenCount < 3 && nextSpace < lineEnd) { + switch (tokenCount) { + case 0: + String actionToken = new String(buf, tokenBegin, nextSpace + - tokenBegin - 1); + tokenBegin = nextSpace; + action = RebaseTodoLine.Action.parse(actionToken); + if (action == null) + return null; // parsing failed + break; + case 1: + nextSpace = RawParseUtils.next(buf, tokenBegin, ' '); + String commitToken = new String(buf, tokenBegin, nextSpace + - tokenBegin - 1); + tokenBegin = nextSpace; + commit = AbbreviatedObjectId.fromString(commitToken); + break; + case 2: + return new RebaseTodoLine(action, commit, RawParseUtils.decode( + buf, tokenBegin, 1 + lineEnd)); + } + tokenCount++; + } + return null; + } + /** * Write a file formatted like a git-rebase-todo file. * |