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

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