1
0
mirror of https://github.com/openSUSE/osc.git synced 2024-11-10 06:46:15 +01:00

commit message template

New functions:
osc.core.get_commit_message_template()
    - check the differences in .changes
    - extract them and add to the commit message
osc.core.dgst_from_string():
    - same as dgst, but work with string argument


Modified functions:
osc.core.edit_message():
    - new optional argument template
osc.commandline.do_commit():
    - use get_commit_message_template to collect the changes and call the
      edit_message with template argument
osc.core.edit_message():
    - used dgst_from_string for header_orig value, so commit message could be
      used as is with template. Used a mtime check for canceling support.
      TODO: maybe is dgst_from_string unecessary
This commit is contained in:
Michal Vyskocil 2009-04-16 13:48:37 +00:00
parent ca911fd99d
commit 41eb9614f0
2 changed files with 56 additions and 6 deletions

View File

@ -1390,15 +1390,17 @@ Please submit there instead, or use --nodevelproject to force direct submission.
# open editor for commit message
# but first, produce status and diff to append to the template
footer = diffs = []
template = []
for pac in pacs:
changed = getStatus([pac], quiet=True)
if changed:
footer += changed
diffs += ['\nDiff for working copy: %s' % pac.dir]
diffs += make_diff(pac, 0)
template.extend(get_commit_message_template(pac))
# if footer is empty, there is nothing to commit, and no edit needed.
if footer:
msg = edit_message(footer='\n'.join(footer))
msg = edit_message(footer='\n'.join(footer), template='\n'.join(template))
if conf.config['do_package_tracking'] and len(pacs) > 0:
prj_paths = {}

View File

@ -23,6 +23,7 @@ import shutil
import oscerr
import conf
import subprocess
import re
try:
from xml.etree import cElementTree as ET
except ImportError:
@ -1907,22 +1908,25 @@ def read_meta_from_spec(specfile, *args):
return spec_data
def edit_message(footer=''):
def edit_message(footer='', template=''):
delim = '--This line, and those below, will be ignored--\n\n' + footer
hash_orig = dgst_from_string('\n' + delim)
if template != '':
delim = template + '\n' + delim
import tempfile
(fd, filename) = tempfile.mkstemp(prefix = 'osc-commitmsg', suffix = '.diff', dir = '/tmp')
f = os.fdopen(fd, 'w')
f.write('\n')
f.write(delim)
f.close()
hash_orig = dgst(filename)
mtime_orig = os.stat(filename).st_mtime
editor = os.getenv('EDITOR', default='vim')
while 1:
subprocess.call('%s %s' % (editor, filename), shell=True)
hash = dgst(filename)
if hash != hash_orig:
mtime = os.stat(filename).st_mtime
if mtime_orig < mtime and hash != hash_orig:
msg = open(filename).read()
os.unlink(filename)
return msg.split(delim)[0].rstrip()
@ -2125,6 +2129,16 @@ def get_binary_file(apiurl, prj, repo, arch,
try: os.unlink(tmpfilename)
except: pass
def dgst_from_string(str):
try:
import hashlib
md5 = hashlib
except ImportError:
import md5
md5 = md5
s = md5.md5()
s.update(str)
return s.hexdigest()
def dgst(file):
@ -3599,3 +3613,37 @@ def getStatus(pacs, prj_obj=None, verbose=False, quiet=False):
lines = [line for line in lines if line[0] == '?'] \
+ [line for line in lines if line[0] != '?']
return lines
def get_commit_message_template(pac):
"""
Read the difference in .changes file(s) and put them as a template to commit message.
"""
diff = ""
template = []
date_re = re.compile(r'\+(Mon|Tue|Wed|Thu|Fri|Sat|Sun) ([A-Z][a-z]{2}) ( ?[0-9]|[0-3][0-9]) .*')
if pac.todo:
files = filter(lambda file: '.changes' in file and pac.status(file) in ('A', 'M'), pac.todo)
if not files:
return template
for file in files:
diff += get_source_file_diff(pac.absdir, file, pac.rev)
if diff:
index = 0
diff = diff.split('\n')
# The first four lines contains a header of diff
for line in diff[4:]:
# this condition is magical, but it removes all unwanted lines from commit message
if not(line) or (line and line[0] != '+') or \
date_re.match(line) or \
line == '+' or line[0:3] == '+++':
continue
if line == '+-------------------------------------------------------------------':
template.append('')
else:
template.append(line[1:])
return template