Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 333899 - Remote tracking using RevCommit as starting point for branch creation does not work.
Summary: Remote tracking using RevCommit as starting point for branch creation does no...
Status: NEW
Alias: None
Product: JGit
Classification: Technology
Component: JGit (show other bugs)
Version: 0.10.0   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-01-10 14:27 EST by Severin Gehwolf CLA
Modified: 2011-01-10 14:27 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Severin Gehwolf CLA 2011-01-10 14:27:01 EST
Here's a code snippet, which uses JGit's ListBranchCommand to list existing remote branches and creates them locally. Although the tracking option is set prior the call to branchCreateCmd.call(), no tracking configuration is written to .git/config.

try {
        // get a list of branches
        ListBranchCommand branchList = git.branchList();
        branchList.setListMode(ListMode.REMOTE); // want all remote branches
        List<Ref> remoteRefs = branchList.call();
        for (Ref remoteRef: remoteRefs) {
        String name = remoteRef.getName();
        int remoteIndex = (Constants.R_REMOTES + "origin/").length(); //$NON-NLS-1$
        // Remove "refs/remotes/origin/" part in branch name
        name = name.substring(remoteIndex);
        if (!name.equals(Constants.MASTER)) {
                CreateBranchCommand branchCreateCmd = git.branchCreate();
                branchCreateCmd.setName(name);
                RevWalk walk = new RevWalk(git.getRepository());
                RevCommit startCommit = walk.parseCommit(remoteRef
                .getObjectId());
                branchCreateCmd.setStartPoint(startCommit);
                // Add remote tracking config
                branchCreateCmd.setUpstreamMode(SetupUpstreamMode.TRACK);
                branchCreateCmd.call();
        }
        }
} catch (IOException ioException) {
        ioException.printStackTrace();
} catch (JGitInternalException e) {
        e.printStackTrace();
} catch (RefAlreadyExistsException e) {
        e.printStackTrace();
} catch (RefNotFoundException e) {
        e.printStackTrace();
} catch (InvalidRefNameException e) {
        e.printStackTrace();
}


Replacing

RevWalk walk = new RevWalk(git.getRepository());
RevCommit startCommit = walk.parseCommit(remoteRef
                .getObjectId());
branchCreateCmd.setStartPoint(startCommit);

with

branchCreateCmd.setStartPoint(remoteRef.getName());

works.