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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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.ConfigConstants;
  62. import org.eclipse.jgit.lib.Constants;
  63. import org.eclipse.jgit.lib.CoreConfig;
  64. import org.eclipse.jgit.lib.ObjectId;
  65. import org.eclipse.jgit.lib.PersonIdent;
  66. import org.eclipse.jgit.lib.Ref;
  67. import org.eclipse.jgit.lib.RefUpdate;
  68. import org.eclipse.jgit.lib.ReflogEntry;
  69. import org.eclipse.jgit.lib.Repository;
  70. import org.eclipse.jgit.util.FileUtils;
  71. /**
  72. * Utility for writing reflog entries using the traditional one-file-per-log
  73. * format.
  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(String name) {
  85. return name + LOCK_SUFFIX;
  86. }
  87. private final RefDirectory refdb;
  88. private final boolean forceWrite;
  89. /**
  90. * Create writer for ref directory.
  91. *
  92. * @param refdb
  93. * a {@link org.eclipse.jgit.internal.storage.file.RefDirectory}
  94. * object.
  95. */
  96. public ReflogWriter(RefDirectory refdb) {
  97. this(refdb, false);
  98. }
  99. /**
  100. * Create writer for ref directory.
  101. *
  102. * @param refdb
  103. * a {@link org.eclipse.jgit.internal.storage.file.RefDirectory}
  104. * object.
  105. * @param forceWrite
  106. * true to write to disk all entries logged, false to respect the
  107. * repository's config and current log file status.
  108. */
  109. public ReflogWriter(RefDirectory refdb, boolean forceWrite) {
  110. this.refdb = refdb;
  111. this.forceWrite = forceWrite;
  112. }
  113. /**
  114. * Create the log directories.
  115. *
  116. * @throws java.io.IOException
  117. * @return this writer.
  118. */
  119. public ReflogWriter create() throws IOException {
  120. FileUtils.mkdir(refdb.logsDir);
  121. FileUtils.mkdir(refdb.logsRefsDir);
  122. FileUtils.mkdir(
  123. new File(refdb.logsRefsDir, R_HEADS.substring(R_REFS.length())));
  124. return this;
  125. }
  126. /**
  127. * Write the given entry to the ref's log.
  128. *
  129. * @param refName
  130. * a {@link java.lang.String} object.
  131. * @param entry
  132. * a {@link org.eclipse.jgit.lib.ReflogEntry} object.
  133. * @return this writer
  134. * @throws java.io.IOException
  135. */
  136. public ReflogWriter log(String refName, ReflogEntry entry)
  137. throws IOException {
  138. return log(refName, entry.getOldId(), entry.getNewId(), entry.getWho(),
  139. entry.getComment());
  140. }
  141. /**
  142. * Write the given entry information to the ref's log
  143. *
  144. * @param refName
  145. * ref name
  146. * @param oldId
  147. * old object id
  148. * @param newId
  149. * new object id
  150. * @param ident
  151. * a {@link org.eclipse.jgit.lib.PersonIdent}
  152. * @param message
  153. * reflog message
  154. * @return this writer
  155. * @throws java.io.IOException
  156. */
  157. public ReflogWriter log(String refName, ObjectId oldId,
  158. ObjectId newId, PersonIdent ident, String message) throws IOException {
  159. byte[] encoded = encode(oldId, newId, ident, message);
  160. return log(refName, encoded);
  161. }
  162. /**
  163. * Write the given ref update to the ref's log.
  164. *
  165. * @param update
  166. * a {@link org.eclipse.jgit.lib.RefUpdate}
  167. * @param msg
  168. * reflog message
  169. * @param deref
  170. * whether to dereference symbolic refs
  171. * @return this writer
  172. * @throws java.io.IOException
  173. */
  174. public ReflogWriter log(RefUpdate update, String msg,
  175. boolean deref) throws IOException {
  176. ObjectId oldId = update.getOldObjectId();
  177. ObjectId newId = update.getNewObjectId();
  178. Ref ref = update.getRef();
  179. PersonIdent ident = update.getRefLogIdent();
  180. if (ident == null)
  181. ident = new PersonIdent(refdb.getRepository());
  182. else
  183. ident = new PersonIdent(ident);
  184. byte[] rec = encode(oldId, newId, ident, msg);
  185. if (deref && ref.isSymbolic()) {
  186. log(ref.getName(), rec);
  187. log(ref.getLeaf().getName(), rec);
  188. } else
  189. log(ref.getName(), rec);
  190. return this;
  191. }
  192. private byte[] encode(ObjectId oldId, ObjectId newId, PersonIdent ident,
  193. String message) {
  194. StringBuilder r = new StringBuilder();
  195. r.append(ObjectId.toString(oldId));
  196. r.append(' ');
  197. r.append(ObjectId.toString(newId));
  198. r.append(' ');
  199. r.append(ident.toExternalString());
  200. r.append('\t');
  201. r.append(
  202. message.replace("\r\n", " ") //$NON-NLS-1$ //$NON-NLS-2$
  203. .replace("\n", " ")); //$NON-NLS-1$ //$NON-NLS-2$
  204. r.append('\n');
  205. return Constants.encode(r.toString());
  206. }
  207. private FileOutputStream getFileOutputStream(File log) throws IOException {
  208. try {
  209. return new FileOutputStream(log, true);
  210. } catch (FileNotFoundException err) {
  211. File dir = log.getParentFile();
  212. if (dir.exists()) {
  213. throw err;
  214. }
  215. if (!dir.mkdirs() && !dir.isDirectory()) {
  216. throw new IOException(MessageFormat
  217. .format(JGitText.get().cannotCreateDirectory, dir));
  218. }
  219. return new FileOutputStream(log, true);
  220. }
  221. }
  222. private ReflogWriter log(String refName, byte[] rec) throws IOException {
  223. File log = refdb.logFor(refName);
  224. boolean write = forceWrite
  225. || shouldAutoCreateLog(refName)
  226. || log.isFile();
  227. if (!write)
  228. return this;
  229. WriteConfig wc = refdb.getRepository().getConfig().get(WriteConfig.KEY);
  230. try (FileOutputStream out = getFileOutputStream(log)) {
  231. if (wc.getFSyncRefFiles()) {
  232. FileChannel fc = out.getChannel();
  233. ByteBuffer buf = ByteBuffer.wrap(rec);
  234. while (0 < buf.remaining()) {
  235. fc.write(buf);
  236. }
  237. fc.force(true);
  238. } else {
  239. out.write(rec);
  240. }
  241. }
  242. return this;
  243. }
  244. private boolean shouldAutoCreateLog(String refName) {
  245. Repository repo = refdb.getRepository();
  246. CoreConfig.LogRefUpdates value = repo.isBare()
  247. ? CoreConfig.LogRefUpdates.FALSE
  248. : CoreConfig.LogRefUpdates.TRUE;
  249. value = repo.getConfig().getEnum(ConfigConstants.CONFIG_CORE_SECTION,
  250. null, ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES, value);
  251. if (value != null) {
  252. switch (value) {
  253. case FALSE:
  254. break;
  255. case TRUE:
  256. return refName.equals(HEAD) || refName.startsWith(R_HEADS)
  257. || refName.startsWith(R_REMOTES)
  258. || refName.startsWith(R_NOTES);
  259. case ALWAYS:
  260. return refName.equals(HEAD) || refName.startsWith(R_REFS);
  261. default:
  262. break;
  263. }
  264. }
  265. return false;
  266. }
  267. }