From aaf49a5c22f8de5544bffe6a960a544fca942378 Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Wed, 1 Jun 2016 10:08:00 +0200 Subject: [PATCH] prevent tracking git branch with detached head --- .../git-with-detached-head/dot_git/HEAD | 1 + .../git-with-detached-head/dot_git/config | 7 +++++++ .../git-with-detached-head/emptyfile.txt | 0 tests/test_project.py | 20 +++++++++++++++++++ wakatime/projects/git.py | 9 +++++++-- 5 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 tests/samples/projects/git-with-detached-head/dot_git/HEAD create mode 100644 tests/samples/projects/git-with-detached-head/dot_git/config create mode 100644 tests/samples/projects/git-with-detached-head/emptyfile.txt diff --git a/tests/samples/projects/git-with-detached-head/dot_git/HEAD b/tests/samples/projects/git-with-detached-head/dot_git/HEAD new file mode 100644 index 0000000..e00132a --- /dev/null +++ b/tests/samples/projects/git-with-detached-head/dot_git/HEAD @@ -0,0 +1 @@ +f4f242d698fa07c298592a66d6546ac9b6b34d1e diff --git a/tests/samples/projects/git-with-detached-head/dot_git/config b/tests/samples/projects/git-with-detached-head/dot_git/config new file mode 100644 index 0000000..6c9406b --- /dev/null +++ b/tests/samples/projects/git-with-detached-head/dot_git/config @@ -0,0 +1,7 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = false + logallrefupdates = true + ignorecase = true + precomposeunicode = true diff --git a/tests/samples/projects/git-with-detached-head/emptyfile.txt b/tests/samples/projects/git-with-detached-head/emptyfile.txt new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_project.py b/tests/test_project.py index c4bdbe2..81b9bd1 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -166,6 +166,26 @@ class LanguagesTestCase(utils.TestCase): self.assertEquals('git', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project']) self.assertEquals('master', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].get('branch')) + def test_git_detached_head_not_used_as_branch(self): + response = Response() + response.status_code = 0 + self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response + + tempdir = tempfile.mkdtemp() + shutil.copytree('tests/samples/projects/git-with-detached-head', os.path.join(tempdir, 'git')) + shutil.move(os.path.join(tempdir, 'git', 'dot_git'), os.path.join(tempdir, 'git', '.git')) + + now = u(int(time.time())) + entity = os.path.join(tempdir, 'git', 'emptyfile.txt') + config = 'tests/samples/configs/good_config.cfg' + + args = ['--file', entity, '--config', config, '--time', now] + + execute(args) + + self.assertEquals('git', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project']) + self.assertNotIn('branch', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]) + def test_svn_project_detected(self): response = Response() response.status_code = 0 diff --git a/wakatime/projects/git.py b/wakatime/projects/git.py index 7f98a4d..e42b702 100644 --- a/wakatime/projects/git.py +++ b/wakatime/projects/git.py @@ -38,11 +38,11 @@ class Git(BaseProject): head = os.path.join(self._project_base(), '.git', 'HEAD') try: with open(head, 'r', encoding='utf-8') as fh: - return u(fh.readline().strip().rsplit('/', 1)[-1]) + return self._get_branch_from_head_file(fh.readline()) except UnicodeDecodeError: # pragma: nocover try: with open(head, 'r', encoding=sys.getfilesystemencoding()) as fh: - return u(fh.readline().strip().rsplit('/', 1)[-1]) + return self._get_branch_from_head_file(fh.readline()) except: log.traceback('warn') except IOError: # pragma: nocover @@ -64,3 +64,8 @@ class Git(BaseProject): if split_path[1] == '': return None return self._find_git_config_file(split_path[0]) + + def _get_branch_from_head_file(self, line): + if u(line.strip()).startswith('ref: '): + return u(line.strip().rsplit('/', 1)[-1]) + return None