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