Coverage Report

Created: 2024-09-08 06:23

/src/git/builtin/fetch-pack.c
Line
Count
Source (jump to first uncovered line)
1
#include "builtin.h"
2
#include "gettext.h"
3
#include "hex.h"
4
#include "object-file.h"
5
#include "pkt-line.h"
6
#include "fetch-pack.h"
7
#include "remote.h"
8
#include "connect.h"
9
#include "oid-array.h"
10
#include "protocol.h"
11
12
static const char fetch_pack_usage[] =
13
"git fetch-pack [--all] [--stdin] [--quiet | -q] [--keep | -k] [--thin] "
14
"[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
15
"[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]";
16
17
static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
18
           const char *name)
19
0
{
20
0
  struct ref *ref;
21
0
  struct object_id oid;
22
0
  const char *p;
23
24
0
  if (!parse_oid_hex(name, &oid, &p)) {
25
0
    if (*p == ' ') {
26
      /* <oid> <ref>, find refname */
27
0
      name = p + 1;
28
0
    } else if (*p == '\0') {
29
0
      ; /* <oid>, leave oid as name */
30
0
    } else {
31
      /* <ref>, clear cruft from oid */
32
0
      oidclr(&oid, the_repository->hash_algo);
33
0
    }
34
0
  } else {
35
    /* <ref>, clear cruft from get_oid_hex */
36
0
    oidclr(&oid, the_repository->hash_algo);
37
0
  }
38
39
0
  ref = alloc_ref(name);
40
0
  oidcpy(&ref->old_oid, &oid);
41
0
  (*nr)++;
42
0
  ALLOC_GROW(*sought, *nr, *alloc);
43
0
  (*sought)[*nr - 1] = ref;
44
0
}
45
46
int cmd_fetch_pack(int argc, const char **argv, const char *prefix UNUSED)
47
0
{
48
0
  int i, ret;
49
0
  struct ref *fetched_refs = NULL, *remote_refs = NULL;
50
0
  const char *dest = NULL;
51
0
  struct ref **sought = NULL;
52
0
  int nr_sought = 0, alloc_sought = 0;
53
0
  int fd[2];
54
0
  struct string_list pack_lockfiles = STRING_LIST_INIT_DUP;
55
0
  struct string_list *pack_lockfiles_ptr = NULL;
56
0
  struct child_process *conn;
57
0
  struct fetch_pack_args args;
58
0
  struct oid_array shallow = OID_ARRAY_INIT;
59
0
  struct string_list deepen_not = STRING_LIST_INIT_DUP;
60
0
  struct packet_reader reader;
61
0
  enum protocol_version version;
62
63
0
  fetch_if_missing = 0;
64
65
0
  packet_trace_identity("fetch-pack");
66
67
0
  memset(&args, 0, sizeof(args));
68
0
  list_objects_filter_init(&args.filter_options);
69
0
  args.uploadpack = "git-upload-pack";
70
71
0
  for (i = 1; i < argc && *argv[i] == '-'; i++) {
72
0
    const char *arg = argv[i];
73
74
0
    if (skip_prefix(arg, "--upload-pack=", &arg)) {
75
0
      args.uploadpack = arg;
76
0
      continue;
77
0
    }
78
0
    if (skip_prefix(arg, "--exec=", &arg)) {
79
0
      args.uploadpack = arg;
80
0
      continue;
81
0
    }
82
0
    if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
83
0
      args.quiet = 1;
84
0
      continue;
85
0
    }
86
0
    if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
87
0
      args.lock_pack = args.keep_pack;
88
0
      args.keep_pack = 1;
89
0
      continue;
90
0
    }
91
0
    if (!strcmp("--thin", arg)) {
92
0
      args.use_thin_pack = 1;
93
0
      continue;
94
0
    }
95
0
    if (!strcmp("--include-tag", arg)) {
96
0
      args.include_tag = 1;
97
0
      continue;
98
0
    }
99
0
    if (!strcmp("--all", arg)) {
100
0
      args.fetch_all = 1;
101
0
      continue;
102
0
    }
103
0
    if (!strcmp("--stdin", arg)) {
104
0
      args.stdin_refs = 1;
105
0
      continue;
106
0
    }
107
0
    if (!strcmp("--diag-url", arg)) {
108
0
      args.diag_url = 1;
109
0
      continue;
110
0
    }
111
0
    if (!strcmp("-v", arg)) {
112
0
      args.verbose = 1;
113
0
      continue;
114
0
    }
115
0
    if (skip_prefix(arg, "--depth=", &arg)) {
116
0
      args.depth = strtol(arg, NULL, 0);
117
0
      continue;
118
0
    }
119
0
    if (skip_prefix(arg, "--shallow-since=", &arg)) {
120
0
      args.deepen_since = xstrdup(arg);
121
0
      continue;
122
0
    }
123
0
    if (skip_prefix(arg, "--shallow-exclude=", &arg)) {
124
0
      string_list_append(&deepen_not, arg);
125
0
      continue;
126
0
    }
127
0
    if (!strcmp(arg, "--deepen-relative")) {
128
0
      args.deepen_relative = 1;
129
0
      continue;
130
0
    }
131
0
    if (!strcmp("--no-progress", arg)) {
132
0
      args.no_progress = 1;
133
0
      continue;
134
0
    }
135
0
    if (!strcmp("--stateless-rpc", arg)) {
136
0
      args.stateless_rpc = 1;
137
0
      continue;
138
0
    }
139
0
    if (!strcmp("--lock-pack", arg)) {
140
0
      args.lock_pack = 1;
141
0
      pack_lockfiles_ptr = &pack_lockfiles;
142
0
      continue;
143
0
    }
144
0
    if (!strcmp("--check-self-contained-and-connected", arg)) {
145
0
      args.check_self_contained_and_connected = 1;
146
0
      continue;
147
0
    }
148
0
    if (!strcmp("--cloning", arg)) {
149
0
      args.cloning = 1;
150
0
      continue;
151
0
    }
152
0
    if (!strcmp("--update-shallow", arg)) {
153
0
      args.update_shallow = 1;
154
0
      continue;
155
0
    }
156
0
    if (!strcmp("--from-promisor", arg)) {
157
0
      args.from_promisor = 1;
158
0
      continue;
159
0
    }
160
0
    if (!strcmp("--refetch", arg)) {
161
0
      args.refetch = 1;
162
0
      continue;
163
0
    }
164
0
    if (skip_prefix(arg, ("--filter="), &arg)) {
165
0
      parse_list_objects_filter(&args.filter_options, arg);
166
0
      continue;
167
0
    }
168
0
    if (!strcmp(arg, ("--no-filter"))) {
169
0
      list_objects_filter_set_no_filter(&args.filter_options);
170
0
      continue;
171
0
    }
172
0
    usage(fetch_pack_usage);
173
0
  }
174
0
  if (deepen_not.nr)
175
0
    args.deepen_not = &deepen_not;
176
177
0
  if (i < argc)
178
0
    dest = argv[i++];
179
0
  else
180
0
    usage(fetch_pack_usage);
181
182
  /*
183
   * Copy refs from cmdline to growable list, then append any
184
   * refs from the standard input:
185
   */
186
0
  for (; i < argc; i++)
187
0
    add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
188
0
  if (args.stdin_refs) {
189
0
    if (args.stateless_rpc) {
190
      /* in stateless RPC mode we use pkt-line to read
191
       * from stdin, until we get a flush packet
192
       */
193
0
      for (;;) {
194
0
        char *line = packet_read_line(0, NULL);
195
0
        if (!line)
196
0
          break;
197
0
        add_sought_entry(&sought, &nr_sought,  &alloc_sought, line);
198
0
      }
199
0
    }
200
0
    else {
201
      /* read from stdin one ref per line, until EOF */
202
0
      struct strbuf line = STRBUF_INIT;
203
0
      while (strbuf_getline_lf(&line, stdin) != EOF)
204
0
        add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
205
0
      strbuf_release(&line);
206
0
    }
207
0
  }
208
209
0
  if (args.stateless_rpc) {
210
0
    conn = NULL;
211
0
    fd[0] = 0;
212
0
    fd[1] = 1;
213
0
  } else {
214
0
    int flags = args.verbose ? CONNECT_VERBOSE : 0;
215
0
    if (args.diag_url)
216
0
      flags |= CONNECT_DIAG_URL;
217
0
    conn = git_connect(fd, dest, "git-upload-pack",
218
0
           args.uploadpack, flags);
219
0
    if (!conn)
220
0
      return args.diag_url ? 0 : 1;
221
0
  }
222
223
0
  packet_reader_init(&reader, fd[0], NULL, 0,
224
0
         PACKET_READ_CHOMP_NEWLINE |
225
0
         PACKET_READ_GENTLE_ON_EOF |
226
0
         PACKET_READ_DIE_ON_ERR_PACKET);
227
228
0
  version = discover_version(&reader);
229
0
  switch (version) {
230
0
  case protocol_v2:
231
0
    get_remote_refs(fd[1], &reader, &remote_refs, 0, NULL, NULL,
232
0
        args.stateless_rpc);
233
0
    break;
234
0
  case protocol_v1:
235
0
  case protocol_v0:
236
0
    get_remote_heads(&reader, &remote_refs, 0, NULL, &shallow);
237
0
    break;
238
0
  case protocol_unknown_version:
239
0
    BUG("unknown protocol version");
240
0
  }
241
242
0
  fetched_refs = fetch_pack(&args, fd, remote_refs, sought, nr_sought,
243
0
       &shallow, pack_lockfiles_ptr, version);
244
245
0
  if (pack_lockfiles.nr) {
246
0
    int i;
247
248
0
    printf("lock %s\n", pack_lockfiles.items[0].string);
249
0
    fflush(stdout);
250
0
    for (i = 1; i < pack_lockfiles.nr; i++)
251
0
      warning(_("Lockfile created but not reported: %s"),
252
0
        pack_lockfiles.items[i].string);
253
0
  }
254
0
  if (args.check_self_contained_and_connected &&
255
0
      args.self_contained_and_connected) {
256
0
    printf("connectivity-ok\n");
257
0
    fflush(stdout);
258
0
  }
259
0
  close(fd[0]);
260
0
  close(fd[1]);
261
0
  if (finish_connect(conn))
262
0
    return 1;
263
264
0
  ret = !fetched_refs;
265
266
  /*
267
   * If the heads to pull were given, we should have consumed
268
   * all of them by matching the remote.  Otherwise, 'git fetch
269
   * remote no-such-ref' would silently succeed without issuing
270
   * an error.
271
   */
272
0
  ret |= report_unmatched_refs(sought, nr_sought);
273
274
0
  for (struct ref *ref = fetched_refs; ref; ref = ref->next)
275
0
    printf("%s %s\n",
276
0
           oid_to_hex(&ref->old_oid), ref->name);
277
278
0
  for (size_t i = 0; i < nr_sought; i++)
279
0
    free_one_ref(sought[i]);
280
0
  free(sought);
281
0
  free_refs(fetched_refs);
282
0
  free_refs(remote_refs);
283
0
  return ret;
284
0
}