Fix git changes panel (#9967)

This commit is contained in:
Tim O'Farrell 2025-07-29 11:21:49 -06:00 committed by GitHub
parent e951612ff4
commit 5553584056
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 41 deletions

View File

@ -25,11 +25,12 @@ def run(cmd: str, cwd: str) -> str:
def get_valid_ref(repo_dir: str) -> str | None:
refs = []
try:
current_branch = run('git --no-pager rev-parse --abbrev-ref HEAD', repo_dir)
refs.append(f'origin/{current_branch}')
except RuntimeError:
# Not a git repository (Or no commits)
return None
pass
try:
default_branch = (
@ -37,21 +38,19 @@ def get_valid_ref(repo_dir: str) -> str | None:
.split()[-1]
.strip()
)
ref_non_default_branch = f'$(git --no-pager merge-base HEAD "$(git --no-pager rev-parse --abbrev-ref origin/{default_branch})")'
ref_default_branch = f'origin/{default_branch}'
refs.append(ref_non_default_branch)
refs.append(ref_default_branch)
except RuntimeError:
# Git repository does not have a remote origin - use current
return current_branch
pass
ref_current_branch = f'origin/{current_branch}'
ref_non_default_branch = f'$(git --no-pager merge-base HEAD "$(git --no-pager rev-parse --abbrev-ref origin/{default_branch})")'
ref_default_branch = f'origin/{default_branch}'
ref_new_repo = '$(git --no-pager rev-parse --verify 4b825dc642cb6eb9a060e54bf8d69288fbee4904)' # compares with empty tree
# compares with empty tree
ref_new_repo = (
'$(git --no-pager rev-parse --verify 4b825dc642cb6eb9a060e54bf8d69288fbee4904)'
)
refs.append(ref_new_repo)
refs = [
ref_current_branch,
ref_non_default_branch,
ref_default_branch,
ref_new_repo,
]
# Find a ref that exists...
for ref in refs:
try:

View File

@ -35,11 +35,12 @@ def run(cmd: str, cwd: str) -> str:
def get_valid_ref(repo_dir: str) -> str | None:
refs = []
try:
current_branch = run('git --no-pager rev-parse --abbrev-ref HEAD', repo_dir)
refs.append(f'origin/{current_branch}')
except RuntimeError:
# Not a git repository (Or no commits)
return None
pass
try:
default_branch = (
@ -47,26 +48,24 @@ def get_valid_ref(repo_dir: str) -> str | None:
.split()[-1]
.strip()
)
ref_non_default_branch = f'$(git --no-pager merge-base HEAD "$(git --no-pager rev-parse --abbrev-ref origin/{default_branch})")'
ref_default_branch = f'origin/{default_branch}'
refs.append(ref_non_default_branch)
refs.append(ref_default_branch)
except RuntimeError:
# Git repository does not have a remote origin - use current
return current_branch
pass
ref_current_branch = f'origin/{current_branch}'
ref_non_default_branch = f'$(git --no-pager merge-base HEAD "$(git --no-pager rev-parse --abbrev-ref origin/{default_branch})")'
ref_default_branch = 'origin/' + default_branch
ref_new_repo = '$(git --no-pager rev-parse --verify 4b825dc642cb6eb9a060e54bf8d69288fbee4904)' # compares with empty tree
# compares with empty tree
ref_new_repo = (
'$(git --no-pager rev-parse --verify 4b825dc642cb6eb9a060e54bf8d69288fbee4904)'
)
refs.append(ref_new_repo)
refs = [
ref_current_branch,
ref_non_default_branch,
ref_default_branch,
ref_new_repo,
]
# Find a ref that exists...
for ref in refs:
try:
run(f'git --no-pager rev-parse --verify {ref}', repo_dir)
return ref
result = run(f'git --no-pager rev-parse --verify {ref}', repo_dir)
return result
except RuntimeError:
# invalid ref - try next
continue

View File

@ -177,17 +177,6 @@ class TestGitHandler(unittest.TestCase):
{'status': 'M', 'path': 'unstaged_modified.txt'},
]
if changes != expected_changes:
raise RuntimeError(
'\n'.join(
[
f'incorrect_changes: {changes};',
f'content: {os.listdir(self.local_dir)}',
f'ref: {git_changes.get_valid_ref(self.local_dir)}',
]
)
)
assert changes == expected_changes
def test_get_git_changes_after_push(self):
@ -220,7 +209,9 @@ class TestGitHandler(unittest.TestCase):
{'status': 'A', 'path': 'committed_add.txt'},
{'status': 'D', 'path': 'committed_delete.txt'},
{'status': 'M', 'path': 'committed_modified.txt'},
{'status': 'A', 'path': 'nested 1/committed_add.txt'},
{'status': 'A', 'path': 'nested 1/staged_add.txt'},
{'status': 'A', 'path': 'nested_2/committed_add.txt'},
{'status': 'A', 'path': 'nested_2/unstaged_add.txt'},
{'status': 'A', 'path': 'staged_add.txt'},
{'status': 'D', 'path': 'staged_delete.txt'},