2005-05-10 05:33:02 +08:00
|
|
|
/*
|
2006-03-26 06:21:03 +08:00
|
|
|
* Copyright (c) 2005, 2006 Rene Scharfe
|
2005-05-10 05:33:02 +08:00
|
|
|
*/
|
2023-05-16 14:33:57 +08:00
|
|
|
#include "builtin.h"
|
2006-01-30 03:05:20 +08:00
|
|
|
#include "commit.h"
|
2006-03-26 06:21:03 +08:00
|
|
|
#include "tar.h"
|
2005-04-29 03:16:43 +08:00
|
|
|
|
2009-11-09 23:04:50 +08:00
|
|
|
static const char builtin_get_tar_commit_id_usage[] =
|
usage: do not insist that standard input must come from a file
The synopsys text and the usage string of subcommands that read list
of things from the standard input are often shown like this:
git gostak [--distim] < <list-of-doshes>
This is problematic in a number of ways:
* The way to use these commands is more often to feed them the
output from another command, not feed them from a file.
* Manual pages outside Git, commands that operate on the data read
from the standard input, e.g "sort", "grep", "sed", etc., are not
described with such a "< redirection-from-file" in their synopsys
text. Our doing so introduces inconsistency.
* We do not insist on where the output should go, by saying
git gostak [--distim] < <list-of-doshes> > <output>
* As it is our convention to enclose placeholders inside <braket>,
the redirection operator followed by a placeholder filename
becomes very hard to read, both in the documentation and in the
help text.
Let's clean them all up, after making sure that the documentation
clearly describes the modes that take information from the standard
input and what kind of things are expected on the input.
[jc: stole example for fmt-merge-msg from Jonathan]
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-10-17 02:27:42 +08:00
|
|
|
"git get-tar-commit-id";
|
2009-11-09 23:04:50 +08:00
|
|
|
|
2006-06-10 22:13:41 +08:00
|
|
|
/* ustar header + extended global header content */
|
2006-09-25 05:42:01 +08:00
|
|
|
#define RECORDSIZE (512)
|
2006-06-10 22:13:41 +08:00
|
|
|
#define HEADERSIZE (2 * RECORDSIZE)
|
|
|
|
|
2024-09-14 05:16:14 +08:00
|
|
|
int cmd_get_tar_commit_id(int argc,
|
|
|
|
const char **argv UNUSED,
|
|
|
|
const char *prefix,
|
|
|
|
struct repository *repo UNUSED)
|
2006-06-10 22:13:41 +08:00
|
|
|
{
|
|
|
|
char buffer[HEADERSIZE];
|
|
|
|
struct ustar_header *header = (struct ustar_header *)buffer;
|
|
|
|
char *content = buffer + RECORDSIZE;
|
2014-10-05 02:54:50 +08:00
|
|
|
const char *comment;
|
2006-06-10 22:13:41 +08:00
|
|
|
ssize_t n;
|
2019-02-19 08:05:18 +08:00
|
|
|
long len;
|
|
|
|
char *end;
|
2006-06-10 22:13:41 +08:00
|
|
|
|
2023-03-29 04:55:17 +08:00
|
|
|
BUG_ON_NON_EMPTY_PREFIX(prefix);
|
|
|
|
|
2009-11-09 23:04:50 +08:00
|
|
|
if (argc != 1)
|
|
|
|
usage(builtin_get_tar_commit_id_usage);
|
|
|
|
|
2007-01-08 23:58:08 +08:00
|
|
|
n = read_in_full(0, buffer, HEADERSIZE);
|
2017-09-27 14:02:11 +08:00
|
|
|
if (n < 0)
|
|
|
|
die_errno("git get-tar-commit-id: read error");
|
2017-09-27 14:00:28 +08:00
|
|
|
if (n != HEADERSIZE)
|
2017-09-27 14:02:11 +08:00
|
|
|
die_errno("git get-tar-commit-id: EOF before reading tar header");
|
2023-04-08 18:25:04 +08:00
|
|
|
if (header->typeflag[0] != TYPEFLAG_GLOBAL_HEADER)
|
2006-06-10 22:13:41 +08:00
|
|
|
return 1;
|
2019-02-19 08:05:18 +08:00
|
|
|
|
2024-08-06 01:10:07 +08:00
|
|
|
errno = 0;
|
2019-02-19 08:05:18 +08:00
|
|
|
len = strtol(content, &end, 10);
|
|
|
|
if (errno == ERANGE || end == content || len < 0)
|
|
|
|
return 1;
|
|
|
|
if (!skip_prefix(end, " comment=", &comment))
|
|
|
|
return 1;
|
|
|
|
len -= comment - content;
|
2019-02-19 08:05:19 +08:00
|
|
|
if (len < 1 || !(len % 2) ||
|
|
|
|
hash_algo_by_length((len - 1) / 2) == GIT_HASH_UNKNOWN)
|
2006-06-10 22:13:41 +08:00
|
|
|
return 1;
|
|
|
|
|
2019-02-19 08:05:18 +08:00
|
|
|
if (write_in_full(1, comment, len) < 0)
|
2009-06-27 23:58:47 +08:00
|
|
|
die_errno("git get-tar-commit-id: write error");
|
2006-06-10 22:13:41 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|