6 static void format_subst(const struct commit
*commit
,
7 const char *src
, size_t len
,
14 to_free
= strbuf_detach(buf
, NULL
);
19 b
= memmem(src
, len
, "$Format:", 8);
22 c
= memchr(b
+ 8, '$', (src
+ len
) - b
- 8);
27 strbuf_add(&fmt
, b
+ 8, c
- b
- 8);
29 strbuf_add(buf
, src
, b
- src
);
30 format_commit_message(commit
, fmt
.buf
, buf
);
34 strbuf_add(buf
, src
, len
);
39 static int convert_to_archive(const char *path
,
40 const void *src
, size_t len
,
42 const struct commit
*commit
)
44 static struct git_attr
*attr_export_subst
;
45 struct git_attr_check check
[1];
50 if (!attr_export_subst
)
51 attr_export_subst
= git_attr("export-subst", 12);
53 check
[0].attr
= attr_export_subst
;
54 if (git_checkattr(path
, ARRAY_SIZE(check
), check
))
56 if (!ATTR_TRUE(check
[0].value
))
59 format_subst(commit
, src
, len
, buf
);
63 void *sha1_file_to_archive(const char *path
, const unsigned char *sha1
,
64 unsigned int mode
, enum object_type
*type
,
66 const struct commit
*commit
)
70 buffer
= read_sha1_file(sha1
, type
, sizep
);
71 if (buffer
&& S_ISREG(mode
)) {
76 strbuf_attach(&buf
, buffer
, *sizep
, *sizep
+ 1);
77 convert_to_working_tree(path
, buf
.buf
, buf
.len
, &buf
);
78 convert_to_archive(path
, buf
.buf
, buf
.len
, &buf
, commit
);
79 buffer
= strbuf_detach(&buf
, &size
);
86 int is_archive_path_ignored(const char *path
)
88 static struct git_attr
*attr_export_ignore
;
89 struct git_attr_check check
[1];
91 if (!attr_export_ignore
)
92 attr_export_ignore
= git_attr("export-ignore", 13);
94 check
[0].attr
= attr_export_ignore
;
95 if (git_checkattr(path
, ARRAY_SIZE(check
), check
))
97 return ATTR_TRUE(check
[0].value
);
100 struct archiver_context
{
101 struct archiver_args
*args
;
102 write_archive_entry_fn_t write_entry
;
105 static int write_archive_entry(const unsigned char *sha1
, const char *base
,
106 int baselen
, const char *filename
, unsigned mode
, int stage
,
109 static struct strbuf path
= STRBUF_INIT
;
110 struct archiver_context
*c
= context
;
111 struct archiver_args
*args
= c
->args
;
112 write_archive_entry_fn_t write_entry
= c
->write_entry
;
114 enum object_type type
;
119 strbuf_grow(&path
, PATH_MAX
);
120 strbuf_add(&path
, base
, baselen
);
121 strbuf_addstr(&path
, filename
);
123 if (is_archive_path_ignored(path
.buf
+ args
->baselen
))
126 if (S_ISDIR(mode
) || S_ISGITLINK(mode
)) {
127 strbuf_addch(&path
, '/');
129 fprintf(stderr
, "%.*s\n", (int)path
.len
, path
.buf
);
130 err
= write_entry(args
, sha1
, path
.buf
, path
.len
, mode
, NULL
, 0);
133 return READ_TREE_RECURSIVE
;
136 buffer
= sha1_file_to_archive(path
.buf
+ args
->baselen
, sha1
, mode
,
137 &type
, &size
, args
->commit
);
139 return error("cannot read %s", sha1_to_hex(sha1
));
141 fprintf(stderr
, "%.*s\n", (int)path
.len
, path
.buf
);
142 err
= write_entry(args
, sha1
, path
.buf
, path
.len
, mode
, buffer
, size
);
147 int write_archive_entries(struct archiver_args
*args
,
148 write_archive_entry_fn_t write_entry
)
150 struct archiver_context context
;
153 if (args
->baselen
> 0 && args
->base
[args
->baselen
- 1] == '/') {
154 size_t len
= args
->baselen
;
156 while (len
> 1 && args
->base
[len
- 2] == '/')
159 fprintf(stderr
, "%.*s\n", (int)len
, args
->base
);
160 err
= write_entry(args
, args
->tree
->object
.sha1
, args
->base
,
161 len
, 040777, NULL
, 0);
167 context
.write_entry
= write_entry
;
169 err
= read_tree_recursive(args
->tree
, args
->base
, args
->baselen
, 0,
170 args
->pathspec
, write_archive_entry
, &context
);
171 if (err
== READ_TREE_RECURSIVE
)