This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 234391
Collapse All | Expand All

(-)a/libs.git/apichanges.xml (+15 lines)
Lines 111-116 Link Here
111
        
111
        
112
        <change>
112
        <change>
113
            <api name="gitlibrary_api"/>
113
            <api name="gitlibrary_api"/>
114
            <summary>Adding a command setting the upstream branch/tracking of local branches</summary>
115
            <version major="1" minor="12"/>
116
            <date day="14" month="8" year="2013"/>
117
            <author login="ovrabec"/>
118
            <compatibility addition="yes"/>
119
            <description>
120
                <p>Adding a new method GitClient.setUpstreamBranch(String, String, ProgressMonitor)
121
                    allowing an API client to set the tracking/upstream branch for a local branch.
122
                </p>
123
            </description>
124
            <class package="org.netbeans.libs.git" name="GitClient"/>
125
            <issue number="234391"/>
126
        </change>
127
        <change>
128
            <api name="gitlibrary_api"/>
114
            <summary>Adding support comparing trees of arbitrary commits</summary>
129
            <summary>Adding support comparing trees of arbitrary commits</summary>
115
            <version major="1" minor="9"/>
130
            <version major="1" minor="9"/>
116
            <date day="12" month="3" year="2013"/>
131
            <date day="12" month="3" year="2013"/>
(-)a/libs.git/manifest.mf (-1 / +1 lines)
Lines 1-4 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.netbeans.libs.git/1
2
OpenIDE-Module: org.netbeans.libs.git/1
3
OpenIDE-Module-Localizing-Bundle: org/netbeans/libs/git/Bundle.properties
3
OpenIDE-Module-Localizing-Bundle: org/netbeans/libs/git/Bundle.properties
4
OpenIDE-Module-Specification-Version: 1.11
4
OpenIDE-Module-Specification-Version: 1.12
(-)a/libs.git/src/org/netbeans/libs/git/GitClient.java (+20 lines)
Lines 97-102 Link Here
97
import org.netbeans.libs.git.jgit.commands.SetRemoteCommand;
97
import org.netbeans.libs.git.jgit.commands.SetRemoteCommand;
98
import org.netbeans.libs.git.jgit.commands.StatusCommand;
98
import org.netbeans.libs.git.jgit.commands.StatusCommand;
99
import org.netbeans.libs.git.jgit.commands.UnignoreCommand;
99
import org.netbeans.libs.git.jgit.commands.UnignoreCommand;
100
import org.netbeans.libs.git.jgit.commands.SetUpstreamBranchCommand;
100
import org.netbeans.libs.git.progress.FileListener;
101
import org.netbeans.libs.git.progress.FileListener;
101
import org.netbeans.libs.git.progress.NotificationListener;
102
import org.netbeans.libs.git.progress.NotificationListener;
102
import org.netbeans.libs.git.progress.ProgressMonitor;
103
import org.netbeans.libs.git.progress.ProgressMonitor;
Lines 1007-1012 Link Here
1007
    }
1008
    }
1008
1009
1009
    /**
1010
    /**
1011
     * Sets the upstream branch of <code>localBranchName</code> to
1012
     * <code>remoteBranch</code>.
1013
     *
1014
     * @param localBranchName local branch supposed to track another branch
1015
     * @param remoteBranch branch from <code>remoteName</code> to be tracked
1016
     * @param monitor progress monitor
1017
     * @return info for the local branch with updated tracking
1018
     * @throws GitException error occurs
1019
     * @since 1.12
1020
     */
1021
    public GitBranch setUpstreamBranch (String localBranchName, String remoteBranch, ProgressMonitor monitor) throws GitException {
1022
        Repository repository = gitRepository.getRepository();
1023
        SetUpstreamBranchCommand cmd = new SetUpstreamBranchCommand(repository, getClassFactory(),
1024
                localBranchName, remoteBranch, monitor);
1025
        cmd.execute();
1026
        return cmd.getTrackingBranch();
1027
    }
1028
1029
    /**
1010
     * Unignores given files
1030
     * Unignores given files
1011
     * @param files files to mark unignored again and remove their respective record from <em>gitignore</em> files.
1031
     * @param files files to mark unignored again and remove their respective record from <em>gitignore</em> files.
1012
     * @param monitor progress monitor
1032
     * @param monitor progress monitor
(-)a/libs.git/src/org/netbeans/libs/git/jgit/Utils.java (+2 lines)
Lines 407-412 Link Here
407
        if (trackedBranchName != null) {
407
        if (trackedBranchName != null) {
408
            if (trackedBranchName.startsWith(Constants.R_HEADS)) {
408
            if (trackedBranchName.startsWith(Constants.R_HEADS)) {
409
                trackedBranchName = trackedBranchName.substring(Constants.R_HEADS.length());
409
                trackedBranchName = trackedBranchName.substring(Constants.R_HEADS.length());
410
            } else if (trackedBranchName.startsWith(Constants.R_REMOTES)) {
411
                trackedBranchName = trackedBranchName.substring(Constants.R_REMOTES.length());
410
            }
412
            }
411
        }
413
        }
412
        if (trackedBranchName == null) {
414
        if (trackedBranchName == null) {
(-)a/libs.git/src/org/netbeans/libs/git/jgit/commands/Bundle.properties (-1 / +2 lines)
Lines 55-58 Link Here
55
MSG_Error_CannotCatRoot = Cannot cat root: {0}
55
MSG_Error_CannotCatRoot = Cannot cat root: {0}
56
MSG_Error_Commit_ConflictsInIndex = Index contains files in conflict, please resolve them before commit
56
MSG_Error_Commit_ConflictsInIndex = Index contains files in conflict, please resolve them before commit
57
MSG_Error_Commit_PartialCommitAfterMerge = Cannot do a partial commit during a merge.
57
MSG_Error_Commit_PartialCommitAfterMerge = Cannot do a partial commit during a merge.
58
MSG_Error_Commit_NotAllowedInCurrentState = Cannot commit in current state: {0}
58
MSG_Error_Commit_NotAllowedInCurrentState = Cannot commit in current state: {0}
59
MSG_Error_UpdateTracking_InvalidReference=Invalid reference: {0}
(-)ed185686231b (+127 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2010 Sun Microsystems, Inc.
41
 */
42
43
package org.netbeans.libs.git.jgit.commands;
44
45
import java.io.IOException;
46
import java.text.MessageFormat;
47
import java.util.Map;
48
import org.eclipse.jgit.lib.ConfigConstants;
49
import org.eclipse.jgit.lib.Constants;
50
import org.eclipse.jgit.lib.Ref;
51
import org.eclipse.jgit.lib.Repository;
52
import org.eclipse.jgit.lib.StoredConfig;
53
import org.netbeans.libs.git.GitBranch;
54
import org.netbeans.libs.git.GitException;
55
import org.netbeans.libs.git.jgit.DelegatingGitProgressMonitor;
56
import org.netbeans.libs.git.jgit.GitClassFactory;
57
import org.netbeans.libs.git.jgit.Utils;
58
import org.netbeans.libs.git.progress.ProgressMonitor;
59
60
/**
61
 *
62
 * @author ondra
63
 */
64
public class SetUpstreamBranchCommand extends GitCommand {
65
    private final String localBranchName;
66
    private final String trackedBranchName;
67
    private GitBranch branch;
68
    private final ProgressMonitor monitor;
69
70
    public SetUpstreamBranchCommand (Repository repository, GitClassFactory gitFactory,
71
            String localBranchName, String trackedBranch, ProgressMonitor monitor) {
72
        super(repository, gitFactory, monitor);
73
        this.localBranchName = localBranchName;
74
        this.trackedBranchName = trackedBranch;
75
        this.monitor = monitor;
76
    }
77
78
    @Override
79
    protected void run () throws GitException {
80
        Repository repository = getRepository();
81
        
82
        try {
83
            Ref ref = repository.getRef(trackedBranchName);
84
            if (ref == null) {
85
                throw new GitException(MessageFormat.format(Utils.getBundle(SetUpstreamBranchCommand.class)
86
                        .getString("MSG_Error_UpdateTracking_InvalidReference"), trackedBranchName)); //NOI18N)
87
            }
88
            String remote = null;
89
            String branchName = ref.getName();
90
            StoredConfig config = repository.getConfig();
91
            if (branchName.startsWith(Constants.R_REMOTES)) {
92
                String[] elements = branchName.split("/", 4);
93
                remote = elements[2];
94
                if (config.getSubsections(ConfigConstants.CONFIG_REMOTE_SECTION).contains(remote)) {
95
                    branchName = Constants.R_HEADS + elements[3];
96
                } else {
97
                    // remote not yet set
98
                    remote = null;
99
                }
100
            }
101
            if (remote == null) {
102
                remote = "."; //NOI18N
103
            }
104
            config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, localBranchName,
105
                    ConfigConstants.CONFIG_KEY_REMOTE, remote);
106
            config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, localBranchName,
107
                    ConfigConstants.CONFIG_KEY_MERGE, branchName);
108
            config.save();
109
        } catch (IOException ex) {
110
            throw new GitException(ex);
111
        }
112
        ListBranchCommand branchCmd = new ListBranchCommand(repository, getClassFactory(), false, new DelegatingGitProgressMonitor(monitor));
113
        branchCmd.run();
114
        Map<String, GitBranch> branches = branchCmd.getBranches();
115
        branch = branches.get(localBranchName);
116
    }
117
118
    @Override
119
    protected String getCommandDescription () {
120
        return new StringBuilder("git branch --set-upstream-to ").append(trackedBranchName) //NOI18N
121
                .append(' ').append(localBranchName).toString();
122
    }
123
    
124
    public GitBranch getTrackingBranch () {
125
        return branch;
126
    }
127
}
(-)a/libs.git/test/unit/src/org/netbeans/libs/git/jgit/CommandsTestSuite.java (+2 lines)
Lines 74-79 Link Here
74
import org.netbeans.libs.git.jgit.commands.RenameTest;
74
import org.netbeans.libs.git.jgit.commands.RenameTest;
75
import org.netbeans.libs.git.jgit.commands.ResetTest;
75
import org.netbeans.libs.git.jgit.commands.ResetTest;
76
import org.netbeans.libs.git.jgit.commands.RevertTest;
76
import org.netbeans.libs.git.jgit.commands.RevertTest;
77
import org.netbeans.libs.git.jgit.commands.SetUpstreamBranchTest;
77
import org.netbeans.libs.git.jgit.commands.StatusTest;
78
import org.netbeans.libs.git.jgit.commands.StatusTest;
78
import org.netbeans.libs.git.jgit.commands.TagTest;
79
import org.netbeans.libs.git.jgit.commands.TagTest;
79
import org.netbeans.libs.git.jgit.commands.UnignoreTest;
80
import org.netbeans.libs.git.jgit.commands.UnignoreTest;
Lines 119-124 Link Here
119
        suite.addTestSuite(RenameTest.class);
120
        suite.addTestSuite(RenameTest.class);
120
        suite.addTestSuite(RevertTest.class);
121
        suite.addTestSuite(RevertTest.class);
121
        suite.addTestSuite(ResetTest.class);
122
        suite.addTestSuite(ResetTest.class);
123
        suite.addTestSuite(SetUpstreamBranchTest.class);
122
        suite.addTestSuite(StatusTest.class);
124
        suite.addTestSuite(StatusTest.class);
123
        suite.addTestSuite(TagTest.class);
125
        suite.addTestSuite(TagTest.class);
124
        suite.addTestSuite(UnignoreTest.class);
126
        suite.addTestSuite(UnignoreTest.class);
(-)ed185686231b (+151 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2010 Sun Microsystems, Inc.
41
 */
42
43
package org.netbeans.libs.git.jgit.commands;
44
45
import java.io.File;
46
import java.io.IOException;
47
import java.util.Arrays;
48
import java.util.Collections;
49
import java.util.Map;
50
import org.eclipse.jgit.lib.Config;
51
import org.eclipse.jgit.lib.ConfigConstants;
52
import org.eclipse.jgit.lib.Constants;
53
import org.eclipse.jgit.lib.Repository;
54
import org.netbeans.libs.git.GitBranch;
55
import org.netbeans.libs.git.GitClient;
56
import org.netbeans.libs.git.GitException;
57
import org.netbeans.libs.git.GitRemoteConfig;
58
import org.netbeans.libs.git.jgit.AbstractGitTestCase;
59
60
/**
61
 *
62
 * @author ondra
63
 */
64
public class SetUpstreamBranchTest extends AbstractGitTestCase {
65
    private File workDir;
66
    private static final String BRANCH = "mybranch";
67
    private Repository repository;
68
69
    public SetUpstreamBranchTest (String testName) throws IOException {
70
        super(testName);
71
    }
72
73
    @Override
74
    protected void setUp() throws Exception {
75
        super.setUp();
76
        workDir = getWorkingDirectory();
77
        repository = getRepository(getLocalGitRepository());
78
    }
79
    
80
    public void testLocalTracking () throws GitException {
81
        GitClient client = getClient(workDir);
82
        File f = new File(workDir, "f");
83
        add(f);
84
        client.commit(new File[] { f }, "init commit", null, null, NULL_PROGRESS_MONITOR);
85
        
86
        // prepare twp branches
87
        GitBranch b = client.createBranch(BRANCH, Constants.MASTER, NULL_PROGRESS_MONITOR);
88
        assertNull(b.getTrackedBranch());
89
        
90
        // set tracking
91
        b = client.setUpstreamBranch(BRANCH, Constants.MASTER, NULL_PROGRESS_MONITOR);
92
        assertEquals(Constants.MASTER, b.getTrackedBranch().getName());
93
    }
94
    
95
    public void testRemoteTrackingNoRemoteSet () throws GitException {
96
        GitClient client = getClient(workDir);
97
        File f = new File(workDir, "f");
98
        add(f);
99
        client.commit(new File[] { f }, "init commit", null, null, NULL_PROGRESS_MONITOR);
100
        
101
        // push to remote
102
        String remoteUri = getRemoteRepository().getWorkTree().toURI().toString();
103
        client.push(remoteUri,
104
                Arrays.asList("refs/heads/master:refs/heads/master"),
105
                Arrays.asList("+refs/heads/*:refs/remotes/origin/*"),
106
                NULL_PROGRESS_MONITOR);
107
        Map<String, GitBranch> branches = client.getBranches(true, NULL_PROGRESS_MONITOR);
108
        assertTrue(branches.containsKey("origin/master"));
109
        assertNull(branches.get("master").getTrackedBranch());
110
        
111
        // set tracking
112
        GitBranch b = client.setUpstreamBranch("master", "origin/master", NULL_PROGRESS_MONITOR);
113
        assertEquals("origin/master", b.getTrackedBranch().getName());
114
        
115
        Config cfg = repository.getConfig();
116
        assertEquals(".", cfg.getString(ConfigConstants.CONFIG_BRANCH_SECTION, "master", ConfigConstants.CONFIG_KEY_REMOTE));
117
        assertEquals("refs/remotes/origin/master", cfg.getString(ConfigConstants.CONFIG_BRANCH_SECTION, "master", ConfigConstants.CONFIG_KEY_MERGE));
118
    }
119
    
120
    public void testRemoteTracking () throws GitException {
121
        GitClient client = getClient(workDir);
122
        File f = new File(workDir, "f");
123
        add(f);
124
        client.commit(new File[] { f }, "init commit", null, null, NULL_PROGRESS_MONITOR);
125
        
126
        // push to remote
127
        String remoteUri = getRemoteRepository().getWorkTree().toURI().toString();
128
        client.setRemote(new GitRemoteConfig("origin",
129
                Arrays.asList(remoteUri),
130
                Collections.<String>emptyList(),
131
                Arrays.asList("+refs/heads/*:refs/remotes/origin/*"),
132
                Collections.<String>emptyList()),
133
                NULL_PROGRESS_MONITOR);
134
        client.push("origin",
135
                Arrays.asList("refs/heads/master:refs/heads/master"),
136
                Arrays.asList("+refs/heads/master:refs/remotes/origin/master"),
137
                NULL_PROGRESS_MONITOR);
138
        Map<String, GitBranch> branches = client.getBranches(true, NULL_PROGRESS_MONITOR);
139
        assertTrue(branches.containsKey("origin/master"));
140
        assertNull(branches.get("master").getTrackedBranch());
141
        
142
        // set tracking
143
        GitBranch b = client.setUpstreamBranch("master", "origin/master", NULL_PROGRESS_MONITOR);
144
        assertEquals("origin/master", b.getTrackedBranch().getName());
145
        
146
        Config cfg = repository.getConfig();
147
        assertEquals("origin", cfg.getString(ConfigConstants.CONFIG_BRANCH_SECTION, "master", ConfigConstants.CONFIG_KEY_REMOTE));
148
        assertEquals("refs/heads/master", cfg.getString(ConfigConstants.CONFIG_BRANCH_SECTION, "master", ConfigConstants.CONFIG_KEY_MERGE));
149
    }
150
    
151
}

Return to bug 234391