mirror of
https://github.com/OpenHands/OpenHands.git
synced 2025-12-26 05:48:36 +08:00
Fix git changes panel (#9967)
This commit is contained in:
parent
e951612ff4
commit
5553584056
@ -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:
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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'},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user