archive: unify file attribute handling
Now that all file attribute handling for git archive has moved to archive.c, we can unexport sha1_file_to_archive() and is_archive_path_ignored() even disappears. Add setup_archive_check(), modelled after similar functions used in the code of other commands that support multiple file attributes. Also remove convert_to_archive(), as it's only remaining function with attribute handling gone was to call format_subst() if commit was not NULL, which is now checked in sha1_file_to_archive(). Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Jul 14, 2008 at 21:22 UTC
1d11d5bb85daf38aa1eb6ecd2387dff862181812
2 files changed
+23
-43
archive.c
+23
-40
@@ -36,34 +36,9 @@ static void format_subst(const struct commit *commit,
36
free(to_free);
37
}
38
39
-static int convert_to_archive(const char *path,
40
- const void *src, size_t len,
41
- struct strbuf *buf,
42
- const struct commit *commit)
43
-{
44
- static struct git_attr *attr_export_subst;
45
- struct git_attr_check check[1];
46
-
47
- if (!commit)
48
- return 0;
49
-
50
- if (!attr_export_subst)
51
- attr_export_subst = git_attr("export-subst", 12);
52
-
53
- check[0].attr = attr_export_subst;
54
- if (git_checkattr(path, ARRAY_SIZE(check), check))
55
- return 0;
56
- if (!ATTR_TRUE(check[0].value))
57
- return 0;
58
-
59
- format_subst(commit, src, len, buf);
60
- return 1;
61
-}
62
-
63
-void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
64
- unsigned int mode, enum object_type *type,
65
- unsigned long *sizep,
66
- const struct commit *commit)
39
+static void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
40
+ unsigned int mode, enum object_type *type,
41
+ unsigned long *sizep, const struct commit *commit)
42
{
43
void *buffer;
44
@@ -75,7 +50,8 @@ void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
50
strbuf_init(&buf, 0);
51
strbuf_attach(&buf, buffer, *sizep, *sizep + 1);
52
convert_to_working_tree(path, buf.buf, buf.len, &buf);
78
- convert_to_archive(path, buf.buf, buf.len, &buf, commit);
53
+ if (commit)
54
+ format_subst(commit, buf.buf, buf.len, &buf);
55
buffer = strbuf_detach(&buf, &size);
56
*sizep = size;
57
}
@@ -83,18 +59,17 @@ void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
59
return buffer;
60
}
61
86
-int is_archive_path_ignored(const char *path)
62
+static void setup_archive_check(struct git_attr_check *check)
63
{
64
static struct git_attr *attr_export_ignore;
89
- struct git_attr_check check[1];
65
+ static struct git_attr *attr_export_subst;
66
91
- if (!attr_export_ignore)
67
+ if (!attr_export_ignore) {
68
attr_export_ignore = git_attr("export-ignore", 13);
93
-
69
+ attr_export_subst = git_attr("export-subst", 12);
70
+ }
71
check[0].attr = attr_export_ignore;
95
- if (git_checkattr(path, ARRAY_SIZE(check), check))
96
- return 0;
97
- return ATTR_TRUE(check[0].value);
72
+ check[1].attr = attr_export_subst;
73
}
74
75
struct archiver_context {
@@ -110,6 +85,9 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
85
struct archiver_context *c = context;
86
struct archiver_args *args = c->args;
87
write_archive_entry_fn_t write_entry = c->write_entry;
88
+ struct git_attr_check check[2];
89
+ const char *path_without_prefix;
90
+ int convert = 0;
91
int err;
92
enum object_type type;
93
unsigned long size;
@@ -119,9 +97,14 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
97
strbuf_grow(&path, PATH_MAX);
98
strbuf_add(&path, base, baselen);
99
strbuf_addstr(&path, filename);
100
+ path_without_prefix = path.buf + args->baselen;
101
123
- if (is_archive_path_ignored(path.buf + args->baselen))
124
- return 0;
102
+ setup_archive_check(check);
103
+ if (!git_checkattr(path_without_prefix, ARRAY_SIZE(check), check)) {
104
+ if (ATTR_TRUE(check[0].value))
105
+ return 0;
106
+ convert = ATTR_TRUE(check[1].value);
107
+ }
108
109
if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
110
strbuf_addch(&path, '/');
@@ -133,8 +116,8 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
116
return READ_TREE_RECURSIVE;
117
}
118
136
- buffer = sha1_file_to_archive(path.buf + args->baselen, sha1, mode,
137
- &type, &size, args->commit);
119
+ buffer = sha1_file_to_archive(path_without_prefix, sha1, mode,
120
+ &type, &size, convert ? args->commit : NULL);
121
if (!buffer)
122
return error("cannot read %s", sha1_to_hex(sha1));
123
if (args->verbose)
archive.h
-3
@@ -43,9 +43,6 @@ extern int write_tar_archive(struct archiver_args *);
43
extern int write_zip_archive(struct archiver_args *);
44
extern void *parse_extra_zip_args(int argc, const char **argv);
45
46
-extern void *sha1_file_to_archive(const char *path, const unsigned char *sha1, unsigned int mode, enum object_type *type, unsigned long *size, const struct commit *commit);
47
-extern int is_archive_path_ignored(const char *path);
48
-
46
extern int write_archive_entries(struct archiver_args *args, write_archive_entry_fn_t write_entry);
47
48
#endif /* ARCHIVE_H */