Allow RefUpdate callers to control the PersonIdent recorded
[egit/chris.git] / org.spearce.jgit / src / org / spearce / jgit / lib / RefLogWriter.java
bloba077051b607f9a634cb15379a1c621a02f05243f
1 /*
2 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
4 * Copyright (C) 2006, Shawn O. Pearce <spearce@spearce.org>
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or
9 * without modification, are permitted provided that the following
10 * conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
20 * - Neither the name of the Git Development Community nor the
21 * names of its contributors may be used to endorse or promote
22 * products derived from this software without specific prior
23 * written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
26 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
27 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 package org.spearce.jgit.lib;
42 import java.io.File;
43 import java.io.FileOutputStream;
44 import java.io.IOException;
46 /**
47 * Utility class to add reflog entries
49 * @author Dave Watson
51 public class RefLogWriter {
52 static void append(final RefUpdate u, final String msg) throws IOException {
53 final ObjectId oldId = u.getOldObjectId();
54 final ObjectId newId = u.getNewObjectId();
55 final Repository db = u.getRepository();
56 final PersonIdent ident = u.getRefLogIdent();
58 appendOneRecord(oldId, newId, ident, msg, db, u.getName());
61 private static void appendOneRecord(final ObjectId oldId,
62 final ObjectId newId, PersonIdent ident, final String msg,
63 final Repository db, final String refName) throws IOException {
64 if (ident == null)
65 ident = new PersonIdent(db);
66 else
67 ident = new PersonIdent(ident);
69 final StringBuilder r = new StringBuilder();
70 r.append(ObjectId.toString(oldId));
71 r.append(' ');
72 r.append(ObjectId.toString(newId));
73 r.append(' ');
74 r.append(ident.toExternalString());
75 r.append('\t');
76 r.append(msg);
77 r.append('\n');
79 final byte[] rec = Constants.encode(r.toString());
80 final File logdir = new File(db.getDirectory(), Constants.LOGS);
81 final File reflog = new File(logdir, refName);
82 final File refdir = reflog.getParentFile();
84 if (!refdir.exists() && !refdir.mkdirs())
85 throw new IOException("Cannot create directory " + refdir);
87 final FileOutputStream out = new FileOutputStream(reflog, true);
88 try {
89 out.write(rec);
90 } finally {
91 out.close();
95 /**
96 * Writes reflog entry for ref specified by refName
98 * @param repo
99 * repository to use
100 * @param oldCommit
101 * previous commit
102 * @param commit
103 * new commit
104 * @param message
105 * reflog message
106 * @param refName
107 * full ref name
108 * @throws IOException
109 * @deprecated rely upon {@link RefUpdate}'s automatic logging instead.
111 public static void writeReflog(Repository repo, ObjectId oldCommit,
112 ObjectId commit, String message, String refName) throws IOException {
113 appendOneRecord(oldCommit, commit, null, message, repo, refName);