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 245078
Collapse All | Expand All

(-)libs.git/apichanges.xml (+17 lines)
Lines 111-116 Link Here
111
        
111
        
112
        <change>
112
        <change>
113
            <api name="gitlibrary_api"/>
113
            <api name="gitlibrary_api"/>
114
            <summary>New method for updating a reference (branch) to a new commit id.</summary>
115
            <version major="1" minor="24"/>
116
            <date day="19" month="6" year="2014"/>
117
            <author login="ovrabec"/>
118
            <compatibility addition="yes"/>
119
            <description>
120
                <p>In order to simplify a trivial use-case of merging a remote branch into a local branch
121
                    not currently checked-out we're adding a new method able to update the local branch to
122
                    the new id. It is possible only if the branch requires a fast-forward merge (does not
123
                    contain any unpushed commits).
124
                </p>
125
            </description>
126
            <class package="org.netbeans.libs.git" name="GitClient"/>
127
            <issue number="245078"/>
128
        </change>
129
        <change>
130
            <api name="gitlibrary_api"/>
114
            <summary>New methods in ProgressMonitor informing about the progress of command's subtasks.</summary>
131
            <summary>New methods in ProgressMonitor informing about the progress of command's subtasks.</summary>
115
            <version major="1" minor="21"/>
132
            <version major="1" minor="21"/>
116
            <date day="4" month="3" year="2014"/>
133
            <date day="4" month="3" year="2014"/>
(-)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.23
4
OpenIDE-Module-Specification-Version: 1.24
(-)libs.git/src/org/netbeans/libs/git/GitClient.java (+20 lines)
Lines 101-106 Link Here
101
import org.netbeans.libs.git.jgit.commands.SubmoduleInitializeCommand;
101
import org.netbeans.libs.git.jgit.commands.SubmoduleInitializeCommand;
102
import org.netbeans.libs.git.jgit.commands.SubmoduleStatusCommand;
102
import org.netbeans.libs.git.jgit.commands.SubmoduleStatusCommand;
103
import org.netbeans.libs.git.jgit.commands.SubmoduleUpdateCommand;
103
import org.netbeans.libs.git.jgit.commands.SubmoduleUpdateCommand;
104
import org.netbeans.libs.git.jgit.commands.UpdateRefCommand;
104
import org.netbeans.libs.git.progress.FileListener;
105
import org.netbeans.libs.git.progress.FileListener;
105
import org.netbeans.libs.git.progress.NotificationListener;
106
import org.netbeans.libs.git.progress.NotificationListener;
106
import org.netbeans.libs.git.progress.ProgressMonitor;
107
import org.netbeans.libs.git.progress.ProgressMonitor;
Lines 1099-1104 Link Here
1099
        cmd.execute();
1100
        cmd.execute();
1100
        return cmd.getModifiedIgnoreFiles();
1101
        return cmd.getModifiedIgnoreFiles();
1101
    }
1102
    }
1103
    
1104
    /**
1105
     * Updates a given reference to a new id. If the new id is also a ref then
1106
     * the ref will be updated to the same commit id referenced by the new id.
1107
     * The update will not be permitted if the reference is not merged into the
1108
     * new commit (i.e. contains its private commits).
1109
     *
1110
     * @param referenceName name of the ref to update
1111
     * @param newId the new reference id
1112
     * @param monitor progress monitor
1113
     * @return result result of the update
1114
     * @throws GitException an unexpected error happens
1115
     */
1116
    public GitRefUpdateResult updateReference (String referenceName, String newId, ProgressMonitor monitor) throws GitException {
1117
        Repository repository = gitRepository.getRepository();
1118
        UpdateRefCommand cmd = new UpdateRefCommand(repository, getClassFactory(), referenceName, newId, monitor);
1119
        cmd.execute();
1120
        return cmd.getResult();
1121
    }
1102
1122
1103
    /**
1123
    /**
1104
     * Updates submodules. An equivalent to submodule update command.
1124
     * Updates submodules. An equivalent to submodule update command.
(-)libs.git/src/org/netbeans/libs/git/jgit/commands/UpdateRefCommand.java (+114 lines)
Line 0 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 org.eclipse.jgit.lib.ObjectId;
47
import org.eclipse.jgit.lib.ObjectIdRef;
48
import org.eclipse.jgit.lib.Ref;
49
import org.eclipse.jgit.lib.RefUpdate;
50
import org.eclipse.jgit.lib.Repository;
51
import org.netbeans.libs.git.GitException;
52
import org.netbeans.libs.git.GitRefUpdateResult;
53
import org.netbeans.libs.git.jgit.GitClassFactory;
54
import org.netbeans.libs.git.progress.ProgressMonitor;
55
56
/**
57
 *
58
 * @author ondra
59
 */
60
public class UpdateRefCommand extends GitCommand {
61
    private final String revision;
62
    private final String refName;
63
    private GitRefUpdateResult result;
64
65
    public UpdateRefCommand (Repository repository, GitClassFactory gitFactory, String refName, String revision, ProgressMonitor monitor) {
66
        super(repository, gitFactory, monitor);
67
        this.refName = refName;
68
        this.revision = revision;
69
    }
70
71
    @Override
72
    protected void run () throws GitException {
73
        Repository repository = getRepository();
74
        try {
75
            
76
            Ref ref = repository.getRef(refName);
77
            if (ref == null || ref.isSymbolic()) {
78
                // currently unable to update symbolic references
79
                result = GitRefUpdateResult.valueOf(RefUpdate.Result.NOT_ATTEMPTED.name());
80
                return;
81
            }
82
            
83
            Ref newRef = repository.getRef(revision);
84
            String name;
85
            if (newRef == null) {
86
                ObjectId id = repository.resolve(revision);
87
                newRef = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, id.name(),id.copy());
88
                name = newRef.getName();
89
            } else {
90
                name = revision;
91
            }
92
            
93
            RefUpdate u = repository.updateRef(ref.getName());
94
            u.setNewObjectId(newRef.isPeeled() ? newRef.getPeeledObjectId() : newRef.getObjectId());
95
            u.setRefLogMessage("merge " + name + ": Fast-forward", false); //NOI18N
96
            u.update();
97
            result = GitRefUpdateResult.valueOf((u.getResult() == null 
98
                ? RefUpdate.Result.NOT_ATTEMPTED
99
                : u.getResult()).name());
100
        } catch (IOException ex) {
101
            throw new GitException(ex);
102
        }
103
    }
104
105
    @Override
106
    protected String getCommandDescription () {
107
        return new StringBuilder("git update-ref ").append(refName).append(revision).toString(); //NOI18N
108
    }
109
110
    public GitRefUpdateResult getResult () {
111
        return result;
112
    }
113
    
114
}
(-)libs.git/test/unit/src/org/netbeans/libs/git/jgit/CommandsTestSuite.java (+2 lines)
Lines 80-85 Link Here
80
import org.netbeans.libs.git.jgit.commands.SubmoduleTest;
80
import org.netbeans.libs.git.jgit.commands.SubmoduleTest;
81
import org.netbeans.libs.git.jgit.commands.TagTest;
81
import org.netbeans.libs.git.jgit.commands.TagTest;
82
import org.netbeans.libs.git.jgit.commands.UnignoreTest;
82
import org.netbeans.libs.git.jgit.commands.UnignoreTest;
83
import org.netbeans.libs.git.jgit.commands.UpdateRefTest;
83
84
84
/**
85
/**
85
 *
86
 *
Lines 128-133 Link Here
128
        suite.addTestSuite(SubmoduleTest.class);
129
        suite.addTestSuite(SubmoduleTest.class);
129
        suite.addTestSuite(TagTest.class);
130
        suite.addTestSuite(TagTest.class);
130
        suite.addTestSuite(UnignoreTest.class);
131
        suite.addTestSuite(UnignoreTest.class);
132
        suite.addTestSuite(UpdateRefTest.class);
131
        return suite;
133
        return suite;
132
    }
134
    }
133
135
(-)libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/UpdateRefTest.java (+156 lines)
Line 0 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 org.eclipse.jgit.lib.ReflogReader;
48
import org.eclipse.jgit.lib.Repository;
49
import org.netbeans.libs.git.GitClient;
50
import org.netbeans.libs.git.GitRefUpdateResult;
51
import org.netbeans.libs.git.GitRevisionInfo;
52
import org.netbeans.libs.git.jgit.AbstractGitTestCase;
53
54
/**
55
 *
56
 * @author ondra
57
 */
58
public class UpdateRefTest extends AbstractGitTestCase {
59
    private Repository repository;
60
    private File workDir;
61
62
    public UpdateRefTest (String testName) throws IOException {
63
        super(testName);
64
    }
65
66
    @Override
67
    protected void setUp() throws Exception {
68
        super.setUp();
69
        workDir = getWorkingDirectory();
70
        repository = getRepository(getLocalGitRepository());
71
    }
72
73
    public void testNotAttempted () throws Exception {
74
        File f = new File(workDir, "f");
75
        write(f, "init");
76
        add(f);
77
        commit(f);
78
        
79
        write(f, "modi");
80
        add(f);
81
        commit(f);
82
        
83
        GitClient client = getClient(workDir);
84
        GitRevisionInfo info = client.log("HEAD", NULL_PROGRESS_MONITOR);
85
        
86
        client.reset("HEAD~1", GitClient.ResetType.HARD, NULL_PROGRESS_MONITOR);
87
        
88
        GitRefUpdateResult res = client.updateReference("HEAD", info.getRevision(), NULL_PROGRESS_MONITOR);
89
        assertEquals(GitRefUpdateResult.NOT_ATTEMPTED, res);
90
    }
91
92
    public void testMoveMergeCommit () throws Exception {
93
        File f = new File(workDir, "f");
94
        write(f, "init");
95
        add(f);
96
        commit(f);
97
        
98
        write(f, "modif");
99
        add(f);
100
        commit(f);
101
        
102
        GitClient client = getClient(workDir);
103
        GitRevisionInfo info = client.log("HEAD", NULL_PROGRESS_MONITOR);
104
        
105
        client.reset("HEAD~1", GitClient.ResetType.HARD, NULL_PROGRESS_MONITOR);
106
        
107
        GitRefUpdateResult res = client.updateReference("master", info.getRevision(), NULL_PROGRESS_MONITOR);
108
        assertEquals(GitRefUpdateResult.FAST_FORWARD, res);
109
        ReflogReader reflogReader = repository.getReflogReader("master");
110
        assertEquals("merge " + info.getRevision() + ": Fast-forward", reflogReader.getLastEntry().getComment());
111
    }
112
113
    public void testMoveMergeRef () throws Exception {
114
        File f = new File(workDir, "f");
115
        write(f, "init");
116
        add(f);
117
        commit(f);
118
        
119
        write(f, "modif");
120
        add(f);
121
        commit(f);
122
        
123
        GitClient client = getClient(workDir);
124
        client.createBranch("BRANCH", "HEAD", NULL_PROGRESS_MONITOR);
125
        
126
        client.reset("HEAD~1", GitClient.ResetType.HARD, NULL_PROGRESS_MONITOR);
127
        
128
        GitRefUpdateResult res = client.updateReference("master", "BRANCH", NULL_PROGRESS_MONITOR);
129
        assertEquals(GitRefUpdateResult.FAST_FORWARD, res);
130
        ReflogReader reflogReader = repository.getReflogReader("master");
131
        assertEquals("merge BRANCH: Fast-forward", reflogReader.getLastEntry().getComment());
132
    }
133
134
    // must fail if would end in a non FF update
135
    public void testMoveMergeRejected () throws Exception {
136
        File f = new File(workDir, "f");
137
        write(f, "init");
138
        add(f);
139
        commit(f);
140
        
141
        write(f, "modif");
142
        add(f);
143
        commit(f);
144
        
145
        GitClient client = getClient(workDir);
146
        client.createBranch("BRANCH", "HEAD", NULL_PROGRESS_MONITOR);
147
        
148
        client.reset("HEAD~1", GitClient.ResetType.HARD, NULL_PROGRESS_MONITOR);
149
        
150
        write(f, "modif2");
151
        add(f);
152
        commit(f);
153
        GitRefUpdateResult res = client.updateReference("master", "BRANCH", NULL_PROGRESS_MONITOR);
154
        assertEquals(GitRefUpdateResult.REJECTED, res);
155
    }
156
}

Return to bug 245078