blob: 2706fcfaf2261e2ac2eaff5a054d0dd7a9291c0b [file] [log] [blame]
Petr Baudis92747a92005-05-09 23:33:02 +02001/*
Rene Scharfeae64bbc2006-03-25 23:21:03 +01002 * Copyright (c) 2005, 2006 Rene Scharfe
Petr Baudis92747a92005-05-09 23:33:02 +02003 */
Rene Scharfe731ab9c2005-04-28 12:16:43 -07004#include "cache.h"
Daniel Barkalowc3f92812006-01-29 14:05:20 -05005#include "commit.h"
Rene Scharfeae64bbc2006-03-25 23:21:03 +01006#include "tar.h"
Peter Eriksen56d13982006-05-23 14:15:31 +02007#include "builtin.h"
Junio C Hamanofd88d9c2006-09-24 14:42:01 -07008#include "quote.h"
Rene Scharfe731ab9c2005-04-28 12:16:43 -07009
Jonathan Niedere9dd0852009-11-09 09:04:50 -060010static const char builtin_get_tar_commit_id_usage[] =
Junio C Hamano33e8fc82015-10-16 11:27:42 -070011"git get-tar-commit-id";
Jonathan Niedere9dd0852009-11-09 09:04:50 -060012
Rene Scharfe52ba03c2006-06-10 16:13:41 +020013/* ustar header + extended global header content */
Junio C Hamanofd88d9c2006-09-24 14:42:01 -070014#define RECORDSIZE (512)
Rene Scharfe52ba03c2006-06-10 16:13:41 +020015#define HEADERSIZE (2 * RECORDSIZE)
16
Linus Torvaldsa633fca2006-07-28 22:44:25 -070017int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
Rene Scharfe52ba03c2006-06-10 16:13:41 +020018{
19 char buffer[HEADERSIZE];
20 struct ustar_header *header = (struct ustar_header *)buffer;
21 char *content = buffer + RECORDSIZE;
René Scharfee3f1da92014-10-04 20:54:50 +020022 const char *comment;
Rene Scharfe52ba03c2006-06-10 16:13:41 +020023 ssize_t n;
24
Jonathan Niedere9dd0852009-11-09 09:04:50 -060025 if (argc != 1)
26 usage(builtin_get_tar_commit_id_usage);
27
Andy Whitcroft93d26e42007-01-08 15:58:08 +000028 n = read_in_full(0, buffer, HEADERSIZE);
Jeff King41dcc4d2017-09-27 02:02:11 -040029 if (n < 0)
30 die_errno("git get-tar-commit-id: read error");
Jeff King61d36332017-09-27 02:00:28 -040031 if (n != HEADERSIZE)
Jeff King41dcc4d2017-09-27 02:02:11 -040032 die_errno("git get-tar-commit-id: EOF before reading tar header");
Rene Scharfe52ba03c2006-06-10 16:13:41 +020033 if (header->typeflag[0] != 'g')
34 return 1;
René Scharfee3f1da92014-10-04 20:54:50 +020035 if (!skip_prefix(content, "52 comment=", &comment))
Rene Scharfe52ba03c2006-06-10 16:13:41 +020036 return 1;
37
Jeff King68a423a2017-09-13 13:11:28 -040038 if (write_in_full(1, comment, 41) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +020039 die_errno("git get-tar-commit-id: write error");
Rene Scharfe52ba03c2006-06-10 16:13:41 +020040
41 return 0;
42}