From 9c8e12517712b97265a49f09753f17577abb381c Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Thu, 16 May 2024 16:31:40 -0400 Subject: [PATCH] Try same-name remote branches even if local branch exists --- src/rapids_pre_commit_hooks/copyright.py | 41 +++++++++++------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/src/rapids_pre_commit_hooks/copyright.py b/src/rapids_pre_commit_hooks/copyright.py index 97d1260..d03b353 100644 --- a/src/rapids_pre_commit_hooks/copyright.py +++ b/src/rapids_pre_commit_hooks/copyright.py @@ -188,31 +188,28 @@ def get_target_branch_upstream_commit(repo, args): try: target_branch = repo.heads[target_branch_name] except IndexError: - - def try_get_ref(remote): - try: - return remote.refs[target_branch_name] - except IndexError: - return None - - try: - upstream_commit = max( - ( - upstream - for remote in repo.remotes - if (upstream := try_get_ref(remote)) - ), - key=lambda upstream: upstream.commit.committed_datetime, - ).commit - except ValueError: - pass - else: - commits_to_try.append(upstream_commit) + pass else: commits_to_try.append(target_branch.commit) - target_branch_upstream = target_branch.tracking_branch() - if target_branch_upstream: + if target_branch_upstream := target_branch.tracking_branch(): commits_to_try.append(target_branch_upstream.commit) + return max(commits_to_try, key=lambda commit: commit.committed_datetime) + + def try_get_ref(remote): + try: + return remote.refs[target_branch_name] + except IndexError: + return None + + try: + upstream_commit = max( + (upstream for remote in repo.remotes if (upstream := try_get_ref(remote))), + key=lambda upstream: upstream.commit.committed_datetime, + ).commit + except ValueError: + pass + else: + commits_to_try.append(upstream_commit) if commits_to_try: return max(commits_to_try, key=lambda commit: commit.committed_datetime)