diff options
author | Guido Günther <agx@sigxcpu.org> | 2018-08-08 10:29:40 +0200 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2018-08-08 11:27:45 +0200 |
commit | 6dda2da54efbb81e4e1593d57eb5f30c4ef1e6d8 (patch) | |
tree | 7815a6a9a4c789d217907c754e34d89b51a3abe3 | |
parent | ea321fdfa1ec917038d09c14036ae9e27108735b (diff) |
pull: allow to set up branch tracking for missing branches
If the remote branch does not exist at all that's currently not fatal.
-rw-r--r-- | docs/manpages/gbp-pull.xml | 15 | ||||
-rwxr-xr-x | gbp/scripts/pull.py | 26 | ||||
-rw-r--r-- | tests/component/deb/test_pull.py | 25 |
3 files changed, 64 insertions, 2 deletions
diff --git a/docs/manpages/gbp-pull.xml b/docs/manpages/gbp-pull.xml index 462004ea..1662383d 100644 --- a/docs/manpages/gbp-pull.xml +++ b/docs/manpages/gbp-pull.xml @@ -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> @@ -101,6 +102,20 @@ </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> <listitem> diff --git a/gbp/scripts/pull.py b/gbp/scripts/pull.py index 7c665c4a..9027d900 100755 --- a/gbp/scripts/pull.py +++ b/gbp/scripts/pull.py @@ -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(): diff --git a/tests/component/deb/test_pull.py b/tests/component/deb/test_pull.py index 030acf10..3b34ce49 100644 --- a/tests/component/deb/test_pull.py +++ b/tests/component/deb/test_pull.py @@ -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']) |