]>
Commit | Line | Data |
---|---|---|
18125644 LH |
1 | #include "cache.h" |
2 | #include "commit.h" | |
3 | #include "attr.h" | |
562e25ab | 4 | #include "archive.h" |
18125644 LH |
5 | |
6 | static void format_subst(const struct commit *commit, | |
7 | const char *src, size_t len, | |
8 | struct strbuf *buf) | |
9 | { | |
10 | char *to_free = NULL; | |
11 | struct strbuf fmt; | |
12 | ||
13 | if (src == buf->buf) | |
14 | to_free = strbuf_detach(buf, NULL); | |
15 | strbuf_init(&fmt, 0); | |
16 | for (;;) { | |
17 | const char *b, *c; | |
18 | ||
19 | b = memmem(src, len, "$Format:", 8); | |
75b7dfbd | 20 | if (!b) |
18125644 | 21 | break; |
75b7dfbd | 22 | c = memchr(b + 8, '$', (src + len) - b - 8); |
18125644 LH |
23 | if (!c) |
24 | break; | |
25 | ||
26 | strbuf_reset(&fmt); | |
27 | strbuf_add(&fmt, b + 8, c - b - 8); | |
28 | ||
29 | strbuf_add(buf, src, b - src); | |
30 | format_commit_message(commit, fmt.buf, buf); | |
31 | len -= c + 1 - src; | |
32 | src = c + 1; | |
33 | } | |
34 | strbuf_add(buf, src, len); | |
35 | strbuf_release(&fmt); | |
36 | free(to_free); | |
37 | } | |
38 | ||
1d11d5bb RS |
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) | |
18125644 LH |
42 | { |
43 | void *buffer; | |
44 | ||
45 | buffer = read_sha1_file(sha1, type, sizep); | |
46 | if (buffer && S_ISREG(mode)) { | |
47 | struct strbuf buf; | |
48 | size_t size = 0; | |
49 | ||
50 | strbuf_init(&buf, 0); | |
51 | strbuf_attach(&buf, buffer, *sizep, *sizep + 1); | |
52 | convert_to_working_tree(path, buf.buf, buf.len, &buf); | |
1d11d5bb RS |
53 | if (commit) |
54 | format_subst(commit, buf.buf, buf.len, &buf); | |
18125644 LH |
55 | buffer = strbuf_detach(&buf, &size); |
56 | *sizep = size; | |
57 | } | |
58 | ||
59 | return buffer; | |
60 | } | |
61 | ||
1d11d5bb | 62 | static void setup_archive_check(struct git_attr_check *check) |
008d896d RS |
63 | { |
64 | static struct git_attr *attr_export_ignore; | |
1d11d5bb | 65 | static struct git_attr *attr_export_subst; |
008d896d | 66 | |
1d11d5bb | 67 | if (!attr_export_ignore) { |
008d896d | 68 | attr_export_ignore = git_attr("export-ignore", 13); |
1d11d5bb RS |
69 | attr_export_subst = git_attr("export-subst", 12); |
70 | } | |
008d896d | 71 | check[0].attr = attr_export_ignore; |
1d11d5bb | 72 | check[1].attr = attr_export_subst; |
008d896d | 73 | } |
562e25ab RS |
74 | |
75 | struct archiver_context { | |
76 | struct archiver_args *args; | |
77 | write_archive_entry_fn_t write_entry; | |
78 | }; | |
79 | ||
80 | static int write_archive_entry(const unsigned char *sha1, const char *base, | |
81 | int baselen, const char *filename, unsigned mode, int stage, | |
82 | void *context) | |
83 | { | |
84 | static struct strbuf path = STRBUF_INIT; | |
85 | struct archiver_context *c = context; | |
86 | struct archiver_args *args = c->args; | |
87 | write_archive_entry_fn_t write_entry = c->write_entry; | |
1d11d5bb RS |
88 | struct git_attr_check check[2]; |
89 | const char *path_without_prefix; | |
90 | int convert = 0; | |
562e25ab RS |
91 | int err; |
92 | enum object_type type; | |
93 | unsigned long size; | |
94 | void *buffer; | |
95 | ||
96 | strbuf_reset(&path); | |
97 | strbuf_grow(&path, PATH_MAX); | |
98 | strbuf_add(&path, base, baselen); | |
99 | strbuf_addstr(&path, filename); | |
1d11d5bb | 100 | path_without_prefix = path.buf + args->baselen; |
562e25ab | 101 | |
1d11d5bb RS |
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 | } | |
562e25ab RS |
108 | |
109 | if (S_ISDIR(mode) || S_ISGITLINK(mode)) { | |
110 | strbuf_addch(&path, '/'); | |
111 | if (args->verbose) | |
112 | fprintf(stderr, "%.*s\n", (int)path.len, path.buf); | |
113 | err = write_entry(args, sha1, path.buf, path.len, mode, NULL, 0); | |
114 | if (err) | |
115 | return err; | |
116 | return READ_TREE_RECURSIVE; | |
117 | } | |
118 | ||
1d11d5bb RS |
119 | buffer = sha1_file_to_archive(path_without_prefix, sha1, mode, |
120 | &type, &size, convert ? args->commit : NULL); | |
562e25ab RS |
121 | if (!buffer) |
122 | return error("cannot read %s", sha1_to_hex(sha1)); | |
123 | if (args->verbose) | |
124 | fprintf(stderr, "%.*s\n", (int)path.len, path.buf); | |
125 | err = write_entry(args, sha1, path.buf, path.len, mode, buffer, size); | |
126 | free(buffer); | |
127 | return err; | |
128 | } | |
129 | ||
130 | int write_archive_entries(struct archiver_args *args, | |
131 | write_archive_entry_fn_t write_entry) | |
132 | { | |
133 | struct archiver_context context; | |
134 | int err; | |
135 | ||
136 | if (args->baselen > 0 && args->base[args->baselen - 1] == '/') { | |
137 | size_t len = args->baselen; | |
138 | ||
139 | while (len > 1 && args->base[len - 2] == '/') | |
140 | len--; | |
141 | if (args->verbose) | |
142 | fprintf(stderr, "%.*s\n", (int)len, args->base); | |
143 | err = write_entry(args, args->tree->object.sha1, args->base, | |
144 | len, 040777, NULL, 0); | |
145 | if (err) | |
146 | return err; | |
147 | } | |
148 | ||
149 | context.args = args; | |
150 | context.write_entry = write_entry; | |
151 | ||
152 | err = read_tree_recursive(args->tree, args->base, args->baselen, 0, | |
153 | args->pathspec, write_archive_entry, &context); | |
154 | if (err == READ_TREE_RECURSIVE) | |
155 | err = 0; | |
156 | return err; | |
157 | } | |
6e94e683 RS |
158 | |
159 | int write_archive(int argc, const char **argv, const char *prefix, | |
160 | int setup_prefix) | |
161 | { | |
162 | const struct archiver *ar = NULL; | |
163 | struct archiver_args args; | |
164 | int tree_idx; | |
165 | ||
166 | tree_idx = parse_archive_args(argc, argv, &ar, &args); | |
167 | if (setup_prefix && prefix == NULL) | |
168 | prefix = setup_git_directory(); | |
169 | ||
170 | argv += tree_idx; | |
171 | parse_treeish_arg(argv, &args, prefix); | |
172 | parse_pathspec_arg(argv + 1, &args); | |
173 | ||
174 | return ar->write_archive(&args); | |
175 | } |