]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-tar-tree.c
cvsexportcommit - add -a (add author line) flag, cleanup warnings
[thirdparty/git.git] / builtin-tar-tree.c
CommitLineData
92747a90 1/*
ae64bbc1 2 * Copyright (c) 2005, 2006 Rene Scharfe
92747a90 3 */
731ab9cc
RS
4#include <time.h>
5#include "cache.h"
1b0c7174 6#include "tree-walk.h"
c3f92812 7#include "commit.h"
ae64bbc1
RS
8#include "strbuf.h"
9#include "tar.h"
56d1398a 10#include "builtin.h"
21754264 11#include "pkt-line.h"
731ab9cc
RS
12
13#define RECORDSIZE (512)
14#define BLOCKSIZE (RECORDSIZE * 20)
15
21754264
JH
16static const char tar_tree_usage[] =
17"git-tar-tree [--remote=<repo>] <ent> [basedir]";
731ab9cc
RS
18
19static char block[BLOCKSIZE];
20static unsigned long offset;
21
731ab9cc
RS
22static time_t archive_time;
23
731ab9cc 24/* tries hard to write, either succeeds or dies in the attempt */
6698060c 25static void reliable_write(const void *data, unsigned long size)
731ab9cc 26{
6698060c
RS
27 const char *buf = data;
28
731ab9cc 29 while (size > 0) {
1c15afb9 30 long ret = xwrite(1, buf, size);
731ab9cc 31 if (ret < 0) {
731ab9cc
RS
32 if (errno == EPIPE)
33 exit(0);
667bb59b 34 die("git-tar-tree: %s", strerror(errno));
731ab9cc 35 } else if (!ret) {
667bb59b 36 die("git-tar-tree: disk full?");
731ab9cc
RS
37 }
38 size -= ret;
39 buf += ret;
40 }
41}
42
43/* writes out the whole block, but only if it is full */
44static void write_if_needed(void)
45{
46 if (offset == BLOCKSIZE) {
47 reliable_write(block, BLOCKSIZE);
48 offset = 0;
49 }
50}
51
731ab9cc
RS
52/*
53 * queues up writes, so that all our write(2) calls write exactly one
54 * full block; pads writes to RECORDSIZE
55 */
6698060c 56static void write_blocked(const void *data, unsigned long size)
731ab9cc 57{
6698060c 58 const char *buf = data;
731ab9cc
RS
59 unsigned long tail;
60
61 if (offset) {
62 unsigned long chunk = BLOCKSIZE - offset;
63 if (size < chunk)
64 chunk = size;
65 memcpy(block + offset, buf, chunk);
66 size -= chunk;
67 offset += chunk;
68 buf += chunk;
69 write_if_needed();
70 }
71 while (size >= BLOCKSIZE) {
72 reliable_write(buf, BLOCKSIZE);
73 size -= BLOCKSIZE;
74 buf += BLOCKSIZE;
75 }
76 if (size) {
77 memcpy(block + offset, buf, size);
731ab9cc
RS
78 offset += size;
79 }
80 tail = offset % RECORDSIZE;
81 if (tail) {
82 memset(block + offset, 0, RECORDSIZE - tail);
83 offset += RECORDSIZE - tail;
84 }
85 write_if_needed();
86}
87
37958be7
RS
88/*
89 * The end of tar archives is marked by 2*512 nul bytes and after that
90 * follows the rest of the block (if any).
91 */
92static void write_trailer(void)
93{
94 int tail = BLOCKSIZE - offset;
95 memset(block + offset, 0, tail);
96 reliable_write(block, BLOCKSIZE);
97 if (tail < 2 * RECORDSIZE) {
98 memset(block, 0, offset);
99 reliable_write(block, BLOCKSIZE);
100 }
101}
102
cb0c6df6 103static void strbuf_append_string(struct strbuf *sb, const char *s)
731ab9cc 104{
cb0c6df6
RS
105 int slen = strlen(s);
106 int total = sb->len + slen;
107 if (total > sb->alloc) {
108 sb->buf = xrealloc(sb->buf, total);
109 sb->alloc = total;
731ab9cc 110 }
cb0c6df6
RS
111 memcpy(sb->buf + sb->len, s, slen);
112 sb->len = total;
731ab9cc
RS
113}
114
ae64bbc1
RS
115/*
116 * pax extended header records have the format "%u %s=%s\n". %u contains
117 * the size of the whole string (including the %u), the first %s is the
118 * keyword, the second one is the value. This function constructs such a
119 * string and appends it to a struct strbuf.
120 */
121static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
122 const char *value, unsigned int valuelen)
71058b1f 123{
ae64bbc1
RS
124 char *p;
125 int len, total, tmp;
71058b1f 126
71058b1f 127 /* "%u %s=%s\n" */
ae64bbc1
RS
128 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
129 for (tmp = len; tmp > 9; tmp /= 10)
71058b1f 130 len++;
71058b1f 131
ae64bbc1
RS
132 total = sb->len + len;
133 if (total > sb->alloc) {
134 sb->buf = xrealloc(sb->buf, total);
135 sb->alloc = total;
136 }
71058b1f 137
ae64bbc1
RS
138 p = sb->buf;
139 p += sprintf(p, "%u %s=", len, keyword);
140 memcpy(p, value, valuelen);
141 p += valuelen;
142 *p = '\n';
143 sb->len = total;
144}
731ab9cc 145
ae64bbc1 146static unsigned int ustar_header_chksum(const struct ustar_header *header)
731ab9cc 147{
ae64bbc1
RS
148 char *p = (char *)header;
149 unsigned int chksum = 0;
150 while (p < header->chksum)
151 chksum += *p++;
152 chksum += sizeof(header->chksum) * ' ';
153 p += sizeof(header->chksum);
154 while (p < (char *)header + sizeof(struct ustar_header))
155 chksum += *p++;
156 return chksum;
731ab9cc
RS
157}
158
4c691724 159static int get_path_prefix(const struct strbuf *path, int maxlen)
87fec8fc 160{
4c691724
RS
161 int i = path->len;
162 if (i > maxlen)
163 i = maxlen;
17cf250a 164 do {
4c691724 165 i--;
17cf250a 166 } while (i > 0 && path->buf[i] != '/');
4c691724 167 return i;
87fec8fc
RS
168}
169
ae64bbc1
RS
170static void write_entry(const unsigned char *sha1, struct strbuf *path,
171 unsigned int mode, void *buffer, unsigned long size)
731ab9cc 172{
ae64bbc1
RS
173 struct ustar_header header;
174 struct strbuf ext_header;
175
176 memset(&header, 0, sizeof(header));
177 ext_header.buf = NULL;
178 ext_header.len = ext_header.alloc = 0;
179
180 if (!sha1) {
181 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
182 mode = 0100666;
183 strcpy(header.name, "pax_global_header");
184 } else if (!path) {
185 *header.typeflag = TYPEFLAG_EXT_HEADER;
186 mode = 0100666;
187 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
731ab9cc 188 } else {
ae64bbc1
RS
189 if (S_ISDIR(mode)) {
190 *header.typeflag = TYPEFLAG_DIR;
191 mode |= 0777;
192 } else if (S_ISLNK(mode)) {
193 *header.typeflag = TYPEFLAG_LNK;
194 mode |= 0777;
195 } else if (S_ISREG(mode)) {
196 *header.typeflag = TYPEFLAG_REG;
197 mode |= (mode & 0100) ? 0777 : 0666;
198 } else {
199 error("unsupported file mode: 0%o (SHA1: %s)",
200 mode, sha1_to_hex(sha1));
201 return;
202 }
203 if (path->len > sizeof(header.name)) {
4c691724
RS
204 int plen = get_path_prefix(path, sizeof(header.prefix));
205 int rest = path->len - plen - 1;
206 if (plen > 0 && rest <= sizeof(header.name)) {
207 memcpy(header.prefix, path->buf, plen);
208 memcpy(header.name, path->buf + plen + 1, rest);
209 } else {
210 sprintf(header.name, "%s.data",
211 sha1_to_hex(sha1));
212 strbuf_append_ext_header(&ext_header, "path",
213 path->buf, path->len);
214 }
ae64bbc1
RS
215 } else
216 memcpy(header.name, path->buf, path->len);
731ab9cc
RS
217 }
218
ae64bbc1
RS
219 if (S_ISLNK(mode) && buffer) {
220 if (size > sizeof(header.linkname)) {
221 sprintf(header.linkname, "see %s.paxheader",
d5776d50 222 sha1_to_hex(sha1));
ae64bbc1
RS
223 strbuf_append_ext_header(&ext_header, "linkpath",
224 buffer, size);
225 } else
226 memcpy(header.linkname, buffer, size);
d5776d50
RS
227 }
228
ae64bbc1
RS
229 sprintf(header.mode, "%07o", mode & 07777);
230 sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
231 sprintf(header.mtime, "%011lo", archive_time);
731ab9cc
RS
232
233 /* XXX: should we provide more meaningful info here? */
ae64bbc1
RS
234 sprintf(header.uid, "%07o", 0);
235 sprintf(header.gid, "%07o", 0);
817151e6
PE
236 strlcpy(header.uname, "git", sizeof(header.uname));
237 strlcpy(header.gname, "git", sizeof(header.gname));
ae64bbc1
RS
238 sprintf(header.devmajor, "%07o", 0);
239 sprintf(header.devminor, "%07o", 0);
731ab9cc 240
ae64bbc1
RS
241 memcpy(header.magic, "ustar", 6);
242 memcpy(header.version, "00", 2);
731ab9cc 243
ae64bbc1 244 sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
731ab9cc 245
ae64bbc1
RS
246 if (ext_header.len > 0) {
247 write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
248 free(ext_header.buf);
249 }
250 write_blocked(&header, sizeof(header));
251 if (S_ISREG(mode) && buffer && size > 0)
252 write_blocked(buffer, size);
253}
731ab9cc 254
bf0f910d 255static void write_global_extended_header(const unsigned char *sha1)
87fec8fc 256{
ae64bbc1
RS
257 struct strbuf ext_header;
258 ext_header.buf = NULL;
259 ext_header.len = ext_header.alloc = 0;
260 strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
261 write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len);
262 free(ext_header.buf);
731ab9cc
RS
263}
264
cb0c6df6 265static void traverse_tree(struct tree_desc *tree, struct strbuf *path)
731ab9cc 266{
cb0c6df6 267 int pathlen = path->len;
4c068a98 268 struct name_entry entry;
731ab9cc 269
4c068a98 270 while (tree_entry(tree, &entry)) {
731ab9cc
RS
271 void *eltbuf;
272 char elttype[20];
273 unsigned long eltsize;
5207234a 274
4c068a98 275 eltbuf = read_sha1_file(entry.sha1, elttype, &eltsize);
731ab9cc 276 if (!eltbuf)
4c068a98 277 die("cannot read %s", sha1_to_hex(entry.sha1));
cb0c6df6
RS
278
279 path->len = pathlen;
4c068a98
LT
280 strbuf_append_string(path, entry.path);
281 if (S_ISDIR(entry.mode))
cb0c6df6
RS
282 strbuf_append_string(path, "/");
283
4c068a98 284 write_entry(entry.sha1, path, entry.mode, eltbuf, eltsize);
cb0c6df6 285
4c068a98 286 if (S_ISDIR(entry.mode)) {
7ec57556
LT
287 struct tree_desc subtree;
288 subtree.buf = eltbuf;
289 subtree.size = eltsize;
cb0c6df6 290 traverse_tree(&subtree, path);
731ab9cc
RS
291 }
292 free(eltbuf);
293 }
294}
295
1af0d112 296static int generate_tar(int argc, const char **argv, char** envp)
731ab9cc 297{
2c6df2d5 298 unsigned char sha1[20], tree_sha1[20];
c3f92812 299 struct commit *commit;
7ec57556 300 struct tree_desc tree;
cb0c6df6
RS
301 struct strbuf current_path;
302
303 current_path.buf = xmalloc(PATH_MAX);
304 current_path.alloc = PATH_MAX;
305 current_path.len = current_path.eof = 0;
731ab9cc 306
53228a5f 307 setup_git_directory();
84a9b58c 308 git_config(git_default_config);
53228a5f 309
731ab9cc
RS
310 switch (argc) {
311 case 3:
cb0c6df6
RS
312 strbuf_append_string(&current_path, argv[2]);
313 strbuf_append_string(&current_path, "/");
731ab9cc
RS
314 /* FALLTHROUGH */
315 case 2:
31fff305
DL
316 if (get_sha1(argv[1], sha1))
317 die("Not a valid object name %s", argv[1]);
731ab9cc
RS
318 break;
319 default:
320 usage(tar_tree_usage);
321 }
322
473d404b 323 commit = lookup_commit_reference_gently(sha1, 1);
c3f92812
DB
324 if (commit) {
325 write_global_extended_header(commit->object.sha1);
326 archive_time = commit->date;
cb0c6df6
RS
327 } else
328 archive_time = time(NULL);
329
8e440259 330 tree.buf = read_object_with_reference(sha1, tree_type, &tree.size,
2c6df2d5 331 tree_sha1);
7ec57556 332 if (!tree.buf)
db413479
RS
333 die("not a reference to a tag, commit or tree object: %s",
334 sha1_to_hex(sha1));
cb0c6df6
RS
335
336 if (current_path.len > 0)
337 write_entry(tree_sha1, &current_path, 040777, NULL, 0);
338 traverse_tree(&tree, &current_path);
731ab9cc 339 write_trailer();
cb0c6df6 340 free(current_path.buf);
731ab9cc
RS
341 return 0;
342}
21754264
JH
343
344static const char *exec = "git-upload-tar";
345
346static int remote_tar(int argc, const char **argv)
347{
348 int fd[2], ret, len;
349 pid_t pid;
350 char buf[1024];
351 char *url;
352
353 if (argc < 3 || 4 < argc)
354 usage(tar_tree_usage);
355
356 /* --remote=<repo> */
357 url = strdup(argv[1]+9);
358 pid = git_connect(fd, url, exec);
359 if (pid < 0)
360 return 1;
361
362 packet_write(fd[1], "want %s\n", argv[2]);
363 if (argv[3])
364 packet_write(fd[1], "base %s\n", argv[3]);
365 packet_flush(fd[1]);
366
367 len = packet_read_line(fd[0], buf, sizeof(buf));
368 if (!len)
369 die("git-tar-tree: expected ACK/NAK, got EOF");
370 if (buf[len-1] == '\n')
371 buf[--len] = 0;
372 if (strcmp(buf, "ACK")) {
373 if (5 < len && !strncmp(buf, "NACK ", 5))
374 die("git-tar-tree: NACK %s", buf + 5);
375 die("git-tar-tree: protocol error");
376 }
377 /* expect a flush */
378 len = packet_read_line(fd[0], buf, sizeof(buf));
379 if (len)
380 die("git-tar-tree: expected a flush");
381
382 /* Now, start reading from fd[0] and spit it out to stdout */
383 ret = copy_fd(fd[0], 1);
384 close(fd[0]);
385
386 ret |= finish_connect(pid);
387 return !!ret;
388}
389
390int cmd_tar_tree(int argc, const char **argv, char **envp)
391{
392 if (argc < 2)
393 usage(tar_tree_usage);
394 if (!strncmp("--remote=", argv[1], 9))
395 return remote_tar(argc, argv);
1af0d112 396 return generate_tar(argc, argv, envp);
21754264 397}
52ba03cb
RS
398
399/* ustar header + extended global header content */
400#define HEADERSIZE (2 * RECORDSIZE)
401
402int cmd_get_tar_commit_id(int argc, const char **argv, char **envp)
403{
404 char buffer[HEADERSIZE];
405 struct ustar_header *header = (struct ustar_header *)buffer;
406 char *content = buffer + RECORDSIZE;
407 ssize_t n;
408
409 n = xread(0, buffer, HEADERSIZE);
410 if (n < HEADERSIZE)
411 die("git-get-tar-commit-id: read error");
412 if (header->typeflag[0] != 'g')
413 return 1;
414 if (memcmp(content, "52 comment=", 11))
415 return 1;
416
417 n = xwrite(1, content + 11, 41);
418 if (n < 41)
419 die("git-get-tar-commit-id: write error");
420
421 return 0;
422}