Coverage Report

Created: 2026-03-31 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/connected.c
Line
Count
Source
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "gettext.h"
5
#include "hex.h"
6
#include "odb.h"
7
#include "run-command.h"
8
#include "sigchain.h"
9
#include "connected.h"
10
#include "transport.h"
11
#include "packfile.h"
12
#include "promisor-remote.h"
13
14
/*
15
 * If we feed all the commits we want to verify to this command
16
 *
17
 *  $ git rev-list --objects --stdin --not --all
18
 *
19
 * and if it does not error out, that means everything reachable from
20
 * these commits locally exists and is connected to our existing refs.
21
 * Note that this does _not_ validate the individual objects.
22
 *
23
 * Returns 0 if everything is connected, non-zero otherwise.
24
 */
25
int check_connected(oid_iterate_fn fn, void *cb_data,
26
        struct check_connected_options *opt)
27
0
{
28
0
  struct child_process rev_list = CHILD_PROCESS_INIT;
29
0
  FILE *rev_list_in;
30
0
  struct check_connected_options defaults = CHECK_CONNECTED_INIT;
31
0
  const struct object_id *oid;
32
0
  int err = 0;
33
0
  struct packed_git *new_pack = NULL;
34
0
  struct transport *transport;
35
0
  size_t base_len;
36
37
0
  if (!opt)
38
0
    opt = &defaults;
39
0
  transport = opt->transport;
40
41
0
  oid = fn(cb_data);
42
0
  if (!oid) {
43
0
    if (opt->err_fd)
44
0
      close(opt->err_fd);
45
0
    return err;
46
0
  }
47
48
0
  if (repo_has_promisor_remote(the_repository)) {
49
    /*
50
     * For partial clones, we don't want to have to do a regular
51
     * connectivity check because we have to enumerate and exclude
52
     * all promisor objects (slow), and then the connectivity check
53
     * itself becomes a no-op because in a partial clone every
54
     * object is a promisor object. Instead, just make sure we
55
     * received, in a promisor packfile, the objects pointed to by
56
     * each wanted ref.
57
     *
58
     * Before checking for promisor packs, be sure we have the
59
     * latest pack-files loaded into memory.
60
     */
61
0
    odb_reprepare(the_repository->objects);
62
0
    do {
63
0
      struct packed_git *p;
64
65
0
      repo_for_each_pack(the_repository, p) {
66
0
        if (!p->pack_promisor)
67
0
          continue;
68
0
        if (find_pack_entry_one(oid, p))
69
0
          goto promisor_pack_found;
70
0
      }
71
      /*
72
       * Fallback to rev-list with oid and the rest of the
73
       * object IDs provided by fn.
74
       */
75
0
      goto no_promisor_pack_found;
76
0
promisor_pack_found:
77
0
      ;
78
0
    } while ((oid = fn(cb_data)) != NULL);
79
0
    return 0;
80
0
  }
81
82
0
no_promisor_pack_found:
83
0
  if (opt->shallow_file) {
84
0
    strvec_push(&rev_list.args, "--shallow-file");
85
0
    strvec_push(&rev_list.args, opt->shallow_file);
86
0
  }
87
0
  strvec_push(&rev_list.args,"rev-list");
88
0
  strvec_push(&rev_list.args, "--objects");
89
0
  strvec_push(&rev_list.args, "--stdin");
90
0
  if (repo_has_promisor_remote(the_repository))
91
0
    strvec_push(&rev_list.args, "--exclude-promisor-objects");
92
0
  if (!opt->is_deepening_fetch) {
93
0
    strvec_push(&rev_list.args, "--not");
94
0
    if (opt->exclude_hidden_refs_section)
95
0
      strvec_pushf(&rev_list.args, "--exclude-hidden=%s",
96
0
             opt->exclude_hidden_refs_section);
97
0
    strvec_push(&rev_list.args, "--all");
98
0
  }
99
0
  strvec_push(&rev_list.args, "--quiet");
100
0
  strvec_push(&rev_list.args, "--alternate-refs");
101
0
  if (opt->progress)
102
0
    strvec_pushf(&rev_list.args, "--progress=%s",
103
0
           _("Checking connectivity"));
104
105
0
  rev_list.git_cmd = 1;
106
0
  if (opt->env)
107
0
    strvec_pushv(&rev_list.env, opt->env);
108
0
  rev_list.in = -1;
109
0
  rev_list.no_stdout = 1;
110
0
  if (opt->err_fd)
111
0
    rev_list.err = opt->err_fd;
112
0
  else
113
0
    rev_list.no_stderr = opt->quiet;
114
115
0
  if (start_command(&rev_list))
116
0
    return error(_("Could not run 'git rev-list'"));
117
118
0
  sigchain_push(SIGPIPE, SIG_IGN);
119
120
0
  rev_list_in = xfdopen(rev_list.in, "w");
121
122
0
  if (transport && transport->smart_options &&
123
0
      transport->smart_options->self_contained_and_connected &&
124
0
      transport->pack_lockfiles.nr == 1 &&
125
0
      strip_suffix(transport->pack_lockfiles.items[0].string,
126
0
       ".keep", &base_len)) {
127
0
    struct strbuf idx_file = STRBUF_INIT;
128
0
    strbuf_add(&idx_file, transport->pack_lockfiles.items[0].string,
129
0
         base_len);
130
0
    strbuf_addstr(&idx_file, ".idx");
131
0
    new_pack = add_packed_git(the_repository, idx_file.buf,
132
0
            idx_file.len, 1);
133
0
    strbuf_release(&idx_file);
134
0
  }
135
136
0
  do {
137
    /*
138
     * If index-pack already checked that:
139
     * - there are no dangling pointers in the new pack
140
     * - the pack is self contained
141
     * Then if the updated ref is in the new pack, then we
142
     * are sure the ref is good and not sending it to
143
     * rev-list for verification.
144
     */
145
0
    if (new_pack && find_pack_entry_one(oid, new_pack))
146
0
      continue;
147
148
0
    if (fprintf(rev_list_in, "%s\n", oid_to_hex(oid)) < 0)
149
0
      break;
150
0
  } while ((oid = fn(cb_data)) != NULL);
151
152
0
  if (ferror(rev_list_in) || fflush(rev_list_in)) {
153
0
    if (errno != EPIPE && errno != EINVAL)
154
0
      error_errno(_("failed write to rev-list"));
155
0
    err = -1;
156
0
  }
157
158
0
  if (fclose(rev_list_in))
159
0
    err = error_errno(_("failed to close rev-list's stdin"));
160
161
0
  sigchain_pop(SIGPIPE);
162
0
  if (new_pack) {
163
0
    close_pack(new_pack);
164
0
    free(new_pack);
165
0
  }
166
0
  return finish_command(&rev_list) || err;
167
0
}