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

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