Commit 6dda2da5 authored by Guido Günther's avatar Guido Günther

pull: allow to set up branch tracking for missing branches

If the remote branch does not exist at all that's currently not fatal.
parent ea321fdf
......@@ -29,6 +29,7 @@
<arg><option>--ignore-branch</option></arg>
<arg><option>--debian-branch=</option><replaceable>branch_name</replaceable></arg>
<arg><option>--upstream-branch=</option><replaceable>branch_name</replaceable></arg>
<arg><option>--track-missing</option></arg>
<arg><option>--depth=</option><replaceable>depth</replaceable></arg>
<arg><replaceable>repository</replaceable></arg>
</cmdsynopsis>
......@@ -100,6 +101,20 @@
onto. Default is <replaceable>upstream</replaceable>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--track-missing</option></term>
<listitem>
<para>
If a branch is missing in the local repository try to
fetch it from the remote side and (if it exists) fetch it and set up
branch tracking for it. This ensures that branches that are added at a
later point to the remote side (e.g.
<replaceable>pristine-tar</replaceable>) are picked up automatically.
Note that it's not an error for the branch to not exist on the remote
side.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--depth</option>=<replaceable>depth</replaceable>
</term>
......
......@@ -99,6 +99,7 @@ def build_parser(name):
branch_group.add_config_file_option(option_name="upstream-branch", dest="upstream_branch")
branch_group.add_config_file_option(option_name="debian-branch", dest="debian_branch")
branch_group.add_boolean_config_file_option(option_name="pristine-tar", dest="pristine_tar")
branch_group.add_boolean_config_file_option(option_name="track-missing", dest="track_missing")
branch_group.add_option("--depth", action="store", dest="depth", default=0,
help="git history depth (for deepening shallow clones)")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
......@@ -129,6 +130,17 @@ def get_remote(repo, current):
return fetch_remote
def track_missing(repo, remote, branch, options):
upstream = "remotes/{}/{}".format(remote, branch)
if not repo.has_branch(branch):
try:
repo.fetch(remote, depth=options.depth, refspec=branch)
except GitRepositoryError:
pass # it's o.k. if the remote branch is missing
else:
repo.create_branch(branch, upstream)
def main(argv):
retval = 0
current = None
......@@ -175,11 +187,21 @@ def main(argv):
fetch_remote = get_remote(repo, current)
for branch in [options.debian_branch, options.upstream_branch]:
if not branch:
continue
if options.track_missing:
track_missing(repo, fetch_remote, branch, options)
if repo.has_branch(branch):
branches.add(branch)
if repo.has_pristine_tar_branch() and options.pristine_tar:
branches.add(repo.pristine_tar_branch)
if options.pristine_tar:
branch = repo.pristine_tar_branch
if options.track_missing:
track_missing(repo, fetch_remote, branch, options)
if repo.has_pristine_tar_branch():
branches.add(repo.pristine_tar_branch)
if options.all:
for branch in repo.get_local_branches():
......
......@@ -79,3 +79,28 @@ class TestPull(ComponentTestBase):
eq_(pull(['argv0', '--all']), 0)
eq_(len(cloned.get_commits(until='foob')), 3)
eq_(len(cloned.get_commits(until='upstream')), 2)
@RepoFixtures.native()
def test_tracking(self, repo):
"""Test that --track-missing picks up missing branches"""
dest = os.path.join(self._tmpdir, 'cloned_repo')
clone(['arg0', repo.path, dest])
cloned = ComponentTestGitRepository(dest)
os.chdir(cloned.path)
self._check_repo_state(cloned, 'master', ['master'])
# Pull initially
eq_(pull(['argv0']), 0)
assert len(repo.get_commits()) == 1
self._check_repo_state(cloned, 'master', ['master'])
# Pick up missing branches (none exist yet)
eq_(pull(['argv0', '--track-missing']), 0)
assert len(repo.get_commits()) == 1
self._check_repo_state(cloned, 'master', ['master'])
# Pick up missing branches
repo.create_branch('pristine-tar')
repo.create_branch('upstream')
eq_(pull(['argv0', '--track-missing', '--pristine-tar']), 0)
assert len(repo.get_commits()) == 1
self._check_repo_state(cloned, 'master', ['master', 'pristine-tar', 'upstream'])
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