Switch jgit library to the EDL (3-clause BSD)
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / lib / RefLogWriter.java
blob8256d701f48f17e77f6c949f3e78e79446c6df1f
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;
45 import java.io.PrintWriter;
47 /**
48 * Utility class to add reflog entries
50 * @author Dave Watson
52 public class RefLogWriter {
53 /**
54 * Writes reflog entry for ref specified by refName
56 * @param repo
57 * repository to use
58 * @param oldCommit
59 * previous commit
60 * @param commit
61 * new commit
62 * @param message
63 * reflog message
64 * @param refName
65 * full ref name
66 * @throws IOException
68 public static void writeReflog(Repository repo, ObjectId oldCommit,
69 ObjectId commit, String message, String refName) throws IOException {
70 String entry = buildReflogString(repo, oldCommit, commit, message);
72 File directory = repo.getDirectory();
73 File reflogfile = new File(directory, "logs/" + refName);
74 File reflogdir = reflogfile.getParentFile();
75 if (!reflogdir.exists())
76 if (!reflogdir.mkdirs())
77 throw new IOException("Cannot create directory "+reflogdir);
78 PrintWriter writer = new PrintWriter(new FileOutputStream(reflogfile, true));
79 writer.println(entry);
80 writer.close();
83 private static String buildReflogString(Repository repo,
84 ObjectId oldCommit, ObjectId commit, String message) {
85 PersonIdent me = new PersonIdent(repo);
86 String initial = "";
87 if (oldCommit == null) {
88 oldCommit = ObjectId.zeroId();
89 initial = " (initial)";
91 String s = oldCommit.toString() + " " + commit.toString() + " "
92 + me.toExternalString() + "\t" + message + initial;
93 return s;