Beispiel #1
0
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)
Beispiel #2
0
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()