Coverage Report

Created: 2024-09-08 06:23

/src/git/builtin/hash-object.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * GIT - The information manager from hell
3
 *
4
 * Copyright (C) Linus Torvalds, 2005
5
 * Copyright (C) Junio C Hamano, 2005
6
 */
7
#include "builtin.h"
8
#include "abspath.h"
9
#include "config.h"
10
#include "gettext.h"
11
#include "hex.h"
12
#include "object-file.h"
13
#include "object-store-ll.h"
14
#include "blob.h"
15
#include "quote.h"
16
#include "parse-options.h"
17
#include "setup.h"
18
#include "strbuf.h"
19
#include "write-or-die.h"
20
21
/*
22
 * This is to create corrupt objects for debugging and as such it
23
 * needs to bypass the data conversion performed by, and the type
24
 * limitation imposed by, index_fd() and its callees.
25
 */
26
static int hash_literally(struct object_id *oid, int fd, const char *type, unsigned flags)
27
0
{
28
0
  struct strbuf buf = STRBUF_INIT;
29
0
  int ret;
30
31
0
  if (strbuf_read(&buf, fd, 4096) < 0)
32
0
    ret = -1;
33
0
  else
34
0
    ret = write_object_file_literally(buf.buf, buf.len, type, oid,
35
0
             flags);
36
0
  close(fd);
37
0
  strbuf_release(&buf);
38
0
  return ret;
39
0
}
40
41
static void hash_fd(int fd, const char *type, const char *path, unsigned flags,
42
        int literally)
43
0
{
44
0
  struct stat st;
45
0
  struct object_id oid;
46
47
0
  if (fstat(fd, &st) < 0 ||
48
0
      (literally
49
0
       ? hash_literally(&oid, fd, type, flags)
50
0
       : index_fd(the_repository->index, &oid, fd, &st,
51
0
      type_from_string(type), path, flags)))
52
0
    die((flags & HASH_WRITE_OBJECT)
53
0
        ? "Unable to add %s to database"
54
0
        : "Unable to hash %s", path);
55
0
  printf("%s\n", oid_to_hex(&oid));
56
0
  maybe_flush_or_die(stdout, "hash to stdout");
57
0
}
58
59
static void hash_object(const char *path, const char *type, const char *vpath,
60
      unsigned flags, int literally)
61
0
{
62
0
  int fd;
63
0
  fd = xopen(path, O_RDONLY);
64
0
  hash_fd(fd, type, vpath, flags, literally);
65
0
}
66
67
static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
68
           int literally)
69
0
{
70
0
  struct strbuf buf = STRBUF_INIT;
71
0
  struct strbuf unquoted = STRBUF_INIT;
72
73
0
  while (strbuf_getline(&buf, stdin) != EOF) {
74
0
    if (buf.buf[0] == '"') {
75
0
      strbuf_reset(&unquoted);
76
0
      if (unquote_c_style(&unquoted, buf.buf, NULL))
77
0
        die("line is badly quoted");
78
0
      strbuf_swap(&buf, &unquoted);
79
0
    }
80
0
    hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags,
81
0
          literally);
82
0
  }
83
0
  strbuf_release(&buf);
84
0
  strbuf_release(&unquoted);
85
0
}
86
87
int cmd_hash_object(int argc, const char **argv, const char *prefix)
88
0
{
89
0
  static const char * const hash_object_usage[] = {
90
0
    N_("git hash-object [-t <type>] [-w] [--path=<file> | --no-filters]\n"
91
0
       "                [--stdin [--literally]] [--] <file>..."),
92
0
    N_("git hash-object [-t <type>] [-w] --stdin-paths [--no-filters]"),
93
0
    NULL
94
0
  };
95
0
  const char *type = blob_type;
96
0
  int hashstdin = 0;
97
0
  int stdin_paths = 0;
98
0
  int no_filters = 0;
99
0
  int literally = 0;
100
0
  int nongit = 0;
101
0
  unsigned flags = HASH_FORMAT_CHECK;
102
0
  const char *vpath = NULL;
103
0
  char *vpath_free = NULL;
104
0
  const struct option hash_object_options[] = {
105
0
    OPT_STRING('t', NULL, &type, N_("type"), N_("object type")),
106
0
    OPT_BIT('w', NULL, &flags, N_("write the object into the object database"),
107
0
      HASH_WRITE_OBJECT),
108
0
    OPT_COUNTUP( 0 , "stdin", &hashstdin, N_("read the object from stdin")),
109
0
    OPT_BOOL( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")),
110
0
    OPT_BOOL( 0 , "no-filters", &no_filters, N_("store file as is without filters")),
111
0
    OPT_BOOL( 0, "literally", &literally, N_("just hash any random garbage to create corrupt objects for debugging Git")),
112
0
    OPT_STRING( 0 , "path", &vpath, N_("file"), N_("process file as it were from this path")),
113
0
    OPT_END()
114
0
  };
115
0
  int i;
116
0
  const char *errstr = NULL;
117
118
0
  argc = parse_options(argc, argv, prefix, hash_object_options,
119
0
           hash_object_usage, 0);
120
121
0
  if (flags & HASH_WRITE_OBJECT)
122
0
    prefix = setup_git_directory();
123
0
  else
124
0
    prefix = setup_git_directory_gently(&nongit);
125
126
0
  if (nongit && !the_hash_algo)
127
0
    repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
128
129
0
  if (vpath && prefix) {
130
0
    vpath_free = prefix_filename(prefix, vpath);
131
0
    vpath = vpath_free;
132
0
  }
133
134
0
  git_config(git_default_config, NULL);
135
136
0
  if (stdin_paths) {
137
0
    if (hashstdin)
138
0
      errstr = "Can't use --stdin-paths with --stdin";
139
0
    else if (argc)
140
0
      errstr = "Can't specify files with --stdin-paths";
141
0
    else if (vpath)
142
0
      errstr = "Can't use --stdin-paths with --path";
143
0
  }
144
0
  else {
145
0
    if (hashstdin > 1)
146
0
      errstr = "Multiple --stdin arguments are not supported";
147
0
    if (vpath && no_filters)
148
0
      errstr = "Can't use --path with --no-filters";
149
0
  }
150
151
0
  if (errstr) {
152
0
    error("%s", errstr);
153
0
    usage_with_options(hash_object_usage, hash_object_options);
154
0
  }
155
156
0
  if (hashstdin)
157
0
    hash_fd(0, type, vpath, flags, literally);
158
159
0
  for (i = 0 ; i < argc; i++) {
160
0
    const char *arg = argv[i];
161
0
    char *to_free = NULL;
162
163
0
    if (prefix)
164
0
      arg = to_free = prefix_filename(prefix, arg);
165
0
    hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg,
166
0
          flags, literally);
167
0
    free(to_free);
168
0
  }
169
170
0
  if (stdin_paths)
171
0
    hash_stdin_paths(type, no_filters, flags, literally);
172
173
0
  free(vpath_free);
174
175
0
  return 0;
176
0
}