Skip to content

Commit ba053ea

Browse files
pcloudsgitster
authored andcommitted
archive: do not read .gitattributes in working directory
The old behaviour still remains with --worktree-attributes, and it is always on for the legacy "git tar-tree". Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 66985e6 commit ba053ea

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

Documentation/git-archive.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SYNOPSIS
1010
--------
1111
[verse]
1212
'git archive' --format=<fmt> [--list] [--prefix=<prefix>/] [<extra>]
13-
[--output=<file>]
13+
[--output=<file>] [--worktree-attributes]
1414
[--remote=<repo> [--exec=<git-upload-archive>]] <tree-ish>
1515
[path...]
1616

@@ -51,6 +51,9 @@ OPTIONS
5151
--output=<file>::
5252
Write the archive to <file> instead of stdout.
5353

54+
--worktree-attributes::
55+
Look for attributes in .gitattributes in working directory too.
56+
5457
<extra>::
5558
This can be any options that the archiver backend understands.
5659
See next section.

archive.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "attr.h"
55
#include "archive.h"
66
#include "parse-options.h"
7+
#include "unpack-trees.h"
78

89
static char const * const archive_usage[] = {
910
"git archive [options] <tree-ish> [path...]",
@@ -150,6 +151,8 @@ int write_archive_entries(struct archiver_args *args,
150151
write_archive_entry_fn_t write_entry)
151152
{
152153
struct archiver_context context;
154+
struct unpack_trees_options opts;
155+
struct tree_desc t;
153156
int err;
154157

155158
if (args->baselen > 0 && args->base[args->baselen - 1] == '/') {
@@ -168,6 +171,22 @@ int write_archive_entries(struct archiver_args *args,
168171
context.args = args;
169172
context.write_entry = write_entry;
170173

174+
/*
175+
* Setup index and instruct attr to read index only
176+
*/
177+
if (!args->worktree_attributes) {
178+
memset(&opts, 0, sizeof(opts));
179+
opts.index_only = 1;
180+
opts.head_idx = -1;
181+
opts.src_index = &the_index;
182+
opts.dst_index = &the_index;
183+
opts.fn = oneway_merge;
184+
init_tree_desc(&t, args->tree->buffer, args->tree->size);
185+
if (unpack_trees(1, &t, &opts))
186+
return -1;
187+
git_attr_set_direction(GIT_ATTR_INDEX, &the_index);
188+
}
189+
171190
err = read_tree_recursive(args->tree, args->base, args->baselen, 0,
172191
args->pathspec, write_archive_entry, &context);
173192
if (err == READ_TREE_RECURSIVE)
@@ -258,13 +277,16 @@ static int parse_archive_args(int argc, const char **argv,
258277
int verbose = 0;
259278
int i;
260279
int list = 0;
280+
int worktree_attributes = 0;
261281
struct option opts[] = {
262282
OPT_GROUP(""),
263283
OPT_STRING(0, "format", &format, "fmt", "archive format"),
264284
OPT_STRING(0, "prefix", &base, "prefix",
265285
"prepend prefix to each pathname in the archive"),
266286
OPT_STRING(0, "output", &output, "file",
267287
"write the archive to this file"),
288+
OPT_BOOLEAN(0, "worktree-attributes", &worktree_attributes,
289+
"read .gitattributes in working directory"),
268290
OPT__VERBOSE(&verbose),
269291
OPT__COMPR('0', &compression_level, "store only", 0),
270292
OPT__COMPR('1', &compression_level, "compress faster", 1),
@@ -324,6 +346,7 @@ static int parse_archive_args(int argc, const char **argv,
324346
args->verbose = verbose;
325347
args->base = base;
326348
args->baselen = strlen(base);
349+
args->worktree_attributes = worktree_attributes;
327350

328351
return argc;
329352
}

archive.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ struct archiver_args {
1010
time_t time;
1111
const char **pathspec;
1212
unsigned int verbose : 1;
13+
unsigned int worktree_attributes : 1;
1314
int compression_level;
1415
};
1516

builtin-tar-tree.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int cmd_tar_tree(int argc, const char **argv, const char *prefix)
2424
* git archive --format-tar --prefix=basedir tree-ish
2525
*/
2626
int i;
27-
const char **nargv = xcalloc(sizeof(*nargv), argc + 2);
27+
const char **nargv = xcalloc(sizeof(*nargv), argc + 3);
2828
char *basedir_arg;
2929
int nargc = 0;
3030

@@ -36,6 +36,13 @@ int cmd_tar_tree(int argc, const char **argv, const char *prefix)
3636
argv++;
3737
argc--;
3838
}
39+
40+
/*
41+
* Because it's just a compatibility wrapper, tar-tree supports only
42+
* the old behaviour of reading attributes from the work tree.
43+
*/
44+
nargv[nargc++] = "--worktree-attributes";
45+
3946
switch (argc) {
4047
default:
4148
usage(tar_tree_usage);

0 commit comments

Comments
 (0)