Petr Baudis | 92747a9 | 2005-05-09 23:33:02 +0200 | [diff] [blame] | 1 | /* |
Rene Scharfe | ae64bbc | 2006-03-25 23:21:03 +0100 | [diff] [blame] | 2 | * Copyright (c) 2005, 2006 Rene Scharfe |
Petr Baudis | 92747a9 | 2005-05-09 23:33:02 +0200 | [diff] [blame] | 3 | */ |
Rene Scharfe | 731ab9c | 2005-04-28 12:16:43 -0700 | [diff] [blame] | 4 | #include "cache.h" |
Daniel Barkalow | c3f9281 | 2006-01-29 14:05:20 -0500 | [diff] [blame] | 5 | #include "commit.h" |
Rene Scharfe | ae64bbc | 2006-03-25 23:21:03 +0100 | [diff] [blame] | 6 | #include "tar.h" |
Peter Eriksen | 56d1398 | 2006-05-23 14:15:31 +0200 | [diff] [blame] | 7 | #include "builtin.h" |
Junio C Hamano | fd88d9c | 2006-09-24 14:42:01 -0700 | [diff] [blame] | 8 | #include "quote.h" |
Rene Scharfe | 731ab9c | 2005-04-28 12:16:43 -0700 | [diff] [blame] | 9 | |
Jonathan Nieder | e9dd085 | 2009-11-09 09:04:50 -0600 | [diff] [blame] | 10 | static const char builtin_get_tar_commit_id_usage[] = |
Junio C Hamano | 33e8fc8 | 2015-10-16 11:27:42 -0700 | [diff] [blame] | 11 | "git get-tar-commit-id"; |
Jonathan Nieder | e9dd085 | 2009-11-09 09:04:50 -0600 | [diff] [blame] | 12 | |
Rene Scharfe | 52ba03c | 2006-06-10 16:13:41 +0200 | [diff] [blame] | 13 | /* ustar header + extended global header content */ |
Junio C Hamano | fd88d9c | 2006-09-24 14:42:01 -0700 | [diff] [blame] | 14 | #define RECORDSIZE (512) |
Rene Scharfe | 52ba03c | 2006-06-10 16:13:41 +0200 | [diff] [blame] | 15 | #define HEADERSIZE (2 * RECORDSIZE) |
| 16 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 17 | int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix) |
Rene Scharfe | 52ba03c | 2006-06-10 16:13:41 +0200 | [diff] [blame] | 18 | { |
| 19 | char buffer[HEADERSIZE]; |
| 20 | struct ustar_header *header = (struct ustar_header *)buffer; |
| 21 | char *content = buffer + RECORDSIZE; |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 22 | const char *comment; |
Rene Scharfe | 52ba03c | 2006-06-10 16:13:41 +0200 | [diff] [blame] | 23 | ssize_t n; |
| 24 | |
Jonathan Nieder | e9dd085 | 2009-11-09 09:04:50 -0600 | [diff] [blame] | 25 | if (argc != 1) |
| 26 | usage(builtin_get_tar_commit_id_usage); |
| 27 | |
Andy Whitcroft | 93d26e4 | 2007-01-08 15:58:08 +0000 | [diff] [blame] | 28 | n = read_in_full(0, buffer, HEADERSIZE); |
Jeff King | 41dcc4d | 2017-09-27 02:02:11 -0400 | [diff] [blame] | 29 | if (n < 0) |
| 30 | die_errno("git get-tar-commit-id: read error"); |
Jeff King | 61d3633 | 2017-09-27 02:00:28 -0400 | [diff] [blame] | 31 | if (n != HEADERSIZE) |
Jeff King | 41dcc4d | 2017-09-27 02:02:11 -0400 | [diff] [blame] | 32 | die_errno("git get-tar-commit-id: EOF before reading tar header"); |
Rene Scharfe | 52ba03c | 2006-06-10 16:13:41 +0200 | [diff] [blame] | 33 | if (header->typeflag[0] != 'g') |
| 34 | return 1; |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 35 | if (!skip_prefix(content, "52 comment=", &comment)) |
Rene Scharfe | 52ba03c | 2006-06-10 16:13:41 +0200 | [diff] [blame] | 36 | return 1; |
| 37 | |
Jeff King | 68a423a | 2017-09-13 13:11:28 -0400 | [diff] [blame] | 38 | if (write_in_full(1, comment, 41) < 0) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 39 | die_errno("git get-tar-commit-id: write error"); |
Rene Scharfe | 52ba03c | 2006-06-10 16:13:41 +0200 | [diff] [blame] | 40 | |
| 41 | return 0; |
| 42 | } |