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.

RebaseTodoFile.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.io.BufferedWriter;
  45. import java.io.File;
  46. import java.io.FileOutputStream;
  47. import java.io.IOException;
  48. import java.io.OutputStreamWriter;
  49. import java.util.LinkedList;
  50. import java.util.List;
  51. import org.eclipse.jgit.lib.RebaseTodoLine.Action;
  52. import org.eclipse.jgit.util.IO;
  53. import org.eclipse.jgit.util.RawParseUtils;
  54. /**
  55. * Offers methods to read and write files formatted like the git-rebase-todo
  56. * file
  57. *
  58. * @since 3.2
  59. */
  60. public class RebaseTodoFile {
  61. private Repository repo;
  62. /**
  63. * @param repo
  64. */
  65. public RebaseTodoFile(Repository repo) {
  66. this.repo = repo;
  67. }
  68. /**
  69. * Read a file formatted like the git-rebase-todo file. The "done" file is
  70. * also formatted like the git-rebase-todo file. These files can be found in
  71. * .git/rebase-merge/ or .git/rebase-append/ folders.
  72. *
  73. * @param path
  74. * path to the file relative to the repository's git-dir. E.g.
  75. * "rebase-merge/git-rebase-todo" or "rebase-append/done"
  76. * @param includeComments
  77. * <code>true</code> if also comments should be reported
  78. * @return the list of steps
  79. * @throws IOException
  80. */
  81. public List<RebaseTodoLine> readRebaseTodo(String path,
  82. boolean includeComments) throws IOException {
  83. byte[] buf = IO.readFully(new File(repo.getDirectory(), path));
  84. int ptr = 0;
  85. int tokenBegin = 0;
  86. List<RebaseTodoLine> r = new LinkedList<RebaseTodoLine>();
  87. while (ptr < buf.length) {
  88. tokenBegin = ptr;
  89. ptr = RawParseUtils.nextLF(buf, ptr);
  90. int lineStart = tokenBegin;
  91. int lineEnd = ptr - 2;
  92. if (lineEnd >= 0 && buf[lineEnd] == '\r')
  93. lineEnd--;
  94. // Handle comments
  95. if (buf[tokenBegin] == '#') {
  96. if (includeComments)
  97. parseComments(buf, tokenBegin, r, lineEnd);
  98. } else {
  99. // skip leading spaces+tabs+cr
  100. tokenBegin = nextParsableToken(buf, tokenBegin, lineEnd);
  101. // Handle empty lines (maybe empty after skipping leading
  102. // whitespace)
  103. if (tokenBegin == -1) {
  104. if (includeComments)
  105. r.add(new RebaseTodoLine(RawParseUtils.decode(buf,
  106. lineStart, 1 + lineEnd)));
  107. continue;
  108. }
  109. RebaseTodoLine line = parseLine(buf, tokenBegin, lineEnd);
  110. if (line == null)
  111. continue;
  112. r.add(line);
  113. }
  114. }
  115. return r;
  116. }
  117. private static void parseComments(byte[] buf, int tokenBegin,
  118. List<RebaseTodoLine> r, int lineEnd) {
  119. RebaseTodoLine line = null;
  120. String commentString = RawParseUtils.decode(buf,
  121. tokenBegin, lineEnd + 1);
  122. try {
  123. int skip = tokenBegin + 1; // skip '#'
  124. skip = nextParsableToken(buf, skip, lineEnd);
  125. if (skip != -1) {
  126. // try to parse the line as non-comment
  127. line = parseLine(buf, skip, lineEnd);
  128. // successfully parsed as non-comment line
  129. // mark this line as a comment explicitly
  130. line.setAction(Action.COMMENT);
  131. // use the read line as comment string
  132. line.setComment(commentString);
  133. }
  134. } catch (Exception e) {
  135. // parsing as non-comment line failed
  136. line = null;
  137. } finally {
  138. if (line == null)
  139. line = new RebaseTodoLine(commentString);
  140. r.add(line);
  141. }
  142. }
  143. /**
  144. * Skip leading space, tab, CR and LF characters
  145. *
  146. * @param buf
  147. * @param tokenBegin
  148. * @param lineEnd
  149. * @return the token within the range of the given {@code buf} that doesn't
  150. * need to be skipped, {@code -1} if no such token found within the
  151. * range (i.e. empty line)
  152. */
  153. private static int nextParsableToken(byte[] buf, int tokenBegin, int lineEnd) {
  154. while (tokenBegin <= lineEnd
  155. && (buf[tokenBegin] == ' ' || buf[tokenBegin] == '\t' || buf[tokenBegin] == '\r'))
  156. tokenBegin++;
  157. if (tokenBegin > lineEnd)
  158. return -1;
  159. return tokenBegin;
  160. }
  161. private static RebaseTodoLine parseLine(byte[] buf, int tokenBegin,
  162. int lineEnd) {
  163. RebaseTodoLine.Action action = null;
  164. AbbreviatedObjectId commit = null;
  165. int nextSpace = RawParseUtils.next(buf, tokenBegin, ' ');
  166. int tokenCount = 0;
  167. while (tokenCount < 3 && nextSpace < lineEnd) {
  168. switch (tokenCount) {
  169. case 0:
  170. String actionToken = new String(buf, tokenBegin, nextSpace
  171. - tokenBegin - 1);
  172. tokenBegin = nextSpace;
  173. action = RebaseTodoLine.Action.parse(actionToken);
  174. if (action == null)
  175. return null; // parsing failed
  176. break;
  177. case 1:
  178. nextSpace = RawParseUtils.next(buf, tokenBegin, ' ');
  179. String commitToken = new String(buf, tokenBegin, nextSpace
  180. - tokenBegin - 1);
  181. tokenBegin = nextSpace;
  182. commit = AbbreviatedObjectId.fromString(commitToken);
  183. break;
  184. case 2:
  185. return new RebaseTodoLine(action, commit, RawParseUtils.decode(
  186. buf, tokenBegin, 1 + lineEnd));
  187. }
  188. tokenCount++;
  189. }
  190. if (tokenCount == 2)
  191. return new RebaseTodoLine(action, commit, ""); //$NON-NLS-1$
  192. return null;
  193. }
  194. /**
  195. * Write a file formatted like a git-rebase-todo file.
  196. *
  197. * @param path
  198. * path to the file relative to the repository's git-dir. E.g.
  199. * "rebase-merge/git-rebase-todo" or "rebase-append/done"
  200. * @param steps
  201. * the steps to be written
  202. * @param append
  203. * whether to append to an existing file or to write a new file
  204. * @throws IOException
  205. */
  206. public void writeRebaseTodoFile(String path, List<RebaseTodoLine> steps,
  207. boolean append) throws IOException {
  208. BufferedWriter fw = new BufferedWriter(new OutputStreamWriter(
  209. new FileOutputStream(new File(repo.getDirectory(), path),
  210. append), Constants.CHARACTER_ENCODING));
  211. try {
  212. StringBuilder sb = new StringBuilder();
  213. for (RebaseTodoLine step : steps) {
  214. sb.setLength(0);
  215. if (RebaseTodoLine.Action.COMMENT.equals(step.action))
  216. sb.append(step.getComment());
  217. else {
  218. sb.append(step.getAction().toToken());
  219. sb.append(" "); //$NON-NLS-1$
  220. sb.append(step.getCommit().name());
  221. sb.append(" "); //$NON-NLS-1$
  222. sb.append(step.getShortMessage().trim());
  223. }
  224. fw.write(sb.toString());
  225. fw.newLine();
  226. }
  227. } finally {
  228. fw.close();
  229. }
  230. }
  231. }