Commit f96d0f92 authored by Michael Pyne's avatar Michael Pyne
Browse files

Reorg. git remote updating to prep for git tag support.

To support git tags we need to take different semantic actions (as
checking-out a git tag leaves you in a detached-HEAD state where
git-{pull,merge,rebase} don't apply). To prepare for having different
methods for these different semantics I'm factoring out the git-remote
setup into a separate function, and removing the needless support for
lists of possibilities. Nowadays kdesrc-build only cares about origin,
though if you want to bring your own remote that's fine, kdesrc-build
will find it.
parent a7b2b9cc
......@@ -2629,32 +2629,24 @@ EOF
return 0;
}
# Updates an already existing git checkout by running git pull.
# Selects a git remote for the user's selected repository (preferring a
# defined remote if available, using 'origin' otherwise).
#
# Return parameter is the number of affected *commits*. Errors are
# returned only via exceptions because of this.
sub updateExistingClone
# Assumes the current directory is already set to the source directory.
#
# Return value: Remote name that should be used for further updates.
#
# See also the 'repository' module option.
sub _setupBestRemote
{
my $self = assert_isa(shift, 'GitUpdate');
my $module = $self->module();
my $srcdir = $module->fullpath('source');
my $cur_repo = $module->getOption('repository');
my $branch = $self->getBranch();
my $result;
p_chdir($srcdir);
note ("Updating g[$module] (to branch b[$branch])");
my $start_commit = $self->commit_id('HEAD');
# Search for an existing remote name first. If none, add our alias.
my @remoteNames = $self->bestRemoteName($cur_repo);
my $remoteName = DEFAULT_GIT_REMOTE;
if (@remoteNames) {
$remoteName = $remoteNames[0];
}
else {
if (!@remoteNames) {
# The desired repo doesn't have a named remote, this should be
# because the user switched it in the rc-file. We control the
# 'origin' remote to fix this.
......@@ -2676,6 +2668,7 @@ EOF
push @remoteNames, DEFAULT_GIT_REMOTE;
}
# Make a notice if the repository we're using has moved.
my $old_repo = $module->getPersistentOption('git-cloned-repository');
if ($old_repo and ($cur_repo ne $old_repo)) {
note (" y[b[*]\ty[$module]'s selected repository has changed");
......@@ -2687,6 +2680,29 @@ EOF
$module->setPersistentOption('git-cloned-repository', $cur_repo);
}
return $remoteNames[0];
}
# Updates an already existing git checkout by running git pull.
#
# Return parameter is the number of affected *commits*. Errors are
# returned only via exceptions because of this.
sub updateExistingClone
{
my $self = assert_isa(shift, 'GitUpdate');
my $module = $self->module();
my $srcdir = $module->fullpath('source');
my $cur_repo = $module->getOption('repository');
my $branch = $self->getBranch();
my $result;
p_chdir($srcdir);
note ("Updating g[$module] (to branch b[$branch])");
my $start_commit = $self->commit_id('HEAD');
my $remoteName = $self->_setupBestRemote();
# Download updated objects. This also updates remote heads so do this
# before we start comparing branches and such, even though we will
# later use git pull.
......@@ -2702,10 +2718,10 @@ EOF
# found must also be remote-tracking. If this is all true we just
# re-use that branch, otherwise we create our own remote-tracking
# branch.
my $branchName = $self->getRemoteBranchName(\@remoteNames, $branch);
my $branchName = $self->getRemoteBranchName($remoteName, $branch);
if (!$branchName) {
my $newName = $self->makeBranchname(\@remoteNames, $branch);
my $newName = $self->makeBranchname($remoteName, $branch);
whisper ("\tUpdating g[$module] with new remote-tracking branch y[$newName]");
if (0 != log_command($module, 'git-checkout-branch',
['git', 'checkout', '-b', $newName, "$remoteName/$branch"]))
......@@ -2936,45 +2952,39 @@ EOF
#
# The current directory must be the source directory of the git module.
#
# First parameter : A *reference* to a list of remote names to check against.
# It is important that this list all really point against
# the same repository URL however. (See bestRemoteName)
# First parameter : The git remote to use (normally origin).
# Second parameter: The remote head name to find a local branch for.
# Returns: Empty string if no match is found, or the name of the local
# remote-tracking branch if one exists.
sub getRemoteBranchName
{
my $self = assert_isa(shift, 'GitUpdate');
my $remoteNamesRef = shift;
my $remoteName = shift;
my $branchName = shift;
# Dereference our remote names.
my @remoteNames = @{$remoteNamesRef};
# Look for our branchName in each possible remote alias.
foreach my $remoteName (@remoteNames) {
# We'll parse git config output to search for branches that have a
# remote of $remoteName and a 'merge' of refs/heads/$branchName.
# We'll parse git config output to search for branches that have a
# remote of $remoteName and a 'merge' of refs/heads/$branchName.
my @branches = slurp_git_config_output(
qw/git config --null --get-regexp branch\..*\.remote/, $remoteName
);
# TODO: Replace with git for-each-ref refs/heads and the %(upstream)
# format.
my @branches = slurp_git_config_output(
qw/git config --null --get-regexp branch\..*\.remote/, $remoteName
);
foreach my $gitBranch (@branches) {
# The key/value is \n separated, we just want the key.
my ($keyName) = split(/\n/, $gitBranch);
my ($thisBranch) = ($keyName =~ m/^branch\.(.*)\.remote$/);
foreach my $gitBranch (@branches) {
# The key/value is \n separated, we just want the key.
my ($keyName) = split(/\n/, $gitBranch);
my ($thisBranch) = ($keyName =~ m/^branch\.(.*)\.remote$/);
# We have the local branch name, see if it points to the remote
# branch we want.
my @configOutput = slurp_git_config_output(
qw/git config --null/, "branch.$thisBranch.merge"
);
# We have the local branch name, see if it points to the remote
# branch we want.
my @configOutput = slurp_git_config_output(
qw/git config --null/, "branch.$thisBranch.merge"
);
if(@configOutput && $configOutput[0] eq "refs/heads/$branchName") {
# We have a winner
return $thisBranch;
}
if (@configOutput && $configOutput[0] eq "refs/heads/$branchName") {
# We have a winner
return $thisBranch;
}
}
......@@ -3050,9 +3060,7 @@ EOF
# exist.
#
# First parameter: The Module being worked on.
# Second parameter: A *reference* to a list of remote names (all pointing to
# the same repository) which are valid. See also
# "bestRemoteName"
# Second parameter: The name of a git remote to use.
# Third parameter: The name of the remote head we need to make a branch name
# of.
# Returns: A useful branch name that doesn't already exist, or '' if no
......@@ -3060,16 +3068,14 @@ EOF
sub makeBranchname
{
my $self = assert_isa(shift, 'GitUpdate');
my $remoteNamesRef = shift;
my $remoteName = shift || 'origin';
my $branch = shift;
my $module = $self->module();
my $chosenName;
# Use "$branch" directly if not already used, otherwise try
# to prefix with the best remote name or origin.
my $bestRemoteName = $remoteNamesRef ? $remoteNamesRef->[0] : 'origin';
for my $possibleBranch ($branch, "$bestRemoteName-$branch", "origin-$branch") {
# Use "$branch" directly if not already used, otherwise try to prefix
# with the remote name.
for my $possibleBranch ($branch, "$remoteName-$branch", "ksdc-$remoteName-$branch") {
my $result = safe_system('git', 'show-ref', '--quiet', '--verify',
'--', "/refs/heads/$possibleBranch");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment