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 <vapier@google.com>
Reviewed-by: Jonathan Nieder <jrn@google.com>
This commit is contained in:
Mike Frysinger 2020-03-24 02:43:46 -04:00
parent e257d56665
commit cdb344c0e7

12
repo
View File

@ -1102,13 +1102,19 @@ def _SetDefaultsTo(gitdir):
global REPO_REV global REPO_REV
REPO_URL = gitdir REPO_URL = gitdir
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: try:
ret = run_git('--git-dir=%s' % gitdir, 'symbolic-ref', 'HEAD') ret = run_git('rev-parse', 'HEAD', cwd=gitdir)
REPO_REV = ret.stdout.strip()
except CloneFailure: except CloneFailure:
print('fatal: %s has no current branch' % gitdir, file=sys.stderr) print('fatal: %s has invalid HEAD' % gitdir, file=sys.stderr)
sys.exit(1) sys.exit(1)
REPO_REV = ret.stdout.strip()
def main(orig_args): def main(orig_args):
cmd, opt, args = _ParseArguments(orig_args) cmd, opt, args = _ParseArguments(orig_args)