Fix #151: git ambiguous argument error (#531)

We check if origin/HEAD is present. In case that reference is
missing, we show a meaningful error message now, with an explanation
on how to add it. Also moved find_out_of_sync_files_tracked() to
pmb.helpers.git
This commit is contained in:
Oliver Smith 2017-09-25 22:05:29 +00:00 committed by GitHub
parent 1416ce241d
commit e60eee7dfa
3 changed files with 53 additions and 6 deletions

View File

@ -23,8 +23,9 @@ import shutil
import pmb.build.other
import pmb.chroot
import pmb.helpers.run
import pmb.helpers.file
import pmb.helpers.git
import pmb.helpers.run
import pmb.parse.apkindex
import pmb.parse.version
@ -127,11 +128,8 @@ def aports_files_out_of_sync_with_git(args, package=None):
git_root = git_root.rstrip()
ret = []
if git_root and os.path.exists(git_root):
# Find tracked files out of sync with upstream
tracked = pmb.helpers.run.user(args, ["git", "diff", "--name-only", "origin"],
working_dir=git_root, return_stdout=True)
# Find all untracked files
# Find all out of sync files
tracked = pmb.helpers.git.find_out_of_sync_files_tracked(args, git_root)
untracked = pmb.helpers.run.user(
args, ["git", "ls-files", "--others", "--exclude-standard"],
working_dir=git_root, return_stdout=True)

View File

@ -22,6 +22,7 @@ import os
import pmb.build
import pmb.chroot.apk
import pmb.config
import pmb.helpers.run
def clone(args, repo_name):
@ -44,3 +45,34 @@ def rev_parse(args, revision="HEAD"):
logging.warning("WARNING: Failed to determine revision of git repository at " + args.aports)
return ""
return rev.rstrip()
def find_out_of_sync_files_tracked(args, git_root):
"""
Find all files tracked by git, that are are out of sync with origin/HEAD.
In some cases (when you rename a remote or add it manually), origin/HEAD
does not exist. We check for that to provide a meaningful error message
instead of a confusing crash (see #151).
See also: <https://stackoverflow.com/a/17639471>
"""
# Return changed files compared to origin/HEAD when it exists
ret = pmb.helpers.run.user(args, ["git", "show-ref",
"refs/remotes/origin/HEAD"],
working_dir=git_root, return_stdout=True,
check=False)
if ret and "refs/remotes/origin/HEAD" in ret:
return pmb.helpers.run.user(args, ["git", "diff", "--name-only",
"origin"], working_dir=git_root,
return_stdout=True)
# Meaningful error
logging.debug("Output of 'git diff --name-only origin': " + str(ret))
logging.info("See also: <https://github.com/postmarketOS/pmbootstrap/"
"issues/151>")
fix_cmds = ("git symbolic-ref refs/remotes/origin/HEAD refs/remotes/"
"origin/master; git fetch")
raise RuntimeError("Your aports repository does not have the"
" 'origin/HEAD' reference. Please add it by"
" running the following commands inside " +
git_root + ": " + fix_cmds)

View File

@ -117,3 +117,20 @@ def test_aport_in_sync_with_git(args):
# TODO:
# - reinstall git, but rm .git, check again
# - remove temporary folder
def test_ambigious_argument(args):
"""
Testcase for #151, forces "fatal: ambiguous argument" in git.
See also: https://stackoverflow.com/a/17639471
"""
# Delete origin/HEAD
aports = temp_aports_repo(args)
pmb.chroot.user(args, ["git", "update-ref", "-d", "refs/remotes/origin/HEAD"],
working_dir=aports)
# Check for exception
with pytest.raises(RuntimeError) as e:
out_of_sync_files(args)
assert "'origin/HEAD' reference" in str(e.value)