1
# Copyright (C) 2011, 2013 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', expand=False)
39
if self.global_message is None:
40
self.global_message = u''
41
file_messages = config.get_user_option(
42
'gtk_file_commit_messages' , expand=False)
43
if file_messages: # unicode and B-encoded:
44
self.file_messages = bencode.bdecode(
45
file_messages.encode('UTF-8'))
47
self.file_messages = {}
50
return self.global_message, self.file_messages
52
def is_not_empty(self):
53
return bool(self.global_message or self.file_messages)
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."""
61
file_message = fi['message']
63
file_id = fi['file_id']
64
assert type(file_id) is str
65
file_messages[file_id] = file_message # utf-8 strings
66
for k,v in file_messages.iteritems():
69
self.file_messages[k] = v + '\n******\n' + self.file_messages[k]
71
self.file_messages[k] = v
72
if self.global_message:
73
self.global_message = global_message + '\n******\n' \
76
self.global_message = global_message
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()
90
config.set_user_option(
91
'gtk_file_commit_messages',
92
bencode.bencode(self.file_messages).decode('UTF-8'))
95
def save_commit_messages(local, master, old_revno, old_revid,
96
new_revno, new_revid):
100
mgr = SavedCommitMessagesManager(None, b)
101
graph = b.repository.get_graph()
102
revid_iterator = graph.iter_lefthand_ancestry(old_revid)
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:
110
rev = b.repository.get_revision(rev_id)
111
file_info = rev.properties.get('file-info', None)
112
if file_info is None:
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)
120
parents = graph.get_parent_map([rev_id]).get(rev_id, None)