From e87ead16f17138ba71a4a609dc41da01e3305996 Mon Sep 17 00:00:00 2001 From: Albert Wang Date: Wed, 15 Feb 2017 02:02:21 -0800 Subject: [PATCH 1/6] Generate a FocusHash when the focus object matches a commit hash --- git_browse/browse.py | 22 ++++++++++++++++++++++ git_browse/tests/test.py | 23 +++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/git_browse/browse.py b/git_browse/browse.py index 08673cd..4202bca 100755 --- a/git_browse/browse.py +++ b/git_browse/browse.py @@ -101,6 +101,11 @@ def default(): return FocusObject(os.sep) +class FocusHash(object): + def __init__(self, commit_hash): + self.commit_hash = commit_hash + + def get_repository_root(): current_directory = '' new_directory = os.getcwd() @@ -166,6 +171,9 @@ def get_focus_object(sys_argv, path): object_path = os.path.join(directory, focus_object[0]) object_path = os.path.normpath(object_path) if not os.path.exists(object_path): + focus_hash = get_commit_hash(focus_object[0]) + if focus_hash: + return focus_hash error = "specified file does not exist: %s" % object_path raise FileNotFoundError(error) is_dir = os.path.isdir(object_path) and object_path[-1] != os.sep @@ -175,6 +183,20 @@ def get_focus_object(sys_argv, path): return FocusObject(object_path) +def get_commit_hash(focus_object): + command = ['git', 'show', focus_object] + process = subprocess.run( + command, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE + ) + if process.returncode != 0: + return None + output = process.stdout.decode('utf-8') + commit_hash = output.split("\n")[0].split(" ")[1] + return FocusHash(commit_hash) + + def open_url(url): print(url) if url.__class__ is list: diff --git a/git_browse/tests/test.py b/git_browse/tests/test.py index a96ba45..e933d9c 100644 --- a/git_browse/tests/test.py +++ b/git_browse/tests/test.py @@ -75,6 +75,11 @@ def test_default(self): self.assertTrue(obj.is_root()) +class FocusHash(unittest.TestCase): + def test_init(self): + obj = browse.FocusHash('abcde') + + class GetRepositoryRoot(unittest.TestCase): def test_get(self): os.chdir(BASE_DIRECTORY) @@ -179,12 +184,30 @@ def test_directory_focus_object(self): self.assertFalse(focus_object.is_root()) self.assertTrue(focus_object.is_directory()) + def test_get_focus_hash(self): + sys_argv = ['asdf', 'v2.0.0'] + focus_object = browse.get_focus_object(sys_argv, os.getcwd()) + self.assertTrue(focus_object.__class__ is browse.FocusHash) + def test_nonexistend_focus_object(self): sys_argv = ['asdf', 'asdf'] with self.assertRaises(FileNotFoundError): browse.get_focus_object(sys_argv, os.getcwd()) +class TestGetCommitHash(unittest.TestCase): + def test_get_unknown_hash(self): + focus_object = '!@#$' + focus_hash = browse.get_commit_hash(focus_object) + self.assertEqual(focus_hash, None) + + def test_get_hash(self): + focus_object = 'v2.0.0' + focus_hash = browse.get_commit_hash(focus_object) + self.assertTrue(focus_hash.__class__ is browse.FocusHash) + self.assertTrue(focus_hash.commit_hash) + + class TestOpenURL(unittest.TestCase): @patch("builtins.print", autospec=True) def test_open_url(self, mock_print): From b11a17f947da6970ea6595b467c1ab916caa4099 Mon Sep 17 00:00:00 2001 From: Albert Wang Date: Wed, 15 Feb 2017 02:12:17 -0800 Subject: [PATCH 2/6] Support browsing commits on github --- git_browse/browse.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/git_browse/browse.py b/git_browse/browse.py index 4202bca..06c6c12 100755 --- a/git_browse/browse.py +++ b/git_browse/browse.py @@ -36,12 +36,21 @@ def get_url(self, focus_object): self.user, self.repository ) + if focus_object.is_commit_hash(): + return self.commit_hash_url(repository_url, focus_object) if focus_object.is_root(): return self.root_url(repository_url, focus_object) if focus_object.is_directory(): return self.directory_url(repository_url, focus_object) return self.file_url(repository_url, focus_object) + def commit_hash_url(self, repository_url, focus_hash): + repository_url = "%s/commit/%s" % ( + repository_url, + focus_hash.commit_hash + ) + return repository_url + def root_url(self, repository_url, focus_object): return repository_url @@ -90,6 +99,9 @@ class FocusObject(object): def __init__(self, path): self.path = path + def is_commit_hash(self): + return False + def is_root(self): return self.path == os.sep @@ -105,6 +117,9 @@ class FocusHash(object): def __init__(self, commit_hash): self.commit_hash = commit_hash + def is_commit_hash(self): + return True + def get_repository_root(): current_directory = '' From 19620c1a54b98d6ffac04c5e2a5dea11e87ddb15 Mon Sep 17 00:00:00 2001 From: Albert Wang Date: Wed, 15 Feb 2017 23:52:02 -0800 Subject: [PATCH 3/6] Add tests for browsing commits on github --- git_browse/tests/test.py | 16 ++++++++++++++++ git_browse/tests/test_git_urls.py | 6 ++++++ 2 files changed, 22 insertions(+) diff --git a/git_browse/tests/test.py b/git_browse/tests/test.py index e933d9c..75ae39b 100644 --- a/git_browse/tests/test.py +++ b/git_browse/tests/test.py @@ -15,6 +15,7 @@ def setUp(self): self.github_host = browse.GithubHost('albertyw', 'git-browse') self.repository_url = 'https://github.com/albertyw/git-browse' self.focus_object = browse.FocusObject('/') + self.focus_hash = browse.FocusHash('v2.0.0') def test_init(self): host = browse.GithubHost('user', 'repository') @@ -48,6 +49,16 @@ def test_file_url(self): 'https://github.com/albertyw/git-browse/blob/master/README.md' ) + def test_commit_hash_url(self): + url = self.github_host.commit_hash_url( + self.repository_url, + self.focus_hash + ) + self.assertEqual( + url, + 'https://github.com/albertyw/git-browse/commit/v2.0.0' + ) + class FocusObject(unittest.TestCase): def test_init(self): @@ -78,6 +89,11 @@ def test_default(self): class FocusHash(unittest.TestCase): def test_init(self): obj = browse.FocusHash('abcde') + self.assertEqual(obj.commit_hash, 'abcde') + + def test_is_commit_hash(self): + obj = browse.FocusHash('abcde') + self.assertTrue(obj.is_commit_hash()) class GetRepositoryRoot(unittest.TestCase): diff --git a/git_browse/tests/test_git_urls.py b/git_browse/tests/test_git_urls.py index e570b67..14fe9a4 100644 --- a/git_browse/tests/test_git_urls.py +++ b/git_browse/tests/test_git_urls.py @@ -49,6 +49,12 @@ TEST_DIR, 'https://github.com/albertyw/git-browse/tree/master/testdir/' ), + ( + 'git@github.com:albertyw/git-browse', + 'v2.0.0', + 'https://github.com/albertyw/git-browse/commit/' + + 'f5631b4c423f2fa5c9c4b64853607f1727d4b7a9' + ), ( 'gitolite@code.uber.internal:a/b', None, From 5903b1c8cf0f895db0dbb2701ccf5e4c8a8bd855 Mon Sep 17 00:00:00 2001 From: Albert Wang Date: Wed, 15 Feb 2017 02:16:35 -0800 Subject: [PATCH 4/6] Support browsing commits in phabricator --- git_browse/browse.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/git_browse/browse.py b/git_browse/browse.py index 06c6c12..d5dba7a 100755 --- a/git_browse/browse.py +++ b/git_browse/browse.py @@ -78,10 +78,13 @@ def create(url_regex_match): return UberPhabricatorHost(None, None) def get_url(self, focus_object): - path = focus_object.path - # arc browse requires an object, provide the root object by default - if focus_object.is_root(): - path = '.' + if focus_object.is_commit_hash(): + path = focus_object.commit_hash + else: + path = focus_object.path + # arc browse requires an object, provide the root object by default + if focus_object.is_root(): + path = '.' command = ['arc', 'browse'] if path: command.append(path) From f5c570ebca9d194906e36babdff6ee72ee74011c Mon Sep 17 00:00:00 2001 From: Albert Wang Date: Wed, 15 Feb 2017 23:54:46 -0800 Subject: [PATCH 5/6] Add tests for browsing commits on phabricator --- git_browse/tests/test_git_urls.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/git_browse/tests/test_git_urls.py b/git_browse/tests/test_git_urls.py index 14fe9a4..75d62e3 100644 --- a/git_browse/tests/test_git_urls.py +++ b/git_browse/tests/test_git_urls.py @@ -69,6 +69,11 @@ 'gitolite@code.uber.internal:a/b', TEST_DIR, ['arc', 'browse', TEST_DIR+'/'] + ), + ( + 'gitolite@code.uber.internal:a/b', + 'v2.0.0', + ['arc', 'browse', 'f5631b4c423f2fa5c9c4b64853607f1727d4b7a9'] ) ] From 7a5f3f26e33e2355be58f8bdff08cd46f8710412 Mon Sep 17 00:00:00 2001 From: Albert Wang Date: Thu, 16 Feb 2017 00:33:28 -0800 Subject: [PATCH 6/6] Switch back from subprocess.run to subprocess.Popen to support python < 3.5 --- git_browse/browse.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/git_browse/browse.py b/git_browse/browse.py index d5dba7a..80c7886 100755 --- a/git_browse/browse.py +++ b/git_browse/browse.py @@ -203,15 +203,16 @@ def get_focus_object(sys_argv, path): def get_commit_hash(focus_object): command = ['git', 'show', focus_object] - process = subprocess.run( + process = subprocess.Popen( command, stdout=subprocess.PIPE, - stderr=subprocess.PIPE + stderr=subprocess.PIPE, + universal_newlines=True ) + out, err = process.communicate() if process.returncode != 0: return None - output = process.stdout.decode('utf-8') - commit_hash = output.split("\n")[0].split(" ")[1] + commit_hash = out.split("\n")[0].split(" ")[1] return FocusHash(commit_hash)