Subject: [PATCH stgit] revised patch for importing series from tarball

From: Clark Williams <[email-masked]>
Date: Sun, 19 Oct 2008 14:16:13 -0500

Catalin,

Attached is my revised patch (v2 I believe) for importing a series directly from a tarball. I looked at the critique offered by Karl (pretty hard actually), but I decided in the end to keep extracting the tarball to a temp directory. It's possible that it would be desirable to extract members directly from a tarball (although as far as I can tell, you still have to extract them to a file) but I didn't judge the churn in imprt.py to be worth it for now. This version is pretty simple, in that you just detect that the input to to import_series is a tarball, call import_tarfile, then return. 

I added a simple test to the test harness for import as well. 

Oh and I added '*.elc' to the .gitignore file. May not be that many folks using emacs with stgit, but hey, I am! :)

Clark
patch to allow importing a series from a tar archive

From: Clark Williams <[email-masked]>

Signed-off-by: Clark Williams <[email-masked]>
---
 .gitignore                                      |    1 
 stgit/commands/imprt.py                         |   47 ++++++++++++++++++++++-
 t/t1800-import.sh                               |   12 ++++++
 t/t1800-import/patches/attribution.patch        |   21 ++++++++++
 t/t1800-import/patches/delete-extra-lines.patch |   22 +++++++++++
 t/t1800-import/patches/fifth-stanza.patch       |   22 +++++++++++
 t/t1800-import/patches/first-stanza.patch       |   18 +++++++++
 t/t1800-import/patches/fourth-stanza.patch      |   22 +++++++++++
 t/t1800-import/patches/second-stanza.patch      |   22 +++++++++++
 t/t1800-import/patches/series                   |   10 +++++
 t/t1800-import/patches/seventh-stanza.patch     |   24 ++++++++++++
 t/t1800-import/patches/sixth-stanza.patch       |   22 +++++++++++
 t/t1800-import/patches/third-stanza.patch       |   22 +++++++++++
 13 files changed, 263 insertions(+), 2 deletions(-)
 create mode 100644 t/t1800-import/patches/attribution.patch
 create mode 100644 t/t1800-import/patches/delete-extra-lines.patch
 create mode 100644 t/t1800-import/patches/fifth-stanza.patch
 create mode 100644 t/t1800-import/patches/first-stanza.patch
 create mode 100644 t/t1800-import/patches/fourth-stanza.patch
 create mode 100644 t/t1800-import/patches/second-stanza.patch
 create mode 100644 t/t1800-import/patches/series
 create mode 100644 t/t1800-import/patches/seventh-stanza.patch
 create mode 100644 t/t1800-import/patches/sixth-stanza.patch
 create mode 100644 t/t1800-import/patches/third-stanza.patch

diff --git a/.gitignore b/.gitignore
index 91dbad2..f0e5d30 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,4 @@ patches-*
 release.sh
 setup.cfg.rpm
 snapshot.sh
+*.elc
diff --git a/stgit/commands/imprt.py b/stgit/commands/imprt.py
index 227743f..6860d0e 100644
--- a/stgit/commands/imprt.py
+++ b/stgit/commands/imprt.py
@@ -19,6 +19,7 @@ import sys, os, re, email
 from mailbox import UnixMailbox
 from StringIO import StringIO
 from optparse import OptionParser, make_option
+import tarfile
 
 from stgit.commands.common import *
 from stgit.utils import *
@@ -52,7 +53,7 @@ options = [make_option('-m', '--mail',
                        help = 'import a series of patches from an mbox file',
                        action = 'store_true'),
            make_option('-s', '--series',
-                       help = 'import a series of patches',
+                       help = 'import a series of patches from a series file or a tar archive',
                        action = 'store_true'),
            make_option('-u', '--url',
                        help = 'import a patch from a URL',
@@ -87,7 +88,7 @@ options = [make_option('-m', '--mail',
            make_option('--commname',
                        help = 'use COMMNAME as the committer name'),
            make_option('--commemail',
-                       help = 'use COMMEMAIL as the committer e-mail')
+                       help = 'use COMMEMAIL as the committer e-mail'),
            ] + make_sign_options()

@@ -234,6 +235,9 @@ def __import_series(filename, options):
     applied = crt_series.get_applied()
 
     if filename:
+        if tarfile.is_tarfile(filename):
+            __import_tarfile(filename, options)
+            return
         f = file(filename)
         patchdir = os.path.dirname(filename)
     else:
@@ -287,6 +291,45 @@ def __import_url(url, options):
     urllib.urlretrieve(url, filename)
     __import_file(filename, options)
 
+def __import_tarfile(tar, options):
+    """Import patch series from a tar archive
+    """
+    import tempfile
+    import shutil
+
+    if not tarfile.is_tarfile(tar):
+        raise CmdException, "%s is not a tarfile!" % tar
+
+
+    t = tarfile.open(tar, 'r')
+    names = t.getnames()
+
+    # verify paths in the tarfile are safe
+    for n in names:
+        if n.startswith('/'):
+            raise CmdException, "Absolute path found in %s" % tar
+        if n.find("..") > -1:
+            raise CmdException, "Relative path found in %s" % tar
+
+    # find the series file
+    seriesfile = '';
+    for m in names:
+        if m.endswith('/series') or m == 'series':
+            seriesfile = m
+            break
+    if seriesfile == '':
+        raise CmdException, "no 'series' file found in %s" % tar
+
+    # unpack into a tmp dir
+    tmpdir = tempfile.mkdtemp('.stg')
+    t.extractall(tmpdir)
+
+    # apply the series
+    __import_series(os.path.join(tmpdir, seriesfile), options)
+
+    # cleanup the tmpdir
+    shutil.rmtree(tmpdir)
+
 def func(parser, options, args):
     """Import a GNU diff file as a new patch
     """
diff --git a/t/t1800-import.sh b/t/t1800-import.sh
index 1352743..5a3384f 100755
--- a/t/t1800-import.sh
+++ b/t/t1800-import.sh
@@ -122,4 +122,16 @@ test_expect_success \
     stg delete ..
     '
 
+test_expect_success \
+    'apply a series from a tarball' \
+    '
+    rm -f jabberwocky.txt && touch jabberwocky.txt &&
+    git add jabberwocky.txt && git commit -m "empty file" jabberwocky.txt &&
+    (cd ../t1800-import; tar -cjf jabberwocky.tar.bz2 patches) &&
+    stg import --series ../t1800-import/jabberwocky.tar.bz2
+    [ $(git cat-file -p $(stg id) \
+        | grep -c "tree 2c33937252a21f1550c0bf21f1de534b68f69635") = 1 ] &&
+    rm ../t1800-import/jabberwocky.tar.bz2
+    '
+    
 test_done
diff --git a/t/t1800-import/patches/attribution.patch b/t/t1800-import/patches/attribution.patch
new file mode 100644
index 0000000..2b7c8f9
--- /dev/null
+++ b/t/t1800-import/patches/attribution.patch
@@ -0,0 +1,21 @@
+attribution
+
+From: Clark Williams <[email-masked]>
+
+
+---
+ jabberwocky.txt |    4 ++++
+ 1 files changed, 4 insertions(+), 0 deletions(-)
+
+diff --git a/jabberwocky.txt b/jabberwocky.txt
+index 066d2e8..a9dd1f3 100644
+--- a/jabberwocky.txt
++++ b/jabberwocky.txt
+@@ -32,3 +32,7 @@ O frabjous day! Callooh! Callay!'
+   Did gyre and gimble in the wabe;
+ All mimsy were the borogoves,
+   And the mome raths outgrabe.
++
++	JABBERWOCKY
++	Lewis Carroll
++	(from Through the Looking-Glass and What Alice Found There, 1872) 
diff --git a/t/t1800-import/patches/delete-extra-lines.patch b/t/t1800-import/patches/delete-extra-lines.patch
new file mode 100644
index 0000000..e5b7a65
--- /dev/null
+++ b/t/t1800-import/patches/delete-extra-lines.patch
@@ -0,0 +1,22 @@
+delete extra lines
+
+From: Clark Williams <[email-masked]>
+
+
+---
+ jabberwocky.txt |    2 --
+ 1 files changed, 0 insertions(+), 2 deletions(-)
+
+diff --git a/jabberwocky.txt b/jabberwocky.txt
+index 98cb716..066d2e8 100644
+--- a/jabberwocky.txt
++++ b/jabberwocky.txt
+@@ -28,8 +28,6 @@ He left it dead, and with its head
+ O frabjous day! Callooh! Callay!'
+   He chortled in his joy.
+ 
+-
+-
+ `Twas brillig, and the slithy toves
+   Did gyre and gimble in the wabe;
+ All mimsy were the borogoves,
diff --git a/t/t1800-import/patches/fifth-stanza.patch b/t/t1800-import/patches/fifth-stanza.patch
new file mode 100644
index 0000000..4f0e77c
--- /dev/null
+++ b/t/t1800-import/patches/fifth-stanza.patch
@@ -0,0 +1,22 @@
+fifth stanza
+
+From: Clark Williams <[email-masked]>
+
+
+---
+ jabberwocky.txt |    5 +++++
+ 1 files changed, 5 insertions(+), 0 deletions(-)
+
+diff --git a/jabberwocky.txt b/jabberwocky.txt
+index b1c2ad3..f1416dc 100644
+--- a/jabberwocky.txt
++++ b/jabberwocky.txt
+@@ -17,3 +17,8 @@ And, as in uffish thought he stood,
+   The Jabberwock, with eyes of flame,
+ Came whiffling through the tulgey wood,
+   And burbled as it came!
++
++One, two! One, two! And through and through
++  The vorpal blade went snicker-snack!
++He left it dead, and with its head
++  He went galumphing back.
diff --git a/t/t1800-import/patches/first-stanza.patch b/t/t1800-import/patches/first-stanza.patch
new file mode 100644
index 0000000..ee7818f
--- /dev/null
+++ b/t/t1800-import/patches/first-stanza.patch
@@ -0,0 +1,18 @@
+first stanza
+
+From: Clark Williams <[email-masked]>
+
+
+---
+ jabberwocky.txt |    4 ++++
+ 1 files changed, 4 insertions(+), 0 deletions(-)
+
+diff --git a/jabberwocky.txt b/jabberwocky.txt
+index e69de29..fba24dc 100644
+--- a/jabberwocky.txt
++++ b/jabberwocky.txt
+@@ -0,0 +1,4 @@
++`Twas brillig, and the slithy toves
++  Did gyre and gimble in the wabe:
++All mimsy were the borogoves,
++  And the mome raths outgrabe.
diff --git a/t/t1800-import/patches/fourth-stanza.patch b/t/t1800-import/patches/fourth-stanza.patch
new file mode 100644
index 0000000..eb2f8f2
--- /dev/null
+++ b/t/t1800-import/patches/fourth-stanza.patch
@@ -0,0 +1,22 @@
+fourth stanza
+
+From: Clark Williams <[email-masked]>
+
+
+---
+ jabberwocky.txt |    5 +++++
+ 1 files changed, 5 insertions(+), 0 deletions(-)
+
+diff --git a/jabberwocky.txt b/jabberwocky.txt
+index 6405f36..b1c2ad3 100644
+--- a/jabberwocky.txt
++++ b/jabberwocky.txt
+@@ -12,3 +12,8 @@ He took his vorpal sword in hand:
+   Long time the manxome foe he sought --
+ So rested he by the Tumtum tree,
+   And stood awhile in thought.
++
++And, as in uffish thought he stood,
++  The Jabberwock, with eyes of flame,
++Came whiffling through the tulgey wood,
++  And burbled as it came!
diff --git a/t/t1800-import/patches/second-stanza.patch b/t/t1800-import/patches/second-stanza.patch
new file mode 100644
index 0000000..bec1622
--- /dev/null
+++ b/t/t1800-import/patches/second-stanza.patch
@@ -0,0 +1,22 @@
+second stanza
+
+From: Clark Williams <[email-masked]>
+
+
+---
+ jabberwocky.txt |    5 +++++
+ 1 files changed, 5 insertions(+), 0 deletions(-)
+
+diff --git a/jabberwocky.txt b/jabberwocky.txt
+index fba24dc..9ed0b49 100644
+--- a/jabberwocky.txt
++++ b/jabberwocky.txt
+@@ -2,3 +2,8 @@
+   Did gyre and gimble in the wabe:
+ All mimsy were the borogoves,
+   And the mome raths outgrabe.
++
++"Beware the Jabberwock, my son!
++  The jaws that bite, the claws that catch!
++Beware the Jubjub bird, and shun
++  The frumious Bandersnatch!"
diff --git a/t/t1800-import/patches/series b/t/t1800-import/patches/series
new file mode 100644
index 0000000..5945c98
--- /dev/null
+++ b/t/t1800-import/patches/series
@@ -0,0 +1,10 @@
+# This series applies on GIT commit 6a8b6f6e2ecbcab26de7656b66b7f30eeba1ee96
+first-stanza.patch
+second-stanza.patch
+third-stanza.patch
+fourth-stanza.patch
+fifth-stanza.patch
+sixth-stanza.patch
+seventh-stanza.patch
+delete-extra-lines.patch
+attribution.patch
diff --git a/t/t1800-import/patches/seventh-stanza.patch b/t/t1800-import/patches/seventh-stanza.patch
new file mode 100644
index 0000000..555c200
--- /dev/null
+++ b/t/t1800-import/patches/seventh-stanza.patch
@@ -0,0 +1,24 @@
+seventh stanza
+
+From: Clark Williams <[email-masked]>
+
+
+---
+ jabberwocky.txt |    7 +++++++
+ 1 files changed, 7 insertions(+), 0 deletions(-)
+
+diff --git a/jabberwocky.txt b/jabberwocky.txt
+index bf732f5..98cb716 100644
+--- a/jabberwocky.txt
++++ b/jabberwocky.txt
+@@ -27,3 +27,10 @@ He left it dead, and with its head
+   Come to my arms, my beamish boy!
+ O frabjous day! Callooh! Callay!'
+   He chortled in his joy.
++
++
++
++`Twas brillig, and the slithy toves
++  Did gyre and gimble in the wabe;
++All mimsy were the borogoves,
++  And the mome raths outgrabe.
diff --git a/t/t1800-import/patches/sixth-stanza.patch b/t/t1800-import/patches/sixth-stanza.patch
new file mode 100644
index 0000000..2349b7e
--- /dev/null
+++ b/t/t1800-import/patches/sixth-stanza.patch
@@ -0,0 +1,22 @@
+sixth stanza
+
+From: Clark Williams <[email-masked]>
+
+
+---
+ jabberwocky.txt |    5 +++++
+ 1 files changed, 5 insertions(+), 0 deletions(-)
+
+diff --git a/jabberwocky.txt b/jabberwocky.txt
+index f1416dc..bf732f5 100644
+--- a/jabberwocky.txt
++++ b/jabberwocky.txt
+@@ -22,3 +22,8 @@ One, two! One, two! And through and through
+   The vorpal blade went snicker-snack!
+ He left it dead, and with its head
+   He went galumphing back.
++
++"And, has thou slain the Jabberwock?
++  Come to my arms, my beamish boy!
++O frabjous day! Callooh! Callay!'
++  He chortled in his joy.
diff --git a/t/t1800-import/patches/third-stanza.patch b/t/t1800-import/patches/third-stanza.patch
new file mode 100644
index 0000000..d942353
--- /dev/null
+++ b/t/t1800-import/patches/third-stanza.patch
@@ -0,0 +1,22 @@
+third stanza
+
+From: Clark Williams <[email-masked]>
+
+
+---
+ jabberwocky.txt |    5 +++++
+ 1 files changed, 5 insertions(+), 0 deletions(-)
+
+diff --git a/jabberwocky.txt b/jabberwocky.txt
+index 9ed0b49..6405f36 100644
+--- a/jabberwocky.txt
++++ b/jabberwocky.txt
+@@ -7,3 +7,8 @@ All mimsy were the borogoves,
+   The jaws that bite, the claws that catch!
+ Beware the Jubjub bird, and shun
+   The frumious Bandersnatch!"
++
++He took his vorpal sword in hand:
++  Long time the manxome foe he sought --
++So rested he by the Tumtum tree,
++  And stood awhile in thought.