# Utility functions
-def git_id(string):
+def git_id(rev):
"""Return the GIT id
"""
- if not string:
+ if not rev:
return None
- string_list = string.split('/')
- if len(string_list) == 2:
- patch_id = string_list[1]
+ rev_list = rev.split('/')
+ if len(rev_list) == 2:
+ patch_id = rev_list[1]
if not patch_id:
patch_id = 'top'
- elif len(string_list) == 1:
+ elif len(rev_list) == 1:
patch_id = 'top'
else:
patch_id = None
- patch_branch = string_list[0].split('@')
+ patch_branch = rev_list[0].split('@')
if len(patch_branch) == 1:
series = crt_series
elif len(patch_branch) == 2:
series = stack.Series(patch_branch[1])
else:
- raise CmdException, 'Unknown id: %s' % string
+ raise CmdException, 'Unknown id: %s' % rev
patch_name = patch_branch[0]
if not patch_name:
return series.get_patch(patch_name).get_old_bottom()
# base
- if patch_name == 'base' and len(string_list) == 1:
+ if patch_name == 'base' and len(rev_list) == 1:
return read_string(series.get_base_file())
# anything else failed
- return git.rev_parse(string)
+ return git.rev_parse(rev)
def check_local_changes():
if git.local_changes():
resolved(filename, reset)
os.remove(os.path.join(git.base_dir, 'conflicts'))
-def name_email(string):
+def name_email(address):
"""Return a tuple consisting of the name and email parsed from a
standard 'name <email>' string
"""
- string = re.sub('([^\w\s<>@.])', '\\\\\\1', string)
- str_list = re.findall('^(.*)\s*<(.*)>\s*$', string)
+ address = re.sub('([^\w\s<>@.])', '\\\\\\1', address)
+ str_list = re.findall('^(.*)\s*<(.*)>\s*$', address)
if not str_list:
- raise CmdException, 'Incorrect "name <email>" string: %s' % string
+ raise CmdException, 'Incorrect "name <email>" string: %s' % address
return str_list[0]
-def name_email_date(string):
+def name_email_date(address):
"""Return a tuple consisting of the name, email and date parsed
from a 'name <email> date' string
"""
- string = re.sub('([^\w\s<>@.])', '\\\\\\1', string)
- str_list = re.findall('^(.*)\s*<(.*)>\s*(.*)\s*$', string)
+ address = re.sub('([^\w\s<>@.])', '\\\\\\1', address)
+ str_list = re.findall('^(.*)\s*<(.*)>\s*(.*)\s*$', address)
if not str_list:
- raise CmdException, 'Incorrect "name <email> date" string: %s' % string
+ raise CmdException, 'Incorrect "name <email> date" string: %s' % address
return str_list[0]
else:
return None
-def __parse_addresses(string):
+def __parse_addresses(addresses):
"""Return a two elements tuple: (from, [to])
"""
- def __addr_list(string):
- m = re.search('[^@\s<,]+@[^>\s,]+', string);
+ def __addr_list(addrs):
+ m = re.search('[^@\s<,]+@[^>\s,]+', addrs);
if (m == None):
return []
- return [ m.group() ] + __addr_list(string[m.end():])
+ return [ m.group() ] + __addr_list(addrs[m.end():])
from_addr_list = []
to_addr_list = []
- for line in string.split('\n'):
+ for line in addresses.split('\n'):
if re.match('from:\s+', line, re.I):
from_addr_list += __addr_list(line)
elif re.match('(to|cc|bcc):\s+', line, re.I):
def _output(cmd):
p=popen2.Popen3(cmd, True)
- string = p.fromchild.read()
+ output = p.fromchild.read()
if p.wait():
raise GitException, '%s failed' % str(cmd)
- return string
+ return output
def _output_one_line(cmd, file_desc = None):
p=popen2.Popen3(cmd, True)
for line in file_desc:
p.tochild.write(line)
p.tochild.close()
- string = p.fromchild.readline().strip()
+ output = p.fromchild.readline().strip()
if p.wait():
raise GitException, '%s failed' % str(cmd)
- return string
+ return output
def _output_lines(cmd):
p=popen2.Popen3(cmd, True)
p=popen2.Popen3('git-apply --stat')
diff(files, rev1, rev2, p.tochild)
p.tochild.close()
- str = p.fromchild.read().rstrip()
+ diff_str = p.fromchild.read().rstrip()
if p.wait():
raise GitException, 'git.diffstat failed'
- return str
+ return diff_str
def files(rev1, rev2):
"""Return the files modified between rev1 and rev2
"""
- str = ''
+ result = ''
for line in _output_lines('git-diff-tree -r %s %s' % (rev1, rev2)):
- str += '%s %s\n' % tuple(line.rstrip().split(' ',4)[-1].split('\t',1))
+ result += '%s %s\n' % tuple(line.rstrip().split(' ',4)[-1].split('\t',1))
- return str.rstrip()
+ return result.rstrip()
def barefiles(rev1, rev2):
"""Return the files modified between rev1 and rev2, without status info
"""
- str = ''
+ result = ''
for line in _output_lines('git-diff-tree -r %s %s' % (rev1, rev2)):
- str += '%s\n' % line.rstrip().split(' ',4)[-1].split('\t',1)[-1]
+ result += '%s\n' % line.rstrip().split(' ',4)[-1].split('\t',1)[-1]
- return str.rstrip()
+ return result.rstrip()
def checkout(files = None, tree_id = None, force = False):
"""Check out the given or all files
f.seek(0); f.truncate()
f.writelines(lines)
-def edit_file(series, string, comment, show_patch = True):
+def edit_file(series, line, comment, show_patch = True):
fname = '.stgit.msg'
tmpl = os.path.join(git.base_dir, 'patchdescr.tmpl')
f = file(fname, 'w+')
- if string:
- print >> f, string
+ if line:
+ print >> f, line
elif os.path.isfile(tmpl):
print >> f, file(tmpl).read().rstrip()
else:
__clean_comments(f)
f.seek(0)
- string = f.read()
+ result = f.read()
f.close()
os.remove(fname)
- return string
+ return result
#
# Classes
def __get_field(self, name, multiline = False):
id_file = os.path.join(self.__dir, name)
if os.path.isfile(id_file):
- string = read_string(id_file, multiline)
- if string == '':
+ line = read_string(id_file, multiline)
+ if line == '':
return None
else:
- return string
+ return line
else:
return None
- def __set_field(self, name, string, multiline = False):
+ def __set_field(self, name, value, multiline = False):
fname = os.path.join(self.__dir, name)
- if string and string != '':
- write_string(fname, string, multiline)
+ if value and value != '':
+ write_string(fname, value, multiline)
elif os.path.isfile(fname):
os.remove(fname)
def get_bottom(self):
return self.__get_field('bottom')
- def set_bottom(self, string, backup = False):
+ def set_bottom(self, value, backup = False):
if backup:
curr = self.__get_field('bottom')
- if curr != string:
+ if curr != value:
self.__set_field('bottom.old', curr)
else:
self.__set_field('bottom.old', None)
- self.__set_field('bottom', string)
+ self.__set_field('bottom', value)
def get_old_top(self):
return self.__get_field('top.old')
def get_top(self):
return self.__get_field('top')
- def set_top(self, string, backup = False):
+ def set_top(self, value, backup = False):
if backup:
curr = self.__get_field('top')
- if curr != string:
+ if curr != value:
self.__set_field('top.old', curr)
else:
self.__set_field('top.old', None)
- self.__set_field('top', string)
+ self.__set_field('top', value)
def restore_old_boundaries(self):
bottom = self.__get_field('bottom.old')
def get_description(self):
return self.__get_field('description', True)
- def set_description(self, string):
- self.__set_field('description', string, True)
+ def set_description(self, line):
+ self.__set_field('description', line, True)
def get_authname(self):
return self.__get_field('authname')
- def set_authname(self, string):
- if not string and config.has_option('stgit', 'authname'):
- string = config.get('stgit', 'authname')
- self.__set_field('authname', string)
+ def set_authname(self, name):
+ if not name and config.has_option('stgit', 'authname'):
+ name = config.get('stgit', 'authname')
+ self.__set_field('authname', name)
def get_authemail(self):
return self.__get_field('authemail')
- def set_authemail(self, string):
- if not string and config.has_option('stgit', 'authemail'):
- string = config.get('stgit', 'authemail')
- self.__set_field('authemail', string)
+ def set_authemail(self, address):
+ if not address and config.has_option('stgit', 'authemail'):
+ address = config.get('stgit', 'authemail')
+ self.__set_field('authemail', address)
def get_authdate(self):
return self.__get_field('authdate')
- def set_authdate(self, string):
- self.__set_field('authdate', string)
+ def set_authdate(self, authdate):
+ self.__set_field('authdate', authdate)
def get_commname(self):
return self.__get_field('commname')
- def set_commname(self, string):
- if not string and config.has_option('stgit', 'commname'):
- string = config.get('stgit', 'commname')
- self.__set_field('commname', string)
+ def set_commname(self, name):
+ if not name and config.has_option('stgit', 'commname'):
+ name = config.get('stgit', 'commname')
+ self.__set_field('commname', name)
def get_commemail(self):
return self.__get_field('commemail')
- def set_commemail(self, string):
- if not string and config.has_option('stgit', 'commemail'):
- string = config.get('stgit', 'commemail')
- self.__set_field('commemail', string)
+ def set_commemail(self, address):
+ if not address and config.has_option('stgit', 'commemail'):
+ address = config.get('stgit', 'commemail')
+ self.__set_field('commemail', address)
class Series:
"""
f = file(filename, 'r')
if multiline:
- string = f.read()
+ result = f.read()
else:
- string = f.readline().strip()
+ result = f.readline().strip()
f.close()
- return string
+ return result
-def write_string(filename, string, multiline = False):
- """Writes string to file and truncates it
+def write_string(filename, line, multiline = False):
+ """Writes 'line' to file and truncates it
"""
f = file(filename, 'w+')
if multiline:
- f.write(string)
+ f.write(line)
else:
- print >> f, string
+ print >> f, line
f.close()
-def append_strings(filename, strings):
- """Appends string sequence to file
+def append_strings(filename, lines):
+ """Appends 'lines' sequence to file
"""
f = file(filename, 'a+')
- for string in strings:
- print >> f, string
+ for line in lines:
+ print >> f, line
f.close()
-def append_string(filename, string):
- """Appends string to file
+def append_string(filename, line):
+ """Appends 'line' to file
"""
f = file(filename, 'a+')
- print >> f, string
+ print >> f, line
f.close()
-def insert_string(filename, string):
- """Inserts a string at the beginning of the file
+def insert_string(filename, line):
+ """Inserts 'line' at the beginning of the file
"""
f = file(filename, 'r+')
lines = f.readlines()
f.seek(0); f.truncate()
- print >> f, string
+ print >> f, line
f.writelines(lines)
f.close()