diff --git a/libs.git/apichanges.xml b/libs.git/apichanges.xml --- a/libs.git/apichanges.xml +++ b/libs.git/apichanges.xml @@ -111,6 +111,19 @@ + Adding new method to GitClient: commit(), amending the last commit + + + + + + Git supports commit amendments, in other words modifying the last commit and changing its + commit message or modified files. Another commit method allows API clients to specify + either to amend the last commit or to make a fresh new commit on top of current HEAD. + + + + Adding new method to GitClient: release() diff --git a/libs.git/manifest.mf b/libs.git/manifest.mf --- a/libs.git/manifest.mf +++ b/libs.git/manifest.mf @@ -1,4 +1,4 @@ Manifest-Version: 1.0 OpenIDE-Module: org.netbeans.libs.git/1 OpenIDE-Module-Localizing-Bundle: org/netbeans/libs/git/Bundle.properties -OpenIDE-Module-Specification-Version: 1.6 +OpenIDE-Module-Specification-Version: 1.7 diff --git a/libs.git/src/org/netbeans/libs/git/GitClient.java b/libs.git/src/org/netbeans/libs/git/GitClient.java --- a/libs.git/src/org/netbeans/libs/git/GitClient.java +++ b/libs.git/src/org/netbeans/libs/git/GitClient.java @@ -342,8 +342,22 @@ * @throws GitException an unexpected error occurs */ public GitRevisionInfo commit(File[] roots, String commitMessage, GitUser author, GitUser commiter, ProgressMonitor monitor) throws GitException { + return commit(roots, commitMessage, author, commiter, false, monitor); + } + + /** + * Commits all changes made in the index to all files under the given roots + * @param roots files or folders to recursively commit. + * @param commitMessage commit message + * @param author person who is the author of the changes to be committed + * @param commiter person who is committing the changes, may not be the same person as author. + * @param amend amends and modifies the last commit instead of adding a completely new commit + * @param monitor progress monitor + * @throws GitException an unexpected error occurs + */ + public GitRevisionInfo commit(File[] roots, String commitMessage, GitUser author, GitUser commiter, boolean amend, ProgressMonitor monitor) throws GitException { Repository repository = gitRepository.getRepository(); - CommitCommand cmd = new CommitCommand(repository, getClassFactory(), roots, commitMessage, author, commiter, monitor); + CommitCommand cmd = new CommitCommand(repository, getClassFactory(), roots, commitMessage, author, commiter, amend, monitor); cmd.execute(); return cmd.revision; } diff --git a/libs.git/src/org/netbeans/libs/git/jgit/commands/CommitCommand.java b/libs.git/src/org/netbeans/libs/git/jgit/commands/CommitCommand.java --- a/libs.git/src/org/netbeans/libs/git/jgit/commands/CommitCommand.java +++ b/libs.git/src/org/netbeans/libs/git/jgit/commands/CommitCommand.java @@ -89,15 +89,16 @@ private final GitUser author; private final GitUser commiter; public GitRevisionInfo revision; + private final boolean amend; - public CommitCommand (Repository repository, GitClassFactory gitFactory, File[] roots, String message, GitUser author, GitUser commiter, ProgressMonitor monitor) { + public CommitCommand (Repository repository, GitClassFactory gitFactory, File[] roots, String message, GitUser author, GitUser commiter, boolean amend, ProgressMonitor monitor) { super(repository, gitFactory, monitor); this.roots = roots; this.message = message; this.monitor = monitor; - this.author = author; this.commiter = commiter; + this.amend = amend; } @Override @@ -151,6 +152,7 @@ } commit.setMessage(message); + commit.setAmend(amend); RevCommit rev = commit.call(); revision = getClassFactory().createRevisionInfo(rev, repository); } finally { @@ -242,6 +244,9 @@ @Override protected String getCommandDescription () { StringBuilder sb = new StringBuilder("git commit -m ").append(message); //NOI18N + if (amend) { + sb.append(" --amend"); //NOI18N + } for (File root : roots) { sb.append(" ").append(root); //NOI18N } diff --git a/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/CommitTest.java b/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/CommitTest.java --- a/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/CommitTest.java +++ b/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/CommitTest.java @@ -56,9 +56,7 @@ import org.eclipse.jgit.lib.*; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.treewalk.TreeWalk; -import org.eclipse.jgit.treewalk.WorkingTreeOptions; import org.eclipse.jgit.treewalk.filter.PathFilter; -import org.eclipse.jgit.util.io.AutoCRLFOutputStream; import org.netbeans.libs.git.GitClient; import org.netbeans.libs.git.GitException; import org.netbeans.libs.git.GitStatus; @@ -620,4 +618,52 @@ assertEquals(e1.getObjectId(), walk.getObjectId(0)); } } + + public void testAmendCommit () throws Exception { + repository.getConfig().setString("user", null, "name", "John"); + repository.getConfig().setString("user", null, "email", "john@git.com"); + repository.getConfig().save(); + + File dir = new File(workDir, "testdir"); + File newOne = new File(dir, "test.txt"); + File another = new File(dir, "test2.txt"); + dir.mkdirs(); + write(newOne, "content1"); + write(another, "content2"); + + GitClient client = getClient(workDir); + client.add(new File[] { newOne, another }, NULL_PROGRESS_MONITOR); + GitRevisionInfo info = client.commit(new File[] { newOne, another }, "initial commit", null, null, NULL_PROGRESS_MONITOR); + Map statuses = client.getStatus(new File[] { newOne, another }, NULL_PROGRESS_MONITOR); + assertStatus(statuses, workDir, newOne, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); + assertStatus(statuses, workDir, another, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); + + write(newOne, "modification1"); + write(another, "modification2"); + + client.add(new File[] { newOne, another }, NULL_PROGRESS_MONITOR); + GitRevisionInfo lastCommit = client.commit(new File[] { newOne }, "second commit", null, null, false, NULL_PROGRESS_MONITOR); + statuses = client.getStatus(new File[] { workDir }, NULL_PROGRESS_MONITOR); + assertStatus(statuses, workDir, newOne, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); + assertStatus(statuses, workDir, another, true, GitStatus.Status.STATUS_MODIFIED, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_MODIFIED, false); + Map modifiedFiles = lastCommit.getModifiedFiles(); + assertTrue(modifiedFiles.get(newOne).getStatus().equals(Status.MODIFIED)); + assertNull(modifiedFiles.get(another)); + + assertEquals(1, lastCommit.getParents().length); + assertEquals(info.getRevision(), lastCommit.getParents()[0]); + assertEquals(lastCommit.getRevision(), client.getBranches(false, NULL_PROGRESS_MONITOR).get("master").getId()); + + lastCommit = client.commit(new File[] { newOne, another }, "second commit, modified message", null, null, true, NULL_PROGRESS_MONITOR); + statuses = client.getStatus(new File[] { workDir }, NULL_PROGRESS_MONITOR); + assertStatus(statuses, workDir, newOne, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); + assertStatus(statuses, workDir, another, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); + modifiedFiles = lastCommit.getModifiedFiles(); + assertTrue(modifiedFiles.get(newOne).getStatus().equals(Status.MODIFIED)); + assertTrue(modifiedFiles.get(another).getStatus().equals(Status.MODIFIED)); + + assertEquals(1, lastCommit.getParents().length); + assertEquals(info.getRevision(), lastCommit.getParents()[0]); + assertEquals(lastCommit.getRevision(), client.getBranches(false, NULL_PROGRESS_MONITOR).get("master").getId()); + } }