]> source.dussan.org Git - jgit.git/commitdiff
Add reflog command to JGit CLI 94/8394/3
authorTomasz Zarna <tomasz.zarna@tasktop.com>
Sun, 4 Nov 2012 22:06:20 +0000 (23:06 +0100)
committerChris Aniszczyk <zx@twitter.com>
Fri, 16 Nov 2012 20:02:28 +0000 (12:02 -0800)
Bug: 394497
Change-Id: Ib8bc1d9fd789d22fe5f10e03068a11cfdd3e46eb
Signed-off-by: Chris Aniszczyk <zx@twitter.com>
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ReflogTest.java [new file with mode: 0644]
org.eclipse.jgit.pgm/META-INF/services/org.eclipse.jgit.pgm.TextBuiltin
org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/CLIText.properties
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Reflog.java [new file with mode: 0644]

diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ReflogTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ReflogTest.java
new file mode 100644 (file)
index 0000000..68346ec
--- /dev/null
@@ -0,0 +1,80 @@
+/*\r
+ * Copyright (C) 2012, Tomasz Zarna <tomasz.zarna@tasktop.com>\r
+ * and other copyright owners as documented in the project's IP log.\r
+ *\r
+ * This program and the accompanying materials are made available\r
+ * under the terms of the Eclipse Distribution License v1.0 which\r
+ * accompanies this distribution, is reproduced below, and is\r
+ * available at http://www.eclipse.org/org/documents/edl-v10.php\r
+ *\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or\r
+ * without modification, are permitted provided that the following\r
+ * conditions are met:\r
+ *\r
+ * - Redistributions of source code must retain the above copyright\r
+ *   notice, this list of conditions and the following disclaimer.\r
+ *\r
+ * - Redistributions in binary form must reproduce the above\r
+ *   copyright notice, this list of conditions and the following\r
+ *   disclaimer in the documentation and/or other materials provided\r
+ *   with the distribution.\r
+ *\r
+ * - Neither the name of the Eclipse Foundation, Inc. nor the\r
+ *   names of its contributors may be used to endorse or promote\r
+ *   products derived from this software without specific prior\r
+ *   written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND\r
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,\r
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\r
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\r
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\r
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ */\r
+package org.eclipse.jgit.pgm;\r
+\r
+import static org.junit.Assert.assertArrayEquals;\r
+import static org.junit.Assert.assertEquals;\r
+\r
+import org.eclipse.jgit.api.Git;\r
+import org.eclipse.jgit.lib.CLIRepositoryTestCase;\r
+import org.junit.Test;\r
+\r
+public class ReflogTest extends CLIRepositoryTestCase {\r
+       @Test\r
+       public void testClean() throws Exception {\r
+               assertArrayEquals(new String[] { "" }, execute("git reflog"));\r
+       }\r
+\r
+       @Test\r
+       public void testSingleCommit() throws Exception {\r
+               new Git(db).commit().setMessage("initial commit").call();\r
+\r
+               assertEquals("6fd41be HEAD@{0}: commit (initial): initial commit",\r
+                               execute("git reflog")[0]);\r
+       }\r
+\r
+       @Test\r
+       public void testBranch() throws Exception {\r
+               Git git = new Git(db);\r
+               git.commit().setMessage("first commit").call();\r
+               git.checkout().setCreateBranch(true).setName("side").call();\r
+               writeTrashFile("file", "side content");\r
+               git.add().addFilepattern("file").call();\r
+               git.commit().setMessage("side commit").call();\r
+\r
+               assertArrayEquals(new String[] {\r
+                               "38890c7 side@{0}: commit: side commit",\r
+                               "d216986 side@{1}: branch: Created from commit first commit",\r
+                               "" }, execute("git reflog refs/heads/side"));\r
+       }\r
+}
\ No newline at end of file
index 488d48af0c657631801ae87a7eaa17640cbdc6c3..1a6cc8551d41b76ef95493ddea1a482ec0c280a7 100644 (file)
@@ -20,6 +20,7 @@ org.eclipse.jgit.pgm.Merge
 org.eclipse.jgit.pgm.MergeBase
 org.eclipse.jgit.pgm.Push
 org.eclipse.jgit.pgm.ReceivePack
+org.eclipse.jgit.pgm.Reflog
 org.eclipse.jgit.pgm.RevList
 org.eclipse.jgit.pgm.RevParse
 org.eclipse.jgit.pgm.Rm
index cb1574f26cc92defc102f14c994022b652de637a..a75bb001d2f329f0d33fcecf391c475aa85c01a9 100644 (file)
@@ -241,6 +241,7 @@ usage_inputOutputFile=Input/output file
 usage_listBothRemoteTrackingAndLocalBranches=list both remote-tracking and local branches
 usage_listCreateOrDeleteBranches=List, create, or delete branches
 usage_logAllPretty=format:%H %ct %P' output=log --all '--pretty=format:%H %ct %P' output
+usage_manageReflogInformation=Manage reflog information
 usage_mergeStrategy=Use the given merge strategy. Can be supplied more than once to specify them in the order they should be tried. If there is no -s option, the resolve strategy is used. Currently the following strategies are supported: ours, theirs, simple-two-way-in-core, resolve
 usage_moveRenameABranch=move/rename a branch
 usage_nameStatus=show only name and status of files
diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Reflog.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Reflog.java
new file mode 100644 (file)
index 0000000..4d877ac
--- /dev/null
@@ -0,0 +1,88 @@
+/*\r
+ * Copyright (C) 2012, Tomasz Zarna <tomasz.zarna@tasktop.com>\r
+ * and other copyright owners as documented in the project's IP log.\r
+ *\r
+ * This program and the accompanying materials are made available\r
+ * under the terms of the Eclipse Distribution License v1.0 which\r
+ * accompanies this distribution, is reproduced below, and is\r
+ * available at http://www.eclipse.org/org/documents/edl-v10.php\r
+ *\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or\r
+ * without modification, are permitted provided that the following\r
+ * conditions are met:\r
+ *\r
+ * - Redistributions of source code must retain the above copyright\r
+ *   notice, this list of conditions and the following disclaimer.\r
+ *\r
+ * - Redistributions in binary form must reproduce the above\r
+ *   copyright notice, this list of conditions and the following\r
+ *   disclaimer in the documentation and/or other materials provided\r
+ *   with the distribution.\r
+ *\r
+ * - Neither the name of the Eclipse Foundation, Inc. nor the\r
+ *   names of its contributors may be used to endorse or promote\r
+ *   products derived from this software without specific prior\r
+ *   written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND\r
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,\r
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\r
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\r
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\r
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ */\r
+package org.eclipse.jgit.pgm;\r
+\r
+import java.util.Collection;\r
+\r
+import org.eclipse.jgit.api.Git;\r
+import org.eclipse.jgit.api.ReflogCommand;\r
+import org.eclipse.jgit.lib.Constants;\r
+import org.eclipse.jgit.lib.ObjectId;\r
+import org.eclipse.jgit.lib.Repository;\r
+import org.eclipse.jgit.storage.file.ReflogEntry;\r
+import org.kohsuke.args4j.Argument;\r
+\r
+@Command(common = true, usage = "usage_manageReflogInformation")\r
+class Reflog extends TextBuiltin {\r
+\r
+       @Argument(metaVar = "metaVar_ref")\r
+       private String ref;\r
+\r
+       @Override\r
+       protected void run() throws Exception {\r
+               ReflogCommand cmd = new Git(db).reflog();\r
+               if (ref != null)\r
+                       cmd.setRef(ref);\r
+               Collection<ReflogEntry> entries = cmd.call();\r
+               int i = 0;\r
+               for (ReflogEntry entry : entries) {\r
+                       outw.println(toString(entry, i++));\r
+               }\r
+       }\r
+\r
+       private String toString(ReflogEntry entry, int i) {\r
+               final StringBuilder s = new StringBuilder();\r
+               s.append(entry.getNewId().abbreviate(7).name());\r
+               s.append(" ");\r
+               s.append(ref == null ? Constants.HEAD : Repository.shortenRefName(ref));\r
+               s.append("@{" + i + "}:");\r
+               s.append(" ");\r
+               // temporary workaround for bug 393463\r
+               if (entry.getOldId().equals(ObjectId.zeroId()))\r
+                       s.append(entry.getComment().replaceFirst("^commit:",\r
+                                       "commit (initial):"));\r
+               else\r
+                       s.append(entry.getComment());\r
+               return s.toString();\r
+       }\r
+}\r