{"thread":{"id":"13918","subject":"[StGit PATCH] compressed import v3","startedAt":"2008-06-12T21:32:50Z","lastAt":"2008-06-14T15:49:03Z","messageCount":5,"participants":["Clark Williams","Karl Hasselström"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"79641","messageId":"48519602.2090103@gmail.com","threadId":"13918","inReplyTo":null,"subject":"[StGit PATCH] compressed import v3","fromName":"Clark Williams","fromEmail":"clark.williams@gmail.com","sentAt":"2008-06-12T21:32:50Z","receivedAt":"2008-06-12T21:32:50Z","isPatch":true,"sender":{"key":"clark.williams@gmail.com","avatar":null},"body":"-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA1\n\nKarl/Catalin, et al,\n\nAttached is my latest stab at StGit importing patches from compressed files. This\nversion doesn't try to differentiate by the file extension; it just tries to open\ngzip or bz2 files and if those fail it reverts to text. I'm not completely happy with\nit, but I've spent about as much time on it as I can afford to (for this week anyway).\n\nYeah, yeah, Karl, there are four new tests in t1800-import. Let me know if you think\nthere should be more (more for compressed input that is; I'm not crazy enough to sign\nup to write more tests for *everything*). :)\n\nClark\n-----BEGIN PGP SIGNATURE-----\nVersion: GnuPG v1.4.9 (GNU/Linux)\nComment: Using GnuPG with Fedora - http://enigmail.mozdev.org\n\niEYEARECAAYFAkhRlgIACgkQqA4JVb61b9d2ygCfTUPc5I9eXM4947VTrTZ+mO0H\n+vIAoJJACG94TdnyUIac73lB4UYCZVlG\n=sa3t\n-----END PGP SIGNATURE-----\n\n\nPatch to allow import from compressed files (gzip and bzip2)\n\nFrom: Clark Williams <williams@redhat.com>\n\nSigned-off-by: Clark Williams <williams@redhat.com>\n---\n\n stgit/commands/imprt.py |   42 ++++++++++++++++++++++++++++++++++++------\n t/t1800-import.sh       |   42 ++++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 78 insertions(+), 6 deletions(-)\n\ndiff --git a/stgit/commands/imprt.py b/stgit/commands/imprt.py\nindex 4a4b792..fc5cdce 100644\n--- a/stgit/commands/imprt.py\n+++ b/stgit/commands/imprt.py\n@@ -175,14 +175,49 @@ def __create_patch(filename, message, author_name, author_email,\n                                  backup = False)\n         out.done()\n \n+def __mkpatchname(name, suffix):\n+    if name.lower().endswith(suffix.lower()):\n+        return name[:-len(suffix)]\n+    return name\n+\n+def __gethandleandname(filename):\n+    \"\"\"return a file handle and a patch name derived from filename\n+    \"\"\"\n+    # see if it's a gzip'ed patch\n+    try:\n+        import gzip\n+        f = gzip.open(filename)\n+        f.read(1)\n+        f.seek(0)\n+        return (f, __mkpatchname(filename, '.gz'))\n+    except IOError, e:\n+            pass\n+    # see if it's a bzip2'ed patch\n+    try:\n+        import bz2\n+        f = bz2.BZ2File(filename)\n+        f.read(1)\n+        f.seek(0)\n+        return (f, __mkpatchname(filename, '.bz2'))\n+    except IOError, e:\n+            pass\n+    # plain old file...\n+    return (open(filename), filename)\n+\n def __import_file(filename, options, patch = None):\n     \"\"\"Import a patch from a file or standard input\n     \"\"\"\n+    pname = None\n     if filename:\n-        f = file(filename)\n+        (f, pname) = __gethandleandname(filename)\n     else:\n         f = sys.stdin\n \n+    if patch:\n+        pname = patch\n+    elif not pname:\n+        pname = filename\n+\n     if options.mail:\n         try:\n             msg = email.message_from_file(f)\n@@ -197,11 +232,6 @@ def __import_file(filename, options, patch = None):\n     if filename:\n         f.close()\n \n-    if patch:\n-        pname = patch\n-    else:\n-        pname = filename\n-\n     __create_patch(pname, message, author_name, author_email,\n                    author_date, diff, options)\n \ndiff --git a/t/t1800-import.sh b/t/t1800-import.sh\nindex 8c8c9a0..1352743 100755\n--- a/t/t1800-import.sh\n+++ b/t/t1800-import.sh\n@@ -80,4 +80,46 @@ test_expect_success \\\n     stg delete ..\n     '\n \n+test_expect_success \\\n+    'Apply a bzip2 patch created with \"git diff\"' \\\n+    '\n+    bzip2 -c ../t1800-import/git-diff >../t1800-import/bzip2-git-diff &&\n+    stg import ../t1800-import/bzip2-git-diff &&\n+    [ $(git cat-file -p $(stg id) \\\n+        | grep -c \"tree e96b1fba2160890ff600b675d7140d46b022b155\") = 1 ] &&\n+    rm ../t1800-import/bzip2-git-diff &&\n+    stg delete .. \n+    '\n+test_expect_success \\\n+    'Apply a bzip2 patch with a .bz2 suffix' \\\n+    '\n+    bzip2 -c ../t1800-import/git-diff >../t1800-import/git-diff.bz2 &&\n+    stg import ../t1800-import/git-diff.bz2 &&\n+    [ $(git cat-file -p $(stg id) \\\n+        | grep -c \"tree e96b1fba2160890ff600b675d7140d46b022b155\") = 1 ] &&\n+    rm ../t1800-import/git-diff.bz2 &&\n+    stg delete .. \n+    '\n+\n+test_expect_success \\\n+    'Apply a gzip patch created with GNU diff' \\\n+    '\n+    gzip -c ../t1800-import/gnu-diff >../t1800-import/gzip-gnu-diff &&\n+    stg import ../t1800-import/gzip-gnu-diff &&\n+    [ $(git cat-file -p $(stg id) \\\n+        | grep -c \"tree e96b1fba2160890ff600b675d7140d46b022b155\") = 1 ] &&\n+    rm ../t1800-import/gzip-gnu-diff &&\n+    stg delete ..\n+    '\n+test_expect_success \\\n+    'Apply a gzip patch with a .gz suffix' \\\n+    '\n+    gzip -c ../t1800-import/gnu-diff >../t1800-import/gnu-diff.gz &&\n+    stg import ../t1800-import/gnu-diff.gz &&\n+    [ $(git cat-file -p $(stg id) \\\n+        | grep -c \"tree e96b1fba2160890ff600b675d7140d46b022b155\") = 1 ] &&\n+    rm ../t1800-import/gnu-diff.gz &&\n+    stg delete ..\n+    '\n+\n test_done\n"},{"id":"79673","messageId":"20080613051127.GA24245@diana.vm.bytemark.co.uk","threadId":"13918","inReplyTo":"48519602.2090103@gmail.com","subject":"Re: [StGit PATCH] compressed import v3","fromName":"Karl Hasselström","fromEmail":"kha@treskal.com","sentAt":"2008-06-13T05:11:27Z","receivedAt":"2008-06-13T05:11:27Z","isPatch":true,"sender":{"key":"kha@treskal.com","avatar":"https://gravatar.com/avatar/f0120c734b5279b345075a28521e1ac66acb20c9913ffe9bf6ae97e53f7f3f13?d=mp&s=160"},"body":"On 2008-06-12 16:32:50 -0500, Clark Williams wrote:\n\n> Attached is my latest stab at StGit importing patches from\n> compressed files. This version doesn't try to differentiate by the\n> file extension; it just tries to open gzip or bz2 files and if those\n> fail it reverts to text.\n\nVery good!\n\n> I'm not completely happy with it, but I've spent about as much time\n> on it as I can afford to (for this week anyway).\n\nHeh. I see an opportunity for condensing the code a bit in\n__gethandleandname(), and you intented the \"pass\"es funny, but I'll\ntake care of it (or not, seeing as they're kind of minor nits -- in\nany case, I'll just take the patch off your hands and stop tormenting\nyou about it).\n\n> Yeah, yeah, Karl, there are four new tests in t1800-import. Let me\n> know if you think there should be more (more for compressed input\n> that is; I'm not crazy enough to sign up to write more tests for\n> *everything*). :)\n\nNo, this is exactly what I was aiming for.\n\nBleed 'em dry, but _no more_! ;-)\n\n-- \nKarl Hasselström, kha@treskal.com\n      www.treskal.com/kalle\n"},{"id":"79729","messageId":"485289FD.50808@gmail.com","threadId":"13918","inReplyTo":"20080613051127.GA24245@diana.vm.bytemark.co.uk","subject":"Re: [StGit PATCH] compressed import v3","fromName":"Clark Williams","fromEmail":"clark.williams@gmail.com","sentAt":"2008-06-13T14:53:49Z","receivedAt":"2008-06-13T14:53:49Z","isPatch":true,"sender":{"key":"clark.williams@gmail.com","avatar":null},"body":"-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA1\n\nKarl Hasselström wrote:\n> On 2008-06-12 16:32:50 -0500, Clark Williams wrote:\n> \n>> Attached is my latest stab at StGit importing patches from\n>> compressed files. This version doesn't try to differentiate by the\n>> file extension; it just tries to open gzip or bz2 files and if those\n>> fail it reverts to text.\n> \n> Very good!\n> \n>> I'm not completely happy with it, but I've spent about as much time\n>> on it as I can afford to (for this week anyway).\n> \n> Heh. I see an opportunity for condensing the code a bit in\n> __gethandleandname(), and you intented the \"pass\"es funny, but I'll\n> take care of it (or not, seeing as they're kind of minor nits -- in\n> any case, I'll just take the patch off your hands and stop tormenting\n> you about it).\n> \n\nUgh, I missed that on the pass'es. I had some other logic in there, attempting to\nmake sure that the IOError was in fact because the input was uncompressed, but I\npunted that and forgot to unindent the pass. I can't believe emacs didn't catch that\nfor me :)\n\nI'm interested to hear how you'd condense the code in __gethandleandname().\n\nClark\n\n-----BEGIN PGP SIGNATURE-----\nVersion: GnuPG v1.4.9 (GNU/Linux)\nComment: Using GnuPG with Fedora - http://enigmail.mozdev.org\n\niEYEARECAAYFAkhSif0ACgkQqA4JVb61b9dA1gCeMVvcY4GobZLp+k7qskLVgozN\nvsAAmQFoTxf2xxohyzCnCrRQRs1Gdf+O\n=lBEr\n-----END PGP SIGNATURE-----\n"},{"id":"79848","messageId":"20080614102646.GD14282@diana.vm.bytemark.co.uk","threadId":"13918","inReplyTo":"485289FD.50808@gmail.com","subject":"Re: [StGit PATCH] compressed import v3","fromName":"Karl Hasselström","fromEmail":"kha@treskal.com","sentAt":"2008-06-14T10:26:46Z","receivedAt":"2008-06-14T10:26:46Z","isPatch":true,"sender":{"key":"kha@treskal.com","avatar":"https://gravatar.com/avatar/f0120c734b5279b345075a28521e1ac66acb20c9913ffe9bf6ae97e53f7f3f13?d=mp&s=160"},"body":"On 2008-06-13 09:53:49 -0500, Clark Williams wrote:\n\n> I'm interested to hear how you'd condense the code in\n> __gethandleandname().\n\nLike this, for example:\n\n    import bz2, gzip\n    for copen, ext in [(gzip.open, '.gz'), (bz2.BZ2File, '.bz2')]:\n        try:\n            f = copen(filename)\n            f.read(1)\n            f.seek(0)\n            return (f, __mkpatchname(filename, ext))\n        except IOError, e:\n            pass\n\nIf you don't mind, I'll just fix that up directly in your patch. (I\nalso took the liberty to rename the function to __get_handle_and_name,\nsince that's more consistent with other functions in that file.)\n\n-- \nKarl Hasselström, kha@treskal.com\n      www.treskal.com/kalle\n"},{"id":"79860","messageId":"4853E86F.1050504@gmail.com","threadId":"13918","inReplyTo":"20080614102646.GD14282@diana.vm.bytemark.co.uk","subject":"Re: [StGit PATCH] compressed import v3","fromName":"Clark Williams","fromEmail":"clark.williams@gmail.com","sentAt":"2008-06-14T15:49:03Z","receivedAt":"2008-06-14T15:49:03Z","isPatch":true,"sender":{"key":"clark.williams@gmail.com","avatar":null},"body":"-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA1\n\nKarl Hasselström wrote:\n> On 2008-06-13 09:53:49 -0500, Clark Williams wrote:\n> \n>> I'm interested to hear how you'd condense the code in\n>> __gethandleandname().\n> \n> Like this, for example:\n> \n>     import bz2, gzip\n>     for copen, ext in [(gzip.open, '.gz'), (bz2.BZ2File, '.bz2')]:\n>         try:\n>             f = copen(filename)\n>             f.read(1)\n>             f.seek(0)\n>             return (f, __mkpatchname(filename, ext))\n>         except IOError, e:\n>             pass\n> \n> If you don't mind, I'll just fix that up directly in your patch. (I\n> also took the liberty to rename the function to __get_handle_and_name,\n> since that's more consistent with other functions in that file.)\n> \n\nAh, that's very nice. Your changes work for me.\n\nClark\n-----BEGIN PGP SIGNATURE-----\nVersion: GnuPG v1.4.9 (GNU/Linux)\nComment: Using GnuPG with Fedora - http://enigmail.mozdev.org\n\niEYEARECAAYFAkhT6G8ACgkQqA4JVb61b9cCeQCfedD0BjOeRa3sbGvkRNe2BPMq\n6roAoJE7klMbgymRCJ3B+pEjzgnTWO2l\n=uZ+n\n-----END PGP SIGNATURE-----\n"}]}