Merge branch 'rs/zip-text'
authorJunio C Hamano <gitster@pobox.com>
Tue, 17 Mar 2015 23:01:27 +0000 (16:01 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 17 Mar 2015 23:01:27 +0000 (16:01 -0700)
"git archive" can now be told to set the 'text' attribute in the
resulting zip archive.

* rs/zip-text:
archive-zip: mark text files in archives

1  2 
archive-zip.c
t/t5003-archive-zip.sh
diff --combined archive-zip.c
index 1a54e1b187178e9818ee3e3f90279c2985fa7b63,0f9e87f46e5d80846287df1039e1815eb2947067..ffb3535e93dca2135724d998657b80c2bc4d6f20
@@@ -5,6 -5,8 +5,8 @@@
  #include "archive.h"
  #include "streaming.h"
  #include "utf8.h"
+ #include "userdiff.h"
+ #include "xdiff-interface.h"
  
  static int zip_date;
  static int zip_time;
@@@ -120,6 -122,7 +122,6 @@@ static void *zlib_deflate_raw(void *dat
        void *buffer;
        int result;
  
 -      memset(&stream, 0, sizeof(stream));
        git_deflate_init_raw(&stream, compression_level);
        maxsize = git_deflate_bound(&stream, size);
        buffer = xmalloc(maxsize);
@@@ -188,6 -191,16 +190,16 @@@ static int has_only_ascii(const char *s
        }
  }
  
+ static int entry_is_binary(const char *path, const void *buffer, size_t size)
+ {
+       struct userdiff_driver *driver = userdiff_find_by_path(path);
+       if (!driver)
+               driver = userdiff_find_by_name("default");
+       if (driver->binary != -1)
+               return driver->binary;
+       return buffer_is_binary(buffer, size);
+ }
  #define STREAM_BUFFER_SIZE (1024 * 16)
  
  static int write_zip_entry(struct archiver_args *args,
        struct git_istream *stream = NULL;
        unsigned long flags = 0;
        unsigned long size;
+       int is_binary = -1;
+       const char *path_without_prefix = path + args->baselen;
  
        crc = crc32(0, NULL, 0);
  
                                return error("cannot read %s",
                                             sha1_to_hex(sha1));
                        crc = crc32(crc, buffer, size);
+                       is_binary = entry_is_binary(path_without_prefix,
+                                                   buffer, size);
                        out = buffer;
                }
                compressed_size = (method == 0) ? size : 0;
        copy_le16(dirent.extra_length, ZIP_EXTRA_MTIME_SIZE);
        copy_le16(dirent.comment_length, 0);
        copy_le16(dirent.disk, 0);
-       copy_le16(dirent.attr1, 0);
        copy_le32(dirent.attr2, attr2);
        copy_le32(dirent.offset, zip_offset);
  
                        if (readlen <= 0)
                                break;
                        crc = crc32(crc, buf, readlen);
+                       if (is_binary == -1)
+                               is_binary = entry_is_binary(path_without_prefix,
+                                                           buf, readlen);
                        write_or_die(1, buf, readlen);
                }
                close_istream(stream);
                size_t out_len;
                unsigned char compressed[STREAM_BUFFER_SIZE * 2];
  
 -              memset(&zstream, 0, sizeof(zstream));
                git_deflate_init_raw(&zstream, args->compression_level);
  
                compressed_size = 0;
                        if (readlen <= 0)
                                break;
                        crc = crc32(crc, buf, readlen);
+                       if (is_binary == -1)
+                               is_binary = entry_is_binary(path_without_prefix,
+                                                           buf, readlen);
  
                        zstream.next_in = buf;
                        zstream.avail_in = readlen;
        free(deflated);
        free(buffer);
  
+       copy_le16(dirent.attr1, !is_binary);
        memcpy(zip_dir + zip_dir_offset, &dirent, ZIP_DIR_HEADER_SIZE);
        zip_dir_offset += ZIP_DIR_HEADER_SIZE;
        memcpy(zip_dir + zip_dir_offset, path, pathlen);
diff --combined t/t5003-archive-zip.sh
index c929db563326614e01962b3e322f087640fa19d9,fb8cdf68bc73192e49ff58760a7e0e7af931bd71..14744b2a4b1e646890176ec53970d36de6f8ec3b
@@@ -33,6 -33,37 +33,37 @@@ check_zip() 
        test_expect_success UNZIP " validate file contents" "
                diff -r a ${dir_with_prefix}a
        "
+       dir=eol_$1
+       dir_with_prefix=$dir/$2
+       extracted=${dir_with_prefix}a
+       original=a
+       test_expect_success UNZIP " extract ZIP archive with EOL conversion" '
+               (mkdir $dir && cd $dir && "$GIT_UNZIP" -a ../$zipfile)
+       '
+       test_expect_success UNZIP " validate that text files are converted" "
+               test_cmp_bin $extracted/text.cr $extracted/text.crlf &&
+               test_cmp_bin $extracted/text.cr $extracted/text.lf
+       "
+       test_expect_success UNZIP " validate that binary files are unchanged" "
+               test_cmp_bin $original/binary.cr   $extracted/binary.cr &&
+               test_cmp_bin $original/binary.crlf $extracted/binary.crlf &&
+               test_cmp_bin $original/binary.lf   $extracted/binary.lf
+       "
+       test_expect_success UNZIP " validate that diff files are converted" "
+               test_cmp_bin $extracted/diff.cr $extracted/diff.crlf &&
+               test_cmp_bin $extracted/diff.cr $extracted/diff.lf
+       "
+       test_expect_success UNZIP " validate that -diff files are unchanged" "
+               test_cmp_bin $original/nodiff.cr   $extracted/nodiff.cr &&
+               test_cmp_bin $original/nodiff.crlf $extracted/nodiff.crlf &&
+               test_cmp_bin $original/nodiff.lf   $extracted/nodiff.lf
+       "
  }
  
  test_expect_success \
       echo simple textfile >a/a &&
       mkdir a/bin &&
       cp /bin/sh a/bin &&
+      printf "text\r"  >a/text.cr &&
+      printf "text\r\n"        >a/text.crlf &&
+      printf "text\n"  >a/text.lf &&
+      printf "text\r"  >a/nodiff.cr &&
+      printf "text\r\n"        >a/nodiff.crlf &&
+      printf "text\n"  >a/nodiff.lf &&
+      printf "\0\r"    >a/binary.cr &&
+      printf "\0\r\n"  >a/binary.crlf &&
+      printf "\0\n"    >a/binary.lf &&
+      printf "\0\r"    >a/diff.cr &&
+      printf "\0\r\n"  >a/diff.crlf &&
+      printf "\0\n"    >a/diff.lf &&
       printf "A\$Format:%s\$O" "$SUBSTFORMAT" >a/substfile1 &&
       printf "A not substituted O" >a/substfile2 &&
       (p=long_path_to_a_file && cd a &&
@@@ -61,12 -104,18 +104,14 @@@ test_expect_success 
      'echo ignore me >a/ignored &&
       echo ignored export-ignore >.git/info/attributes'
  
 -test_expect_success \
 -    'add files to repository' \
 -    'find a -type f | xargs git update-index --add &&
 -     find a -type l | xargs git update-index --add &&
 -     treeid=`git write-tree` &&
 -     echo $treeid >treeid &&
 -     git update-ref HEAD $(TZ=GMT GIT_COMMITTER_DATE="2005-05-27 22:00:00" \
 -     git commit-tree $treeid </dev/null)'
 +test_expect_success 'add files to repository' '
 +      git add a &&
 +      GIT_COMMITTER_DATE="2005-05-27 22:00" git commit -m initial
 +'
  
- test_expect_success 'setup export-subst' '
+ test_expect_success 'setup export-subst and diff attributes' '
+       echo "a/nodiff.* -diff" >>.git/info/attributes &&
+       echo "a/diff.* diff" >>.git/info/attributes &&
        echo "substfile?" export-subst >>.git/info/attributes &&
        git log --max-count=1 "--pretty=format:A${SUBSTFORMAT}O" HEAD \
                >a/substfile1