From cdb344c0e7a92e478d738e29a995726c98ae1ffb Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Tue, 24 Mar 2020 02:43:46 -0400 Subject: [PATCH] launcher: avoid crash when executing out of checkout When developing repo itself, it helps to run repo directly out of it and to run bisection tools. The current _SetDefaultsTo logic fails in that situation though as it wants a branch, but the source isn't checked out to one. Now that we support tracking commits via the --repo-rev setting, fall back to using the current HEAD commit. Change-Id: I37d79fd9f7bea87d212421ebed6c8267ec95145f Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/260192 Tested-by: Mike Frysinger Reviewed-by: Jonathan Nieder --- repo | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/repo b/repo index 12904c3e..39162eff 100755 --- a/repo +++ b/repo @@ -1102,12 +1102,18 @@ def _SetDefaultsTo(gitdir): global REPO_REV REPO_URL = gitdir - try: - ret = run_git('--git-dir=%s' % gitdir, 'symbolic-ref', 'HEAD') - REPO_REV = ret.stdout.strip() - except CloneFailure: - print('fatal: %s has no current branch' % gitdir, file=sys.stderr) - sys.exit(1) + ret = run_git('--git-dir=%s' % gitdir, 'symbolic-ref', 'HEAD', check=False) + if ret.returncode: + # If we're not tracking a branch (bisect/etc...), then fall back to commit. + print('repo: warning: %s has no current branch; using HEAD' % gitdir, + file=sys.stderr) + try: + ret = run_git('rev-parse', 'HEAD', cwd=gitdir) + except CloneFailure: + print('fatal: %s has invalid HEAD' % gitdir, file=sys.stderr) + sys.exit(1) + + REPO_REV = ret.stdout.strip() def main(orig_args):