Parcourir la source

Teach BranchTrackingStatus to handle tracking of local branches

EGit wasn't able to decorate local branches tracking another local
branch with number of commits the checked out local branch differs from
the other local branch it's tracking.

Bug: 376970 
Change-Id: I74e932d5eacd74dbf6b0dffcfc65ba3222a8250e
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
tags/v2.1.0.201209190230-r
Matthias Sohn il y a 11 ans
Parent
révision
376a741d8f

+ 40
- 0
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/BranchConfigTest.java Voir le fichier

/* /*
* Copyright (C) 2011, Robin Stocker <robin@nibor.org> * Copyright (C) 2011, Robin Stocker <robin@nibor.org>
* Copyright (C) 2012, Matthias Sohn <matthias.sohn@sap.com>
* and other copyright owners as documented in the project's IP log. * and other copyright owners as documented in the project's IP log.
* *
* This program and the accompanying materials are made available * This program and the accompanying materials are made available
assertNull(branchConfig.getRemoteTrackingBranch()); assertNull(branchConfig.getRemoteTrackingBranch());
} }


@Test
public void getTrackingBranchShouldReturnMergeBranchForLocalBranch() {
Config c = parse("" //
+ "[remote \"origin\"]\n"
+ " fetch = +refs/heads/*:refs/remotes/origin/*\n"
+ "[branch \"master\"]\n"
+ " remote = .\n"
+ " merge = refs/heads/master\n");

BranchConfig branchConfig = new BranchConfig(c, "master");
assertEquals("refs/heads/master",
branchConfig.getTrackingBranch());
}

@Test
public void getTrackingBranchShouldReturnNullWithoutMergeBranchForLocalBranch() {
Config c = parse("" //
+ "[remote \"origin\"]\n"
+ " fetch = +refs/heads/onlyone:refs/remotes/origin/onlyone\n"
+ "[branch \"master\"]\n" //
+ " remote = .\n");
BranchConfig branchConfig = new BranchConfig(c, "master");
assertNull(branchConfig.getTrackingBranch());
}

@Test
public void getTrackingBranchShouldHandleNormalCaseForRemoteTrackingBranch() {
Config c = parse("" //
+ "[remote \"origin\"]\n"
+ " fetch = +refs/heads/*:refs/remotes/origin/*\n"
+ "[branch \"master\"]\n"
+ " remote = origin\n"
+ " merge = refs/heads/master\n");

BranchConfig branchConfig = new BranchConfig(c, "master");
assertEquals("refs/remotes/origin/master",
branchConfig.getTrackingBranch());
}

private Config parse(final String content) { private Config parse(final String content) {
final Config c = new Config(null); final Config c = new Config(null);
try { try {

+ 34
- 2
org.eclipse.jgit/src/org/eclipse/jgit/lib/BranchConfig.java Voir le fichier

/* /*
* Copyright (C) 2011, Robin Stocker <robin@nibor.org> * Copyright (C) 2011, Robin Stocker <robin@nibor.org>
* Copyright (C) 2012, Matthias Sohn <matthias.sohn@sap.com>
* and other copyright owners as documented in the project's IP log. * and other copyright owners as documented in the project's IP log.
* *
* This program and the accompanying materials are made available * This program and the accompanying materials are made available
} }


/** /**
* @return the full remote-tracking branch name or <code>null</code> if it
* could not be determined
* @return the full tracking branch name or <code>null</code> if it could
* not be determined
*/
public String getTrackingBranch() {
String remote = getRemote();
String mergeRef = getMergeBranch();
if (remote == null || mergeRef == null)
return null;

if (remote.equals("."))
return mergeRef;

return findRemoteTrackingBranch(remote, mergeRef);
}

/**
* @return the full remote-tracking branch name or {@code null} if it could
* not be determined. If you also want local tracked branches use
* {@link #getTrackingBranch()} instead.
*/ */
public String getRemoteTrackingBranch() { public String getRemoteTrackingBranch() {
String remote = getRemote(); String remote = getRemote();
if (remote == null || mergeRef == null) if (remote == null || mergeRef == null)
return null; return null;


return findRemoteTrackingBranch(remote, mergeRef);
}

/**
* Finds the tracked remote tracking branch
*
* @param remote
* Remote name
* @param mergeRef
* merge Ref of the local branch tracking the remote tracking
* branch
* @return full remote tracking branch name or null
*/
private String findRemoteTrackingBranch(String remote, String mergeRef) {
RemoteConfig remoteConfig; RemoteConfig remoteConfig;
try { try {
remoteConfig = new RemoteConfig(config, remote); remoteConfig = new RemoteConfig(config, remote);

+ 5
- 4
org.eclipse.jgit/src/org/eclipse/jgit/lib/BranchTrackingStatus.java Voir le fichier

/* /*
* Copyright (C) 2011, Robin Stocker <robin@nibor.org> * Copyright (C) 2011, Robin Stocker <robin@nibor.org>
* Copyright (C) 2012, Matthias Sohn <matthias.sohn@sap.com>
* and other copyright owners as documented in the project's IP log. * and other copyright owners as documented in the project's IP log.
* *
* This program and the accompanying materials are made available * This program and the accompanying materials are made available
BranchConfig branchConfig = new BranchConfig(repository.getConfig(), BranchConfig branchConfig = new BranchConfig(repository.getConfig(),
branchName); branchName);


String remoteTrackingBranch = branchConfig.getRemoteTrackingBranch();
if (remoteTrackingBranch == null)
String trackingBranch = branchConfig.getTrackingBranch();
if (trackingBranch == null)
return null; return null;


Ref tracking = repository.getRef(remoteTrackingBranch);
Ref tracking = repository.getRef(trackingBranch);
if (tracking == null) if (tracking == null)
return null; return null;


int aheadCount = RevWalkUtils.count(walk, localCommit, mergeBase); int aheadCount = RevWalkUtils.count(walk, localCommit, mergeBase);
int behindCount = RevWalkUtils.count(walk, trackingCommit, mergeBase); int behindCount = RevWalkUtils.count(walk, trackingCommit, mergeBase);


return new BranchTrackingStatus(remoteTrackingBranch, aheadCount, behindCount);
return new BranchTrackingStatus(trackingBranch, aheadCount, behindCount);
} }


private final String remoteTrackingBranch; private final String remoteTrackingBranch;

Chargement…
Annuler
Enregistrer