/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to olive/diff.py

  • Committer: Vincent Ladeuil
  • Date: 2008-06-10 15:25:47 UTC
  • mto: This revision was merged to the branch mainline in revision 504.
  • Revision ID: v.ladeuil+lp@free.fr-20080610152547-dwmil1p8pd0mfpnl
Fix third failing test (thanks to jam).

* tests/test_commit.py:
(TestPendingRevisions.test_pending_revisions_multi_merge): Fix
provided by jam: bzr we now filter the pending merges so that only
the 'heads()' are included. We just ensure that the pending merges
contain the unique tips for the ancestries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
2
 
#
3
 
# Some parts of the code:
4
 
# Copyright (C) 2005 by Canonical Ltd.
5
 
# Author: Scott James Remnant <scott@ubuntu.com>
6
 
#
7
 
# This program is free software; you can redistribute it and/or modify
8
 
# it under the terms of the GNU General Public License as published by
9
 
# the Free Software Foundation; either version 2 of the License, or
10
 
# (at your option) any later version.
11
 
#
12
 
# This program is distributed in the hope that it will be useful,
13
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
# GNU General Public License for more details.
16
 
#
17
 
# You should have received a copy of the GNU General Public License
18
 
# along with this program; if not, write to the Free Software
19
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 
 
21
 
import sys
22
 
 
23
 
from cStringIO import StringIO
24
 
 
25
 
try:
26
 
    import pygtk
27
 
    pygtk.require("2.0")
28
 
except:
29
 
    pass
30
 
try:
31
 
    import gtk
32
 
    import gtk.glade
33
 
    import gobject
34
 
    import pango
35
 
except:
36
 
    sys.exit(1)
37
 
 
38
 
try:
39
 
    import gtksourceview
40
 
    have_gtksourceview = True
41
 
except ImportError:
42
 
    have_gtksourceview = False
43
 
 
44
 
import bzrlib
45
 
 
46
 
from bzrlib.diff import show_diff_trees
47
 
import bzrlib.errors as errors
48
 
from bzrlib.workingtree import WorkingTree
49
 
 
50
 
class OliveDiff:
51
 
    """ Display Diff window and perform the needed actions. """
52
 
    def __init__(self, gladefile, comm):
53
 
        """ Initialize the Diff window. """
54
 
        self.gladefile = gladefile
55
 
        self.glade = gtk.glade.XML(self.gladefile, 'window_diff', 'olive-gtk')
56
 
        
57
 
        # Communication object
58
 
        self.comm = comm
59
 
        
60
 
        # Get some important widgets
61
 
        self.window = self.glade.get_widget('window_diff')
62
 
        self.treeview = self.glade.get_widget('treeview_diff_files')
63
 
 
64
 
        # Check if current location is a branch
65
 
        try:
66
 
            (self.wt, path) = WorkingTree.open_containing(self.comm.get_path())
67
 
            branch = self.wt.branch
68
 
        except errors.NotBranchError:
69
 
            self.notbranch = True
70
 
            return
71
 
        except:
72
 
            raise
73
 
 
74
 
        file_id = self.wt.path2id(path)
75
 
 
76
 
        self.notbranch = False
77
 
        if file_id is None:
78
 
            self.notbranch = True
79
 
            return
80
 
        
81
 
        # Set the old working tree
82
 
        self.old_tree = self.wt.branch.repository.revision_tree(self.wt.branch.last_revision())
83
 
        
84
 
        # Dictionary for signal_autoconnect
85
 
        dic = { "on_button_diff_close_clicked": self.close,
86
 
                "on_treeview_diff_files_cursor_changed": self.cursor_changed }
87
 
        
88
 
        # Connect the signals to the handlers
89
 
        self.glade.signal_autoconnect(dic)
90
 
        
91
 
        # Create the file list
92
 
        self._create_file_view()
93
 
        
94
 
        # Generate initial diff
95
 
        self._init_diff()
96
 
    
97
 
    def display(self):
98
 
        """ Display the Diff window. """
99
 
        if self.notbranch:
100
 
            error_dialog(_('Directory is not a branch'),
101
 
                                     _('You can perform this action only in a branch.'))
102
 
            self.close()
103
 
        else:
104
 
            self.window.show_all()
105
 
    
106
 
    def _create_file_view(self):
107
 
        """ Create the list of files. """
108
 
        self.model = gtk.TreeStore(str, str)
109
 
        self.treeview.set_model(self.model)
110
 
        
111
 
        cell = gtk.CellRendererText()
112
 
        cell.set_property("width-chars", 20)
113
 
        column = gtk.TreeViewColumn()
114
 
        column.pack_start(cell, expand=True)
115
 
        column.add_attribute(cell, "text", 0)
116
 
        self.treeview.append_column(column)
117
 
        
118
 
        if have_gtksourceview:
119
 
            self.buffer = gtksourceview.SourceBuffer()
120
 
            slm = gtksourceview.SourceLanguagesManager()
121
 
            gsl = slm.get_language_from_mime_type("text/x-patch")
122
 
            self.buffer.set_language(gsl)
123
 
            self.buffer.set_highlight(True)
124
 
 
125
 
            sourceview = gtksourceview.SourceView(self.buffer)
126
 
        else:
127
 
            self.buffer = gtk.TextBuffer()
128
 
            sourceview = gtk.TextView(self.buffer)
129
 
 
130
 
        sourceview.set_editable(False)
131
 
        sourceview.modify_font(pango.FontDescription("Monospace"))
132
 
        scrollwin_diff = self.glade.get_widget('scrolledwindow_diff_diff')
133
 
        scrollwin_diff.add(sourceview)
134
 
    
135
 
    def _init_diff(self):
136
 
        """ Generate initial diff. """
137
 
        self.model.clear()
138
 
        delta = self.wt.changes_from(self.old_tree)
139
 
 
140
 
        self.model.append(None, [ _('Complete Diff'), "" ])
141
 
 
142
 
        if len(delta.added):
143
 
            titer = self.model.append(None, [ _('Added'), None ])
144
 
            for path, id, kind in delta.added:
145
 
                self.model.append(titer, [ path, path ])
146
 
 
147
 
        if len(delta.removed):
148
 
            titer = self.model.append(None, [ _('Removed'), None ])
149
 
            for path, id, kind in delta.removed:
150
 
                self.model.append(titer, [ path, path ])
151
 
 
152
 
        if len(delta.renamed):
153
 
            titer = self.model.append(None, [ _('Renamed'), None ])
154
 
            for oldpath, newpath, id, kind, text_modified, meta_modified \
155
 
                    in delta.renamed:
156
 
                self.model.append(titer, [ oldpath, newpath ])
157
 
 
158
 
        if len(delta.modified):
159
 
            titer = self.model.append(None, [ _('Modified'), None ])
160
 
            for path, id, kind, text_modified, meta_modified in delta.modified:
161
 
                self.model.append(titer, [ path, path ])
162
 
 
163
 
        self.treeview.expand_all()
164
 
    
165
 
    def cursor_changed(self, *args):
166
 
        """ Callback when the TreeView cursor changes. """
167
 
        (path, col) = self.treeview.get_cursor()
168
 
        specific_files = [ self.model[path][1] ]
169
 
        if specific_files == [ None ]:
170
 
            return
171
 
        elif specific_files == [ "" ]:
172
 
            specific_files = []
173
 
 
174
 
        s = StringIO()
175
 
        show_diff_trees(self.old_tree, self.wt, s, specific_files)
176
 
        self.buffer.set_text(s.getvalue())
177
 
    
178
 
    def close(self, widget=None):
179
 
        self.window.destroy()