// specified explicitly
throw new DetachedHeadException();
}
branchName = fullBranch.substring(Constants.R_HEADS.length());
} catch (IOException e) {
throw new JGitInternalException(
JGitText.get().exceptionCaughtDuringExecutionOfPullCommand,
e);
}
if (!repo.getRepositoryState().equals(RepositoryState.SAFE))
throw new WrongRepositoryStateException(MessageFormat.format(
JGitText.get().cannotPullOnARepoWithState, repo
.getRepositoryState().name()));
// get the configured remote for the currently checked out branch
// stored in configuration key branch.<branch name>.remote
Config repoConfig = repo.getConfig();
String remote = repoConfig.getString(
ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
ConfigConstants.CONFIG_KEY_REMOTE);
if (remote == null)
// fall back to default remote
remote = Constants.DEFAULT_REMOTE_NAME;
// get the name of the branch in the remote repository
// stored in configuration key branch.<branch name>.merge
String remoteBranchName = repoConfig.getString(
ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
ConfigConstants.CONFIG_KEY_MERGE);
// check if the branch is configured for pull-rebase
boolean doRebase = repoConfig.getBoolean(
ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
ConfigConstants.CONFIG_KEY_REBASE, false);
if (remoteBranchName == null) {
String missingKey = ConfigConstants.CONFIG_BRANCH_SECTION + DOT
+ branchName + DOT + ConfigConstants.CONFIG_KEY_MERGE;
throw new InvalidConfigurationException(MessageFormat.format(
JGitText.get().missingConfigurationForKey, missingKey));
}
final boolean isRemote = !remote.equals(".");
String remoteUri;
FetchResult fetchRes;
if (isRemote) {
remoteUri = repoConfig.getString(
ConfigConstants.CONFIG_REMOTE_SECTION, remote,
ConfigConstants.CONFIG_KEY_URL);
if (remoteUri == null) {
String missingKey = ConfigConstants.CONFIG_REMOTE_SECTION + DOT
+ remote + DOT + ConfigConstants.CONFIG_KEY_URL;
throw new InvalidConfigurationException(MessageFormat.format(
JGitText.get().missingConfigurationForKey, missingKey));
}
if (monitor.isCancelled())
throw new CanceledException(MessageFormat.format(
JGitText.get().operationCanceled,
JGitText.get().pullTaskName));
FetchCommand fetch = new FetchCommand(repo);
fetch.setRemote(remote);
fetch.setProgressMonitor(monitor);
configure(fetch);
fetchRes = fetch.call();
} else {
// we can skip the fetch altogether
remoteUri = "local repository";
fetchRes = null;
}
monitor.update(1);
if (monitor.isCancelled())
throw new CanceledException(MessageFormat.format(
JGitText.get().operationCanceled,
JGitText.get().pullTaskName));
// we check the updates to see which of the updated branches
// corresponds
// to the remote branch name
AnyObjectId commitToMerge;
if (isRemote) {
Ref r = null;
if (fetchRes != null) {
r = fetchRes.getAdvertisedRef(remoteBranchName);
if (r == null)
r = fetchRes.getAdvertisedRef(Constants.R_HEADS
+ remoteBranchName);
}
if (r == null)
throw new JGitInternalException(MessageFormat.format(JGitText
.get().couldNotGetAdvertisedRef, remoteBranchName));
else
commitToMerge = r.getObjectId();
} else {
try {
commitToMerge = repo.resolve(remoteBranchName);
if (commitToMerge == null)
throw new RefNotFoundException(MessageFormat.format(
JGitText.get().refNotResolved, remoteBranchName));
} catch (IOException e) {
throw new JGitInternalException(
JGitText.get().exceptionCaughtDuringExecutionOfPullCommand,
e);
}
}
PullResult result;
if (doRebase) {
RebaseCommand rebase = new RebaseCommand(repo);
try {
RebaseResult rebaseRes = rebase.setUpstream(commitToMerge)
.setProgressMonitor(monitor).setOperation(
Operation.BEGIN).call();
result = new PullResult(fetchRes, remote, rebaseRes);
} catch (NoHeadException e) {
throw new JGitInternalException(e.getMessage(), e);
} catch (RefNotFoundException e) {
throw new JGitInternalException(e.getMessage(), e);
} catch (JGitInternalException e) {
throw new JGitInternalException(e.getMessage(), e);
} catch (GitAPIException e) {
throw new JGitInternalException(e.getMessage(), e);
}
} else {
MergeCommand merge = new MergeCommand(repo);
String name = "branch \'"
+ Repository.shortenRefName(remoteBranchName) + "\' of "
+ remoteUri;
merge.include(name, commitToMerge);
MergeResult mergeRes;
try {
mergeRes = merge.call();
monitor.update(1);
result = new PullResult(fetchRes, remote, mergeRes);
} catch (NoHeadException e) {
throw new JGitInternalException(e.getMessage(), e);
} catch (ConcurrentRefUpdateException e) {
throw new JGitInternalException(e.getMessage(), e);
} catch (CheckoutConflictException e) {
throw new JGitInternalException(e.getMessage(), e);
} catch (InvalidMergeHeadsException e) {
throw new JGitInternalException(e.getMessage(), e);
} catch (WrongRepositoryStateException e) {
throw new JGitInternalException(e.getMessage(), e);
} catch (NoMessageException e) {
throw new JGitInternalException(e.getMessage(), e);
}
}
monitor.endTask();
return result;
}