+ if options.name:
+ patch = options.name
+ elif filename:
+ patch = os.path.basename(filename)
+ else:
+ patch = ''
+ if options.strip:
+ patch = __strip_patch_name(patch)
+
+ if not patch:
+ if options.ignore or options.replace:
+ unacceptable_name = lambda name: False
+ else:
+ unacceptable_name = crt_series.patch_exists
+ patch = make_patch_name(message, unacceptable_name)
+ else:
+ # fix possible invalid characters in the patch name
+ patch = re.sub('[^\w.]+', '-', patch).strip('-')
+
+ if options.ignore and patch in crt_series.get_applied():
+ out.info('Ignoring already applied patch "%s"' % patch)
+ return
+ if options.replace and patch in crt_series.get_unapplied():
+ crt_series.delete_patch(patch, keep_log = True)
+
+ # refresh_patch() will invoke the editor in this case, with correct
+ # patch content
+ if not message:
+ can_edit = False
+
+ committer_name = committer_email = None
+
+ if options.author:
+ options.authname, options.authemail = name_email(options.author)
+
+ # override the automatically parsed settings
+ if options.authname:
+ author_name = options.authname
+ if options.authemail:
+ author_email = options.authemail
+ if options.authdate:
+ author_date = options.authdate
+ if options.commname:
+ committer_name = options.commname
+ if options.commemail:
+ committer_email = options.commemail
+
+ crt_series.new_patch(patch, message = message, can_edit = False,
+ author_name = author_name,
+ author_email = author_email,
+ author_date = author_date,
+ committer_name = committer_name,
+ committer_email = committer_email)
+
+ if not diff:
+ out.warn('No diff found, creating empty patch')
+ else:
+ out.start('Importing patch "%s"' % patch)
+ if options.base:
+ git.apply_patch(diff = diff,
+ base = git_id(crt_series, options.base))
+ else:
+ git.apply_patch(diff = diff)
+ crt_series.refresh_patch(edit = options.edit,
+ show_patch = options.showpatch,
+ sign_str = options.sign_str,
+ backup = False)
+ out.done()
+
+def __mkpatchname(name, suffix):
+ if name.lower().endswith(suffix.lower()):
+ return name[:-len(suffix)]
+ return name
+
+def __get_handle_and_name(filename):
+ """Return a file object and a patch name derived from filename
+ """
+ # see if it's a gzip'ed or bzip2'ed patch
+ import bz2, gzip
+ for copen, ext in [(gzip.open, '.gz'), (bz2.BZ2File, '.bz2')]:
+ try:
+ f = copen(filename)
+ f.read(1)
+ f.seek(0)
+ return (f, __mkpatchname(filename, ext))
+ except IOError, e:
+ pass
+
+ # plain old file...
+ return (open(filename), filename)
+
+def __import_file(filename, options, patch = None):
+ """Import a patch from a file or standard input
+ """
+ pname = None