git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
blob ab0eb9f97402e9c1ec3e08868815c3564b1bafc8 9480 bytes (raw)
name: connected.c 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
 
#include "cache.h"
#include "object-store.h"
#include "run-command.h"
#include "sigchain.h"
#include "connected.h"
#include "transport.h"
#include "packfile.h"
#include "promisor-remote.h"
#include "object.h"
#include "tree-walk.h"
#include "commit.h"
#include "tag.h"
#include "progress.h"
#include "tmp-objdir.h"
#include "oidset.h"

/*
 * If we feed all the commits we want to verify to this command
 *
 *  $ git rev-list --objects --stdin --not --all
 *
 * and if it does not error out, that means everything reachable from
 * these commits locally exists and is connected to our existing refs.
 * Note that this does _not_ validate the individual objects.
 *
 * Returns 0 if everything is connected, non-zero otherwise.
 */
int check_connected(oid_iterate_fn fn, void *cb_data,
		    struct check_connected_options *opt)
{
	struct child_process rev_list = CHILD_PROCESS_INIT;
	FILE *rev_list_in;
	struct check_connected_options defaults = CHECK_CONNECTED_INIT;
	struct object_id oid;
	int err = 0;
	struct packed_git *new_pack = NULL;
	struct transport *transport;
	size_t base_len;

	if (!opt)
		opt = &defaults;
	transport = opt->transport;

	if (fn(cb_data, &oid)) {
		if (opt->err_fd)
			close(opt->err_fd);
		return err;
	}

	if (transport && transport->smart_options &&
	    transport->smart_options->self_contained_and_connected &&
	    transport->pack_lockfiles.nr == 1 &&
	    strip_suffix(transport->pack_lockfiles.items[0].string,
			 ".keep", &base_len)) {
		struct strbuf idx_file = STRBUF_INIT;
		strbuf_add(&idx_file, transport->pack_lockfiles.items[0].string,
			   base_len);
		strbuf_addstr(&idx_file, ".idx");
		new_pack = add_packed_git(idx_file.buf, idx_file.len, 1);
		strbuf_release(&idx_file);
	}

	if (has_promisor_remote()) {
		/*
		 * For partial clones, we don't want to have to do a regular
		 * connectivity check because we have to enumerate and exclude
		 * all promisor objects (slow), and then the connectivity check
		 * itself becomes a no-op because in a partial clone every
		 * object is a promisor object. Instead, just make sure we
		 * received, in a promisor packfile, the objects pointed to by
		 * each wanted ref.
		 *
		 * Before checking for promisor packs, be sure we have the
		 * latest pack-files loaded into memory.
		 */
		reprepare_packed_git(the_repository);
		do {
			struct packed_git *p;

			for (p = get_all_packs(the_repository); p; p = p->next) {
				if (!p->pack_promisor)
					continue;
				if (find_pack_entry_one(oid.hash, p))
					goto promisor_pack_found;
			}
			/*
			 * Fallback to rev-list with oid and the rest of the
			 * object IDs provided by fn.
			 */
			goto no_promisor_pack_found;
promisor_pack_found:
			;
		} while (!fn(cb_data, &oid));
		return 0;
	}

no_promisor_pack_found:
	if (opt->shallow_file) {
		strvec_push(&rev_list.args, "--shallow-file");
		strvec_push(&rev_list.args, opt->shallow_file);
	}
	strvec_push(&rev_list.args,"rev-list");
	strvec_push(&rev_list.args, "--objects");
	strvec_push(&rev_list.args, "--stdin");
	if (has_promisor_remote())
		strvec_push(&rev_list.args, "--exclude-promisor-objects");
	if (!opt->is_deepening_fetch) {
		strvec_push(&rev_list.args, "--not");
		strvec_push(&rev_list.args, "--all");
	}
	strvec_push(&rev_list.args, "--quiet");
	strvec_push(&rev_list.args, "--alternate-refs");
	if (opt->progress)
		strvec_pushf(&rev_list.args, "--progress=%s",
			     _("Checking connectivity"));

	rev_list.git_cmd = 1;
	rev_list.env = opt->env;
	rev_list.in = -1;
	rev_list.no_stdout = 1;
	if (opt->err_fd)
		rev_list.err = opt->err_fd;
	else
		rev_list.no_stderr = opt->quiet;

	if (start_command(&rev_list))
		return error(_("Could not run 'git rev-list'"));

	sigchain_push(SIGPIPE, SIG_IGN);

	rev_list_in = xfdopen(rev_list.in, "w");

	do {
		/*
		 * If index-pack already checked that:
		 * - there are no dangling pointers in the new pack
		 * - the pack is self contained
		 * Then if the updated ref is in the new pack, then we
		 * are sure the ref is good and not sending it to
		 * rev-list for verification.
		 */
		if (new_pack && find_pack_entry_one(oid.hash, new_pack))
			continue;

		if (fprintf(rev_list_in, "%s\n", oid_to_hex(&oid)) < 0)
			break;
	} while (!fn(cb_data, &oid));

	if (ferror(rev_list_in) || fflush(rev_list_in)) {
		if (errno != EPIPE && errno != EINVAL)
			error_errno(_("failed write to rev-list"));
		err = -1;
	}

	if (fclose(rev_list_in))
		err = error_errno(_("failed to close rev-list's stdin"));

	sigchain_pop(SIGPIPE);
	return finish_command(&rev_list) || err;
}

struct connected_payload {
	struct repository *repo;
	struct progress *progress;
	struct oidset seen;
	size_t checked_objects;
	size_t missing_objects;
};

static void check_missing(struct connected_payload *payload, const struct object_id *oid)
{
	if (oidset_contains(&payload->seen, oid))
		return;
	if (oid_object_info(payload->repo, oid, NULL) <= 0)
		payload->missing_objects++;
	oidset_insert(&payload->seen, oid);
	display_progress(payload->progress, ++payload->checked_objects);
}

static int check_quarantined_object(const struct object_id *oid, enum object_type type,
				    void *content, unsigned long size, void *data)
{
	struct connected_payload *payload = data;
	struct object *object;
	int eaten;

	object = parse_object_buffer(payload->repo, oid, type, size, content, &eaten);
	if (!object) {
		if (!eaten)
			free(content);
		error(_("could not parse quarantined object %s"), oid_to_hex(oid));
		return -1;
	}
	if (!eaten)
		free(content);

	if (type == OBJ_TREE) {
		struct tree *tree = (struct tree *)object;
		struct tree_desc tree_desc;
		struct name_entry entry;

		if (init_tree_desc_gently(&tree_desc, tree->buffer, tree->size))
			return -1;
		while (tree_entry_gently(&tree_desc, &entry))
			check_missing(payload, &entry.oid);

		free_tree_buffer(tree);
	} else if (type == OBJ_COMMIT) {
		struct commit *commit = (struct commit *) object;
		struct commit_list *parents;

		check_missing(payload, get_commit_tree_oid(commit));
		for (parents = commit->parents; parents; parents = parents->next)
			check_missing(payload, &parents->item->object.oid);
	} else if (type == OBJ_TAG) {
		check_missing(payload, get_tagged_oid((struct tag *) object));
	} else {
		error(_("unexpected object type"));
		return -1;
	}

	return 0;
}

static int loose_object_iterator(const struct object_id *oid,
				 const char *path, void *_data)
{
	struct connected_payload *payload = _data;
	enum object_type type;
	unsigned long size;
	void *content;

	if (read_loose_object(path, oid, &type, &size, NULL) < 0)
		return -1;

	oidset_insert(&payload->seen, oid);
	display_progress(payload->progress, ++payload->checked_objects);

	if (type == OBJ_BLOB)
		return 0;
	if (read_loose_object(path, oid, &type, &size, &content) < 0)
		return -1;

	return check_quarantined_object(oid, type, content, size, payload);
}

static int packed_object_iterator(const struct object_id *oid,
				  struct packed_git *pack, uint32_t pos,
				  void *_data)
{
	off_t off = nth_packed_object_offset(pack, pos);
	struct connected_payload *payload = _data;
	struct object_info oi = OBJECT_INFO_INIT;
	enum object_type type;
	unsigned long size;
	void *content;

	oi.typep = &type;
	if (packed_object_info(payload->repo, pack, off, &oi) < 0)
		return -1;

	oidset_insert(&payload->seen, oid);
	display_progress(payload->progress, ++payload->checked_objects);

	if (type == OBJ_BLOB)
		return 0;

	oi.contentp = &content;
	oi.sizep = &size;

	if (packed_object_info(payload->repo, pack, off, &oi) < 0)
		return -1;

	return check_quarantined_object(oid, type, content, size, payload);
}

static int pack_dir_iterator(const char *full_path, size_t full_path_len,
			     const char *file_path, void *data)
{
	if (ends_with(full_path, ".idx")) {
		struct packed_git *pack = add_packed_git(full_path, full_path_len, 1);
		if (!pack) {
			error(_("unable to add quarantined pack"));
			return -1;
		}

		if (for_each_object_in_pack(pack, packed_object_iterator, data,
					    FOR_EACH_OBJECT_PACK_ORDER) < 0) {
			error(_("unable to iterate packed objects"));
			return -1;
		}
	}

	return 0;
}

int check_connected_quarantine(struct repository *repo,
			       struct tmp_objdir *quarantine,
			       oid_iterate_fn fn, void *cb_data)
{
	struct connected_payload payload = {
		.repo = repo,
		.seen = OIDSET_INIT,
	};
	struct object_id oid;
	int ret;

	payload.progress = start_delayed_progress("Checking connectivity", 0);

	/*
	 * Iterate through all loose and packed objects part of the object
	 * quarantine and verify that for each of them, all directly referenced
	 * objects exist.
	 */
	if (for_each_loose_file_in_objdir(tmp_objdir_path(quarantine),
					  loose_object_iterator, NULL,
					  NULL, &payload) < 0) {
		error(_("unable to check quarantined loose objects"));
		ret = -1;
		goto out;
	}

	if (for_each_file_in_pack_dir(tmp_objdir_path(quarantine),
				      pack_dir_iterator, &payload) < 0) {
		error(_("unable to check quarantined packed objects"));
		ret = -1;
		goto out;
	}

	/*
	 * After we've verified that all quarantined objects are indeed
	 * connected, we need to verify that all new tips are contained in the
	 * repository, too.
	 */
	while (!fn(cb_data, &oid)) {
		if (oid_object_info(repo, &oid, NULL) <= 0)
			payload.missing_objects++;
		display_progress(payload.progress, ++payload.checked_objects);
	}

out:
	stop_progress(&payload.progress);
	oidset_clear(&payload.seen);
	return ret || payload.missing_objects != 0;
}

debug log:

solving ab0eb9f974 ...
found ab0eb9f974 in https://public-inbox.org/git/d01685a3ec82c3415654779c122927388fc433db.1621451532.git.ps@pks.im/
found b18299fdf0 in https://80x24.org/mirrors/git.git
preparing index
index prepared:
100644 b18299fdf0e5224924915810d66196a1d38cbe56	connected.c

applying [1/1] https://public-inbox.org/git/d01685a3ec82c3415654779c122927388fc433db.1621451532.git.ps@pks.im/
diff --git a/connected.c b/connected.c
index b18299fdf0..ab0eb9f974 100644

Checking patch connected.c...
Applied patch connected.c cleanly.

index at:
100644 ab0eb9f97402e9c1ec3e08868815c3564b1bafc8	connected.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).