Skip to content

Commit 42e9db8

Browse files
committed
Add feature to jump directly to the commit that introduced the current line (#3)
1 parent 5b53715 commit 42e9db8

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

gitbrowse/browser.py

+33
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,39 @@ def _move_commit(self, method_name):
9696

9797
self._update_mapping(start, finish)
9898

99+
def _jump_to_commit(self, sha):
100+
101+
start = self.file_history.current_commit.sha
102+
103+
if not self.file_history.jump_to_commit(sha):
104+
curses.beep()
105+
return
106+
107+
finish = sha
108+
109+
if start == finish:
110+
curses.beep()
111+
return
112+
113+
self._update_mapping(start, finish)
114+
115+
@ModalScrollingInterface.key_bindings('i')
116+
def info(self, times=1):
117+
with open('debug.out', 'a') as f:
118+
f.write("%s\n" % self.highlight_line)
119+
blame_line = self.content()[self.highlight_line]
120+
f.write("--- blame line\n")
121+
f.write("sha %s\n" % blame_line.sha)
122+
f.write("line %s\n" % blame_line.line)
123+
f.write("current %s\n" % blame_line.current)
124+
f.write("original_line %s\n" % blame_line.original_line)
125+
f.write("final_line %s\n" % blame_line.final_line)
126+
127+
@ModalScrollingInterface.key_bindings('j')
128+
def info(self, times=1):
129+
blame_line = self.content()[self.highlight_line]
130+
self._jump_to_commit(blame_line.sha)
131+
99132
@ModalScrollingInterface.key_bindings(']')
100133
def next_commit(self, times=1):
101134
for i in range(0,times):

gitbrowse/git.py

+20
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ def prev(self):
8787
self._blame = None
8888
return True
8989

90+
def jump_to_commit(self, sha):
91+
"""
92+
Moves to the given commit SHA, returning False if it doesn't exist.
93+
"""
94+
found_index = None
95+
for i, commit in enumerate(self.commits):
96+
if commit.sha == sha:
97+
found_index = i
98+
99+
with open('debug.out', 'a') as f:
100+
f.write("found_index %i\n" % found_index)
101+
102+
103+
if found_index is None:
104+
return False
105+
106+
self._index = found_index
107+
self._blame = None
108+
return True
109+
90110
def blame(self):
91111
"""
92112
Returns blame information for this file at the current commit as

0 commit comments

Comments
 (0)