/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
768 by Jelmer Vernooij
Drop support for old bencode location.
17
from bzrlib import (
18
    bencode,
19
    osutils,
20
    )
742 by Jelmer Vernooij
Move commit message saving to separate file.
21
22
class SavedCommitMessagesManager(object):
23
    """Save global and per-file commit messages.
24
25
    Saves global commit message and utf-8 file_id->message dictionary
26
    of per-file commit messages on disk. Re-reads them later for re-using.
27
    """
28
29
    def __init__(self, tree=None, branch=None):
30
        """If branch is None, builds empty messages, otherwise reads them
31
        from branch's disk storage. 'tree' argument is for the future."""
32
        if branch is None:
33
            self.global_message = u''
34
            self.file_messages = {}
35
        else:
36
            config = branch.get_config()
37
            self.global_message = config.get_user_option(
38
                'gtk_global_commit_message')
39
            if self.global_message is None:
40
                self.global_message = u''
41
            file_messages = config.get_user_option('gtk_file_commit_messages')
42
            if file_messages: # unicode and B-encoded:
43
                self.file_messages = bencode.bdecode(
44
                    file_messages.encode('UTF-8'))
45
            else:
46
                self.file_messages = {}
47
48
    def get(self):
49
        return self.global_message, self.file_messages
50
51
    def is_not_empty(self):
52
        return bool(self.global_message or self.file_messages)
53
54
    def insert(self, global_message, file_info):
55
        """Formats per-file commit messages (list of dictionaries, one per file)
56
        into one utf-8 file_id->message dictionary and merges this with
57
        previously existing dictionary. Merges global commit message too."""
58
        file_messages = {}
59
        for fi in file_info:
60
            file_message = fi['message']
61
            if file_message:
769 by Jelmer Vernooij
Cope with some strings being unicode when returned by some versions of gtk.
62
                file_id = fi['file_id']
63
                assert type(file_id) is str
64
                file_messages[file_id] = file_message # utf-8 strings
742 by Jelmer Vernooij
Move commit message saving to separate file.
65
        for k,v in file_messages.iteritems():
769 by Jelmer Vernooij
Cope with some strings being unicode when returned by some versions of gtk.
66
            assert type(k) is str
742 by Jelmer Vernooij
Move commit message saving to separate file.
67
            try:
68
                self.file_messages[k] = v + '\n******\n' + self.file_messages[k]
69
            except KeyError:
70
                self.file_messages[k] = v
71
        if self.global_message:
72
            self.global_message = global_message + '\n******\n' \
73
                + self.global_message
74
        else:
75
            self.global_message = global_message
76
77
    def save(self, tree, branch):
78
        # We store in branch's config, which can be a problem if two gcommit
79
        # are done in two checkouts of one single branch (comments overwrite
80
        # each other). Ideally should be in working tree. But uncommit does
81
        # not always have a working tree, though it always has a branch.
82
        # 'tree' argument is for the future
83
        config = branch.get_config()
84
        # should it be named "gtk_" or some more neutral name ("gui_" ?) to
85
        # be compatible with qbzr in the future?
86
        config.set_user_option('gtk_global_commit_message', self.global_message)
87
        # bencode() does not know unicode objects but set_user_option()
88
        # requires one:
89
        config.set_user_option(
90
            'gtk_file_commit_messages',
91
            bencode.bencode(self.file_messages).decode('UTF-8'))
92
93
94
def save_commit_messages(local, master, old_revno, old_revid,
95
                         new_revno, new_revid):
96
    b = local
97
    if b is None:
98
        b = master
99
    mgr = SavedCommitMessagesManager(None, b)
743 by Jelmer Vernooij
Avoid Repository.iter_reverse_revision_history.
100
    graph = b.repository.get_graph()
101
    revid_iterator = graph.iter_lefthand_ancestry(old_revid)
742 by Jelmer Vernooij
Move commit message saving to separate file.
102
    cur_revno = old_revno
103
    new_revision_id = old_revid
104
    graph = b.repository.get_graph()
105
    for rev_id in revid_iterator:
106
        if cur_revno == new_revno:
107
            break
108
        cur_revno -= 1
109
        rev = b.repository.get_revision(rev_id)
110
        file_info = rev.properties.get('file-info', None)
111
        if file_info is None:
112
            file_info = {}
113
        else:
114
            file_info = bencode.bdecode(file_info.encode('UTF-8'))
115
        global_message = osutils.safe_unicode(rev.message)
116
        # Concatenate comment of the uncommitted revision
117
        mgr.insert(global_message, file_info)
118
119
        parents = graph.get_parent_map([rev_id]).get(rev_id, None)
120
        if not parents:
121
            continue
122
    mgr.save(None, b)