Coverage Report

Created: 2024-09-08 06:23

/src/git/builtin/mktree.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * GIT - the stupid content tracker
3
 *
4
 * Copyright (c) Junio C Hamano, 2006, 2009
5
 */
6
#include "builtin.h"
7
#include "gettext.h"
8
#include "hex.h"
9
#include "quote.h"
10
#include "strbuf.h"
11
#include "tree.h"
12
#include "parse-options.h"
13
#include "object-store-ll.h"
14
15
static struct treeent {
16
  unsigned mode;
17
  struct object_id oid;
18
  int len;
19
  char name[FLEX_ARRAY];
20
} **entries;
21
static int alloc, used;
22
23
static void append_to_tree(unsigned mode, struct object_id *oid, char *path)
24
0
{
25
0
  struct treeent *ent;
26
0
  size_t len = strlen(path);
27
0
  if (strchr(path, '/'))
28
0
    die("path %s contains slash", path);
29
30
0
  FLEX_ALLOC_MEM(ent, name, path, len);
31
0
  ent->mode = mode;
32
0
  ent->len = len;
33
0
  oidcpy(&ent->oid, oid);
34
35
0
  ALLOC_GROW(entries, used + 1, alloc);
36
0
  entries[used++] = ent;
37
0
}
38
39
static int ent_compare(const void *a_, const void *b_)
40
0
{
41
0
  struct treeent *a = *(struct treeent **)a_;
42
0
  struct treeent *b = *(struct treeent **)b_;
43
0
  return base_name_compare(a->name, a->len, a->mode,
44
0
         b->name, b->len, b->mode);
45
0
}
46
47
static void write_tree(struct object_id *oid)
48
0
{
49
0
  struct strbuf buf;
50
0
  size_t size;
51
0
  int i;
52
53
0
  QSORT(entries, used, ent_compare);
54
0
  for (size = i = 0; i < used; i++)
55
0
    size += 32 + entries[i]->len;
56
57
0
  strbuf_init(&buf, size);
58
0
  for (i = 0; i < used; i++) {
59
0
    struct treeent *ent = entries[i];
60
0
    strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
61
0
    strbuf_add(&buf, ent->oid.hash, the_hash_algo->rawsz);
62
0
  }
63
64
0
  write_object_file(buf.buf, buf.len, OBJ_TREE, oid);
65
0
  strbuf_release(&buf);
66
0
}
67
68
static const char *mktree_usage[] = {
69
  "git mktree [-z] [--missing] [--batch]",
70
  NULL
71
};
72
73
static void mktree_line(char *buf, int nul_term_line, int allow_missing)
74
0
{
75
0
  char *ptr, *ntr;
76
0
  const char *p;
77
0
  unsigned mode;
78
0
  enum object_type mode_type; /* object type derived from mode */
79
0
  enum object_type obj_type; /* object type derived from sha */
80
0
  struct object_info oi = OBJECT_INFO_INIT;
81
0
  char *path, *to_free = NULL;
82
0
  struct object_id oid;
83
84
0
  ptr = buf;
85
  /*
86
   * Read non-recursive ls-tree output format:
87
   *     mode SP type SP sha1 TAB name
88
   */
89
0
  mode = strtoul(ptr, &ntr, 8);
90
0
  if (ptr == ntr || !ntr || *ntr != ' ')
91
0
    die("input format error: %s", buf);
92
0
  ptr = ntr + 1; /* type */
93
0
  ntr = strchr(ptr, ' ');
94
0
  if (!ntr || parse_oid_hex(ntr + 1, &oid, &p) ||
95
0
      *p != '\t')
96
0
    die("input format error: %s", buf);
97
98
  /* It is perfectly normal if we do not have a commit from a submodule */
99
0
  if (S_ISGITLINK(mode))
100
0
    allow_missing = 1;
101
102
103
0
  *ntr++ = 0; /* now at the beginning of SHA1 */
104
105
0
  path = (char *)p + 1;  /* at the beginning of name */
106
0
  if (!nul_term_line && path[0] == '"') {
107
0
    struct strbuf p_uq = STRBUF_INIT;
108
0
    if (unquote_c_style(&p_uq, path, NULL))
109
0
      die("invalid quoting");
110
0
    path = to_free = strbuf_detach(&p_uq, NULL);
111
0
  }
112
113
  /*
114
   * Object type is redundantly derivable three ways.
115
   * These should all agree.
116
   */
117
0
  mode_type = object_type(mode);
118
0
  if (mode_type != type_from_string(ptr)) {
119
0
    die("entry '%s' object type (%s) doesn't match mode type (%s)",
120
0
      path, ptr, type_name(mode_type));
121
0
  }
122
123
  /* Check the type of object identified by oid without fetching objects */
124
0
  oi.typep = &obj_type;
125
0
  if (oid_object_info_extended(the_repository, &oid, &oi,
126
0
             OBJECT_INFO_LOOKUP_REPLACE |
127
0
             OBJECT_INFO_QUICK |
128
0
             OBJECT_INFO_SKIP_FETCH_OBJECT) < 0)
129
0
    obj_type = -1;
130
131
0
  if (obj_type < 0) {
132
0
    if (allow_missing) {
133
0
      ; /* no problem - missing objects are presumed to be of the right type */
134
0
    } else {
135
0
      die("entry '%s' object %s is unavailable", path, oid_to_hex(&oid));
136
0
    }
137
0
  } else {
138
0
    if (obj_type != mode_type) {
139
      /*
140
       * The object exists but is of the wrong type.
141
       * This is a problem regardless of allow_missing
142
       * because the new tree entry will never be correct.
143
       */
144
0
      die("entry '%s' object %s is a %s but specified type was (%s)",
145
0
        path, oid_to_hex(&oid), type_name(obj_type), type_name(mode_type));
146
0
    }
147
0
  }
148
149
0
  append_to_tree(mode, &oid, path);
150
0
  free(to_free);
151
0
}
152
153
int cmd_mktree(int ac, const char **av, const char *prefix)
154
0
{
155
0
  struct strbuf sb = STRBUF_INIT;
156
0
  struct object_id oid;
157
0
  int nul_term_line = 0;
158
0
  int allow_missing = 0;
159
0
  int is_batch_mode = 0;
160
0
  int got_eof = 0;
161
0
  strbuf_getline_fn getline_fn;
162
163
0
  const struct option option[] = {
164
0
    OPT_BOOL('z', NULL, &nul_term_line, N_("input is NUL terminated")),
165
0
    OPT_SET_INT( 0 , "missing", &allow_missing, N_("allow missing objects"), 1),
166
0
    OPT_SET_INT( 0 , "batch", &is_batch_mode, N_("allow creation of more than one tree"), 1),
167
0
    OPT_END()
168
0
  };
169
170
0
  ac = parse_options(ac, av, prefix, option, mktree_usage, 0);
171
0
  getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
172
173
0
  while (!got_eof) {
174
0
    while (1) {
175
0
      if (getline_fn(&sb, stdin) == EOF) {
176
0
        got_eof = 1;
177
0
        break;
178
0
      }
179
0
      if (sb.buf[0] == '\0') {
180
        /* empty lines denote tree boundaries in batch mode */
181
0
        if (is_batch_mode)
182
0
          break;
183
0
        die("input format error: (blank line only valid in batch mode)");
184
0
      }
185
0
      mktree_line(sb.buf, nul_term_line, allow_missing);
186
0
    }
187
0
    if (is_batch_mode && got_eof && used < 1) {
188
      /*
189
       * Execution gets here if the last tree entry is terminated with a
190
       * new-line.  The final new-line has been made optional to be
191
       * consistent with the original non-batch behaviour of mktree.
192
       */
193
0
      ; /* skip creating an empty tree */
194
0
    } else {
195
0
      write_tree(&oid);
196
0
      puts(oid_to_hex(&oid));
197
0
      fflush(stdout);
198
0
    }
199
0
    used=0; /* reset tree entry buffer for re-use in batch mode */
200
0
  }
201
0
  strbuf_release(&sb);
202
0
  return 0;
203
0
}