Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Fix git changes panel (#9967)

Tim O'Farrell555358405640
diff --git a/openhands/runtime/utils/git_changes.py b/openhands/runtime/utils/git_changes.py
index 754cf3948c..acc84a0e8f 100644
--- a/openhands/runtime/utils/git_changes.py
+++ b/openhands/runtime/utils/git_changes.py
@@ -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
-
- 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
-
- refs = [
- ref_current_branch,
- ref_non_default_branch,
- ref_default_branch,
- ref_new_repo,
- ]
+ pass
+
+ # compares with empty tree
+ ref_new_repo = (
+ '$(git --no-pager rev-parse --verify 4b825dc642cb6eb9a060e54bf8d69288fbee4904)'
+ )
+ refs.append(ref_new_repo)
+
# Find a ref that exists...
for ref in refs:
try:
diff --git a/openhands/runtime/utils/git_diff.py b/openhands/runtime/utils/git_diff.py
index 92c854061e..a9b5d263e1 100644
--- a/openhands/runtime/utils/git_diff.py
+++ b/openhands/runtime/utils/git_diff.py
@@ -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
-
- 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
-
- refs = [
- ref_current_branch,
- ref_non_default_branch,
- ref_default_branch,
- ref_new_repo,
- ]
+ pass
+
+ # compares with empty tree
+ ref_new_repo = (
+ '$(git --no-pager rev-parse --verify 4b825dc642cb6eb9a060e54bf8d69288fbee4904)'
+ )
+ refs.append(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
diff --git a/tests/unit/test_git_handler.py b/tests/unit/test_git_handler.py
index 7f94d0120d..1c7908ed3d 100644
--- a/tests/unit/test_git_handler.py
+++ b/tests/unit/test_git_handler.py
@@ -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'},