/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
742 by Jelmer Vernooij
Move commit message saving to separate file.
1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
try:
18
    from bzrlib import bencode
19
except ImportError:
20
    from bzrlib.util import bencode
21
22
from bzrlib import osutils
23
24
class SavedCommitMessagesManager(object):
25
    """Save global and per-file commit messages.
26
27
    Saves global commit message and utf-8 file_id->message dictionary
28
    of per-file commit messages on disk. Re-reads them later for re-using.
29
    """
30
31
    def __init__(self, tree=None, branch=None):
32
        """If branch is None, builds empty messages, otherwise reads them
33
        from branch's disk storage. 'tree' argument is for the future."""
34
        if branch is None:
35
            self.global_message = u''
36
            self.file_messages = {}
37
        else:
38
            config = branch.get_config()
39
            self.global_message = config.get_user_option(
40
                'gtk_global_commit_message')
41
            if self.global_message is None:
42
                self.global_message = u''
43
            file_messages = config.get_user_option('gtk_file_commit_messages')
44
            if file_messages: # unicode and B-encoded:
45
                self.file_messages = bencode.bdecode(
46
                    file_messages.encode('UTF-8'))
47
            else:
48
                self.file_messages = {}
49
50
    def get(self):
51
        return self.global_message, self.file_messages
52
53
    def is_not_empty(self):
54
        return bool(self.global_message or self.file_messages)
55
56
    def insert(self, global_message, file_info):
57
        """Formats per-file commit messages (list of dictionaries, one per file)
58
        into one utf-8 file_id->message dictionary and merges this with
59
        previously existing dictionary. Merges global commit message too."""
60
        file_messages = {}
61
        for fi in file_info:
62
            file_message = fi['message']
63
            if file_message:
64
                file_messages[fi['file_id']] = file_message # utf-8 strings
65
        for k,v in file_messages.iteritems():
66
            try:
67
                self.file_messages[k] = v + '\n******\n' + self.file_messages[k]
68
            except KeyError:
69
                self.file_messages[k] = v
70
        if self.global_message:
71
            self.global_message = global_message + '\n******\n' \
72
                + self.global_message
73
        else:
74
            self.global_message = global_message
75
76
    def save(self, tree, branch):
77
        # We store in branch's config, which can be a problem if two gcommit
78
        # are done in two checkouts of one single branch (comments overwrite
79
        # each other). Ideally should be in working tree. But uncommit does
80
        # not always have a working tree, though it always has a branch.
81
        # 'tree' argument is for the future
82
        config = branch.get_config()
83
        # should it be named "gtk_" or some more neutral name ("gui_" ?) to
84
        # be compatible with qbzr in the future?
85
        config.set_user_option('gtk_global_commit_message', self.global_message)
86
        # bencode() does not know unicode objects but set_user_option()
87
        # requires one:
88
        config.set_user_option(
89
            'gtk_file_commit_messages',
90
            bencode.bencode(self.file_messages).decode('UTF-8'))
91
92
93
def save_commit_messages(local, master, old_revno, old_revid,
94
                         new_revno, new_revid):
95
    b = local
96
    if b is None:
97
        b = master
98
    mgr = SavedCommitMessagesManager(None, b)
743 by Jelmer Vernooij
Avoid Repository.iter_reverse_revision_history.
99
    graph = b.repository.get_graph()
100
    revid_iterator = graph.iter_lefthand_ancestry(old_revid)
742 by Jelmer Vernooij
Move commit message saving to separate file.
101
    cur_revno = old_revno
102
    new_revision_id = old_revid
103
    graph = b.repository.get_graph()
104
    for rev_id in revid_iterator:
105
        if cur_revno == new_revno:
106
            break
107
        cur_revno -= 1
108
        rev = b.repository.get_revision(rev_id)
109
        file_info = rev.properties.get('file-info', None)
110
        if file_info is None:
111
            file_info = {}
112
        else:
113
            file_info = bencode.bdecode(file_info.encode('UTF-8'))
114
        global_message = osutils.safe_unicode(rev.message)
115
        # Concatenate comment of the uncommitted revision
116
        mgr.insert(global_message, file_info)
117
118
        parents = graph.get_parent_map([rev_id]).get(rev_id, None)
119
        if not parents:
120
            continue
121
    mgr.save(None, b)