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.

ReflogWriter.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  3. * Copyright (C) 2009-2010, Google Inc.
  4. * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  5. * Copyright (C) 2006, Shawn O. Pearce <spearce@spearce.org>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.internal.storage.file;
  47. import static org.eclipse.jgit.lib.Constants.HEAD;
  48. import static org.eclipse.jgit.lib.Constants.LOGS;
  49. import static org.eclipse.jgit.lib.Constants.R_HEADS;
  50. import static org.eclipse.jgit.lib.Constants.R_REFS;
  51. import static org.eclipse.jgit.lib.Constants.R_REMOTES;
  52. import static org.eclipse.jgit.lib.Constants.R_STASH;
  53. import java.io.File;
  54. import java.io.FileNotFoundException;
  55. import java.io.FileOutputStream;
  56. import java.io.IOException;
  57. import java.nio.ByteBuffer;
  58. import java.nio.channels.FileChannel;
  59. import java.text.MessageFormat;
  60. import org.eclipse.jgit.internal.JGitText;
  61. import org.eclipse.jgit.lib.Constants;
  62. import org.eclipse.jgit.lib.CoreConfig;
  63. import org.eclipse.jgit.lib.ObjectId;
  64. import org.eclipse.jgit.lib.PersonIdent;
  65. import org.eclipse.jgit.lib.Ref;
  66. import org.eclipse.jgit.lib.RefUpdate;
  67. import org.eclipse.jgit.lib.ReflogEntry;
  68. import org.eclipse.jgit.lib.Repository;
  69. import org.eclipse.jgit.util.FS;
  70. import org.eclipse.jgit.util.FileUtils;
  71. /**
  72. * Utility for writing reflog entries
  73. */
  74. public class ReflogWriter {
  75. /**
  76. * Get the ref name to be used for when locking a ref's log for rewriting
  77. *
  78. * @param name
  79. * name of the ref, relative to the Git repository top level
  80. * directory (so typically starts with refs/).
  81. * @return the name of the ref's lock ref
  82. */
  83. public static String refLockFor(final String name) {
  84. return name + LockFile.SUFFIX;
  85. }
  86. private final Repository parent;
  87. private final File logsDir;
  88. private final File logsRefsDir;
  89. private final boolean forceWrite;
  90. /**
  91. * Create write for repository
  92. *
  93. * @param repository
  94. */
  95. public ReflogWriter(final Repository repository) {
  96. this(repository, false);
  97. }
  98. /**
  99. * Create write for repository
  100. *
  101. * @param repository
  102. * @param forceWrite
  103. * true to write to disk all entries logged, false to respect the
  104. * repository's config and current log file status
  105. */
  106. public ReflogWriter(final Repository repository, final boolean forceWrite) {
  107. final FS fs = repository.getFS();
  108. parent = repository;
  109. File gitDir = repository.getDirectory();
  110. logsDir = fs.resolve(gitDir, LOGS);
  111. logsRefsDir = fs.resolve(gitDir, LOGS + '/' + R_REFS);
  112. this.forceWrite = forceWrite;
  113. }
  114. /**
  115. * Get repository that reflog is being written for
  116. *
  117. * @return file repository
  118. */
  119. public Repository getRepository() {
  120. return parent;
  121. }
  122. /**
  123. * Create the log directories
  124. *
  125. * @throws IOException
  126. * @return this writer
  127. */
  128. public ReflogWriter create() throws IOException {
  129. FileUtils.mkdir(logsDir);
  130. FileUtils.mkdir(logsRefsDir);
  131. FileUtils.mkdir(new File(logsRefsDir,
  132. R_HEADS.substring(R_REFS.length())));
  133. return this;
  134. }
  135. /**
  136. * Locate the log file on disk for a single reference name.
  137. *
  138. * @param name
  139. * name of the ref, relative to the Git repository top level
  140. * directory (so typically starts with refs/).
  141. * @return the log file location.
  142. */
  143. public File logFor(String name) {
  144. if (name.startsWith(R_REFS)) {
  145. name = name.substring(R_REFS.length());
  146. return new File(logsRefsDir, name);
  147. }
  148. return new File(logsDir, name);
  149. }
  150. /**
  151. * Write the given {@link ReflogEntry} entry to the ref's log
  152. *
  153. * @param refName
  154. *
  155. * @param entry
  156. * @return this writer
  157. * @throws IOException
  158. */
  159. public ReflogWriter log(final String refName, final ReflogEntry entry)
  160. throws IOException {
  161. return log(refName, entry.getOldId(), entry.getNewId(), entry.getWho(),
  162. entry.getComment());
  163. }
  164. /**
  165. * Write the given entry information to the ref's log
  166. *
  167. * @param refName
  168. * @param oldId
  169. * @param newId
  170. * @param ident
  171. * @param message
  172. * @return this writer
  173. * @throws IOException
  174. */
  175. public ReflogWriter log(final String refName, final ObjectId oldId,
  176. final ObjectId newId, final PersonIdent ident, final String message)
  177. throws IOException {
  178. byte[] encoded = encode(oldId, newId, ident, message);
  179. return log(refName, encoded);
  180. }
  181. /**
  182. * Write the given ref update to the ref's log
  183. *
  184. * @param update
  185. * @param msg
  186. * @param deref
  187. * @return this writer
  188. * @throws IOException
  189. */
  190. public ReflogWriter log(final RefUpdate update, final String msg,
  191. final boolean deref) throws IOException {
  192. final ObjectId oldId = update.getOldObjectId();
  193. final ObjectId newId = update.getNewObjectId();
  194. final Ref ref = update.getRef();
  195. PersonIdent ident = update.getRefLogIdent();
  196. if (ident == null)
  197. ident = new PersonIdent(parent);
  198. else
  199. ident = new PersonIdent(ident);
  200. final byte[] rec = encode(oldId, newId, ident, msg);
  201. if (deref && ref.isSymbolic()) {
  202. log(ref.getName(), rec);
  203. log(ref.getLeaf().getName(), rec);
  204. } else
  205. log(ref.getName(), rec);
  206. return this;
  207. }
  208. private byte[] encode(ObjectId oldId, ObjectId newId, PersonIdent ident,
  209. String message) {
  210. final StringBuilder r = new StringBuilder();
  211. r.append(ObjectId.toString(oldId));
  212. r.append(' ');
  213. r.append(ObjectId.toString(newId));
  214. r.append(' ');
  215. r.append(ident.toExternalString());
  216. r.append('\t');
  217. r.append(message.replace("\r\n", " ").replace("\n", " ")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  218. r.append('\n');
  219. return Constants.encode(r.toString());
  220. }
  221. private ReflogWriter log(final String refName, final byte[] rec)
  222. throws IOException {
  223. final File log = logFor(refName);
  224. final boolean write = forceWrite
  225. || (isLogAllRefUpdates() && shouldAutoCreateLog(refName))
  226. || log.isFile();
  227. if (!write)
  228. return this;
  229. WriteConfig wc = getRepository().getConfig().get(WriteConfig.KEY);
  230. FileOutputStream out;
  231. try {
  232. out = new FileOutputStream(log, true);
  233. } catch (FileNotFoundException err) {
  234. final File dir = log.getParentFile();
  235. if (dir.exists())
  236. throw err;
  237. if (!dir.mkdirs() && !dir.isDirectory())
  238. throw new IOException(MessageFormat.format(
  239. JGitText.get().cannotCreateDirectory, dir));
  240. out = new FileOutputStream(log, true);
  241. }
  242. try {
  243. if (wc.getFSyncRefFiles()) {
  244. FileChannel fc = out.getChannel();
  245. ByteBuffer buf = ByteBuffer.wrap(rec);
  246. while (0 < buf.remaining())
  247. fc.write(buf);
  248. fc.force(true);
  249. } else
  250. out.write(rec);
  251. } finally {
  252. out.close();
  253. }
  254. return this;
  255. }
  256. private boolean isLogAllRefUpdates() {
  257. return parent.getConfig().get(CoreConfig.KEY).isLogAllRefUpdates();
  258. }
  259. private boolean shouldAutoCreateLog(final String refName) {
  260. return refName.equals(HEAD) //
  261. || refName.startsWith(R_HEADS) //
  262. || refName.startsWith(R_REMOTES) //
  263. || refName.equals(R_STASH);
  264. }
  265. }