git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
705e54f3aac3df6ccd8f39bc8c340d4067c8cef8 blob 10617 bytes (raw)

  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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
 
#include "cache.h"
#include "metacommit.h"
#include "commit.h"
#include "change-table.h"
#include "refs.h"

void init_metacommit_data(struct metacommit_data *state)
{
	memset(state, 0, sizeof(*state));
}

void clear_metacommit_data(struct metacommit_data *state)
{
	oid_array_clear(&state->replace);
	oid_array_clear(&state->origin);
}

static void compute_default_change_name(struct commit *initial_commit,
	struct strbuf* result)
{
	const char *buffer = get_commit_buffer(initial_commit, NULL);
	const char *subject;
	const char *eol;
	int len;
	find_commit_subject(buffer, &subject);
	eol = strchrnul(subject, '\n');
	for (len = 0;subject < eol && len < 10; ++subject, ++len) {
		char next = *subject;
		if (isspace(next)) {
			continue;
		}

		strbuf_addch(result, next);
	}
}

/*
 * Computes a change name for a change rooted at the given initial commit. Good
 * change names should be memorable, unique, and easy to type. They are not
 * required to match the commit comment.
 */
static void compute_change_name(struct commit *initial_commit, struct strbuf* result)
{
	struct strbuf default_name;
	struct object_id unused;

	strbuf_init(&default_name, 0);
	if (initial_commit) {
		compute_default_change_name(initial_commit, &default_name);
	} else {
		strbuf_addstr(&default_name, "change");
	}
	strbuf_addstr(result, "refs/metas/");
	strbuf_addstr(result, default_name.buf);

	// If there is already a change of this name, append a suffix
	if (!read_ref(result->buf, &unused)) {
		int suffix = 2;
		int original_length = result->len;

		while (1) {
			strbuf_addf(result, "%d", suffix);
			if (read_ref(result->buf, &unused)) {
				break;
			}
			strbuf_remove(result, original_length, result->len - original_length);
			++suffix;
		}
	}

	strbuf_release(&default_name);
}

struct resolve_metacommit_callback_data
{
	struct change_table* active_changes;
	struct string_list *changes;
	struct oid_array *heads;
};

static int resolve_metacommit_callback(const char *refname, void *cb_data)
{
	struct resolve_metacommit_callback_data *data = (struct resolve_metacommit_callback_data *)cb_data;
	struct change_head *chhead;

	chhead = get_change_head(data->active_changes, refname);

	if (data->changes) {
		string_list_append(data->changes, refname)->util = &(chhead->head);
	}
	if (data->heads) {
		oid_array_append(data->heads, &(chhead->head));
	}

	return 0;
}

/*
 * Produces the final form of a metacommit based on the current change refs.
 */
static void resolve_metacommit(
	struct repository* repo,
	struct change_table* active_changes,
	const struct metacommit_data *to_resolve,
	struct metacommit_data *resolved_output,
	struct string_list *to_advance,
	int allow_append)
{
	int i;
	int len = to_resolve->replace.nr;
	struct resolve_metacommit_callback_data cbdata;
	int old_change_list_length = to_advance->nr;
	struct commit* content;

	oidcpy(&resolved_output->content, &to_resolve->content);

	// First look for changes that point to any of the replacement edges in the
	// metacommit. These will be the changes that get advanced by this metacommit.
	resolved_output->abandoned = to_resolve->abandoned;
	cbdata.active_changes = active_changes;
	cbdata.changes = to_advance;
	cbdata.heads = &(resolved_output->replace);

	if (allow_append) {
		for (i = 0; i < len; i++) {
			int old_number = resolved_output->replace.nr;
			for_each_change_referencing(active_changes, &(to_resolve->replace.oid[i]),
				resolve_metacommit_callback, &cbdata);
			// If no changes were found, use the unresolved value
			if (old_number == resolved_output->replace.nr) {
				oid_array_append(&(resolved_output->replace), &(to_resolve->replace.oid[i]));
			}
		}
	}

	cbdata.changes = NULL;
	cbdata.heads = &(resolved_output->origin);

	len = to_resolve->origin.nr;
	for (i = 0; i < len; i++) {
		int old_number = resolved_output->origin.nr;
		for_each_change_referencing(active_changes, &(to_resolve->origin.oid[i]),
			resolve_metacommit_callback, &cbdata);
		if (old_number == resolved_output->origin.nr) {
			oid_array_append(&(resolved_output->origin), &(to_resolve->origin.oid[i]));
		}
	}

	// If no changes were advanced by this metacommit, we'll need to create a new
	// one.
	if (to_advance->nr == old_change_list_length) {
		struct strbuf change_name;

		strbuf_init(&change_name, 80);
		content = lookup_commit_reference_gently(repo, &(to_resolve->content), 1);

		compute_change_name(content, &change_name);
		string_list_append(to_advance, change_name.buf);
		strbuf_release(&change_name);
	}
}

static void lookup_commits(
	struct repository *repo,
	struct oid_array *to_lookup,
	struct commit_list **result)
{
	int i = to_lookup->nr;

	while (--i >= 0) {
		struct object_id *next = &(to_lookup->oid[i]);
		struct commit *commit = lookup_commit_reference_gently(repo, next, 1);
		commit_list_insert(commit, result);
	}
}

#define PARENT_TYPE_PREFIX "parent-type "

/*
 * Creates a new metacommit object with the given content. Writes the object
 * id of the newly-created commit to result.
 */
int write_metacommit(struct repository *repo, struct metacommit_data *state,
	struct object_id *result)
{
	struct commit_list *parents = NULL;
	struct strbuf comment;
	int i;
	struct commit *content;

	strbuf_init(&comment, strlen(PARENT_TYPE_PREFIX)
		+ 1 + 2 * (state->origin.nr + state->replace.nr));
	lookup_commits(repo, &state->origin, &parents);
	lookup_commits(repo, &state->replace, &parents);
	content = lookup_commit_reference_gently(repo, &state->content, 1);
	if (!content) {
		strbuf_release(&comment);
		free_commit_list(parents);
		return -1;
	}
	commit_list_insert(content, &parents);

	strbuf_addstr(&comment, PARENT_TYPE_PREFIX);
	strbuf_addstr(&comment, state->abandoned ? "a" : "c");
	for (i = 0; i < state->replace.nr; i++) {
		strbuf_addstr(&comment, " r");
	}

	for (i = 0; i < state->origin.nr; i++) {
		strbuf_addstr(&comment, " o");
	}

	// The parents list will be freed by this call
	commit_tree(comment.buf, comment.len, repo->hash_algo->empty_tree, parents,
		result, NULL, NULL);

	strbuf_release(&comment);
	return 0;
}

/*
 * Returns true iff the given metacommit is abandoned, has one or more origin
 * parents, or has one or more replacement parents.
 */
static int is_nontrivial_metacommit(struct metacommit_data *state)
{
	return state->replace.nr || state->origin.nr || state->abandoned;
}

/*
 * Records the relationships described by the given metacommit in the
 * repository.
 *
 * If override_change is NULL (the default), an attempt will be made
 * to append to existing changes wherever possible instead of creating new ones.
 * If override_change is non-null, only the given change ref will be updated.
 *
 * options is a bitwise combination of the UPDATE_OPTION_* flags.
 */
int record_metacommit(struct repository *repo,
	const struct metacommit_data *metacommit,
	const char* override_change, int options, struct strbuf *err)
{
	static const char *msg = "updating change";
	struct metacommit_data resolved_metacommit;
	struct string_list changes;
	struct object_id commit_target;
	struct ref_transaction *transaction = NULL;
	struct object_id old_head_working;
	const struct object_id *old_head;
	struct change_table chtable;
	int i;
	int ret = 0;
	int force = (options & UPDATE_OPTION_FORCE);

	init_metacommit_data(&resolved_metacommit);
	string_list_init(&changes, 1);

	change_table_init(&chtable);

	change_table_add_all_visible(&chtable, repo);

	resolve_metacommit(repo, &chtable, metacommit, &resolved_metacommit, &changes,
		(options & UPDATE_OPTION_NOAPPEND) == 0);

	if (override_change) {
		old_head = &old_head_working;
		string_list_clear(&changes, 0);
		if (get_oid_committish(override_change, &old_head_working)) {
			// ...then this is a newly-created change
			old_head = &null_oid;
		} else if (!force) {
			if (!oid_array_contains_nondestructive(&(resolved_metacommit.replace),
				&old_head_working)) {
				// Attempted non-fast-forward change
				strbuf_addf(err, _("non-fast-forward update to '%s'"),
					override_change);
				ret = -1;
				goto cleanup;
			}
		}
		// The expected "current" head of the change is stored in the util pointer
		string_list_append(&changes, override_change)->util = (void*)old_head;
	}

	if (is_nontrivial_metacommit(&resolved_metacommit)) {
		// If there are any origin or replacement parents, create a new metacommit
		// object.
		if (write_metacommit(repo, &resolved_metacommit, &commit_target) < 0) {
			ret = -1;
			goto cleanup;
		}
	} else {
		// If the metacommit would only contain a content commit, point to the
		// commit itself rather than creating a trivial metacommit.
		oidcpy(&commit_target, &(resolved_metacommit.content));
	}

	// If a change already exists with this target and we're not forcing an
	// update to some specific override_change && change, there's nothing to do.
	if (!override_change 
		&& change_table_has_change_referencing(&chtable, &commit_target)) {
		// Not an error
		goto cleanup;
	}

	transaction = ref_transaction_begin(err);

	// Update the refs for each affected change
	if (!transaction) {
		ret = -1;
	} else {
		for (i = 0; i < changes.nr; i++) {
			struct string_list_item *it = &(changes.items[i]);

			// The expected current head of the change is stored in the util pointer.
			// It is null if the change should be newly-created.
			if (it->util) {
				if (ref_transaction_update(transaction, it->string, &commit_target,
					force ? NULL : it->util, 0, msg, err)) {

					ret = -1;
				}
			} else {
				if (ref_transaction_create(transaction, it->string,
					&commit_target, 0, msg, err)) {

					ret = -1;
				}
			}
		}

		if (!ret) {
			if (ref_transaction_commit(transaction, err)) {
				ret = -1;
			}
		}
	}

cleanup:
	ref_transaction_free(transaction);
	string_list_clear(&changes, 0);
	clear_metacommit_data(&resolved_metacommit);
	change_table_clear(&chtable);
	return ret;
}

/*
 * Should be invoked after a command that has "modify" semantics - commands that
 * create a new commit based on an old commit and treat the new one as a
 * replacement for the old one. This method records the replacement in the
 * change graph, such that a future evolve operation will rebase children of
 * the old commit onto the new commit.
 */
void modify_change(
	struct repository *repo,
	const struct object_id *old_commit,
	const struct object_id *new_commit,
	struct strbuf *err)
{
	struct metacommit_data metacommit;

	init_metacommit_data(&metacommit);
	oidcpy(&(metacommit.content), new_commit);
	oid_array_append(&(metacommit.replace), old_commit);

	record_metacommit(repo, &metacommit, NULL, 0, err);

	clear_metacommit_data(&metacommit);
}
debug log:

solving 705e54f3aa ...
found 705e54f3aa in https://public-inbox.org/git/20190121223216.66659-6-sxenos@google.com/ ||
	https://public-inbox.org/git/20190127194128.161250-6-sxenos@google.com/

applying [1/2] https://public-inbox.org/git/20190121223216.66659-6-sxenos@google.com/
diff --git a/metacommit.c b/metacommit.c
new file mode 100644
index 0000000000..705e54f3aa

1:307: trailing whitespace.
	if (!override_change 
Checking patch metacommit.c...
Applied patch metacommit.c cleanly.
warning: 1 line adds whitespace errors.

skipping https://public-inbox.org/git/20190127194128.161250-6-sxenos@google.com/ for 705e54f3aa
index at:
100644 705e54f3aac3df6ccd8f39bc8c340d4067c8cef8	metacommit.c

Code repositories for project(s) associated with this 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).