Adding a new method GitClient.setUpstreamBranch(String, String, ProgressMonitor) + allowing an API client to set the tracking/upstream branch for a local branch. +
+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