1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
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.
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.
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
22
class SavedCommitMessagesManager(object):
23
"""Save global and per-file commit messages.
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.
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."""
33
self.global_message = u''
34
self.file_messages = {}
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'))
46
self.file_messages = {}
49
return self.global_message, self.file_messages
51
def is_not_empty(self):
52
return bool(self.global_message or self.file_messages)
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."""
60
file_message = fi['message']
62
file_id = fi['file_id']
63
assert type(file_id) is str
64
file_messages[file_id] = file_message # utf-8 strings
65
for k,v in file_messages.iteritems():
68
self.file_messages[k] = v + '\n******\n' + self.file_messages[k]
70
self.file_messages[k] = v
71
if self.global_message:
72
self.global_message = global_message + '\n******\n' \
75
self.global_message = global_message
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()
89
config.set_user_option(
90
'gtk_file_commit_messages',
91
bencode.bencode(self.file_messages).decode('UTF-8'))
94
def save_commit_messages(local, master, old_revno, old_revid,
95
new_revno, new_revid):
99
mgr = SavedCommitMessagesManager(None, b)
100
graph = b.repository.get_graph()
101
revid_iterator = graph.iter_lefthand_ancestry(old_revid)
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:
109
rev = b.repository.get_revision(rev_id)
110
file_info = rev.properties.get('file-info', None)
111
if file_info is None:
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)
119
parents = graph.get_parent_map([rev_id]).get(rev_id, None)