From e60eee7dfa80d2ae07f31c83d1bbb9c56e3d20e4 Mon Sep 17 00:00:00 2001 From: Oliver Smith Date: Mon, 25 Sep 2017 22:05:29 +0000 Subject: [PATCH] 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 --- pmb/build/other.py | 10 ++++----- pmb/helpers/git.py | 32 +++++++++++++++++++++++++++++ test/test_aport_in_sync_with_git.py | 17 +++++++++++++++ 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/pmb/build/other.py b/pmb/build/other.py index f5f08b43..90faf1cb 100644 --- a/pmb/build/other.py +++ b/pmb/build/other.py @@ -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) diff --git a/pmb/helpers/git.py b/pmb/helpers/git.py index fc255019..a14cb044 100644 --- a/pmb/helpers/git.py +++ b/pmb/helpers/git.py @@ -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: + """ + # 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: ") + 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) diff --git a/test/test_aport_in_sync_with_git.py b/test/test_aport_in_sync_with_git.py index 35e763f9..52bf2b59 100644 --- a/test/test_aport_in_sync_with_git.py +++ b/test/test_aport_in_sync_with_git.py @@ -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)