File: commitmsgs.py

package info (click to toggle)
bzr-gtk 0.103.0+bzr792-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,524 kB
  • sloc: python: 9,616; makefile: 16; sh: 10
file content (122 lines) | stat: -rw-r--r-- 5,013 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from bzrlib import (
    bencode,
    osutils,
    )

class SavedCommitMessagesManager(object):
    """Save global and per-file commit messages.

    Saves global commit message and utf-8 file_id->message dictionary
    of per-file commit messages on disk. Re-reads them later for re-using.
    """

    def __init__(self, tree=None, branch=None):
        """If branch is None, builds empty messages, otherwise reads them
        from branch's disk storage. 'tree' argument is for the future."""
        if branch is None:
            self.global_message = u''
            self.file_messages = {}
        else:
            config = branch.get_config()
            self.global_message = config.get_user_option(
                'gtk_global_commit_message')
            if self.global_message is None:
                self.global_message = u''
            file_messages = config.get_user_option('gtk_file_commit_messages')
            if file_messages: # unicode and B-encoded:
                self.file_messages = bencode.bdecode(
                    file_messages.encode('UTF-8'))
            else:
                self.file_messages = {}

    def get(self):
        return self.global_message, self.file_messages

    def is_not_empty(self):
        return bool(self.global_message or self.file_messages)

    def insert(self, global_message, file_info):
        """Formats per-file commit messages (list of dictionaries, one per file)
        into one utf-8 file_id->message dictionary and merges this with
        previously existing dictionary. Merges global commit message too."""
        file_messages = {}
        for fi in file_info:
            file_message = fi['message']
            if file_message:
                file_id = fi['file_id']
                assert type(file_id) is str
                file_messages[file_id] = file_message # utf-8 strings
        for k,v in file_messages.iteritems():
            assert type(k) is str
            try:
                self.file_messages[k] = v + '\n******\n' + self.file_messages[k]
            except KeyError:
                self.file_messages[k] = v
        if self.global_message:
            self.global_message = global_message + '\n******\n' \
                + self.global_message
        else:
            self.global_message = global_message

    def save(self, tree, branch):
        # We store in branch's config, which can be a problem if two gcommit
        # are done in two checkouts of one single branch (comments overwrite
        # each other). Ideally should be in working tree. But uncommit does
        # not always have a working tree, though it always has a branch.
        # 'tree' argument is for the future
        config = branch.get_config()
        # should it be named "gtk_" or some more neutral name ("gui_" ?) to
        # be compatible with qbzr in the future?
        config.set_user_option('gtk_global_commit_message', self.global_message)
        # bencode() does not know unicode objects but set_user_option()
        # requires one:
        config.set_user_option(
            'gtk_file_commit_messages',
            bencode.bencode(self.file_messages).decode('UTF-8'))


def save_commit_messages(local, master, old_revno, old_revid,
                         new_revno, new_revid):
    b = local
    if b is None:
        b = master
    mgr = SavedCommitMessagesManager(None, b)
    graph = b.repository.get_graph()
    revid_iterator = graph.iter_lefthand_ancestry(old_revid)
    cur_revno = old_revno
    new_revision_id = old_revid
    graph = b.repository.get_graph()
    for rev_id in revid_iterator:
        if cur_revno == new_revno:
            break
        cur_revno -= 1
        rev = b.repository.get_revision(rev_id)
        file_info = rev.properties.get('file-info', None)
        if file_info is None:
            file_info = {}
        else:
            file_info = bencode.bdecode(file_info.encode('UTF-8'))
        global_message = osutils.safe_unicode(rev.message)
        # Concatenate comment of the uncommitted revision
        mgr.insert(global_message, file_info)

        parents = graph.get_parent_map([rev_id]).get(rev_id, None)
        if not parents:
            continue
    mgr.save(None, b)