]> source.dussan.org Git - jgit.git/commitdiff
Add "all" parameter to the commit Command 48/1148/4
authorStefan Lay <stefan.lay@sap.com>
Wed, 28 Jul 2010 09:00:22 +0000 (11:00 +0200)
committerStefan Lay <stefan.lay@sap.com>
Wed, 4 Aug 2010 11:53:08 +0000 (13:53 +0200)
When the add parameter is set all modified and deleted files
are staged prior to commit.

Change-Id: Id23bc25730fcdd151386cd495a7cdc0935cbc00b
Signed-off-by: Stefan Lay <stefan.lay@sap.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitAndLogCommandTests.java
org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java

index a62045dc9fb7a49c9a5d080995d4408f1aa52246..cf30039ab7ad78db6662a35def5a17c63366ac67 100644 (file)
@@ -45,6 +45,7 @@ package org.eclipse.jgit.api;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.PrintWriter;
 
 import org.eclipse.jgit.errors.UnmergedPathException;
 import org.eclipse.jgit.lib.Constants;
@@ -53,6 +54,7 @@ import org.eclipse.jgit.lib.PersonIdent;
 import org.eclipse.jgit.lib.RefUpdate;
 import org.eclipse.jgit.lib.RepositoryTestCase;
 import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.treewalk.TreeWalk;
 
 public class CommitAndLogCommandTests extends RepositoryTestCase {
        public void testSomeCommits() throws NoHeadException, NoMessageException,
@@ -151,4 +153,36 @@ public class CommitAndLogCommandTests extends RepositoryTestCase {
                assertEquals(parents[1], second);
                assertTrue(parents.length==2);
        }
+
+       public void testAddUnstagedChanges() throws IOException, NoHeadException,
+                       NoMessageException, ConcurrentRefUpdateException,
+                       JGitInternalException, WrongRepositoryStateException,
+                       NoFilepatternException {
+               File file = new File(db.getWorkTree(), "a.txt");
+               file.createNewFile();
+               PrintWriter writer = new PrintWriter(file);
+               writer.print("content");
+               writer.close();
+
+               Git git = new Git(db);
+               git.add().addFilepattern("a.txt").call();
+               RevCommit commit = git.commit().setMessage("initial commit").call();
+               TreeWalk tw = TreeWalk.forPath(db, "a.txt", commit.getTree());
+               assertEquals("6b584e8ece562ebffc15d38808cd6b98fc3d97ea",
+                               tw.getObjectId(0).getName());
+
+               writer = new PrintWriter(file);
+               writer.print("content2");
+               writer.close();
+               commit = git.commit().setMessage("second commit").call();
+               tw = TreeWalk.forPath(db, "a.txt", commit.getTree());
+               assertEquals("6b584e8ece562ebffc15d38808cd6b98fc3d97ea",
+                               tw.getObjectId(0).getName());
+
+               commit = git.commit().setAll(true).setMessage("third commit")
+                               .setAll(true).call();
+               tw = TreeWalk.forPath(db, "a.txt", commit.getTree());
+               assertEquals("db00fd65b218578127ea51f3dffac701f12f486a",
+                               tw.getObjectId(0).getName());
+       }
 }
index 0dbe51281fe990a8133c4a9fbecc4936a312feea..ae4b33477bc3244ba35e87c7f281887dae4f1a91 100644 (file)
@@ -79,6 +79,8 @@ public class CommitCommand extends GitCommand<RevCommit> {
 
        private String message;
 
+       private boolean all;
+
        /**
         * parents this commit should have. The current HEAD will be in this list
         * and also all commits mentioned in .git/MERGE_HEAD
@@ -127,6 +129,18 @@ public class CommitCommand extends GitCommand<RevCommit> {
                processOptions(state);
 
                try {
+                       if (all && !repo.isBare() && repo.getWorkTree() != null) {
+                               Git git = new Git(repo);
+                               try {
+                                       git.add()
+                                                       .addFilepattern(".")
+                                                       .setUpdate(true).call();
+                               } catch (NoFilepatternException e) {
+                                       // should really not happen
+                                       throw new JGitInternalException(e.getMessage(), e);
+                               }
+                       }
+
                        Ref head = repo.getRef(Constants.HEAD);
                        if (head == null)
                                throw new NoHeadException(
@@ -353,4 +367,19 @@ public class CommitCommand extends GitCommand<RevCommit> {
        public PersonIdent getAuthor() {
                return author;
        }
+
+       /**
+        * If set to true the Commit command automatically stages files that have
+        * been modified and deleted, but new files you not known by the repository
+        * are not affected. This corresponds to the parameter -a on the command
+        * line.
+        *
+        * @param all
+        * @return {@code this}
+        */
+       public CommitCommand setAll(boolean all) {
+               this.all = all;
+               return this;
+       }
+
 }