Petr Baudis | 92747a9 | 2005-05-09 21:33:02 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2005 Rene Scharfe |
| 3 | */ |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 4 | #include <time.h> |
| 5 | #include "cache.h" |
| 6 | |
| 7 | #define RECORDSIZE (512) |
| 8 | #define BLOCKSIZE (RECORDSIZE * 20) |
| 9 | |
Rene Scharfe | 03d791f | 2005-05-06 20:55:52 | [diff] [blame] | 10 | #define TYPEFLAG_AUTO '\0' |
| 11 | #define TYPEFLAG_REG '0' |
Rene Scharfe | d5776d5 | 2005-05-06 20:56:16 | [diff] [blame] | 12 | #define TYPEFLAG_LNK '2' |
Rene Scharfe | 03d791f | 2005-05-06 20:55:52 | [diff] [blame] | 13 | #define TYPEFLAG_DIR '5' |
| 14 | #define TYPEFLAG_GLOBAL_HEADER 'g' |
| 15 | #define TYPEFLAG_EXT_HEADER 'x' |
| 16 | |
Rene Scharfe | d5776d5 | 2005-05-06 20:56:16 | [diff] [blame] | 17 | #define EXT_HEADER_PATH 1 |
| 18 | #define EXT_HEADER_LINKPATH 2 |
| 19 | |
Petr Baudis | 4d1f119 | 2005-07-29 09:01:26 | [diff] [blame] | 20 | static const char tar_tree_usage[] = "git-tar-tree <key> [basedir]"; |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 21 | |
| 22 | static char block[BLOCKSIZE]; |
| 23 | static unsigned long offset; |
| 24 | |
| 25 | static const char *basedir; |
| 26 | static time_t archive_time; |
| 27 | |
| 28 | struct path_prefix { |
| 29 | struct path_prefix *prev; |
| 30 | const char *name; |
| 31 | }; |
| 32 | |
| 33 | /* tries hard to write, either succeeds or dies in the attempt */ |
| 34 | static void reliable_write(void *buf, unsigned long size) |
| 35 | { |
| 36 | while (size > 0) { |
| 37 | long ret = write(1, buf, size); |
| 38 | if (ret < 0) { |
| 39 | if (errno == EAGAIN) |
| 40 | continue; |
| 41 | if (errno == EPIPE) |
| 42 | exit(0); |
Alexey Nezhdanov | 667bb59 | 2005-05-19 11:17:16 | [diff] [blame] | 43 | die("git-tar-tree: %s", strerror(errno)); |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 44 | } else if (!ret) { |
Alexey Nezhdanov | 667bb59 | 2005-05-19 11:17:16 | [diff] [blame] | 45 | die("git-tar-tree: disk full?"); |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 46 | } |
| 47 | size -= ret; |
| 48 | buf += ret; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /* writes out the whole block, but only if it is full */ |
| 53 | static void write_if_needed(void) |
| 54 | { |
| 55 | if (offset == BLOCKSIZE) { |
| 56 | reliable_write(block, BLOCKSIZE); |
| 57 | offset = 0; |
| 58 | } |
| 59 | } |
| 60 | |
Rene Scharfe | a90a6e6 | 2005-05-06 20:55:42 | [diff] [blame] | 61 | /* acquire the next record from the buffer; user must call write_if_needed() */ |
| 62 | static char *get_record(void) |
| 63 | { |
| 64 | char *p = block + offset; |
| 65 | memset(p, 0, RECORDSIZE); |
| 66 | offset += RECORDSIZE; |
| 67 | return p; |
| 68 | } |
| 69 | |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 70 | /* |
| 71 | * The end of tar archives is marked by 1024 nul bytes and after that |
| 72 | * follows the rest of the block (if any). |
| 73 | */ |
| 74 | static void write_trailer(void) |
| 75 | { |
Rene Scharfe | 9b5b9f3 | 2005-06-02 18:50:48 | [diff] [blame] | 76 | get_record(); |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 77 | write_if_needed(); |
Rene Scharfe | 9b5b9f3 | 2005-06-02 18:50:48 | [diff] [blame] | 78 | get_record(); |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 79 | write_if_needed(); |
Rene Scharfe | a325a11 | 2005-06-03 11:25:18 | [diff] [blame] | 80 | while (offset) { |
Rene Scharfe | 9b5b9f3 | 2005-06-02 18:50:48 | [diff] [blame] | 81 | get_record(); |
| 82 | write_if_needed(); |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * queues up writes, so that all our write(2) calls write exactly one |
| 88 | * full block; pads writes to RECORDSIZE |
| 89 | */ |
| 90 | static void write_blocked(void *buf, unsigned long size) |
| 91 | { |
| 92 | unsigned long tail; |
| 93 | |
| 94 | if (offset) { |
| 95 | unsigned long chunk = BLOCKSIZE - offset; |
| 96 | if (size < chunk) |
| 97 | chunk = size; |
| 98 | memcpy(block + offset, buf, chunk); |
| 99 | size -= chunk; |
| 100 | offset += chunk; |
| 101 | buf += chunk; |
| 102 | write_if_needed(); |
| 103 | } |
| 104 | while (size >= BLOCKSIZE) { |
| 105 | reliable_write(buf, BLOCKSIZE); |
| 106 | size -= BLOCKSIZE; |
| 107 | buf += BLOCKSIZE; |
| 108 | } |
| 109 | if (size) { |
| 110 | memcpy(block + offset, buf, size); |
| 111 | buf += size; |
| 112 | offset += size; |
| 113 | } |
| 114 | tail = offset % RECORDSIZE; |
| 115 | if (tail) { |
| 116 | memset(block + offset, 0, RECORDSIZE - tail); |
| 117 | offset += RECORDSIZE - tail; |
| 118 | } |
| 119 | write_if_needed(); |
| 120 | } |
| 121 | |
| 122 | static void append_string(char **p, const char *s) |
| 123 | { |
| 124 | unsigned int len = strlen(s); |
| 125 | memcpy(*p, s, len); |
| 126 | *p += len; |
| 127 | } |
| 128 | |
| 129 | static void append_char(char **p, char c) |
| 130 | { |
| 131 | **p = c; |
| 132 | *p += 1; |
| 133 | } |
| 134 | |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 135 | static void append_path_prefix(char **buffer, struct path_prefix *prefix) |
| 136 | { |
| 137 | if (!prefix) |
| 138 | return; |
| 139 | append_path_prefix(buffer, prefix->prev); |
| 140 | append_string(buffer, prefix->name); |
| 141 | append_char(buffer, '/'); |
| 142 | } |
| 143 | |
| 144 | static unsigned int path_prefix_len(struct path_prefix *prefix) |
| 145 | { |
| 146 | if (!prefix) |
| 147 | return 0; |
| 148 | return path_prefix_len(prefix->prev) + strlen(prefix->name) + 1; |
| 149 | } |
| 150 | |
| 151 | static void append_path(char **p, int is_dir, const char *basepath, |
| 152 | struct path_prefix *prefix, const char *path) |
| 153 | { |
| 154 | if (basepath) { |
| 155 | append_string(p, basepath); |
| 156 | append_char(p, '/'); |
| 157 | } |
| 158 | append_path_prefix(p, prefix); |
| 159 | append_string(p, path); |
| 160 | if (is_dir) |
| 161 | append_char(p, '/'); |
| 162 | } |
| 163 | |
| 164 | static unsigned int path_len(int is_dir, const char *basepath, |
| 165 | struct path_prefix *prefix, const char *path) |
| 166 | { |
| 167 | unsigned int len = 0; |
| 168 | if (basepath) |
| 169 | len += strlen(basepath) + 1; |
| 170 | len += path_prefix_len(prefix) + strlen(path); |
| 171 | if (is_dir) |
| 172 | len++; |
| 173 | return len; |
| 174 | } |
| 175 | |
Rene Scharfe | 71058b1 | 2005-05-06 20:56:01 | [diff] [blame] | 176 | static void append_extended_header_prefix(char **p, unsigned int size, |
| 177 | const char *keyword) |
| 178 | { |
| 179 | int len = sprintf(*p, "%u %s=", size, keyword); |
| 180 | *p += len; |
| 181 | } |
| 182 | |
| 183 | static unsigned int extended_header_len(const char *keyword, |
| 184 | unsigned int valuelen) |
| 185 | { |
| 186 | /* "%u %s=%s\n" */ |
| 187 | unsigned int len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1; |
| 188 | if (len > 9) |
| 189 | len++; |
| 190 | if (len > 99) |
| 191 | len++; |
| 192 | return len; |
| 193 | } |
| 194 | |
| 195 | static void append_extended_header(char **p, const char *keyword, |
| 196 | const char *value, unsigned int len) |
| 197 | { |
| 198 | unsigned int size = extended_header_len(keyword, len); |
| 199 | append_extended_header_prefix(p, size, keyword); |
| 200 | memcpy(*p, value, len); |
| 201 | *p += len; |
| 202 | append_char(p, '\n'); |
| 203 | } |
| 204 | |
Brian Gerst | bf0f910 | 2005-05-18 12:14:09 | [diff] [blame] | 205 | static void write_header(const unsigned char *, char, const char *, struct path_prefix *, |
Rene Scharfe | 08f09da | 2005-05-06 20:56:08 | [diff] [blame] | 206 | const char *, unsigned int, void *, unsigned long); |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 207 | |
| 208 | /* stores a pax extended header directly in the block buffer */ |
| 209 | static void write_extended_header(const char *headerfilename, int is_dir, |
Rene Scharfe | 08f09da | 2005-05-06 20:56:08 | [diff] [blame] | 210 | unsigned int flags, const char *basepath, |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 211 | struct path_prefix *prefix, |
Rene Scharfe | 08f09da | 2005-05-06 20:56:08 | [diff] [blame] | 212 | const char *path, unsigned int namelen, |
| 213 | void *content, unsigned int contentsize) |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 214 | { |
Rene Scharfe | e7d3dd2 | 2005-05-07 00:57:06 | [diff] [blame] | 215 | char *buffer, *p; |
Rene Scharfe | d5776d5 | 2005-05-06 20:56:16 | [diff] [blame] | 216 | unsigned int pathlen, size, linkpathlen = 0; |
Rene Scharfe | 71058b1 | 2005-05-06 20:56:01 | [diff] [blame] | 217 | |
| 218 | size = pathlen = extended_header_len("path", namelen); |
Rene Scharfe | d5776d5 | 2005-05-06 20:56:16 | [diff] [blame] | 219 | if (flags & EXT_HEADER_LINKPATH) { |
| 220 | linkpathlen = extended_header_len("linkpath", contentsize); |
| 221 | size += linkpathlen; |
| 222 | } |
Rene Scharfe | 03d791f | 2005-05-06 20:55:52 | [diff] [blame] | 223 | write_header(NULL, TYPEFLAG_EXT_HEADER, NULL, NULL, headerfilename, |
Rene Scharfe | 08f09da | 2005-05-06 20:56:08 | [diff] [blame] | 224 | 0100600, NULL, size); |
Rene Scharfe | 71058b1 | 2005-05-06 20:56:01 | [diff] [blame] | 225 | |
Rene Scharfe | e7d3dd2 | 2005-05-07 00:57:06 | [diff] [blame] | 226 | buffer = p = malloc(size); |
| 227 | if (!buffer) |
| 228 | die("git-tar-tree: %s", strerror(errno)); |
Rene Scharfe | 71058b1 | 2005-05-06 20:56:01 | [diff] [blame] | 229 | append_extended_header_prefix(&p, pathlen, "path"); |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 230 | append_path(&p, is_dir, basepath, prefix, path); |
| 231 | append_char(&p, '\n'); |
Rene Scharfe | d5776d5 | 2005-05-06 20:56:16 | [diff] [blame] | 232 | if (flags & EXT_HEADER_LINKPATH) |
| 233 | append_extended_header(&p, "linkpath", content, contentsize); |
Rene Scharfe | e7d3dd2 | 2005-05-07 00:57:06 | [diff] [blame] | 234 | write_blocked(buffer, size); |
| 235 | free(buffer); |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 236 | } |
| 237 | |
Brian Gerst | bf0f910 | 2005-05-18 12:14:09 | [diff] [blame] | 238 | static void write_global_extended_header(const unsigned char *sha1) |
Rene Scharfe | 87fec8f | 2005-04-30 02:51:04 | [diff] [blame] | 239 | { |
| 240 | char *p; |
Rene Scharfe | 71058b1 | 2005-05-06 20:56:01 | [diff] [blame] | 241 | unsigned int size; |
| 242 | |
| 243 | size = extended_header_len("comment", 40); |
Rene Scharfe | 03d791f | 2005-05-06 20:55:52 | [diff] [blame] | 244 | write_header(NULL, TYPEFLAG_GLOBAL_HEADER, NULL, NULL, |
Rene Scharfe | 08f09da | 2005-05-06 20:56:08 | [diff] [blame] | 245 | "pax_global_header", 0100600, NULL, size); |
Rene Scharfe | 71058b1 | 2005-05-06 20:56:01 | [diff] [blame] | 246 | |
Rene Scharfe | a90a6e6 | 2005-05-06 20:55:42 | [diff] [blame] | 247 | p = get_record(); |
Rene Scharfe | 71058b1 | 2005-05-06 20:56:01 | [diff] [blame] | 248 | append_extended_header(&p, "comment", sha1_to_hex(sha1), 40); |
Rene Scharfe | 87fec8f | 2005-04-30 02:51:04 | [diff] [blame] | 249 | write_if_needed(); |
| 250 | } |
| 251 | |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 252 | /* stores a ustar header directly in the block buffer */ |
Brian Gerst | bf0f910 | 2005-05-18 12:14:09 | [diff] [blame] | 253 | static void write_header(const unsigned char *sha1, char typeflag, const char *basepath, |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 254 | struct path_prefix *prefix, const char *path, |
Rene Scharfe | 08f09da | 2005-05-06 20:56:08 | [diff] [blame] | 255 | unsigned int mode, void *buffer, unsigned long size) |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 256 | { |
| 257 | unsigned int namelen; |
Rene Scharfe | d5776d5 | 2005-05-06 20:56:16 | [diff] [blame] | 258 | char *header = NULL; |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 259 | unsigned int checksum = 0; |
| 260 | int i; |
Rene Scharfe | d5776d5 | 2005-05-06 20:56:16 | [diff] [blame] | 261 | unsigned int ext_header = 0; |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 262 | |
Rene Scharfe | 03d791f | 2005-05-06 20:55:52 | [diff] [blame] | 263 | if (typeflag == TYPEFLAG_AUTO) { |
| 264 | if (S_ISDIR(mode)) |
| 265 | typeflag = TYPEFLAG_DIR; |
Rene Scharfe | d5776d5 | 2005-05-06 20:56:16 | [diff] [blame] | 266 | else if (S_ISLNK(mode)) |
| 267 | typeflag = TYPEFLAG_LNK; |
Rene Scharfe | 03d791f | 2005-05-06 20:55:52 | [diff] [blame] | 268 | else |
| 269 | typeflag = TYPEFLAG_REG; |
| 270 | } |
| 271 | |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 272 | namelen = path_len(S_ISDIR(mode), basepath, prefix, path); |
Rene Scharfe | e7d3dd2 | 2005-05-07 00:57:06 | [diff] [blame] | 273 | if (namelen > 100) |
Rene Scharfe | d5776d5 | 2005-05-06 20:56:16 | [diff] [blame] | 274 | ext_header |= EXT_HEADER_PATH; |
| 275 | if (typeflag == TYPEFLAG_LNK && size > 100) |
| 276 | ext_header |= EXT_HEADER_LINKPATH; |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 277 | |
Rene Scharfe | d5776d5 | 2005-05-06 20:56:16 | [diff] [blame] | 278 | /* the extended header must be written before the normal one */ |
| 279 | if (ext_header) { |
| 280 | char headerfilename[51]; |
| 281 | sprintf(headerfilename, "%s.paxheader", sha1_to_hex(sha1)); |
| 282 | write_extended_header(headerfilename, S_ISDIR(mode), |
| 283 | ext_header, basepath, prefix, path, |
| 284 | namelen, buffer, size); |
| 285 | } |
| 286 | |
| 287 | header = get_record(); |
| 288 | |
| 289 | if (ext_header) { |
| 290 | sprintf(header, "%s.data", sha1_to_hex(sha1)); |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 291 | } else { |
Rene Scharfe | d5776d5 | 2005-05-06 20:56:16 | [diff] [blame] | 292 | char *p = header; |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 293 | append_path(&p, S_ISDIR(mode), basepath, prefix, path); |
| 294 | } |
| 295 | |
Rene Scharfe | d5776d5 | 2005-05-06 20:56:16 | [diff] [blame] | 296 | if (typeflag == TYPEFLAG_LNK) { |
| 297 | if (ext_header & EXT_HEADER_LINKPATH) { |
| 298 | sprintf(&header[157], "see %s.paxheader", |
| 299 | sha1_to_hex(sha1)); |
| 300 | } else { |
| 301 | if (buffer) |
| 302 | strncpy(&header[157], buffer, size); |
| 303 | } |
| 304 | } |
| 305 | |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 306 | if (S_ISDIR(mode)) |
| 307 | mode |= 0755; /* GIT doesn't store permissions of dirs */ |
Rene Scharfe | d5776d5 | 2005-05-06 20:56:16 | [diff] [blame] | 308 | if (S_ISLNK(mode)) |
| 309 | mode |= 0777; /* ... nor of symlinks */ |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 310 | sprintf(&header[100], "%07o", mode & 07777); |
| 311 | |
| 312 | /* XXX: should we provide more meaningful info here? */ |
| 313 | sprintf(&header[108], "%07o", 0); /* uid */ |
| 314 | sprintf(&header[116], "%07o", 0); /* gid */ |
| 315 | strncpy(&header[265], "git", 31); /* uname */ |
| 316 | strncpy(&header[297], "git", 31); /* gname */ |
| 317 | |
Rene Scharfe | d5776d5 | 2005-05-06 20:56:16 | [diff] [blame] | 318 | if (S_ISDIR(mode) || S_ISLNK(mode)) |
| 319 | size = 0; |
| 320 | sprintf(&header[124], "%011lo", size); |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 321 | sprintf(&header[136], "%011lo", archive_time); |
| 322 | |
Rene Scharfe | 87fec8f | 2005-04-30 02:51:04 | [diff] [blame] | 323 | header[156] = typeflag; |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 324 | |
| 325 | memcpy(&header[257], "ustar", 6); |
| 326 | memcpy(&header[263], "00", 2); |
| 327 | |
Timo Sirainen | 4ec99bf | 2005-08-09 15:30:22 | [diff] [blame] | 328 | sprintf(&header[329], "%07o", 0); /* devmajor */ |
| 329 | sprintf(&header[337], "%07o", 0); /* devminor */ |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 330 | |
| 331 | memset(&header[148], ' ', 8); |
| 332 | for (i = 0; i < RECORDSIZE; i++) |
| 333 | checksum += header[i]; |
| 334 | sprintf(&header[148], "%07o", checksum & 0x1fffff); |
| 335 | |
| 336 | write_if_needed(); |
| 337 | } |
| 338 | |
| 339 | static void traverse_tree(void *buffer, unsigned long size, |
| 340 | struct path_prefix *prefix) |
| 341 | { |
| 342 | struct path_prefix this_prefix; |
| 343 | this_prefix.prev = prefix; |
| 344 | |
| 345 | while (size) { |
| 346 | int namelen = strlen(buffer)+1; |
| 347 | void *eltbuf; |
| 348 | char elttype[20]; |
| 349 | unsigned long eltsize; |
| 350 | unsigned char *sha1 = buffer + namelen; |
| 351 | char *path = strchr(buffer, ' ') + 1; |
| 352 | unsigned int mode; |
| 353 | |
| 354 | if (size < namelen + 20 || sscanf(buffer, "%o", &mode) != 1) |
| 355 | die("corrupt 'tree' file"); |
Junio C Hamano | 38ec15a | 2005-10-01 19:01:07 | [diff] [blame] | 356 | if (S_ISDIR(mode) || S_ISREG(mode)) |
| 357 | mode |= (mode & 0100) ? 0777 : 0666; |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 358 | buffer = sha1 + 20; |
| 359 | size -= namelen + 20; |
| 360 | |
| 361 | eltbuf = read_sha1_file(sha1, elttype, &eltsize); |
| 362 | if (!eltbuf) |
| 363 | die("cannot read %s", sha1_to_hex(sha1)); |
Rene Scharfe | 03d791f | 2005-05-06 20:55:52 | [diff] [blame] | 364 | write_header(sha1, TYPEFLAG_AUTO, basedir, prefix, path, |
Rene Scharfe | 08f09da | 2005-05-06 20:56:08 | [diff] [blame] | 365 | mode, eltbuf, eltsize); |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 366 | if (!strcmp(elttype, "tree")) { |
| 367 | this_prefix.name = path; |
| 368 | traverse_tree(eltbuf, eltsize, &this_prefix); |
Rene Scharfe | d5776d5 | 2005-05-06 20:56:16 | [diff] [blame] | 369 | } else if (!strcmp(elttype, "blob") && !S_ISLNK(mode)) { |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 370 | write_blocked(eltbuf, eltsize); |
| 371 | } |
| 372 | free(eltbuf); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /* get commit time from committer line of commit object */ |
Linus Torvalds | e99d59f | 2005-05-20 18:46:10 | [diff] [blame] | 377 | static time_t commit_time(void * buffer, unsigned long size) |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 378 | { |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 379 | time_t result = 0; |
Rene Scharfe | db41347 | 2005-04-29 05:19:02 | [diff] [blame] | 380 | char *p = buffer; |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 381 | |
Rene Scharfe | db41347 | 2005-04-29 05:19:02 | [diff] [blame] | 382 | while (size > 0) { |
| 383 | char *endp = memchr(p, '\n', size); |
| 384 | if (!endp || endp == p) |
| 385 | break; |
| 386 | *endp = '\0'; |
| 387 | if (endp - p > 10 && !memcmp(p, "committer ", 10)) { |
| 388 | char *nump = strrchr(p, '>'); |
| 389 | if (!nump) |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 390 | break; |
Rene Scharfe | db41347 | 2005-04-29 05:19:02 | [diff] [blame] | 391 | nump++; |
| 392 | result = strtoul(nump, &endp, 10); |
| 393 | if (*endp != ' ') |
| 394 | result = 0; |
| 395 | break; |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 396 | } |
Rene Scharfe | db41347 | 2005-04-29 05:19:02 | [diff] [blame] | 397 | size -= endp - p - 1; |
| 398 | p = endp + 1; |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 399 | } |
| 400 | return result; |
| 401 | } |
| 402 | |
| 403 | int main(int argc, char **argv) |
| 404 | { |
| 405 | unsigned char sha1[20]; |
Rene Scharfe | 87fec8f | 2005-04-30 02:51:04 | [diff] [blame] | 406 | unsigned char commit_sha1[20]; |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 407 | void *buffer; |
| 408 | unsigned long size; |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 409 | |
| 410 | switch (argc) { |
| 411 | case 3: |
| 412 | basedir = argv[2]; |
| 413 | /* FALLTHROUGH */ |
| 414 | case 2: |
Linus Torvalds | 3c249c9 | 2005-05-01 23:36:56 | [diff] [blame] | 415 | if (get_sha1(argv[1], sha1) < 0) |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 416 | usage(tar_tree_usage); |
| 417 | break; |
| 418 | default: |
| 419 | usage(tar_tree_usage); |
| 420 | } |
| 421 | |
Rene Scharfe | 87fec8f | 2005-04-30 02:51:04 | [diff] [blame] | 422 | buffer = read_object_with_reference(sha1, "commit", &size, commit_sha1); |
Rene Scharfe | db41347 | 2005-04-29 05:19:02 | [diff] [blame] | 423 | if (buffer) { |
Rene Scharfe | 87fec8f | 2005-04-30 02:51:04 | [diff] [blame] | 424 | write_global_extended_header(commit_sha1); |
Rene Scharfe | db41347 | 2005-04-29 05:19:02 | [diff] [blame] | 425 | archive_time = commit_time(buffer, size); |
| 426 | free(buffer); |
| 427 | } |
| 428 | buffer = read_object_with_reference(sha1, "tree", &size, NULL); |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 429 | if (!buffer) |
Rene Scharfe | db41347 | 2005-04-29 05:19:02 | [diff] [blame] | 430 | die("not a reference to a tag, commit or tree object: %s", |
| 431 | sha1_to_hex(sha1)); |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 432 | if (!archive_time) |
| 433 | archive_time = time(NULL); |
| 434 | if (basedir) |
Mika Kukkonen | d565b34 | 2005-06-21 20:04:33 | [diff] [blame] | 435 | write_header((unsigned char *)"0", TYPEFLAG_DIR, NULL, NULL, |
| 436 | basedir, 040755, NULL, 0); |
Rene Scharfe | 731ab9c | 2005-04-28 19:16:43 | [diff] [blame] | 437 | traverse_tree(buffer, size, NULL); |
| 438 | free(buffer); |
| 439 | write_trailer(); |
| 440 | return 0; |
| 441 | } |