|
1244
by Alexander Belchenko
added simple post_commit hook to store commit messages. |
1 |
# -*- coding: utf-8 -*-
|
2 |
#
|
|
3 |
# QBzr - Qt frontend to Bazaar commands
|
|
4 |
# Copyright (C) 2010 Alexander Belchenko
|
|
5 |
#
|
|
6 |
# This program is free software; you can redistribute it and/or
|
|
7 |
# modify it under the terms of the GNU General Public License
|
|
8 |
# as published by the Free Software Foundation; either version 2
|
|
9 |
# of the License, or (at your option) any later version.
|
|
10 |
#
|
|
11 |
# This program is distributed in the hope that it will be useful,
|
|
12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 |
# GNU General Public License for more details.
|
|
15 |
#
|
|
16 |
# You should have received a copy of the GNU General Public License
|
|
17 |
# along with this program; if not, write to the Free Software
|
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
19 |
||
20 |
"""Commit History manager: store/retrieve last N messages
|
|
21 |
of successfull commits to be able reuse them in GUI commit dialog(s).
|
|
|
1245
by Alexander Belchenko
added base factory for CommitHistory. Automatically creating new commit-history file if it does not exists. |
22 |
|
23 |
The history saved in the ~/.bazaar/commit-history file.
|
|
24 |
Format of this file is simple config-based (ConfigObj ftw):
|
|
25 |
||
26 |
limit = N
|
|
27 |
order = a b c d e f
|
|
28 |
mXX = ...
|
|
29 |
mYY = ...
|
|
30 |
||
31 |
Parameters used here as following:
|
|
32 |
||
33 |
* limit - determine how many entries will be stored.
|
|
34 |
0 means don't store anything. When new message overflow this limit
|
|
35 |
it will overwrite the oldest one.
|
|
36 |
||
37 |
* order - determine the order of messages from newest to oldest,
|
|
38 |
as list of message variables `mXX`.
|
|
39 |
||
40 |
* mXX - message variable which holds the actual saved message.
|
|
41 |
"""
|
|
42 |
||
43 |
_FILENAME = 'commit-history' |
|
44 |
||
45 |
_SKELETON = """\ |
|
46 |
limit = 10
|
|
47 |
order = ""
|
|
48 |
"""
|
|
49 |
||
50 |
class NullCommitHistory(object): |
|
51 |
||
52 |
def __init__(self, cfg=None): |
|
53 |
pass
|
|
54 |
||
55 |
def add(self, revision): |
|
56 |
pass
|
|
57 |
||
|
1248
by Alexander Belchenko
commit_history.py: create valid summary list and dict with full messages. |
58 |
def get_summary_list(self, depth=None): |
59 |
return [] |
|
60 |
||
61 |
def get_message(self, index=None): |
|
62 |
return '' |
|
63 |
||
|
1245
by Alexander Belchenko
added base factory for CommitHistory. Automatically creating new commit-history file if it does not exists. |
64 |
|
|
1246
by Alexander Belchenko
working CommitHistory.add method (with overflow rotating) |
65 |
class CommitHistory(object): |
|
1245
by Alexander Belchenko
added base factory for CommitHistory. Automatically creating new commit-history file if it does not exists. |
66 |
|
67 |
def __init__(self, cfg): |
|
68 |
self._cfg = cfg |
|
|
1248
by Alexander Belchenko
commit_history.py: create valid summary list and dict with full messages. |
69 |
self.items = {} |
|
1245
by Alexander Belchenko
added base factory for CommitHistory. Automatically creating new commit-history file if it does not exists. |
70 |
|
71 |
def add(self, revision): |
|
|
1246
by Alexander Belchenko
working CommitHistory.add method (with overflow rotating) |
72 |
# get slot name
|
73 |
order_list = self.get_order_as_list() |
|
74 |
limit = _get_limit(self._cfg) |
|
75 |
if len(order_list)+1 > limit: |
|
76 |
slot_name = order_list[-1] |
|
77 |
else: |
|
78 |
n = len(order_list) + 1 |
|
79 |
if limit < 100: |
|
80 |
template = 'm%02d' |
|
81 |
else: |
|
82 |
template = 'm%06d' |
|
83 |
while True: |
|
84 |
slot_name = template % n |
|
|
1249
by Alexander Belchenko
commit_history.py: using python dict api of ConfigObj when possible |
85 |
if slot_name not in self._cfg.keys(): |
|
1246
by Alexander Belchenko
working CommitHistory.add method (with overflow rotating) |
86 |
break
|
87 |
n += 1 |
|
88 |
# insert message
|
|
89 |
self._cfg[slot_name] = revision.message |
|
90 |
if slot_name in order_list: |
|
91 |
order_list.remove(slot_name) |
|
92 |
order_list.insert(0, slot_name) |
|
93 |
self._cfg['order'] = ' '.join(order_list) |
|
94 |
# write new file
|
|
95 |
self._cfg.write() |
|
96 |
||
97 |
def get_order_as_list(self): |
|
98 |
order_str = self._cfg.get('order', '') |
|
99 |
order_list = order_str.split() |
|
100 |
return order_list |
|
101 |
||
|
1247
by Alexander Belchenko
qci: added combobox above message edit area to paste some old message. |
102 |
def get_summary_list(self, depth=10): |
|
1248
by Alexander Belchenko
commit_history.py: create valid summary list and dict with full messages. |
103 |
order_list = self.get_order_as_list() |
104 |
summary_list = [] |
|
105 |
ix = 0 |
|
106 |
for o in order_list[:depth]: |
|
107 |
message = self._cfg.get(o, '') |
|
108 |
if message: |
|
109 |
self.items[ix] = message |
|
110 |
ix += 1 |
|
111 |
summary = message.replace('\n',' ') |
|
112 |
if len(summary) > 80: |
|
113 |
summary = summary[:77]+'...' |
|
114 |
summary_list.append(summary) |
|
115 |
return summary_list |
|
|
1247
by Alexander Belchenko
qci: added combobox above message edit area to paste some old message. |
116 |
|
117 |
def get_message(self, index): |
|
|
1248
by Alexander Belchenko
commit_history.py: create valid summary list and dict with full messages. |
118 |
return self.items.get(index, '') |
|
1247
by Alexander Belchenko
qci: added combobox above message edit area to paste some old message. |
119 |
|
|
1246
by Alexander Belchenko
working CommitHistory.add method (with overflow rotating) |
120 |
|
121 |
def _get_limit(cfg): |
|
122 |
try: |
|
123 |
limit = int(cfg['limit']) |
|
124 |
except (ValueError, KeyError): |
|
125 |
limit = 0 |
|
126 |
return limit |
|
127 |
||
128 |
def open_commit_history(): |
|
|
1245
by Alexander Belchenko
added base factory for CommitHistory. Automatically creating new commit-history file if it does not exists. |
129 |
"""Factory."""
|
130 |
# open config and check the limit
|
|
131 |
from bzrlib import config, osutils, trace |
|
132 |
path = osutils.pathjoin(config.config_dir(), _FILENAME) |
|
133 |
if not osutils.isfile(path): |
|
134 |
config.ensure_config_dir_exists() |
|
135 |
try: |
|
136 |
f = open(path, 'wb') |
|
137 |
try: |
|
138 |
f.write(_SKELETON) |
|
139 |
finally: |
|
140 |
f.close() |
|
141 |
except (IOError, OSError), e: |
|
142 |
trace.mutter("can't create commit-history file: %r" % e) |
|
143 |
return NullCommitHistory() |
|
144 |
cfg = config.ConfigObj(path, encoding='utf-8') |
|
|
1246
by Alexander Belchenko
working CommitHistory.add method (with overflow rotating) |
145 |
klass = CommitHistory |
146 |
if _get_limit(cfg) == 0: |
|
|
1245
by Alexander Belchenko
added base factory for CommitHistory. Automatically creating new commit-history file if it does not exists. |
147 |
klass = NullCommitHistory |
148 |
return klass(cfg) |