git cl: fix a bug that may output stale upstream branch

git cl upstream will run `git branch --set-upstream-to xxx` command
which modifies the git config. However, it doesn't invalidate config
cache. I think an ideal fix would be the git scm exposes method to
invalidate cache and the caller of git command needs to explicitly
invalidate the cache right after git command invocation. However,
I decide to take a "clever" approach here because this seems to
be the only place in git cl that requires explicit cache eviction.
It may not be worth to expose the existence of cache just for this
reason. The "clever" approach swaps the order of subseqent config
modification and config reading. The config modification will always
invalidate the cache. Therefore, It will ensure we are always reading
up-to-date upstream branch name from the fresh config cahce.

Bug: 40942309
Change-Id: I3786fd37799092ce269c7c4d3945c51d077335cb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5479950
Reviewed-by: Josip Sokcevic <sokcevic@chromium.org>
Commit-Queue: Yiwei Zhang <yiwzhang@google.com>
changes/50/5479950/2
Yiwei Zhang 1 year ago committed by LUCI CQ
parent b0e9b98aef
commit 85e409e69a

@ -5822,11 +5822,10 @@ def CMDupstream(parser, args):
# One arg means set upstream branch.
branch = cl.GetBranch()
RunGit(['branch', '--set-upstream-to', args[0], branch])
cl = Changelist()
print('Upstream branch set to %s' % (cl.GetUpstreamBranch(), ))
# Clear configured merge-base, if there is one.
git_common.remove_merge_base(branch)
cl = Changelist()
print('Upstream branch set to %s' % (cl.GetUpstreamBranch(), ))
else:
print(cl.GetUpstreamBranch())
return 0

Loading…
Cancel
Save