class DiffViewer(Table): def __init__(self, parent, app): self.app = app self.commit = None self.win = parent Table.__init__(self, parent, padding=(5,5)) self.show() # description entry self.entry = Entry(self, text='Unknown', line_wrap=ELM_WRAP_MIXED, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH, editable=False) self.pack(self.entry, 0, 0, 1, 1) self.entry.show() # gravatar picture self.picture = GravatarPict(self) self.picture.size_hint_align = 1.0, 0.0 self.picture.show() self.pack(self.picture, 1, 0, 1, 1) # action buttons box self.action_box = Box(self, horizontal=True, size_hint_weight=EXPAND_HORIZ, size_hint_align=(1.0, 1.0)) self.pack(self.action_box, 0, 1, 2, 1) self.action_box.show() # panes panes = Panes(self, content_left_size = 0.3, horizontal=True, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) self.pack(panes, 0, 2, 2, 1) panes.show() # file list self.itc = GenlistItemClass(item_style='default', text_get_func=self._gl_text_get, content_get_func=self._gl_content_get) self.diff_list = Genlist(self, homogeneous=True, mode=ELM_LIST_COMPRESS, select_mode=ELM_OBJECT_SELECT_MODE_ALWAYS, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) self.diff_list.callback_selected_add(self._list_selected_cb) panes.part_content_set('left', self.diff_list) # diff entry self.diff_entry = DiffedEntry(self) panes.part_content_set('right', self.diff_entry) def _gl_text_get(self, li, part, item_data): if isinstance(item_data, tuple): # in real commits mod, staged, name, new = item_data else: # in local changes (item_data is the path) mod, staged, name, new = self.app.repo.status.changes[item_data] return '{} → {}'.format(name, new) if new else name def _gl_content_get(self, li, part, item_data): if isinstance(item_data, tuple): # in real commits mod, staged, name, new = item_data else: # in local changes (item_data is the path) mod, staged, name, new = self.app.repo.status.changes[item_data] if part == 'elm.swallow.icon': return Icon(self, standard='git-mod-'+mod) elif part == 'elm.swallow.end' and staged is not None: ck = Check(self, state=staged, propagate_events=False) ck.callback_changed_add(self._stage_unstage_check_cb) ck.data['path'] = name return ck def update_action_buttons(self, buttons): self.action_box.clear() if 'checkout' in buttons: bt = Button(self, text='Checkout') bt.callback_clicked_add(lambda b: \ self.app.checkout_ref(self.commit.sha)) self.action_box.pack_end(bt) bt.show() if 'revert' in buttons: bt = Button(self, text='Revert') bt.callback_clicked_add(lambda b: \ CommitDialog(self.app, revert_commit=self.commit)) self.action_box.pack_end(bt) bt.show() if 'cherrypick' in buttons: bt = Button(self, text='Cherry-pick') bt.callback_clicked_add(lambda b: \ CommitDialog(self.app, cherrypick_commit=self.commit)) self.action_box.pack_end(bt) bt.show() if 'commit' in buttons: bt = Button(self, text='Commit', content=Icon(self, standard='git-commit')) bt.callback_clicked_add(lambda b: \ CommitDialog(self.app)) self.action_box.pack_end(bt) bt.show() if 'stash' in buttons: bt = Button(self, text='Stash', content=Icon(self, standard='git-stash')) bt.callback_clicked_add(lambda b: self.app.action_stash_save()) self.action_box.pack_end(bt) bt.show() if 'discard' in buttons: bt = Button(self, text='Discard', content=Icon(self, standard='user-trash')) bt.callback_clicked_add(lambda b: DiscardDialog(self.app)) self.action_box.pack_end(bt) bt.show() def show_commit(self, commit): self.commit = commit self.picture.email_set(commit.author_email) line1 = '<name>{}</name> <b>{}</b> {}<br>'.format(commit.sha[:9], commit.author, format_date(commit.commit_date)) line2 = line3 = line4 = '' if commit.committer and commit.committer != commit.author: line2 = '<name>Committed by:</name> <b>{}</b><br>'.format( commit.committer) if commit.title: line3 = '<bigger><b>{}</b></bigger><br>'.format( utf8_to_markup(commit.title.strip())) if commit.message: line4 = '<br>{}'.format(utf8_to_markup(commit.message.strip())) text = line1 + line2 + line3 + line4 self.entry.text = text self.update_action_buttons(['checkout', 'revert', 'cherrypick']) self.diff_entry.text = '' self.diff_list.clear() self.app.repo.request_changes(self._changes_done_cb, commit1=commit) def show_local_status(self): self.commit = None self.entry.text = '<bigger><b>Local status</b></bigger>' self.diff_entry.text = '' self.picture.email_set(None) self.update_action_buttons(['commit', 'stash', 'discard']) self.diff_list.clear() for path in sorted(self.app.repo.status.changes): self.diff_list.item_append(self.itc, path) def refresh_diff(self): if self.diff_list.selected_item: self._list_selected_cb(self.diff_list, self.diff_list.selected_item) def _stage_unstage_check_cb(self, check): path = check.data['path'] if check.state is True: self.app.repo.stage_file(self._stage_unstage_done_cb, path, path) else: self.app.repo.unstage_file(self._stage_unstage_done_cb, path, path) def _stage_unstage_done_cb(self, success, path): self.app.action_update_header() self.diff_list.realized_items_update() def _changes_done_cb(self, success, lines): for mod, name, new_name in lines: item_data = (mod, None, name, new_name) self.diff_list.item_append(self.itc, item_data) self.diff_list.first_item.selected = True def _list_selected_cb(self, li, item): if isinstance(item.data, tuple): # in real commits mod, staged, name, new = item.data else: # in local changes (item_data is the path) mod, staged, name, new = self.app.repo.status.changes[item.data] self.app.repo.request_diff(self._diff_done_cb, ref1=self.commit.sha if self.commit else None, path=name) self.diff_entry.line_wrap = \ ELM_WRAP_MIXED if options.diff_text_wrap else ELM_WRAP_NONE self.diff_entry.loading_set() def _diff_done_cb(self, lines, success): self.diff_entry.lines_set(lines)
class CommitDialog(DialogWindow): def __init__(self, app, revert_commit=None, cherrypick_commit=None): self.app = app self.confirmed = False self.revert_commit = revert_commit self.cherrypick_commit = cherrypick_commit DialogWindow.__init__(self, app.win, 'Egitu', 'Egitu', size=(500,500), autodel=True) vbox = Box(self, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) self.resize_object_add(vbox) vbox.show() # title if revert_commit: title = 'Revert commit' elif cherrypick_commit: title = 'Cherry-pick commit' else: title = 'Commit changes' en = Entry(self, editable=False, text='<title><align=center>%s</align></title>' % title, size_hint_weight=EXPAND_HORIZ, size_hint_align=FILL_HORIZ) vbox.pack_end(en) en.show() # auto-commit checkbox (for revert and cherry-pick) if revert_commit or cherrypick_commit: ck = Check(vbox, state=True) ck.text = 'Automatically commit, in branch: %s' % \ app.repo.status.current_branch.name ck.callback_changed_add(lambda c: self.msg_entry.disabled_set(not c.state)) vbox.pack_end(ck) ck.show() self.autocommit_chk = ck # Panes panes = Panes(self, content_left_size = 0.2, horizontal=True, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) vbox.pack_end(panes) panes.show() # message entry en = Entry(self, editable=True, scrollable=True, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) en.part_text_set('guide', 'Enter commit message here') panes.part_content_set("left", en) if revert_commit: en.text = 'Revert "%s"<br><br>This reverts commit %s.<br><br>' % \ (utf8_to_markup(revert_commit.title), revert_commit.sha) elif cherrypick_commit: en.text = '%s<br><br>%s<br>(cherry picked from commit %s)<br>' % \ (utf8_to_markup(cherrypick_commit.title), utf8_to_markup(cherrypick_commit.message), cherrypick_commit.sha) en.cursor_end_set() en.show() self.msg_entry = en # diff entry self.diff_entry = DiffedEntry(self) panes.part_content_set('right', self.diff_entry) self.diff_entry.show() # buttons hbox = Box(self, horizontal=True, size_hint_weight=EXPAND_HORIZ, size_hint_align=FILL_HORIZ) vbox.pack_end(hbox) hbox.show() bt = Button(self, text='Cancel') bt.callback_clicked_add(lambda b: self.delete()) hbox.pack_end(bt) bt.show() if revert_commit: label = 'Revert' elif cherrypick_commit: label = 'Cherry-pick' else: label = 'Commit' bt = Button(self, text=label) bt.callback_clicked_add(self.commit_button_cb) hbox.pack_end(bt) bt.show() # show the window and give focus to the editable entry self.show() en.focus = True # load the diff if revert_commit: app.repo.request_diff(self.diff_done_cb, revert=True, ref1=revert_commit.sha) elif cherrypick_commit: app.repo.request_diff(self.diff_done_cb, ref1=cherrypick_commit.sha) else: app.repo.request_diff(self.diff_done_cb, only_staged=True) def diff_done_cb(self, lines, success): self.diff_entry.lines_set(lines) def commit_button_cb(self, bt): if not self.confirmed: self.confirmed = True bt.text = 'Are you sure?' elif self.revert_commit: bt.text = 'Revert' self.confirmed = False self.app.repo.revert(self.commit_done_cb, self.revert_commit, auto_commit=self.autocommit_chk.state, commit_msg=markup_to_utf8(self.msg_entry.text)) elif self.cherrypick_commit: bt.text = 'Cherry-pick' self.confirmed = False self.app.repo.cherrypick(self.commit_done_cb, self.cherrypick_commit, auto_commit=self.autocommit_chk.state, commit_msg=markup_to_utf8(self.msg_entry.text)) else: bt.text = 'Commit' self.confirmed = False self.app.repo.commit(self.commit_done_cb, markup_to_utf8(self.msg_entry.text)) def commit_done_cb(self, success, err_msg=None): if success: self.delete() self.app.action_update_all() else: ErrorPopup(self, 'Operation Failed', utf8_to_markup(err_msg))
class StashDialog(DialogWindow): def __init__(self, parent, app, stash=None): self.app = app self.stash = stash or app.repo.stash[0] self.idx = parseint(stash.ref) if stash else 0 DialogWindow.__init__(self, app.win, 'egitu-stash', 'stash', size=(500,500), autodel=True) # main vertical box (inside a padding frame) vbox = Box(self, padding=(0, 6), size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) fr = Frame(self, style='pad_medium', size_hint_weight=EXPAND_BOTH) self.resize_object_add(fr) fr.content = vbox fr.show() vbox.show() # header horizontal box hbox = Box(self, horizontal=True, size_hint_expand=EXPAND_HORIZ, size_hint_fill=FILL_HORIZ) vbox.pack_end(hbox) hbox.show() # title en = Entry(self, editable=False, scrollable=False, size_hint_expand=EXPAND_HORIZ, size_hint_align=(-1,0.0)) hbox.pack_end(en) en.show() self.title_entry = en # header separator sep = Separator(self) hbox.pack_end(sep) sep.show() # navigation table tb = Table(self, size_hint_align=(0.5,0.0)) hbox.pack_end(tb) tb.show() lb = Label(self) tb.pack(lb, 0, 0, 2, 1) lb.show() self.nav_label = lb ic = SafeIcon(self, 'go-previous') bt = Button(self, text='Prev', content=ic) bt.callback_clicked_add(self._prev_clicked_cb) tb.pack(bt, 0, 1, 1, 1) bt.show() self.prev_btn = bt ic = SafeIcon(self, 'go-next') bt = Button(self, text='Next', content=ic) bt.callback_clicked_add(self._next_clicked_cb) tb.pack(bt, 1, 1, 1, 1) bt.show() self.next_btn = bt # diff entry self.diff_entry = DiffedEntry(self) vbox.pack_end(self.diff_entry) self.diff_entry.show() # buttons sep = Separator(self, horizontal=True, size_hint_expand=EXPAND_HORIZ) vbox.pack_end(sep) sep.show() hbox = Box(self, horizontal=True, size_hint_expand=EXPAND_HORIZ, size_hint_fill=FILL_BOTH) vbox.pack_end(hbox) hbox.show() bt = Button(self, text='Apply') bt.callback_clicked_add(self._apply_clicked_cb) hbox.pack_end(bt) bt.show() bt = Button(self, text='Pop (apply & delete)') bt.callback_clicked_add(self._pop_clicked_cb) hbox.pack_end(bt) bt.show() bt = Button(self, text='Branch & Delete', content=SafeIcon(self, 'git-branch')) bt.callback_clicked_add(self._branch_clicked_cb) hbox.pack_end(bt) bt.show() bt = Button(self, text='Delete', content=SafeIcon(self, 'user-trash')) bt.callback_clicked_add(self._drop_clicked_cb) hbox.pack_end(bt) bt.show() sep = Separator(self, size_hint_expand=EXPAND_HORIZ) hbox.pack_end(sep) bt = Button(self, text='Close') bt.callback_clicked_add(lambda b: self.delete()) hbox.pack_end(bt) bt.show() # request the diff and show the dialog self.update(self.idx) self.show() def update(self, idx): self.idx = idx self.stash = self.app.repo.stash[idx] stash_len = len(self.app.repo.stash) self.title = self.stash.ref self.title_entry.text = \ '<name>Stash item</> #{} <name>Created</> {}<br>' \ '<subtitle>{}</>'.format(idx, format_date(self.stash.ts), self.stash.desc) self.nav_label.text = \ '{} {}<br>in the stash'.format(stash_len, 'items' if stash_len > 1 else 'item') self.prev_btn.disabled = (idx == 0) self.next_btn.disabled = (idx >= stash_len - 1) self.diff_entry.loading_set() self.app.repo.stash_request_diff(self._diff_done_cb, self.stash) def _prev_clicked_cb(self, btn): self.update(self.idx - 1) def _next_clicked_cb(self, btn): self.update(self.idx + 1) def _diff_done_cb(self, lines, success): self.diff_entry.lines_set(lines) def _drop_clicked_cb(self, btn): self.app.action_stash_drop(self.stash) self.delete() def _apply_clicked_cb(self, btn): self.app.action_stash_apply(self.stash) self.delete() def _pop_clicked_cb(self, btn): self.app.action_stash_pop(self.stash) self.delete() def _branch_clicked_cb(self, btn): self.app.action_stash_branch(self.stash) self.delete()
class CommitDialog(StandardWindow): def __init__(self, repo, win): self.repo = repo self.win = win self.confirmed = False StandardWindow.__init__(self, 'Egitu', 'Egitu', autodel=True) vbox = Box(self, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) self.resize_object_add(vbox) vbox.show() # title en = Entry(self, editable=False, text='<title><align=center>Commit changes</align></title>', size_hint_weight=EXPAND_HORIZ, size_hint_align=FILL_HORIZ) vbox.pack_end(en) en.show() panes = Panes(self, content_left_size = 0.2, horizontal=True, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) vbox.pack_end(panes) panes.show() # message entry en = Entry(self, editable=True, scrollable=True, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) en.part_text_set('guide', 'Enter commit message here') panes.part_content_set("left", en) en.show() self.msg_entry = en # diff entry self.diff_entry = DiffedEntry(self) panes.part_content_set("right", self.diff_entry) self.diff_entry.show() # buttons hbox = Box(self, horizontal=True, size_hint_weight=EXPAND_HORIZ, size_hint_align=FILL_HORIZ) vbox.pack_end(hbox) hbox.show() bt = Button(self, text="Cancel") bt.callback_clicked_add(lambda b: self.delete()) hbox.pack_end(bt) bt.show() bt = Button(self, text="Commit") bt.callback_clicked_add(self.commit_button_cb) hbox.pack_end(bt) bt.show() # show the window and give focus to the editable entry self.size = 500, 500 self.show() en.focus = True # load the diff repo.request_diff(self.diff_done_cb, only_staged=True) def diff_done_cb(self, lines, success): self.diff_entry.lines_set(lines) def commit_button_cb(self, bt): if not self.confirmed: self.confirmed = True bt.text = 'Are you sure?' else: bt.text = 'Commit' self.confirmed = False self.repo.commit(self.commit_done_cb, markup_to_utf8(self.msg_entry.text)) def commit_done_cb(self, success, err_msg=None): if success: self.delete() self.win.update_header() self.win.graph.populate(self.repo) else: ErrorPopup(self, 'Commit Failed', utf8_to_markup(err_msg))
class DiffViewer(Table): def __init__(self, parent, repo): self.repo = repo self.commit = None self.win = parent Table.__init__(self, parent, padding=(5,5)) self.show() # gravatar picture self.picture = GravatarPict(self) self.picture.size_hint_align = 0.0, 0.0 self.picture.show() self.pack(self.picture, 0, 0, 1, 2) # description entry self.entry = Entry(self, text="Unknown", line_wrap=ELM_WRAP_MIXED, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH, editable=False) self.pack(self.entry, 1, 0, 1, 1) self.entry.show() # action buttons box self.action_box = Box(self, horizontal=True, size_hint_weight=EXPAND_HORIZ, size_hint_align=(0.98, 0.98)) self.pack(self.action_box, 1, 1, 1, 1) self.action_box.show() # panes panes = Panes(self, content_left_size = 0.3, horizontal=True, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) self.pack(panes, 0, 2, 2, 1) panes.show() # file list self.diff_list = List(self, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) self.diff_list.callback_selected_add(self.change_selected_cb) panes.part_content_set("left", self.diff_list) # diff entry self.diff_entry = DiffedEntry(self) panes.part_content_set("right", self.diff_entry) def update_action_buttons(self, buttons): self.action_box.clear() if 'revert' in buttons: bt = Button(self, text="Revert", disabled=True) self.action_box.pack_end(bt) bt.show() if 'commit' in buttons: bt = Button(self, text="Commit") bt.callback_clicked_add(lambda b: CommitDialog(self.repo, self.win)) self.action_box.pack_end(bt) bt.show() if 'discard' in buttons: bt = Button(self, text="Discard", disabled=True) self.action_box.pack_end(bt) bt.show() def commit_set(self, repo, commit): self.repo = repo self.commit = commit self.diff_list.clear() self.diff_entry.text = '' if commit.sha: # a real commit text = u'<name>{0}</name> <b>{1}</b> {2}<br>' \ '<bigger><b>{3}</b></bigger>'.format(commit.sha[:9], commit.author, format_date(commit.commit_date), commit.title) if commit.message: msg = commit.message.strip().replace('\n', '<br>') text += u'<br><br>{}'.format(msg) repo.request_changes(self.changes_done_cb, commit1=commit) self.update_action_buttons(['revert']) else: # or the fake 'local changes' commit text = "<bigger><b>Local changes</b></bigger>" self.show_local_status() self.update_action_buttons(['commit', 'discard']) self.entry.text = text self.picture.email_set(commit.author_email) def show_local_status(self): sortd = sorted(self.repo.status.changes, key=lambda c: c[2]) for mod, staged, name in sortd: icon_name = 'mod_{}.png'.format(mod.lower()) icon = Icon(self, file=theme_resource_get(icon_name)) check = Check(self, text="", state=staged) check.callback_changed_add(self.stage_unstage_cb) check.data['path'] = name it = self.diff_list.item_append(name, icon, check) it.data['change'] = mod, name self.diff_list.go() def stage_unstage_cb(self, check): def stage_unstage_done_cb(success): self.win.update_header() if check.state is True: self.repo.stage_file(stage_unstage_done_cb, check.data['path']) else: self.repo.unstage_file(stage_unstage_done_cb, check.data['path']) def refresh_diff(self): if self.diff_list.selected_item: self.change_selected_cb(self.diff_list, self.diff_list.selected_item) def changes_done_cb(self, success, lines): for mod, name in lines: if mod in ('M', 'A', 'D'): icon_name = 'mod_{}.png'.format(mod.lower()) icon = Icon(self, file=theme_resource_get(icon_name)) it = self.diff_list.item_append(name, icon) else: it = self.diff_list.item_append('[{}] {}'.format(mod, name)) it.data['change'] = mod, name self.diff_list.first_item.selected = True self.diff_list.go() def change_selected_cb(self, li, item): mod, path = item.data['change'] self.repo.request_diff(self.diff_done_cb, commit1=self.commit, path=path) self.diff_entry.line_wrap = \ ELM_WRAP_MIXED if options.diff_text_wrap else ELM_WRAP_NONE self.diff_entry.text = '<info>Loading diff, please wait...</info>' def diff_done_cb(self, lines, success): self.diff_entry.lines_set(lines)