Skip to content
This repository was archived by the owner on Oct 9, 2021. It is now read-only.

Commit c14df20

Browse files
committed
support slashes in Mercurial and Git branch names
1 parent 87d435f commit c14df20

8 files changed

Lines changed: 42 additions & 3 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/branch/with/slash
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = false
5+
logallrefupdates = true
6+
ignorecase = true
7+
precomposeunicode = true

tests/samples/projects/git-branch-with-slash/emptyfile.txt

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
branch/with/slash

tests/samples/projects/hg-branch-with-slash/emptyfile.txt

Whitespace-only changes.

tests/test_project.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,25 @@ def test_mercurial_project_detected(self):
399399
self.assertEquals('hg', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
400400
self.assertEquals('test-hg-branch', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['branch'])
401401

402+
def test_mercurial_project_branch_with_slash_detected(self):
403+
response = Response()
404+
response.status_code = 0
405+
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
406+
407+
with mock.patch('wakatime.projects.git.Git.process') as mock_git:
408+
mock_git.return_value = False
409+
410+
now = u(int(time.time()))
411+
entity = 'tests/samples/projects/hg-branch-with-slash/emptyfile.txt'
412+
config = 'tests/samples/configs/good_config.cfg'
413+
414+
args = ['--file', entity, '--config', config, '--time', now]
415+
416+
execute(args)
417+
418+
self.assertEquals('hg-branch-with-slash', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
419+
self.assertEquals('branch/with/slash', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['branch'])
420+
402421
@log_capture()
403422
def test_ioerror_when_reading_mercurial_branch(self, logs):
404423
logging.disable(logging.NOTSET)
@@ -668,6 +687,17 @@ def test_git_path_from_gitdir_link_file_handles_invalid_link(self, logs):
668687
self.assertNothingPrinted()
669688
self.assertNothingLogged(logs)
670689

690+
def test_git_branch_with_slash(self):
691+
tempdir = tempfile.mkdtemp()
692+
shutil.copytree('tests/samples/projects/git-branch-with-slash', os.path.join(tempdir, 'git'))
693+
shutil.move(os.path.join(tempdir, 'git', 'dot_git'), os.path.join(tempdir, 'git', '.git'))
694+
695+
self.shared(
696+
expected_project='git',
697+
expected_branch='branch/with/slash',
698+
entity=os.path.join(tempdir, 'git', 'emptyfile.txt'),
699+
)
700+
671701
@log_capture()
672702
def test_project_map(self, logs):
673703
logging.disable(logging.NOTSET)

wakatime/projects/git.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def _find_git_config_file(self, path):
7878

7979
def _get_branch_from_head_file(self, line):
8080
if u(line.strip()).startswith('ref: '):
81-
return u(line.strip().rsplit('/', 1)[-1])
81+
return u(line.strip().split('/', 2)[-1])
8282
return None
8383

8484
def _submodules_supported_for_path(self, path):

wakatime/projects/mercurial.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ def branch(self):
3636
branch_file = os.path.join(self.configDir, 'branch')
3737
try:
3838
with open(branch_file, 'r', encoding='utf-8') as fh:
39-
return u(fh.readline().strip().rsplit('/', 1)[-1])
39+
return u(fh.readline().strip())
4040
except UnicodeDecodeError: # pragma: nocover
4141
try:
4242
with open(branch_file, 'r', encoding=sys.getfilesystemencoding()) as fh:
43-
return u(fh.readline().strip().rsplit('/', 1)[-1])
43+
return u(fh.readline().strip())
4444
except:
4545
log.traceback(logging.WARNING)
4646
except IOError: # pragma: nocover

0 commit comments

Comments
 (0)