/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
795.1.1 by Vincent Ladeuil
Commit messages never contain option references, trying to expand them makes no sense.
1
# Copyright (C) 2011, 2013 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
742 by Jelmer Vernooij
Move commit message saving to separate file.
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(
795.1.1 by Vincent Ladeuil
Commit messages never contain option references, trying to expand them makes no sense.
38
                'gtk_global_commit_message', expand=False)
742 by Jelmer Vernooij
Move commit message saving to separate file.
39
            if self.global_message is None:
40
                self.global_message = u''
795.1.1 by Vincent Ladeuil
Commit messages never contain option references, trying to expand them makes no sense.
41
            file_messages = config.get_user_option(
42
                'gtk_file_commit_messages' , expand=False)
742 by Jelmer Vernooij
Move commit message saving to separate file.
43
            if file_messages: # unicode and B-encoded:
44
                self.file_messages = bencode.bdecode(
45
                    file_messages.encode('UTF-8'))
46
            else:
47
                self.file_messages = {}
48
49
    def get(self):
50
        return self.global_message, self.file_messages
51
52
    def is_not_empty(self):
53
        return bool(self.global_message or self.file_messages)
54
55
    def insert(self, global_message, file_info):
56
        """Formats per-file commit messages (list of dictionaries, one per file)
57
        into one utf-8 file_id->message dictionary and merges this with
58
        previously existing dictionary. Merges global commit message too."""
59
        file_messages = {}
60
        for fi in file_info:
61
            file_message = fi['message']
62
            if file_message:
769 by Jelmer Vernooij
Cope with some strings being unicode when returned by some versions of gtk.
63
                file_id = fi['file_id']
64
                assert type(file_id) is str
65
                file_messages[file_id] = file_message # utf-8 strings
742 by Jelmer Vernooij
Move commit message saving to separate file.
66
        for k,v in file_messages.iteritems():
769 by Jelmer Vernooij
Cope with some strings being unicode when returned by some versions of gtk.
67
            assert type(k) is str
742 by Jelmer Vernooij
Move commit message saving to separate file.
68
            try:
69
                self.file_messages[k] = v + '\n******\n' + self.file_messages[k]
70
            except KeyError:
71
                self.file_messages[k] = v
72
        if self.global_message:
73
            self.global_message = global_message + '\n******\n' \
74
                + self.global_message
75
        else:
76
            self.global_message = global_message
77
78
    def save(self, tree, branch):
79
        # We store in branch's config, which can be a problem if two gcommit
80
        # are done in two checkouts of one single branch (comments overwrite
81
        # each other). Ideally should be in working tree. But uncommit does
82
        # not always have a working tree, though it always has a branch.
83
        # 'tree' argument is for the future
84
        config = branch.get_config()
85
        # should it be named "gtk_" or some more neutral name ("gui_" ?) to
86
        # be compatible with qbzr in the future?
87
        config.set_user_option('gtk_global_commit_message', self.global_message)
88
        # bencode() does not know unicode objects but set_user_option()
89
        # requires one:
90
        config.set_user_option(
91
            'gtk_file_commit_messages',
92
            bencode.bencode(self.file_messages).decode('UTF-8'))
93
94
95
def save_commit_messages(local, master, old_revno, old_revid,
96
                         new_revno, new_revid):
97
    b = local
98
    if b is None:
99
        b = master
100
    mgr = SavedCommitMessagesManager(None, b)
743 by Jelmer Vernooij
Avoid Repository.iter_reverse_revision_history.
101
    graph = b.repository.get_graph()
102
    revid_iterator = graph.iter_lefthand_ancestry(old_revid)
742 by Jelmer Vernooij
Move commit message saving to separate file.
103
    cur_revno = old_revno
104
    new_revision_id = old_revid
105
    graph = b.repository.get_graph()
106
    for rev_id in revid_iterator:
107
        if cur_revno == new_revno:
108
            break
109
        cur_revno -= 1
110
        rev = b.repository.get_revision(rev_id)
111
        file_info = rev.properties.get('file-info', None)
112
        if file_info is None:
113
            file_info = {}
114
        else:
115
            file_info = bencode.bdecode(file_info.encode('UTF-8'))
116
        global_message = osutils.safe_unicode(rev.message)
117
        # Concatenate comment of the uncommitted revision
118
        mgr.insert(global_message, file_info)
119
120
        parents = graph.get_parent_map([rev_id]).get(rev_id, None)
121
        if not parents:
122
            continue
123
    mgr.save(None, b)