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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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.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.Repository;
  68. import org.eclipse.jgit.util.FS;
  69. import org.eclipse.jgit.util.FileUtils;
  70. /**
  71. * Utility for writing reflog entries
  72. *
  73. * @since 2.0
  74. */
  75. public class ReflogWriter {
  76. /**
  77. * Get the ref name to be used for when locking a ref's log for rewriting
  78. *
  79. * @param name
  80. * name of the ref, relative to the Git repository top level
  81. * directory (so typically starts with refs/).
  82. * @return the name of the ref's lock ref
  83. */
  84. public static String refLockFor(final String name) {
  85. return name + LockFile.SUFFIX;
  86. }
  87. private final Repository parent;
  88. private final File logsDir;
  89. private final File logsRefsDir;
  90. private final boolean forceWrite;
  91. /**
  92. * Create write for repository
  93. *
  94. * @param repository
  95. */
  96. public ReflogWriter(final Repository repository) {
  97. this(repository, false);
  98. }
  99. /**
  100. * Create write for repository
  101. *
  102. * @param repository
  103. * @param forceWrite
  104. * true to write to disk all entries logged, false to respect the
  105. * repository's config and current log file status
  106. */
  107. public ReflogWriter(final Repository repository, final boolean forceWrite) {
  108. final FS fs = repository.getFS();
  109. parent = repository;
  110. File gitDir = repository.getDirectory();
  111. logsDir = fs.resolve(gitDir, LOGS);
  112. logsRefsDir = fs.resolve(gitDir, LOGS + '/' + R_REFS);
  113. this.forceWrite = forceWrite;
  114. }
  115. /**
  116. * Get repository that reflog is being written for
  117. *
  118. * @return file repository
  119. */
  120. public Repository getRepository() {
  121. return parent;
  122. }
  123. /**
  124. * Create the log directories
  125. *
  126. * @throws IOException
  127. * @return this writer
  128. */
  129. public ReflogWriter create() throws IOException {
  130. FileUtils.mkdir(logsDir);
  131. FileUtils.mkdir(logsRefsDir);
  132. FileUtils.mkdir(new File(logsRefsDir,
  133. R_HEADS.substring(R_REFS.length())));
  134. return this;
  135. }
  136. /**
  137. * Locate the log file on disk for a single reference name.
  138. *
  139. * @param name
  140. * name of the ref, relative to the Git repository top level
  141. * directory (so typically starts with refs/).
  142. * @return the log file location.
  143. */
  144. public File logFor(String name) {
  145. if (name.startsWith(R_REFS)) {
  146. name = name.substring(R_REFS.length());
  147. return new File(logsRefsDir, name);
  148. }
  149. return new File(logsDir, name);
  150. }
  151. /**
  152. * Write the given {@link ReflogEntry} entry to the ref's log
  153. *
  154. * @param refName
  155. *
  156. * @param entry
  157. * @return this writer
  158. * @throws IOException
  159. */
  160. public ReflogWriter log(final String refName, final ReflogEntry entry)
  161. throws IOException {
  162. return log(refName, entry.getOldId(), entry.getNewId(), entry.getWho(),
  163. entry.getComment());
  164. }
  165. /**
  166. * Write the given entry information to the ref's log
  167. *
  168. * @param refName
  169. * @param oldId
  170. * @param newId
  171. * @param ident
  172. * @param message
  173. * @return this writer
  174. * @throws IOException
  175. */
  176. public ReflogWriter log(final String refName, final ObjectId oldId,
  177. final ObjectId newId, final PersonIdent ident, final String message)
  178. throws IOException {
  179. byte[] encoded = encode(oldId, newId, ident, message);
  180. return log(refName, encoded);
  181. }
  182. /**
  183. * Write the given ref update to the ref's log
  184. *
  185. * @param update
  186. * @param msg
  187. * @param deref
  188. * @return this writer
  189. * @throws IOException
  190. */
  191. public ReflogWriter log(final RefUpdate update, final String msg,
  192. final boolean deref) throws IOException {
  193. final ObjectId oldId = update.getOldObjectId();
  194. final ObjectId newId = update.getNewObjectId();
  195. final Ref ref = update.getRef();
  196. PersonIdent ident = update.getRefLogIdent();
  197. if (ident == null)
  198. ident = new PersonIdent(parent);
  199. else
  200. ident = new PersonIdent(ident);
  201. final byte[] rec = encode(oldId, newId, ident, msg);
  202. if (deref && ref.isSymbolic()) {
  203. log(ref.getName(), rec);
  204. log(ref.getLeaf().getName(), rec);
  205. } else
  206. log(ref.getName(), rec);
  207. return this;
  208. }
  209. private byte[] encode(ObjectId oldId, ObjectId newId, PersonIdent ident,
  210. String message) {
  211. final StringBuilder r = new StringBuilder();
  212. r.append(ObjectId.toString(oldId));
  213. r.append(' ');
  214. r.append(ObjectId.toString(newId));
  215. r.append(' ');
  216. r.append(ident.toExternalString());
  217. r.append('\t');
  218. r.append(message);
  219. r.append('\n');
  220. return Constants.encode(r.toString());
  221. }
  222. private ReflogWriter log(final String refName, final byte[] rec)
  223. throws IOException {
  224. final File log = logFor(refName);
  225. final boolean write = forceWrite
  226. || (isLogAllRefUpdates() && shouldAutoCreateLog(refName))
  227. || log.isFile();
  228. if (!write)
  229. return this;
  230. WriteConfig wc = getRepository().getConfig().get(WriteConfig.KEY);
  231. FileOutputStream out;
  232. try {
  233. out = new FileOutputStream(log, true);
  234. } catch (FileNotFoundException err) {
  235. final File dir = log.getParentFile();
  236. if (dir.exists())
  237. throw err;
  238. if (!dir.mkdirs() && !dir.isDirectory())
  239. throw new IOException(MessageFormat.format(
  240. JGitText.get().cannotCreateDirectory, dir));
  241. out = new FileOutputStream(log, true);
  242. }
  243. try {
  244. if (wc.getFSyncRefFiles()) {
  245. FileChannel fc = out.getChannel();
  246. ByteBuffer buf = ByteBuffer.wrap(rec);
  247. while (0 < buf.remaining())
  248. fc.write(buf);
  249. fc.force(true);
  250. } else
  251. out.write(rec);
  252. } finally {
  253. out.close();
  254. }
  255. return this;
  256. }
  257. private boolean isLogAllRefUpdates() {
  258. return parent.getConfig().get(CoreConfig.KEY).isLogAllRefUpdates();
  259. }
  260. private boolean shouldAutoCreateLog(final String refName) {
  261. return refName.equals(HEAD) //
  262. || refName.startsWith(R_HEADS) //
  263. || refName.startsWith(R_REMOTES) //
  264. || refName.equals(R_STASH);
  265. }
  266. }