Commit 720e010b authored by Edward Lesmes's avatar Edward Lesmes Committed by LUCI CQ

Revert "bot_update: Refactor branch to detach and checkout."

This reverts commit b99e61c5.

Reason for revert:
Doesn't work for refs/branch-heads/*

Original change's description:
> bot_update: Refactor branch to detach and checkout.
> 
> Refactor _git_checkout to use information from
> get_target_branch_and_revision to know what branch to checkout after
> cloning.
> 
> Bug: 1104182
> Change-Id: Ib3ba57ca0b6803f172b85121c2a4b123f17bfb8c
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2303291
> Reviewed-by: Josip Sokcevic <sokcevic@google.com>
> Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>

TBR=ehmaldonado@chromium.org,infra-scoped@luci-project-accounts.iam.gserviceaccount.com,sokcevic@google.com

Change-Id: If51423df99fd9c164f8e42e0220ee2d9bc2a39f6
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: 1104182
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2310293Reviewed-by: 's avatarEdward Lesmes <ehmaldonado@chromium.org>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
parent 9949ab7a
...@@ -588,30 +588,54 @@ def get_total_disk_space(): ...@@ -588,30 +588,54 @@ def get_total_disk_space():
return (total, free) return (total, free)
def get_target_branch_and_revision(solution_name, git_url, revisions): def _get_target_branch_and_revision(solution_name, git_url, revisions):
solution_name = solution_name.strip('/') normalized_name = solution_name.strip('/')
configured = revisions.get(solution_name) or revisions.get(git_url) if normalized_name in revisions:
configured = revisions[normalized_name]
if configured is None or COMMIT_HASH_RE.match(configured): elif git_url in revisions:
# TODO(crbug.com/1104182): Get the default branch instead of assuming configured = revisions[git_url]
# 'master'.
branch = 'origin/master'
revision = configured or 'HEAD'
return branch, revision
elif ':' in configured:
branch, revision = configured.split(':', 1)
else: else:
branch = configured return 'master', 'HEAD'
revision = 'HEAD'
parts = configured.split(':', 1)
if len(parts) == 2:
# Support for "branch:revision" syntax.
return parts
if COMMIT_HASH_RE.match(configured):
return 'master', configured
return configured, 'HEAD'
def get_target_pin(solution_name, git_url, revisions):
"""Returns revision to be checked out if it is pinned, else None."""
_, revision = _get_target_branch_and_revision(
solution_name, git_url, revisions)
if COMMIT_HASH_RE.match(revision):
return revision
return None
if branch.startswith('refs/heads/'):
branch = 'refs/remotes/origin/' + branch[len('refs/heads/'):]
elif branch.startswith('refs/branch-heads/'):
branch = 'refs/remotes/branch-heads/' + branch[len('refs/branch-heads/'):]
elif not branch.startswith(('refs/', 'origin/')):
branch = 'origin/' + branch
return branch, revision def force_solution_revision(solution_name, git_url, revisions, cwd):
branch, revision = _get_target_branch_and_revision(
solution_name, git_url, revisions)
if revision and revision.upper() != 'HEAD':
treeish = revision
else:
# TODO(machenbach): This won't work with branch-heads, as Gerrit's
# destination branch would be e.g. refs/branch-heads/123. But here
# we need to pass refs/remotes/branch-heads/123 to check out.
# This will also not work if somebody passes a local refspec like
# refs/heads/master. It needs to translate to refs/remotes/origin/master
# first. See also https://crbug.com/740456 .
if branch.startswith(('refs/', 'origin/')):
treeish = branch
else:
treeish = 'origin/' + branch
# Note that -- argument is necessary to ensure that git treats `treeish`
# argument as revision or ref, and not as a file/directory which happens to
# have the exact same name.
git('checkout', '--force', treeish, '--', cwd=cwd)
def _has_in_git_cache(revision_sha1, refs, git_cache_dir, url): def _has_in_git_cache(revision_sha1, refs, git_cache_dir, url):
...@@ -696,10 +720,8 @@ def _git_checkout(sln, sln_dir, revisions, refs, no_fetch_tags, git_cache_dir, ...@@ -696,10 +720,8 @@ def _git_checkout(sln, sln_dir, revisions, refs, no_fetch_tags, git_cache_dir,
'GIT_TRACE_PERFORMANCE': 'true', 'GIT_TRACE_PERFORMANCE': 'true',
} }
branch, revision = get_target_branch_and_revision(name, url, revisions)
pin = revision if COMMIT_HASH_RE.match(revision) else None
# Step 1: populate/refresh cache, if necessary. # Step 1: populate/refresh cache, if necessary.
pin = get_target_pin(name, url, revisions)
if not pin: if not pin:
# Refresh only once. # Refresh only once.
git(*populate_cmd, env=env) git(*populate_cmd, env=env)
...@@ -753,10 +775,7 @@ def _git_checkout(sln, sln_dir, revisions, refs, no_fetch_tags, git_cache_dir, ...@@ -753,10 +775,7 @@ def _git_checkout(sln, sln_dir, revisions, refs, no_fetch_tags, git_cache_dir,
# to master in contrast with the non-clone case, which results in a # to master in contrast with the non-clone case, which results in a
# detached HEAD. This prevents fetching the default branch so detach the # detached HEAD. This prevents fetching the default branch so detach the
# HEAD after cloning. # HEAD after cloning.
# Note that the '--' argument is needed to ensure that git treats git('checkout', 'HEAD', '--detach', cwd=sln_dir)
# 'pin or branch' as revision or ref, and not as file/directory which
# happens to have the exact same name.
git('checkout', pin or branch, '--detach', '--', cwd=sln_dir)
_git_disable_gc(sln_dir) _git_disable_gc(sln_dir)
else: else:
_git_disable_gc(sln_dir) _git_disable_gc(sln_dir)
...@@ -773,10 +792,7 @@ def _git_checkout(sln, sln_dir, revisions, refs, no_fetch_tags, git_cache_dir, ...@@ -773,10 +792,7 @@ def _git_checkout(sln, sln_dir, revisions, refs, no_fetch_tags, git_cache_dir,
if sys.platform.startswith('win'): if sys.platform.startswith('win'):
_maybe_break_locks(sln_dir, tries=3) _maybe_break_locks(sln_dir, tries=3)
# Note that the '--' argument is needed to ensure that git treats force_solution_revision(name, url, revisions, sln_dir)
# 'pin or branch' as revision or ref, and not as file/directory which
# happens to have the exact same name.
git('checkout', '--force', pin or branch, '--', cwd=sln_dir)
git('clean', '-dff', cwd=sln_dir) git('clean', '-dff', cwd=sln_dir)
return return
except SubprocessFailed as e: except SubprocessFailed as e:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment