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
18
from bzrlib import bencode
20
from bzrlib.util import bencode
22
from bzrlib import osutils
24
class SavedCommitMessagesManager(object):
25
"""Save global and per-file commit messages.
27
Saves global commit message and utf-8 file_id->message dictionary
28
of per-file commit messages on disk. Re-reads them later for re-using.
31
def __init__(self, tree=None, branch=None):
32
"""If branch is None, builds empty messages, otherwise reads them
33
from branch's disk storage. 'tree' argument is for the future."""
35
self.global_message = u''
36
self.file_messages = {}
38
config = branch.get_config()
39
self.global_message = config.get_user_option(
40
'gtk_global_commit_message')
41
if self.global_message is None:
42
self.global_message = u''
43
file_messages = config.get_user_option('gtk_file_commit_messages')
44
if file_messages: # unicode and B-encoded:
45
self.file_messages = bencode.bdecode(
46
file_messages.encode('UTF-8'))
48
self.file_messages = {}
51
return self.global_message, self.file_messages
53
def is_not_empty(self):
54
return bool(self.global_message or self.file_messages)
56
def insert(self, global_message, file_info):
57
"""Formats per-file commit messages (list of dictionaries, one per file)
58
into one utf-8 file_id->message dictionary and merges this with
59
previously existing dictionary. Merges global commit message too."""
62
file_message = fi['message']
64
file_messages[fi['file_id']] = file_message # utf-8 strings
65
for k,v in file_messages.iteritems():
67
self.file_messages[k] = v + '\n******\n' + self.file_messages[k]
69
self.file_messages[k] = v
70
if self.global_message:
71
self.global_message = global_message + '\n******\n' \
74
self.global_message = global_message
76
def save(self, tree, branch):
77
# We store in branch's config, which can be a problem if two gcommit
78
# are done in two checkouts of one single branch (comments overwrite
79
# each other). Ideally should be in working tree. But uncommit does
80
# not always have a working tree, though it always has a branch.
81
# 'tree' argument is for the future
82
config = branch.get_config()
83
# should it be named "gtk_" or some more neutral name ("gui_" ?) to
84
# be compatible with qbzr in the future?
85
config.set_user_option('gtk_global_commit_message', self.global_message)
86
# bencode() does not know unicode objects but set_user_option()
88
config.set_user_option(
89
'gtk_file_commit_messages',
90
bencode.bencode(self.file_messages).decode('UTF-8'))
93
def save_commit_messages(local, master, old_revno, old_revid,
94
new_revno, new_revid):
98
mgr = SavedCommitMessagesManager(None, b)
99
graph = b.repository.get_graph()
100
revid_iterator = graph.iter_lefthand_ancestry(old_revid)
101
cur_revno = old_revno
102
new_revision_id = old_revid
103
graph = b.repository.get_graph()
104
for rev_id in revid_iterator:
105
if cur_revno == new_revno:
108
rev = b.repository.get_revision(rev_id)
109
file_info = rev.properties.get('file-info', None)
110
if file_info is None:
113
file_info = bencode.bdecode(file_info.encode('UTF-8'))
114
global_message = osutils.safe_unicode(rev.message)
115
# Concatenate comment of the uncommitted revision
116
mgr.insert(global_message, file_info)
118
parents = graph.get_parent_map([rev_id]).get(rev_id, None)