1
0
Fork 1
mirror of https://gitlab.postmarketos.org/postmarketOS/pmbootstrap.git synced 2025-07-13 11:29:46 +03:00

pmbootstrap pull: new action (!1848)

Add a shortcut for "git pull --ff-only" in all repositories cloned by
pmbootstrap (currently pmaports and aports_upstream, new pmdevices
repository coming soon).

'pmbootstrap pull' will only update the repositories, if:
* they are on an officially supported branch (e.g. master)
* the history is not conflicting (fast-forward is possible)
* the git workdirs are clean

Otherwise it shows the user a descriptive message about what to do. The
list of supported branches is only "master" right now, and will be
extended in later commits, so we can have a stable branch for pmaports
based on Alpine's releases. More about that in the project direction
2020 issue.

Closes: #1858
This commit is contained in:
Oliver Smith 2020-01-06 06:39:57 +01:00 committed by Alexey Min
parent 16e2d3c77c
commit e04712a636
No known key found for this signature in database
GPG key ID: EBF5ECFFFEE34DED
5 changed files with 283 additions and 3 deletions

View file

@ -78,3 +78,97 @@ def rev_parse(args, path, revision="HEAD", extra_args: list = []):
command = ["git", "rev-parse"] + extra_args + [revision]
rev = pmb.helpers.run.user(args, command, path, output_return=True)
return rev.rstrip()
def can_fast_forward(args, path, branch_upstream, branch="HEAD"):
command = ["git", "merge-base", "--is-ancestor", branch, branch_upstream]
ret = pmb.helpers.run.user(args, command, path, check=False)
if ret == 0:
return True
elif ret == 1:
return False
else:
raise RuntimeError("Unexpected exit code from git: " + str(ret))
def clean_worktree(args, path):
""" Check if there are not any modified files in the git dir. """
command = ["git", "status", "--porcelain"]
return pmb.helpers.run.user(args, command, path, output_return=True) == ""
def get_upstream_remote(args, name_repo):
""" Find the remote, which matches the git URL from the config. Usually
"origin", but the user may have set up their git repository
differently. """
url = pmb.config.git_repos[name_repo]
path = get_path(args, name_repo)
command = ["git", "remote", "-v"]
output = pmb.helpers.run.user(args, command, path, output_return=True)
for line in output.split("\n"):
if url in line:
return line.split("\t", 1)[0]
raise RuntimeError("{}: could not find remote name for URL '{}' in git"
" repository: {}".format(name_repo, url, path))
def pull(args, name_repo):
""" Check if on official branch and essentially try 'git pull --ff-only'.
Instead of really doing 'git pull --ff-only', do it in multiple steps
(fetch, merge --ff-only), so we can display useful messages depending
on which part fails.
:returns: integer, >= 0 on success, < 0 on error """
branches_official = ["master"]
# Skip if repo wasn't cloned
path = get_path(args, name_repo)
if not os.path.exists(path):
logging.debug(name_repo + ": repo was not cloned, skipping pull!")
return 1
# Skip if not on official branch
branch = rev_parse(args, path, extra_args=["--abbrev-ref"])
msg_start = "{} (branch: {}):".format(name_repo, branch)
if branch not in branches_official:
logging.warning("{} not on one of the official branches ({}), skipping"
" pull!"
"".format(msg_start, ", ".join(branches_official)))
return -1
# Skip if workdir is not clean
if not clean_worktree(args, path):
logging.warning(msg_start + " workdir is not clean, skipping pull!")
return -2
# Skip if branch is tracking different remote
branch_upstream = get_upstream_remote(args, name_repo) + "/" + branch
remote_ref = rev_parse(args, path, branch + "@{u}", ["--abbrev-ref"])
if remote_ref != branch_upstream:
logging.warning("{} is tracking unexpected remote branch '{}' instead"
" of '{}'".format(msg_start, remote_ref,
branch_upstream))
return -3
# Fetch (exception on failure, meaning connection to server broke)
logging.info(msg_start + " git pull --ff-only")
if not args.offline:
pmb.helpers.run.user(args, ["git", "fetch"], path)
# Skip if already up to date
if rev_parse(args, path, branch) == rev_parse(args, path, branch_upstream):
logging.info(msg_start + " already up to date")
return 2
# Skip if we can't fast-forward
if not can_fast_forward(args, path, branch_upstream):
logging.warning("{} can't fast-forward to {}, looks like you changed"
" the git history of your local branch. Skipping pull!"
"".format(msg_start, branch_upstream))
return -4
# Fast-forward now (should not fail due to checks above, so it's fine to
# throw an exception on error)
command = ["git", "merge", "--ff-only", branch_upstream]
pmb.helpers.run.user(args, command, path, "stdout")
return 0