forked from jisaacks/GitGutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_gutter_compare.py
118 lines (95 loc) · 4.36 KB
/
git_gutter_compare.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# -*- coding: utf-8 -*-
import functools
import sublime
class GitGutterCompareCommit(object):
def __init__(self, git_handler):
self.git_handler = git_handler
def run(self):
self.commit_list().then(self._show_quick_panel)
def commit_list(self):
"""Built a list of quick panel items with all commits."""
def parse_results(results):
"""Parse git output and create the quick panel items."""
if results:
return [r.split('\a') for r in results.split('\n')]
sublime.message_dialog('No commits found in repository.')
return []
return self.git_handler.git_commits().then(parse_results)
def item_to_commit(self, item):
return item[0].split(' ')[0]
def _show_quick_panel(self, results):
if results:
self.git_handler.view.window().show_quick_panel(
results, functools.partial(self._on_select, results))
def _on_select(self, results, selected):
if 0 > selected < len(results):
return
item = results[selected]
commit = self.item_to_commit(item)
self.git_handler.set_compare_against(commit)
class GitGutterCompareFileCommit(GitGutterCompareCommit):
def commit_list(self):
"""Built a list of quick panel items with all file commits."""
def parse_results(results):
"""Parse git output and create the quick panel items."""
if results:
# sort splitted lines by author date in reversed order
sorted_results = sorted(results.split('\n'), reverse=True)
# split each line by \a and strip time stamp from beginning
return [r.split('\a')[1:] for r in sorted_results]
sublime.message_dialog(
'No commits of this file found in repository.')
return []
return self.git_handler.git_file_commits().then(parse_results)
class GitGutterCompareBranch(GitGutterCompareCommit):
def commit_list(self):
"""Built a list of quick panel items with all local branches."""
def parse_result(result):
"""Create a quick panel item for one line of git's output."""
pieces = result.split('\a')
message = pieces[0]
branch = pieces[1][11:] # skip 'refs/heads/'
commit = pieces[2][0:7] # 7-digit commit hash
return [branch, '%s %s' % (commit, message)]
def parse_results(results):
"""Parse git output and create the quick panel items."""
if results:
return [parse_result(r) for r in results.split('\n')]
sublime.message_dialog('No branches found in repository.')
return []
return self.git_handler.git_branches().then(parse_results)
def item_to_commit(self, item):
return 'refs/heads/%s' % item[0]
class GitGutterCompareTag(GitGutterCompareCommit):
def commit_list(self):
"""Built a list of quick panel items with all tags."""
def parse_result(result):
"""Create a quick panel item for one line of git's output."""
pieces = result.split(' ')
commit = pieces[0] # 7-digit commit hash
tag = pieces[1][10:] # skip 'refs/tags/'
return [tag, commit]
def parse_results(results):
"""Parse git output and create the quick panel items."""
if results:
return [parse_result(r) for r in results.split('\n')]
sublime.message_dialog('No tags found in repository.')
return []
return self.git_handler.git_tags().then(parse_results)
def item_to_commit(self, item):
return 'refs/tags/%s' % item[0]
class GitGutterCompareHead(GitGutterCompareCommit):
def run(self):
self.git_handler.set_compare_against('HEAD', True)
class GitGutterCompareOrigin(GitGutterCompareCommit):
def run(self):
def on_branch_name(branch_name):
if branch_name:
self.git_handler.set_compare_against(
'origin/%s' % branch_name, True)
self.git_handler.git_current_branch().then(on_branch_name)
class GitGutterShowCompare(GitGutterCompareCommit):
def run(self):
comparing = self.git_handler.format_compare_against()
sublime.message_dialog(
'GitGutter is comparing against: %s' % comparing)