diff -r ed185686231b libs.git/apichanges.xml --- a/libs.git/apichanges.xml Wed Aug 14 13:13:19 2013 +0200 +++ b/libs.git/apichanges.xml Wed Aug 14 16:03:58 2013 +0200 @@ -111,6 +111,21 @@ + Adding a command setting the upstream branch/tracking of local branches + + + + + +

Adding a new method GitClient.setUpstreamBranch(String, String, ProgressMonitor) + allowing an API client to set the tracking/upstream branch for a local branch. +

+
+ + +
+ + Adding support comparing trees of arbitrary commits diff -r ed185686231b libs.git/manifest.mf --- a/libs.git/manifest.mf Wed Aug 14 13:13:19 2013 +0200 +++ b/libs.git/manifest.mf Wed Aug 14 16:03:58 2013 +0200 @@ -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.11 +OpenIDE-Module-Specification-Version: 1.12 diff -r ed185686231b libs.git/src/org/netbeans/libs/git/GitClient.java --- a/libs.git/src/org/netbeans/libs/git/GitClient.java Wed Aug 14 13:13:19 2013 +0200 +++ b/libs.git/src/org/netbeans/libs/git/GitClient.java Wed Aug 14 16:03:58 2013 +0200 @@ -97,6 +97,7 @@ import org.netbeans.libs.git.jgit.commands.SetRemoteCommand; import org.netbeans.libs.git.jgit.commands.StatusCommand; import org.netbeans.libs.git.jgit.commands.UnignoreCommand; +import org.netbeans.libs.git.jgit.commands.SetUpstreamBranchCommand; import org.netbeans.libs.git.progress.FileListener; import org.netbeans.libs.git.progress.NotificationListener; import org.netbeans.libs.git.progress.ProgressMonitor; @@ -1007,6 +1008,25 @@ } /** + * Sets the upstream branch of localBranchName to + * remoteBranch. + * + * @param localBranchName local branch supposed to track another branch + * @param remoteBranch branch from remoteName to be tracked + * @param monitor progress monitor + * @return info for the local branch with updated tracking + * @throws GitException error occurs + * @since 1.12 + */ + public GitBranch setUpstreamBranch (String localBranchName, String remoteBranch, ProgressMonitor monitor) throws GitException { + Repository repository = gitRepository.getRepository(); + SetUpstreamBranchCommand cmd = new SetUpstreamBranchCommand(repository, getClassFactory(), + localBranchName, remoteBranch, monitor); + cmd.execute(); + return cmd.getTrackingBranch(); + } + + /** * Unignores given files * @param files files to mark unignored again and remove their respective record from gitignore files. * @param monitor progress monitor diff -r ed185686231b libs.git/src/org/netbeans/libs/git/jgit/Utils.java --- a/libs.git/src/org/netbeans/libs/git/jgit/Utils.java Wed Aug 14 13:13:19 2013 +0200 +++ b/libs.git/src/org/netbeans/libs/git/jgit/Utils.java Wed Aug 14 16:03:58 2013 +0200 @@ -407,6 +407,8 @@ if (trackedBranchName != null) { if (trackedBranchName.startsWith(Constants.R_HEADS)) { trackedBranchName = trackedBranchName.substring(Constants.R_HEADS.length()); + } else if (trackedBranchName.startsWith(Constants.R_REMOTES)) { + trackedBranchName = trackedBranchName.substring(Constants.R_REMOTES.length()); } } if (trackedBranchName == null) { diff -r ed185686231b libs.git/src/org/netbeans/libs/git/jgit/commands/Bundle.properties --- a/libs.git/src/org/netbeans/libs/git/jgit/commands/Bundle.properties Wed Aug 14 13:13:19 2013 +0200 +++ b/libs.git/src/org/netbeans/libs/git/jgit/commands/Bundle.properties Wed Aug 14 16:03:58 2013 +0200 @@ -55,4 +55,5 @@ MSG_Error_CannotCatRoot = Cannot cat root: {0} MSG_Error_Commit_ConflictsInIndex = Index contains files in conflict, please resolve them before commit MSG_Error_Commit_PartialCommitAfterMerge = Cannot do a partial commit during a merge. -MSG_Error_Commit_NotAllowedInCurrentState = Cannot commit in current state: {0} \ No newline at end of file +MSG_Error_Commit_NotAllowedInCurrentState = Cannot commit in current state: {0} +MSG_Error_UpdateTracking_InvalidReference=Invalid reference: {0} diff -r ed185686231b libs.git/src/org/netbeans/libs/git/jgit/commands/SetUpstreamBranchCommand.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libs.git/src/org/netbeans/libs/git/jgit/commands/SetUpstreamBranchCommand.java Wed Aug 14 16:03:58 2013 +0200 @@ -0,0 +1,127 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2010 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2010 Sun Microsystems, Inc. + */ + +package org.netbeans.libs.git.jgit.commands; + +import java.io.IOException; +import java.text.MessageFormat; +import java.util.Map; +import org.eclipse.jgit.lib.ConfigConstants; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.lib.StoredConfig; +import org.netbeans.libs.git.GitBranch; +import org.netbeans.libs.git.GitException; +import org.netbeans.libs.git.jgit.DelegatingGitProgressMonitor; +import org.netbeans.libs.git.jgit.GitClassFactory; +import org.netbeans.libs.git.jgit.Utils; +import org.netbeans.libs.git.progress.ProgressMonitor; + +/** + * + * @author ondra + */ +public class SetUpstreamBranchCommand extends GitCommand { + private final String localBranchName; + private final String trackedBranchName; + private GitBranch branch; + private final ProgressMonitor monitor; + + public SetUpstreamBranchCommand (Repository repository, GitClassFactory gitFactory, + String localBranchName, String trackedBranch, ProgressMonitor monitor) { + super(repository, gitFactory, monitor); + this.localBranchName = localBranchName; + this.trackedBranchName = trackedBranch; + this.monitor = monitor; + } + + @Override + protected void run () throws GitException { + Repository repository = getRepository(); + + try { + Ref ref = repository.getRef(trackedBranchName); + if (ref == null) { + throw new GitException(MessageFormat.format(Utils.getBundle(SetUpstreamBranchCommand.class) + .getString("MSG_Error_UpdateTracking_InvalidReference"), trackedBranchName)); //NOI18N) + } + String remote = null; + String branchName = ref.getName(); + StoredConfig config = repository.getConfig(); + if (branchName.startsWith(Constants.R_REMOTES)) { + String[] elements = branchName.split("/", 4); + remote = elements[2]; + if (config.getSubsections(ConfigConstants.CONFIG_REMOTE_SECTION).contains(remote)) { + branchName = Constants.R_HEADS + elements[3]; + } else { + // remote not yet set + remote = null; + } + } + if (remote == null) { + remote = "."; //NOI18N + } + config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, localBranchName, + ConfigConstants.CONFIG_KEY_REMOTE, remote); + config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, localBranchName, + ConfigConstants.CONFIG_KEY_MERGE, branchName); + config.save(); + } catch (IOException ex) { + throw new GitException(ex); + } + ListBranchCommand branchCmd = new ListBranchCommand(repository, getClassFactory(), false, new DelegatingGitProgressMonitor(monitor)); + branchCmd.run(); + Map branches = branchCmd.getBranches(); + branch = branches.get(localBranchName); + } + + @Override + protected String getCommandDescription () { + return new StringBuilder("git branch --set-upstream-to ").append(trackedBranchName) //NOI18N + .append(' ').append(localBranchName).toString(); + } + + public GitBranch getTrackingBranch () { + return branch; + } +} diff -r ed185686231b libs.git/test/unit/src/org/netbeans/libs/git/jgit/CommandsTestSuite.java --- a/libs.git/test/unit/src/org/netbeans/libs/git/jgit/CommandsTestSuite.java Wed Aug 14 13:13:19 2013 +0200 +++ b/libs.git/test/unit/src/org/netbeans/libs/git/jgit/CommandsTestSuite.java Wed Aug 14 16:03:58 2013 +0200 @@ -74,6 +74,7 @@ import org.netbeans.libs.git.jgit.commands.RenameTest; import org.netbeans.libs.git.jgit.commands.ResetTest; import org.netbeans.libs.git.jgit.commands.RevertTest; +import org.netbeans.libs.git.jgit.commands.SetUpstreamBranchTest; import org.netbeans.libs.git.jgit.commands.StatusTest; import org.netbeans.libs.git.jgit.commands.TagTest; import org.netbeans.libs.git.jgit.commands.UnignoreTest; @@ -119,6 +120,7 @@ suite.addTestSuite(RenameTest.class); suite.addTestSuite(RevertTest.class); suite.addTestSuite(ResetTest.class); + suite.addTestSuite(SetUpstreamBranchTest.class); suite.addTestSuite(StatusTest.class); suite.addTestSuite(TagTest.class); suite.addTestSuite(UnignoreTest.class); diff -r ed185686231b libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/SetUpstreamBranchTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/SetUpstreamBranchTest.java Wed Aug 14 16:03:58 2013 +0200 @@ -0,0 +1,151 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2010 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2010 Sun Microsystems, Inc. + */ + +package org.netbeans.libs.git.jgit.commands; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.Map; +import org.eclipse.jgit.lib.Config; +import org.eclipse.jgit.lib.ConfigConstants; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.Repository; +import org.netbeans.libs.git.GitBranch; +import org.netbeans.libs.git.GitClient; +import org.netbeans.libs.git.GitException; +import org.netbeans.libs.git.GitRemoteConfig; +import org.netbeans.libs.git.jgit.AbstractGitTestCase; + +/** + * + * @author ondra + */ +public class SetUpstreamBranchTest extends AbstractGitTestCase { + private File workDir; + private static final String BRANCH = "mybranch"; + private Repository repository; + + public SetUpstreamBranchTest (String testName) throws IOException { + super(testName); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + workDir = getWorkingDirectory(); + repository = getRepository(getLocalGitRepository()); + } + + public void testLocalTracking () throws GitException { + GitClient client = getClient(workDir); + File f = new File(workDir, "f"); + add(f); + client.commit(new File[] { f }, "init commit", null, null, NULL_PROGRESS_MONITOR); + + // prepare twp branches + GitBranch b = client.createBranch(BRANCH, Constants.MASTER, NULL_PROGRESS_MONITOR); + assertNull(b.getTrackedBranch()); + + // set tracking + b = client.setUpstreamBranch(BRANCH, Constants.MASTER, NULL_PROGRESS_MONITOR); + assertEquals(Constants.MASTER, b.getTrackedBranch().getName()); + } + + public void testRemoteTrackingNoRemoteSet () throws GitException { + GitClient client = getClient(workDir); + File f = new File(workDir, "f"); + add(f); + client.commit(new File[] { f }, "init commit", null, null, NULL_PROGRESS_MONITOR); + + // push to remote + String remoteUri = getRemoteRepository().getWorkTree().toURI().toString(); + client.push(remoteUri, + Arrays.asList("refs/heads/master:refs/heads/master"), + Arrays.asList("+refs/heads/*:refs/remotes/origin/*"), + NULL_PROGRESS_MONITOR); + Map branches = client.getBranches(true, NULL_PROGRESS_MONITOR); + assertTrue(branches.containsKey("origin/master")); + assertNull(branches.get("master").getTrackedBranch()); + + // set tracking + GitBranch b = client.setUpstreamBranch("master", "origin/master", NULL_PROGRESS_MONITOR); + assertEquals("origin/master", b.getTrackedBranch().getName()); + + Config cfg = repository.getConfig(); + assertEquals(".", cfg.getString(ConfigConstants.CONFIG_BRANCH_SECTION, "master", ConfigConstants.CONFIG_KEY_REMOTE)); + assertEquals("refs/remotes/origin/master", cfg.getString(ConfigConstants.CONFIG_BRANCH_SECTION, "master", ConfigConstants.CONFIG_KEY_MERGE)); + } + + public void testRemoteTracking () throws GitException { + GitClient client = getClient(workDir); + File f = new File(workDir, "f"); + add(f); + client.commit(new File[] { f }, "init commit", null, null, NULL_PROGRESS_MONITOR); + + // push to remote + String remoteUri = getRemoteRepository().getWorkTree().toURI().toString(); + client.setRemote(new GitRemoteConfig("origin", + Arrays.asList(remoteUri), + Collections.emptyList(), + Arrays.asList("+refs/heads/*:refs/remotes/origin/*"), + Collections.emptyList()), + NULL_PROGRESS_MONITOR); + client.push("origin", + Arrays.asList("refs/heads/master:refs/heads/master"), + Arrays.asList("+refs/heads/master:refs/remotes/origin/master"), + NULL_PROGRESS_MONITOR); + Map branches = client.getBranches(true, NULL_PROGRESS_MONITOR); + assertTrue(branches.containsKey("origin/master")); + assertNull(branches.get("master").getTrackedBranch()); + + // set tracking + GitBranch b = client.setUpstreamBranch("master", "origin/master", NULL_PROGRESS_MONITOR); + assertEquals("origin/master", b.getTrackedBranch().getName()); + + Config cfg = repository.getConfig(); + assertEquals("origin", cfg.getString(ConfigConstants.CONFIG_BRANCH_SECTION, "master", ConfigConstants.CONFIG_KEY_REMOTE)); + assertEquals("refs/heads/master", cfg.getString(ConfigConstants.CONFIG_BRANCH_SECTION, "master", ConfigConstants.CONFIG_KEY_MERGE)); + } + +}