Read about our upcoming Code of Conduct on this issue

Commit 2893e0aa authored by Georges Racinet's avatar Georges Racinet 🦑
Browse files

fetch-stable-branches: converted as a Python script

Better for uniformity, clarity and also allows us to
introduce the `--min-gitlab-version` option.
parent 05fe57938be7
Pipeline #20079 passed with stage
in 31 seconds
#!/bin/sh
#
# GitLab must have some kind of protection, because a blank 'git fetch'
# does not fetch all branches
if ! test -d .git; then
echo "Please run from the upstream Git clone (gitlab-ce, omnibus-gitlab etc.)"
exit 1
fi
set -e
......@@ -12,17 +2,4 @@
if [ -z "$1" ]; then
echo "Fetching all stable branches from remote 'origin'"
BRANCHES=$(git ls-remote origin \
| grep -E 'refs/heads/13-[0-9]+.*-stable$' \
| sed 's/.*refs\/heads\///g')
fi
for BRANCH in $BRANCHES; do
echo "Fetching Git branch $BRANCH"
git fetch origin $BRANCH
git branch -f $BRANCH origin/$BRANCH
echo
done
echo "Fetching all tags"
git fetch --tags
echo "This script has been replaced by a Python script. "
echo "Please run ~/.local/bin/fetch-stable-branches instead. "
echo "If not installed, run 'make help'"
# Copyright 2020-2021 Georges Racinet <georges.racinet@octobus.net>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
#
# SPDX-License-Identifier: GPL-2.0-or-later
import argparse
import os
import subprocess
from .version import level2_version
from .upstream_branch import extract_stable_branch_version
# constants for ArgumentParser, useful because the formatter with defaults
# is unsatisfactory, to say the least
DEFAULT_REPO = 'gitlab-ce'
DEFAULT_GIT_REMOTE = 'origin'
GIT_BRANCH_REF_PREFIX = 'refs/heads/'
def git_refs_to_branches(refs):
"""Convert Git branch full ref paths to branch names.
It works for lists or any iterables::
>>> git_refs_to_branches(['refs/heads/main', 'refs/tags/v1.2.3'])
['main']
>>> git_refs_to_branches(('refs/heads/' + br for br in ['main', 'xp']))
['main', 'xp']
"""
return [ref[len(GIT_BRANCH_REF_PREFIX):]
for ref in refs if ref.startswith(GIT_BRANCH_REF_PREFIX)]
def ls_git_branches(remote):
"""Query remote Git branches
This serves two purposes:
- on GitLab upstream repos, a blank `git fetch` does nothing
- we will want to filter only the remote branches that are of interest
to us, while including new ones (hence can't rely on local branches).
"""
git_refs = subprocess.check_output(
('git', 'ls-remote', remote)).decode().splitlines()
return git_refs_to_branches(line.split()[-1] for line in git_refs)
def main():
parser = argparse.ArgumentParser(
description="Fetch stable Git branches from upstream. "
"All paths are relative to the current working directory")
parser.add_argument("-R", "--repo", "--gitlab-repo",
help="Local Git clone of upstream component "
"(default %r" % DEFAULT_REPO,
default=DEFAULT_REPO)
parser.add_argument("--min-gitlab-version", type=level2_version,
help="Minimal GitLab version to consider "
"(default 13.9)",
default=(13, 9))
parser.add_argument("--git-remote", default=DEFAULT_GIT_REMOTE,
help="Git remote name "
"(default %r)" % DEFAULT_GIT_REMOTE)
cl_args = parser.parse_args()
remote = cl_args.git_remote
min_version = cl_args.min_gitlab_version
os.chdir(cl_args.repo)
print("Fetching stable branches from remote %r" % remote)
branches = ls_git_branches(remote)
for branch in branches:
version = extract_stable_branch_version(branch)
if version is None or version < min_version:
continue
print("Fetching remote branch %r" % branch)
subprocess.check_call(('git', 'fetch', '--tags', remote, branch))
subprocess.check_call(('git', 'branch', '-f', branch,
'/'.join((remote, branch))))
......@@ -34,9 +34,7 @@
- [ ] Main repository:
```
cd gitlab-ce
../fetch-stable-branches.sh
cd
fetch-stable-branches
convert-pull-local --branch-prefix {git_branch}
cd heptapod-rails
hg push -fB {git_branch} [URL]
......@@ -46,9 +44,7 @@
- [ ] Omnibus:
```
cd omnibus-gitlab
../fetch-stable-branches.sh
cd
fetch-stable-branches --gitlab-repo omnibus-gitlab
convert-pull-local --gitlab-repo omnibus-gitlab \
--heptapod-repo omnibus-heptapod \
--branch-prefix {git_branch} \
......
......@@ -4,6 +4,7 @@
scripts = { # name to (module in main package, function)
'convert-pull-local': ('convert', 'main'),
'fetch-stable-branches': ('fetch_stable_branches', 'main'),
'fetch-watch-tags': ('fetch_watch_tags', 'main'),
}
......
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