X-Git-Url: https://git.distorted.org.uk/~mdw/stgit/blobdiff_plain/6ef533bcc52fbf933e9f428d8ea2bf690f0ad76a..99c529152847b7e20e48dc477925e315918eed5d:/stgit/git.py diff --git a/stgit/git.py b/stgit/git.py index 5f3f030..f5e2f32 100644 --- a/stgit/git.py +++ b/stgit/git.py @@ -698,33 +698,40 @@ def pull(repository = 'origin', refspec = None): if __run(config.get('stgit', 'pullcmd'), args) != 0: raise GitException, 'Failed "git-pull %s"' % repository -def apply_patch(filename = None, diff = None, base = None): +def apply_patch(filename = None, diff = None, base = None, + fail_dump = True): """Apply a patch onto the current or given index. There must not be any local changes in the tree, otherwise the command fails """ - def __apply_patch(): - try: - if filename: - return __run('git-apply --index', [filename]) == 0 - elif diff: - _input_str('git-apply --index', diff) - else: - _input('git-apply --index', sys.stdin) - except GitException: - return False - return True - if base: orig_head = get_head() switch(base) else: - refresh_index() # needed since __apply_patch() doesn't do it + refresh_index() + + if diff is None: + if filename: + f = file(filename) + else: + f = sys.stdin + diff = f.read() + if filename: + f.close() - if not __apply_patch(): + try: + _input_str('git-apply --index', diff) + except GitException: if base: switch(orig_head) - raise GitException, 'Patch does not apply cleanly' - elif base: + if fail_dump: + # write the failed diff to a file + f = file('.stgit-failed.patch', 'w+') + f.write(diff) + f.close() + + raise + + if base: top = commit(message = 'temporary commit used for applying a patch', parents = [base]) switch(orig_head)