/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:
62
                file_messages[fi['file_id']] = file_message # utf-8 strings
63
        for k,v in file_messages.iteritems():
64
            try:
65
                self.file_messages[k] = v + '\n******\n' + self.file_messages[k]
66
            except KeyError:
67
                self.file_messages[k] = v
68
        if self.global_message:
69
            self.global_message = global_message + '\n******\n' \
70
                + self.global_message
71
        else:
72
            self.global_message = global_message
73
74
    def save(self, tree, branch):
75
        # We store in branch's config, which can be a problem if two gcommit
76
        # are done in two checkouts of one single branch (comments overwrite
77
        # each other). Ideally should be in working tree. But uncommit does
78
        # not always have a working tree, though it always has a branch.
79
        # 'tree' argument is for the future
80
        config = branch.get_config()
81
        # should it be named "gtk_" or some more neutral name ("gui_" ?) to
82
        # be compatible with qbzr in the future?
83
        config.set_user_option('gtk_global_commit_message', self.global_message)
84
        # bencode() does not know unicode objects but set_user_option()
85
        # requires one:
86
        config.set_user_option(
87
            'gtk_file_commit_messages',
88
            bencode.bencode(self.file_messages).decode('UTF-8'))
89
90
91
def save_commit_messages(local, master, old_revno, old_revid,
92
                         new_revno, new_revid):
93
    b = local
94
    if b is None:
95
        b = master
96
    mgr = SavedCommitMessagesManager(None, b)
743 by Jelmer Vernooij
Avoid Repository.iter_reverse_revision_history.
97
    graph = b.repository.get_graph()
98
    revid_iterator = graph.iter_lefthand_ancestry(old_revid)
742 by Jelmer Vernooij
Move commit message saving to separate file.
99
    cur_revno = old_revno
100
    new_revision_id = old_revid
101
    graph = b.repository.get_graph()
102
    for rev_id in revid_iterator:
103
        if cur_revno == new_revno:
104
            break
105
        cur_revno -= 1
106
        rev = b.repository.get_revision(rev_id)
107
        file_info = rev.properties.get('file-info', None)
108
        if file_info is None:
109
            file_info = {}
110
        else:
111
            file_info = bencode.bdecode(file_info.encode('UTF-8'))
112
        global_message = osutils.safe_unicode(rev.message)
113
        # Concatenate comment of the uncommitted revision
114
        mgr.insert(global_message, file_info)
115
116
        parents = graph.get_parent_map([rev_id]).get(rev_id, None)
117
        if not parents:
118
            continue
119
    mgr.save(None, b)