chiark / gitweb /
Merge branch 'stable'
authorCatalin Marinas <catalin.marinas@gmail.com>
Tue, 9 Sep 2008 22:12:19 +0000 (23:12 +0100)
committerCatalin Marinas <catalin.marinas@gmail.com>
Tue, 9 Sep 2008 22:12:19 +0000 (23:12 +0100)
1  2 
stgit/commands/imprt.py
t/t1800-import.sh

diff --combined stgit/commands/imprt.py
index e649ed7c632ba15a72533f6b52130148d2db606e,cd44d3fa4af913dc44706f7523bd655092cc6001..5fc83e935d241f1ad00a26b74447cb5337defd62
@@@ -23,7 -23,8 +23,7 @@@ from optparse import OptionParser, make
  from stgit.commands.common import *
  from stgit.utils import *
  from stgit.out import *
 -from stgit import stack, git
 -
 +from stgit import argparse, stack, git
  
  help = 'import a GNU diff file as a new patch'
  usage = """%prog [options] [<file>|<url>]
@@@ -87,7 -88,7 +87,7 @@@ options = [make_option('-m', '--mail'
                         help = 'use COMMNAME as the committer name'),
             make_option('--commemail',
                         help = 'use COMMEMAIL as the committer e-mail')
 -           ] + make_sign_options()
 +           ] + argparse.sign_options()
  
  
  def __strip_patch_name(name):
@@@ -174,42 -175,14 +174,42 @@@ def __create_patch(filename, message, a
                                   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)
                   parse_mail(msg)
      else:
          message, author_name, author_email, author_date, diff = \
 -                 parse_patch(f)
 +                 parse_patch(f.read())
  
      if filename:
          f.close()
  
 -    if patch:
 -        pname = patch
 -    else:
 -        pname = filename
 -
      __create_patch(pname, message, author_name, author_email,
                     author_date, diff, options)
  
@@@ -301,6 -279,10 +301,10 @@@ def func(parser, options, args)
      else:
          filename = None
  
+     if filename:
+         filename = os.path.abspath(filename)
+     directory.cd_to_topdir()
      if options.series:
          __import_series(filename, options)
      elif options.mbox:
diff --combined t/t1800-import.sh
index 1352743ddb498d2784361682a9b32a661bebdb43,624e51c7b53fe045cfabcaf8aa3e6658154bc249..9c317411f0dc2aaead640031850d4c4b9cafb57c
@@@ -21,6 -21,17 +21,17 @@@ test_expect_success 
      stg delete ..
      '
  
+ test_expect_success \
+     'Apply a patch created with "git diff" from a subdirectory' \
+     '
+     mkdir subdir && cd subdir &&
+     stg import ../../t1800-import/git-diff &&
+     [ $(git cat-file -p $(stg id) \
+         | grep -c "tree e96b1fba2160890ff600b675d7140d46b022b155") = 1 ] &&
+     stg delete .. &&
+     cd ..
+     '
  test_expect_success \
      'Apply a patch created with GNU diff' \
      '
@@@ -80,46 -91,4 +91,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