From: Clark Williams Date: Tue, 24 Jun 2008 04:09:21 +0000 (+0200) Subject: Patch to allow import from compressed files (gzip and bzip2) X-Git-Tag: v0.15-rc1~205 X-Git-Url: https://git.distorted.org.uk/~mdw/stgit/commitdiff_plain/354d203152c0fda8e0221aff9fdfa9b18d53e83d?hp=-c Patch to allow import from compressed files (gzip and bzip2) Signed-off-by: Clark Williams Signed-off-by: Karl Hasselström --- 354d203152c0fda8e0221aff9fdfa9b18d53e83d diff --git a/stgit/commands/imprt.py b/stgit/commands/imprt.py index 4a4b792..227743f 100644 --- a/stgit/commands/imprt.py +++ b/stgit/commands/imprt.py @@ -175,14 +175,42 @@ def __create_patch(filename, message, author_name, author_email, 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 if filename: - f = file(filename) + (f, pname) = __get_handle_and_name(filename) else: f = sys.stdin + if patch: + pname = patch + elif not pname: + pname = filename + if options.mail: try: msg = email.message_from_file(f) @@ -197,11 +225,6 @@ def __import_file(filename, options, patch = None): if filename: f.close() - if patch: - pname = patch - else: - pname = filename - __create_patch(pname, message, author_name, author_email, author_date, diff, options) diff --git a/t/t1800-import.sh b/t/t1800-import.sh index 8c8c9a0..1352743 100755 --- a/t/t1800-import.sh +++ b/t/t1800-import.sh @@ -80,4 +80,46 @@ test_expect_success \ stg delete .. ' +test_expect_success \ + 'Apply a bzip2 patch created with "git diff"' \ + ' + bzip2 -c ../t1800-import/git-diff >../t1800-import/bzip2-git-diff && + stg import ../t1800-import/bzip2-git-diff && + [ $(git cat-file -p $(stg id) \ + | grep -c "tree e96b1fba2160890ff600b675d7140d46b022b155") = 1 ] && + rm ../t1800-import/bzip2-git-diff && + stg delete .. + ' +test_expect_success \ + 'Apply a bzip2 patch with a .bz2 suffix' \ + ' + bzip2 -c ../t1800-import/git-diff >../t1800-import/git-diff.bz2 && + stg import ../t1800-import/git-diff.bz2 && + [ $(git cat-file -p $(stg id) \ + | grep -c "tree e96b1fba2160890ff600b675d7140d46b022b155") = 1 ] && + rm ../t1800-import/git-diff.bz2 && + stg delete .. + ' + +test_expect_success \ + 'Apply a gzip patch created with GNU diff' \ + ' + gzip -c ../t1800-import/gnu-diff >../t1800-import/gzip-gnu-diff && + stg import ../t1800-import/gzip-gnu-diff && + [ $(git cat-file -p $(stg id) \ + | grep -c "tree e96b1fba2160890ff600b675d7140d46b022b155") = 1 ] && + rm ../t1800-import/gzip-gnu-diff && + stg delete .. + ' +test_expect_success \ + 'Apply a gzip patch with a .gz suffix' \ + ' + gzip -c ../t1800-import/gnu-diff >../t1800-import/gnu-diff.gz && + stg import ../t1800-import/gnu-diff.gz && + [ $(git cat-file -p $(stg id) \ + | grep -c "tree e96b1fba2160890ff600b675d7140d46b022b155") = 1 ] && + rm ../t1800-import/gnu-diff.gz && + stg delete .. + ' + test_done