[GHF] Fix ghstack branches in sync logic (#93298)

Test plan:
```python
from git_utils import are_ghstack_branches_in_sync,GitRepo
repo=GitRepo("/Users/nshulga/git/pytorch/pytorch")
are_ghstack_branches_in_sync(repo, "gh/SS-JIA/206/head")
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/93298
Approved by: https://github.com/clee2000, https://github.com/ZainRizvi
This commit is contained in:
Nikita Shulga
2023-01-30 21:00:51 +00:00
committed by PyTorch MergeBot
parent 54056c1705
commit 7a621c443b
3 changed files with 59 additions and 5 deletions

View File

@ -1,6 +1,11 @@
#!/usr/bin/env python3
from gitutils import PeekableIterator, patterns_to_regex
from unittest import TestCase, main
from gitutils import PeekableIterator, patterns_to_regex, GitRepo, are_ghstack_branches_in_sync, _shasum
from unittest import TestCase, main, SkipTest
from pathlib import Path
BASE_DIR = Path(__file__).parent
class TestPeekableIterator(TestCase):
def test_iterator(self, input_: str = "abcdef") -> None:
@ -35,5 +40,34 @@ class TestPattern(TestCase):
self.assertTrue(patterns_re.match(filename))
class TestGitRepo(TestCase):
def setUp(self) -> None:
repo_dir = BASE_DIR.parent.parent.absolute()
if not (repo_dir / ".git").is_dir():
raise SkipTest("Can't find git directory, make sure to run this test on real repo checkout")
self.repo = GitRepo(str(repo_dir))
def _skip_if_ref_does_not_exist(self, ref: str) -> None:
""" Skip test if ref is missing as stale branches are deleted with time """
try:
self.repo.show_ref(ref)
except RuntimeError as e:
raise SkipTest(f"Can't find head ref {ref} due to {str(e)}") from e
def test_compute_diff(self) -> None:
diff = self.repo.diff("HEAD")
sha = _shasum(diff)
self.assertEqual(len(sha), 64)
def test_ghstack_branches_in_sync(self) -> None:
head_ref = "gh/SS-JIA/206/head"
self._skip_if_ref_does_not_exist(head_ref)
self.assertTrue(are_ghstack_branches_in_sync(self.repo, head_ref))
def test_ghstack_branches_not_in_sync(self) -> None:
head_ref = "gh/clee2000/1/head"
self._skip_if_ref_does_not_exist(head_ref)
self.assertFalse(are_ghstack_branches_in_sync(self.repo, head_ref))
if __name__ == '__main__':
main()