Skip to content

Commit

Permalink
Display unmerged diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
Atsuo Ishimoto committed Mar 13, 2016
1 parent 4b5452a commit 9933c98
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 34 deletions.
19 changes: 14 additions & 5 deletions kaa/commands/gitcommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@

class GitCommands(Commands):

def _get_cur_dir(self, wnd):
if wnd.document.fileinfo:
return wnd.document.fileinfo.dirname
else:
return os.getcwd()

@commandid('git.status')
@norec
@norerun
def status(self, wnd):
from kaa.ui.git import statusmode
if wnd.document.fileinfo:
dirname = wnd.document.fileinfo.dirname
else:
dirname = os.getcwd()
statusmode.show_git_status(dirname)
statusmode.show_git_status(self._get_cur_dir(wnd))

@commandid('git.log')
@norec
@norerun
def log(self, wnd):
from kaa.ui.git import logmode
logmode.show_git_log(self._get_cur_dir(wnd))
1 change: 1 addition & 0 deletions kaa/filetype/default/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@

'GIT':
[['&Status', None, 'git.status'],
['&Log', None, 'git.log'],
],

'WINDOW':
Expand Down
85 changes: 65 additions & 20 deletions kaa/ui/git/statusmode.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import socket
import tempfile
from git import Repo
from git import BaseIndexEntry
from pathlib import Path
import kaa
from kaa.keyboard import *
Expand Down Expand Up @@ -278,34 +279,57 @@ def callback_commit():
if os.path.exists(fname):
os.unlink(fname)

def _add_new_file(self, style, name, mark_prefix):
f = self.document.append('new file: ', style)
def _add_new_file(self, indent, style, name, mark_prefix):
mark = mark_prefix + name
if mark in self.document.marks:
return

f = self.document.append(indent+'new file: ', style)
t = self.document.append(name, style)
self.document.marks[mark_prefix + name] = (f, t)
self.document.marks[mark] = (f, t)
return True

def _add_diff(self, d, style, mark_prefix):
def _add_diff(self, indent, d, style, mark_prefix):
if d.new_file:
self._add_new_file(style, d.b_path, mark_prefix)
return self._add_new_file(indent, style, d.b_path, mark_prefix)

elif d.deleted_file:
f = self.document.append('deleted: ', style)
mark = mark_prefix + d.b_path
if mark in self.document.marks:
return

f = self.document.append(indent + 'deleted: ', style)
t = self.document.append(d.b_path, style)
self.document.marks[mark_prefix + d.b_path] = (f, t)
self.document.marks[mark] = (f, t)

elif d.renamed:
f = self.document.append('renamed: ', style)
mark_from = mark_prefix + d.rename_from
if mark_from in self.document.marks:
return

mark_to = mark_prefix + d.rename_to
if mark_to in self.document.marks:
return

f = self.document.append(indent + 'renamed: ', style)
t = self.document.append(d.rename_from, style)
self.document.marks[mark_prefix + d.rename_from] = (f, t)
self.document.marks[mark_from] = (f, t)

f = self.document.append(' -> ', style)
t = self.document.append(d.rename_to, style)
self.document.marks[mark_prefix + d.rename_to] = (f, t)
self.document.marks[mark_to] = (f, t)

else:
f = self.document.append('modified: ', style)
mark = mark_prefix + d.b_path
if mark in self.document.marks:
return

f = self.document.append(indent + 'modified: ', style)
t = self.document.append(d.b_path, style)
self.document.marks[mark_prefix + d.b_path] = (f, t)

return True

def _refresh(self, wnd):
if wnd:
lineno = self.document.buf.lineno.lineno(wnd.cursor.pos)
Expand Down Expand Up @@ -335,31 +359,52 @@ def _refresh(self, wnd):

self.document.append('\n\n')

# unmerged
unmerged = {}
if self._repo.head.is_valid():
d = self._repo.index.unmerged_blobs()
for path, blobs in d.items():
stagemask = 0
for stage, blob in blobs:
stagemask |= (1 << (stage-1)) # see man git-merge, wt-status.c: unmerged_mask()

unmerged[path] = stagemask


# add staged files
unmerged_diffs = []
self.document.append('Changes to be committed:\n\n', style_header)
if self._repo.head.is_valid():
d = self._repo.head.commit.diff()
for c in d:
self.document.append(indent)
self._add_diff(c, style_staged, 's_')
self.document.append('\n')
if c.b_path in unmerged:
unmerged_diffs.append(c)
continue

if self._add_diff(indent, c, style_staged, 's_'):
self.document.append('\n')
else:
files = [name for name, state in self._repo.index.entries]
files.sort()
for file in files:
self.document.append(indent)
self._add_new_file(style_staged, file, 's_')
self.document.append('\n')
if self._add_new_file(indent, style_staged, file, 's_'):
self.document.append('\n')

# add unmerged files
if unmerged_diffs:
self.document.append('\nUnmerged paths:\n\n', style_header)
for c in unmerged_diffs:
if self._add_diff(indent, c, style_staged, 'n_'):
self.document.append('\n')

# add not staged files
self.document.append(
'\nChanges not staged for commit:\n\n', style_header)

d = self._repo.index.diff(None)
for c in d:
self.document.append(indent)
self._add_diff(c, style_not_staged, 'n_')
self.document.append('\n')
if self._add_diff(indent, c, style_not_staged, 'n_'):
self.document.append('\n')

# add untracked files
self.document.append('\n\nUntracked fies:\n\n', style_header)
Expand Down
18 changes: 9 additions & 9 deletions kaa/ui/wordcomplete/wordcompletemode.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@ def start(self, list):
wnd = self.document.wnds[0]

curword = ''
# p = max(0, self.orgpos - 1)
word = self.target.document.mode.get_word_at(self.orgpos)
if word:
f, t, cg = word
if f < t and cg[0] in 'LMN': # Letter, Mark, Number
self.wordpos = (f, t)

curword = self.target.document.gettext(f, t)
if curword:
self.target.screen.selection.set_range(f, t)
self.set_query(wnd, curword)
if self.orgpos != 0 and self.orgpos == f and (cg[0] not in 'LMN'):
# cursor is at top of non-word char.
# check if we are at end of word.
prev = self.target.document.mode.get_word_at(self.orgpos-1)
if prev:
pf, pt, pcg = prev
if pt == self.orgpos and (pcg[0] in 'LMN'):
# select previous word
f, t, cg = pf, pt, pcg

# build word list
# word at cursor position should not appear in the list.
Expand Down

0 comments on commit 9933c98

Please sign in to comment.