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 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. if (line != null) {
  133. // successfully parsed as non-comment line
  134. // mark this line as a comment explicitly
  135. line.setAction(Action.COMMENT);
  136. // use the read line as comment string
  137. line.setComment(commentString);
  138. }
  139. }
  140. } catch (Exception e) {
  141. // parsing as non-comment line failed
  142. line = null;
  143. } finally {
  144. if (line == null)
  145. line = new RebaseTodoLine(commentString);
  146. r.add(line);
  147. }
  148. }
  149. /**
  150. * Skip leading space, tab, CR and LF characters
  151. *
  152. * @param buf
  153. * @param tokenBegin
  154. * @param lineEnd
  155. * @return the token within the range of the given {@code buf} that doesn't
  156. * need to be skipped, {@code -1} if no such token found within the
  157. * range (i.e. empty line)
  158. */
  159. private static int nextParsableToken(byte[] buf, int tokenBegin, int lineEnd) {
  160. while (tokenBegin <= lineEnd
  161. && (buf[tokenBegin] == ' ' || buf[tokenBegin] == '\t' || buf[tokenBegin] == '\r'))
  162. tokenBegin++;
  163. if (tokenBegin > lineEnd)
  164. return -1;
  165. return tokenBegin;
  166. }
  167. private static RebaseTodoLine parseLine(byte[] buf, int tokenBegin,
  168. int lineEnd) {
  169. RebaseTodoLine.Action action = null;
  170. AbbreviatedObjectId commit = null;
  171. int nextSpace = RawParseUtils.next(buf, tokenBegin, ' ');
  172. int tokenCount = 0;
  173. while (tokenCount < 3 && nextSpace <= lineEnd) {
  174. switch (tokenCount) {
  175. case 0:
  176. String actionToken = new String(buf, tokenBegin,
  177. nextSpace - tokenBegin - 1, UTF_8);
  178. tokenBegin = nextSpace;
  179. action = RebaseTodoLine.Action.parse(actionToken);
  180. if (action == null)
  181. return null; // parsing failed
  182. break;
  183. case 1:
  184. nextSpace = RawParseUtils.next(buf, tokenBegin, ' ');
  185. String commitToken;
  186. if (nextSpace > lineEnd + 1) {
  187. commitToken = new String(buf, tokenBegin,
  188. lineEnd - tokenBegin + 1, UTF_8);
  189. } else {
  190. commitToken = new String(buf, tokenBegin,
  191. nextSpace - tokenBegin - 1, UTF_8);
  192. }
  193. tokenBegin = nextSpace;
  194. commit = AbbreviatedObjectId.fromString(commitToken);
  195. break;
  196. case 2:
  197. return new RebaseTodoLine(action, commit,
  198. RawParseUtils.decode(buf, tokenBegin, 1 + lineEnd));
  199. }
  200. tokenCount++;
  201. }
  202. if (tokenCount == 2)
  203. return new RebaseTodoLine(action, commit, ""); //$NON-NLS-1$
  204. return null;
  205. }
  206. /**
  207. * Write a file formatted like a git-rebase-todo file.
  208. *
  209. * @param path
  210. * path to the file relative to the repository's git-dir. E.g.
  211. * "rebase-merge/git-rebase-todo" or "rebase-append/done"
  212. * @param steps
  213. * the steps to be written
  214. * @param append
  215. * whether to append to an existing file or to write a new file
  216. * @throws java.io.IOException
  217. */
  218. public void writeRebaseTodoFile(String path, List<RebaseTodoLine> steps,
  219. boolean append) throws IOException {
  220. try (OutputStream fw = new BufferedOutputStream(new FileOutputStream(
  221. new File(repo.getDirectory(), path), append))) {
  222. StringBuilder sb = new StringBuilder();
  223. for (RebaseTodoLine step : steps) {
  224. sb.setLength(0);
  225. if (RebaseTodoLine.Action.COMMENT.equals(step.action))
  226. sb.append(step.getComment());
  227. else {
  228. sb.append(step.getAction().toToken());
  229. sb.append(" "); //$NON-NLS-1$
  230. sb.append(step.getCommit().name());
  231. sb.append(" "); //$NON-NLS-1$
  232. sb.append(step.getShortMessage().trim());
  233. }
  234. sb.append('\n');
  235. fw.write(Constants.encode(sb.toString()));
  236. }
  237. }
  238. }
  239. }