Subject: [PATCH v2 3/3] archive: do not read .gitattributes in working directory

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Thu, 9 Apr 2009 17:01:30 +1000

The old behaviour still remains with --fix-attributes.
Also fix tests in t5000-tar-tree.sh to use --fix-attributes.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
  2009/4/9 Junio C Hamano <[email-masked]>:
  > Hmmm, if you read_tree() into the_index upfront and do not change anything
  > else to the archive.c code, shouldn't it work without such a regression at
  > all?  Am I missing something?
  >
  > It would allow you to export the index into an archive, but I doubt it is
  > worth the amount of code churn.
  > Hmmm, if you read_tree() into the_index upfront and do not change anything
  > else to the archive.c code, shouldn't it work without such a regression at
  > all?  Am I missing something?
  >
  > It would allow you to export the index into an archive, but I doubt it is
  > worth the amount of code churn.
  
  I skipped the idea originally because of data duplication. But given
  the amount of code change in my approach, just loading index is better.
  
  2009/4/9 René Scharfe <[email-masked]>:
  > I don't like the need to prepare an index of all paths up front, but
  > that's just a gut feeling.  I haven't looked into implementing in-tree
  > attribute support in attr.c; is it really that hard?  Other commands
  > would benefit from this, too, right (e.g. any command using attributes
  > in a bare repo)?
  
  You could try. At least with index, I only need a couple lines of
  modification in attr.c :) If it traverses directory upward for
  .gitattributes, then you may have problem. I'm not sure though.

 Documentation/git-archive.txt |    5 ++++-
 archive.c                     |   22 ++++++++++++++++++++++
 archive.h                     |    1 +
 builtin-tar-tree.c            |    5 +++++
 t/t5000-tar-tree.sh           |   28 ++++++++++++++++------------
 5 files changed, 48 insertions(+), 13 deletions(-)

diff --git a/Documentation/git-archive.txt b/Documentation/git-archive.txt
index 2e31142..f468523 100644
--- a/Documentation/git-archive.txt
+++ b/Documentation/git-archive.txt
@@ -10,7 +10,7 @@ SYNOPSIS
 --------
 [verse]
 'git archive' [-f <fmt>|--format=<fmt>] [--list] [-p <prefix>/|--prefix=<prefix>/] [<extra>]
-	      [--output=<file>]
+	      [--output=<file>] [--fix-attributes]
 	      [--remote=<repo> [--exec=<git-upload-archive>]] <tree-ish>
 	      [path...]
 
@@ -53,6 +53,9 @@ OPTIONS
 --output=<file>::
 	Write the archive to <file> instead of stdout.
 
+--fix-attributes::
+	Look for attributes in .gitattributes in working directory too.
+
 <extra>::
 	This can be any options that the archiver backend understands.
 	See next section.
diff --git a/archive.c b/archive.c
index e87fed7..c1c5c3c 100644
--- a/archive.c
+++ b/archive.c
@@ -4,6 +4,7 @@
 #include "attr.h"
 #include "archive.h"
 #include "parse-options.h"
+#include "unpack-trees.h"
 
 static char const * const archive_usage[] = {
 	"git archive [options] <tree-ish> [path...]",
@@ -150,6 +151,8 @@ int write_archive_entries(struct archiver_args *args,
 		write_archive_entry_fn_t write_entry)
 {
 	struct archiver_context context;
+	struct unpack_trees_options opts;
+	struct tree_desc t;
 	int err;
 
 	if (args->baselen > 0 && args->base[args->baselen - 1] == '/') {
@@ -168,6 +171,22 @@ int write_archive_entries(struct archiver_args *args,
 	context.args = args;
 	context.write_entry = write_entry;
 
+	/*
+	 * Setup index and instruct attr to read index only
+	 */
+	if (!args->worktree_attributes) {
+		memset(&opts, 0, sizeof(opts));
+		opts.index_only = 1;
+		opts.head_idx = -1;
+		opts.src_index = &the_index;
+		opts.dst_index = &the_index;
+		opts.fn = oneway_merge;
+		init_tree_desc(&t, args->tree->buffer, args->tree->size);
+		if (unpack_trees(1, &t, &opts))
+			return -1;
+		git_attr_set_direction(GIT_ATTR_INDEX, &the_index);
+	}
+
 	err =  read_tree_recursive(args->tree, args->base, args->baselen, 0,
 			args->pathspec, write_archive_entry, &context);
 	if (err == READ_TREE_RECURSIVE)
@@ -258,6 +277,7 @@ static int parse_archive_args(int argc, const char **argv,
 	int verbose = 0;
 	int i;
 	int list = 0;
+	int worktree_attributes = 0;
 	struct option opts[] = {
 		OPT_GROUP(""),
 		OPT_STRING('f', "format", &format, "fmt", "archive format"),
@@ -265,6 +285,7 @@ static int parse_archive_args(int argc, const char **argv,
 			"prepend prefix to each pathname in the archive"),
 		OPT_STRING(0, "output", &output, "file",
 			"write the archive to this file"),
+		OPT_BOOLEAN(0, "fix-attributes", &worktree_attributes, "read .gitattributes in working directory"),
 		OPT__VERBOSE(&verbose),
 		OPT__COMPR('0', &compression_level, "store only", 0),
 		OPT__COMPR('1', &compression_level, "compress faster", 1),
@@ -324,6 +345,7 @@ static int parse_archive_args(int argc, const char **argv,
 	args->verbose = verbose;
 	args->base = base;
 	args->baselen = strlen(base);
+	args->worktree_attributes = worktree_attributes;
 
 	return argc;
 }
diff --git a/archive.h b/archive.h
index 0b15b35..038ac35 100644
--- a/archive.h
+++ b/archive.h
@@ -10,6 +10,7 @@ struct archiver_args {
 	time_t time;
 	const char **pathspec;
 	unsigned int verbose : 1;
+	unsigned int worktree_attributes : 1;
 	int compression_level;
 };
 
diff --git a/builtin-tar-tree.c b/builtin-tar-tree.c
index 0713bca..69a93fc 100644
--- a/builtin-tar-tree.c
+++ b/builtin-tar-tree.c
@@ -36,6 +36,11 @@ int cmd_tar_tree(int argc, const char **argv, const char *prefix)
 		argv++;
 		argc--;
 	}
+	if (2 <= argc && !strcmp(argv[1], "--fix-attributes")) {
+		nargv[nargc++] = argv[1];
+		argv++;
+		argc--;
+	}
 	switch (argc) {
 	default:
 		usage(tar_tree_usage);
diff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh
index 7641e0d..7ff600b 100755
--- a/t/t5000-tar-tree.sh
+++ b/t/t5000-tar-tree.sh
@@ -71,12 +71,16 @@ test_expect_success \
     'rm a/ignored'
 
 test_expect_success \
-    'git archive' \
-    'git archive HEAD >b.tar'
+    'git archive without --fix-attributes' \
+    'git archive HEAD | tar t | grep -q ignored'
+
+test_expect_success \
+    'git archive --fix-attributes' \
+    'git archive --fix-attributes HEAD >b.tar'
 
 test_expect_success \
     'git tar-tree' \
-    'git tar-tree HEAD >b2.tar'
+    'git tar-tree --fix-attributes HEAD >b2.tar'
 
 test_expect_success \
     'git archive vs. git tar-tree' \
@@ -84,14 +88,14 @@ test_expect_success \
 
 test_expect_success \
     'git archive in a bare repo' \
-    '(cd bare.git && git archive HEAD) >b3.tar'
+    '(cd bare.git && git archive --fix-attributes HEAD) >b3.tar'
 
 test_expect_success \
     'git archive vs. the same in a bare repo' \
     'test_cmp b.tar b3.tar'
 
 test_expect_success 'git archive with --output' \
-    'git archive --output=b4.tar HEAD &&
+    'git archive --fix-attributes --output=b4.tar HEAD &&
     test_cmp b.tar b4.tar'
 
 test_expect_success \
@@ -122,7 +126,7 @@ test_expect_success \
 
 test_expect_success \
     'git tar-tree with prefix' \
-    'git tar-tree HEAD prefix >c.tar'
+    'git tar-tree --fix-attributes HEAD prefix >c.tar'
 
 test_expect_success \
     'extract tar archive with prefix' \
@@ -140,8 +144,8 @@ test_expect_success \
 test_expect_success \
     'create archives with substfiles' \
     'echo "substfile?" export-subst >a/.gitattributes &&
-     git archive HEAD >f.tar &&
-     git archive --prefix=prefix/ HEAD >g.tar &&
+     git archive --fix-attributes HEAD >f.tar &&
+     git archive --fix-attributes --prefix=prefix/ HEAD >g.tar &&
      rm a/.gitattributes'
 
 test_expect_success \
@@ -170,18 +174,18 @@ test_expect_success \
 
 test_expect_success \
     'git archive --format=zip' \
-    'git archive --format=zip HEAD >d.zip'
+    'git archive --fix-attributes --format=zip HEAD >d.zip'
 
 test_expect_success \
     'git archive --format=zip in a bare repo' \
-    '(cd bare.git && git archive --format=zip HEAD) >d1.zip'
+    '(cd bare.git && git archive --fix-attributes --format=zip HEAD) >d1.zip'
 
 test_expect_success \
     'git archive --format=zip vs. the same in a bare repo' \
     'test_cmp d.zip d1.zip'
 
 test_expect_success 'git archive --format=zip with --output' \
-    'git archive --format=zip --output=d2.zip HEAD &&
+    'git archive --fix-attributes --format=zip --output=d2.zip HEAD &&
     test_cmp d.zip d2.zip'
 
 $UNZIP -v >/dev/null 2>&1
@@ -206,7 +210,7 @@ test_expect_success UNZIP \
 
 test_expect_success \
     'git archive --format=zip with prefix' \
-    'git archive --format=zip --prefix=prefix/ HEAD >e.zip'
+    'git archive --fix-attributes --format=zip --prefix=prefix/ HEAD >e.zip'
 
 test_expect_success UNZIP \
     'extract ZIP archive with prefix' \
-- 
1.6.2.2.602.g83ee9f