git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
blob 84ce6588a2399fc4d2f8f48c30742c576a1e7bb7 5212 bytes (raw)
name: builtin/midx.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
 
#include "builtin.h"
#include "cache.h"
#include "config.h"
#include "dir.h"
#include "git-compat-util.h"
#include "lockfile.h"
#include "packfile.h"
#include "parse-options.h"
#include "midx.h"

static char const * const builtin_midx_usage[] = {
	N_("git midx [--pack-dir <packdir>]"),
	N_("git midx --write [--update-head] [--pack-dir <packdir>]"),
	NULL
};

static struct opts_midx {
	const char *pack_dir;
	int write;
	int update_head;
	int has_existing;
	struct object_id old_midx_oid;
} opts;

static int build_midx_from_packs(
	const char *pack_dir,
	const char **pack_names, uint32_t nr_packs,
	const char **midx_id)
{
	struct packed_git **packs;
	const char **installed_pack_names;
	uint32_t i, j, nr_installed_packs = 0;
	uint32_t nr_objects = 0;
	struct pack_midx_entry *objects;
	struct pack_midx_entry **obj_ptrs;
	uint32_t nr_total_packs = nr_packs;
	uint32_t pack_offset = 0;
	struct strbuf pack_path = STRBUF_INIT;
	int baselen;

	if (!nr_total_packs) {
		*midx_id = NULL;
		return 0;
	}

	ALLOC_ARRAY(packs, nr_total_packs);
	ALLOC_ARRAY(installed_pack_names, nr_total_packs);

	strbuf_addstr(&pack_path, pack_dir);
	strbuf_addch(&pack_path, '/');
	baselen = pack_path.len;
	for (i = 0; i < nr_packs; i++) {
		strbuf_setlen(&pack_path, baselen);
		strbuf_addstr(&pack_path, pack_names[i]);

		strbuf_strip_suffix(&pack_path, ".pack");
		strbuf_addstr(&pack_path, ".idx");

		packs[nr_installed_packs] = add_packed_git(pack_path.buf, pack_path.len, 0);

		if (packs[nr_installed_packs] != NULL) {
			if (open_pack_index(packs[nr_installed_packs]))
				continue;

			nr_objects += packs[nr_installed_packs]->num_objects;
			installed_pack_names[nr_installed_packs] = pack_names[i];
			nr_installed_packs++;
		}
	}
	strbuf_release(&pack_path);

	if (!nr_objects || !nr_installed_packs) {
		FREE_AND_NULL(packs);
		FREE_AND_NULL(installed_pack_names);
		*midx_id = NULL;
		return 0;
	}

	ALLOC_ARRAY(objects, nr_objects);
	nr_objects = 0;

	for (i = pack_offset; i < nr_installed_packs; i++) {
		struct packed_git *p = packs[i];

		for (j = 0; j < p->num_objects; j++) {
			struct pack_midx_entry entry;

			if (!nth_packed_object_oid(&entry.oid, p, j))
				die("unable to get sha1 of object %u in %s",
				    i, p->pack_name);

			entry.pack_int_id = i;
			entry.offset = nth_packed_object_offset(p, j);

			objects[nr_objects] = entry;
			nr_objects++;
		}
	}

	ALLOC_ARRAY(obj_ptrs, nr_objects);
	for (i = 0; i < nr_objects; i++)
		obj_ptrs[i] = &objects[i];

	*midx_id = write_midx_file(pack_dir, NULL,
		installed_pack_names, nr_installed_packs,
		obj_ptrs, nr_objects);

	FREE_AND_NULL(packs);
	FREE_AND_NULL(installed_pack_names);
	FREE_AND_NULL(obj_ptrs);
	FREE_AND_NULL(objects);

	return 0;
}

static void update_head_file(const char *pack_dir, const char *midx_id)
{
	int fd;
	struct lock_file lk = LOCK_INIT;
	char *head_path = get_midx_head_filename(pack_dir);

	fd = hold_lock_file_for_update(&lk, head_path, LOCK_DIE_ON_ERROR);
	FREE_AND_NULL(head_path);

	if (fd < 0)
		die_errno("unable to open midx-head");

	write_in_full(fd, midx_id, GIT_MAX_HEXSZ);
	commit_lock_file(&lk);
}

static int midx_write(void)
{
	const char **pack_names = NULL;
	uint32_t i, nr_packs = 0;
	const char *midx_id = 0;
	DIR *dir;
	struct dirent *de;

	dir = opendir(opts.pack_dir);
	if (!dir) {
		error_errno("unable to open object pack directory: %s",
			    opts.pack_dir);
		return 1;
	}

	nr_packs = 256;
	ALLOC_ARRAY(pack_names, nr_packs);

	i = 0;
	while ((de = readdir(dir)) != NULL) {
		if (is_dot_or_dotdot(de->d_name))
			continue;

		if (ends_with(de->d_name, ".pack")) {
			ALLOC_GROW(pack_names, i + 1, nr_packs);
			pack_names[i++] = xstrdup(de->d_name);
		}
	}

	nr_packs = i;
	closedir(dir);

	if (!nr_packs)
		goto cleanup;

	if (build_midx_from_packs(opts.pack_dir, pack_names, nr_packs, &midx_id))
		die("failed to build MIDX");

	if (midx_id == NULL)
		goto cleanup;

	printf("%s\n", midx_id);

	if (opts.update_head)
		update_head_file(opts.pack_dir, midx_id);

cleanup:
	if (pack_names)
		FREE_AND_NULL(pack_names);
	return 0;
}

int cmd_midx(int argc, const char **argv, const char *prefix)
{
	static struct option builtin_midx_options[] = {
		{ OPTION_STRING, 'p', "pack-dir", &opts.pack_dir,
			N_("dir"),
			N_("The pack directory containing set of packfile and pack-index pairs.") },
		OPT_BOOL('w', "write", &opts.write,
			N_("write midx file")),
		OPT_BOOL('u', "update-head", &opts.update_head,
			N_("update midx-head to written midx file")),
		OPT_END(),
	};

	if (argc == 2 && !strcmp(argv[1], "-h"))
		usage_with_options(builtin_midx_usage, builtin_midx_options);

	git_config(git_default_config, NULL);
	if (!core_midx)
		die(_("git-midx requires core.midx=true"));

	argc = parse_options(argc, argv, prefix,
			     builtin_midx_options,
			     builtin_midx_usage, 0);

	if (!opts.pack_dir) {
		struct strbuf path = STRBUF_INIT;
		strbuf_addstr(&path, get_object_directory());
		strbuf_addstr(&path, "/pack");
		opts.pack_dir = strbuf_detach(&path, NULL);
	}

	opts.has_existing = !!get_midx_head_oid(opts.pack_dir, &opts.old_midx_oid);

	if (opts.write)
		return midx_write();

	if (opts.has_existing)
		printf("%s\n", oid_to_hex(&opts.old_midx_oid));
	return 0;
}

debug log:

solving 84ce6588a2 ...
found 84ce6588a2 in https://public-inbox.org/git/20180107181459.222909-8-dstolee@microsoft.com/
found 4aae14cf8e in https://public-inbox.org/git/20180107181459.222909-6-dstolee@microsoft.com/

applying [1/2] https://public-inbox.org/git/20180107181459.222909-6-dstolee@microsoft.com/
diff --git a/builtin/midx.c b/builtin/midx.c
new file mode 100644
index 0000000000..4aae14cf8e


applying [2/2] https://public-inbox.org/git/20180107181459.222909-8-dstolee@microsoft.com/
diff --git a/builtin/midx.c b/builtin/midx.c
index 4aae14cf8e..84ce6588a2 100644

Checking patch builtin/midx.c...
Applied patch builtin/midx.c cleanly.
Checking patch builtin/midx.c...
Applied patch builtin/midx.c cleanly.

index at:
100644 84ce6588a2399fc4d2f8f48c30742c576a1e7bb7	builtin/midx.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).