Coverage Report

Created: 2024-09-16 06:10

/src/git/builtin/get-tar-commit-id.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2005, 2006 Rene Scharfe
3
 */
4
#include "builtin.h"
5
#include "commit.h"
6
#include "tar.h"
7
8
static const char builtin_get_tar_commit_id_usage[] =
9
"git get-tar-commit-id";
10
11
/* ustar header + extended global header content */
12
0
#define RECORDSIZE  (512)
13
0
#define HEADERSIZE (2 * RECORDSIZE)
14
15
int cmd_get_tar_commit_id(int argc, const char **argv UNUSED, const char *prefix)
16
0
{
17
0
  char buffer[HEADERSIZE];
18
0
  struct ustar_header *header = (struct ustar_header *)buffer;
19
0
  char *content = buffer + RECORDSIZE;
20
0
  const char *comment;
21
0
  ssize_t n;
22
0
  long len;
23
0
  char *end;
24
25
0
  BUG_ON_NON_EMPTY_PREFIX(prefix);
26
27
0
  if (argc != 1)
28
0
    usage(builtin_get_tar_commit_id_usage);
29
30
0
  n = read_in_full(0, buffer, HEADERSIZE);
31
0
  if (n < 0)
32
0
    die_errno("git get-tar-commit-id: read error");
33
0
  if (n != HEADERSIZE)
34
0
    die_errno("git get-tar-commit-id: EOF before reading tar header");
35
0
  if (header->typeflag[0] != TYPEFLAG_GLOBAL_HEADER)
36
0
    return 1;
37
38
0
  errno = 0;
39
0
  len = strtol(content, &end, 10);
40
0
  if (errno == ERANGE || end == content || len < 0)
41
0
    return 1;
42
0
  if (!skip_prefix(end, " comment=", &comment))
43
0
    return 1;
44
0
  len -= comment - content;
45
0
  if (len < 1 || !(len % 2) ||
46
0
      hash_algo_by_length((len - 1) / 2) == GIT_HASH_UNKNOWN)
47
0
    return 1;
48
49
0
  if (write_in_full(1, comment, len) < 0)
50
0
    die_errno("git get-tar-commit-id: write error");
51
52
0
  return 0;
53
0
}