git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
blob 67d708dc8dec3b2a3ecbe5dbd44e7607745ab9ca 7208 bytes (raw)
name: builtin/change.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
 
#include "builtin.h"
#include "ref-filter.h"
#include "parse-options.h"
#include "metacommit.h"
#include "change-table.h"
#include "config.h"

static const char * const builtin_change_usage[] = {
	N_("git change list [<pattern>...]"),
	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
	NULL
};

static const char * const builtin_list_usage[] = {
	N_("git change list [<pattern>...]"),
	NULL
};

static const char * const builtin_update_usage[] = {
	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
	NULL
};

static int change_list(int argc, const char **argv, const char* prefix)
{
	struct option options[] = {
		OPT_END()
	};
	struct ref_filter filter;
	/* TODO: See below
	struct ref_sorting *sorting;
	struct string_list sorting_options = STRING_LIST_INIT_DUP; */
	struct ref_format format = REF_FORMAT_INIT;
	struct ref_array array;
	int i;

	argc = parse_options(argc, argv, prefix, options, builtin_list_usage, 0);

	setup_ref_filter_porcelain_msg();

	memset(&filter, 0, sizeof(filter));
	memset(&array, 0, sizeof(array));

	filter.kind = FILTER_REFS_CHANGES;
	filter.name_patterns = argv;

	filter_refs(&array, &filter, FILTER_REFS_CHANGES);

	/* TODO: This causes a crash. It sets one of the atom_value handlers to
	 * something invalid, which causes a crash later when we call
	 * show_ref_array_item. Figure out why this happens and put back the sorting.
	 *
	 * sorting = ref_sorting_options(&sorting_options);
	 * ref_array_sort(sorting, &array); */

	if (!format.format)
		format.format = "%(refname:lstrip=1)";

	if (verify_ref_format(&format))
		die(_("unable to parse format string"));

	for (i = 0; i < array.nr; i++) {
		struct strbuf output = STRBUF_INIT;
		struct strbuf err = STRBUF_INIT;
		if (format_ref_array_item(array.items[i], &format, &output, &err))
			die("%s", err.buf);
		fwrite(output.buf, 1, output.len, stdout);
		putchar('\n');

		strbuf_release(&err);
		strbuf_release(&output);
	}

	ref_array_clear(&array);
	/* TODO: see above
	ref_sorting_release(sorting); */

	return 0;
}

struct update_state {
	int options;
	const char* change;
	const char* content;
	struct string_list replace;
	struct string_list origin;
};

static void init_update_state(struct update_state *state)
{
	memset(state, 0, sizeof(*state));
	state->content = "HEAD";
	string_list_init_nodup(&state->replace);
	string_list_init_nodup(&state->origin);
}

static void clear_update_state(struct update_state *state)
{
	string_list_clear(&state->replace, 0);
	string_list_clear(&state->origin, 0);
}

static int update_option_parse_replace(const struct option *opt,
				       const char *arg, int unset)
{
	struct update_state *state = opt->value;
	string_list_append(&state->replace, arg);
	return 0;
}

static int update_option_parse_origin(const struct option *opt,
				      const char *arg, int unset)
{
	struct update_state *state = opt->value;
	string_list_append(&state->origin, arg);
	return 0;
}

static int resolve_commit(const char *committish, struct object_id *result)
{
	struct commit *commit;
	if (get_oid_committish(committish, result))
		die(_("Failed to resolve '%s' as a valid revision."), committish);
	commit = lookup_commit_reference(the_repository, result);
	if (!commit)
		die(_("Could not parse object '%s'."), committish);
	oidcpy(result, &commit->object.oid);
	return 0;
}

static void resolve_commit_list(const struct string_list *commitsish_list,
	struct oid_array* result)
{
	int i;
	for (i = 0; i < commitsish_list->nr; i++) {
		struct string_list_item *item = &commitsish_list->items[i];
		struct object_id next;
		resolve_commit(item->string, &next);
		oid_array_append(result, &next);
	}
}

/*
 * Given the command-line options for the update command, fills in a
 * metacommit_data with the corresponding changes.
 */
static void get_metacommit_from_command_line(
	const struct update_state* commands, struct metacommit_data *result)
{
	resolve_commit(commands->content, &(result->content));
	resolve_commit_list(&(commands->replace), &(result->replace));
	resolve_commit_list(&(commands->origin), &(result->origin));
}

static int perform_update(
	struct repository *repo,
	const struct update_state *state,
	struct strbuf *err)
{
	struct metacommit_data metacommit;
	struct change_table chtable;
	struct string_list changes;
	int ret;
	int i;

	change_table_init(&chtable);
	change_table_add_all_visible(&chtable, repo);
	string_list_init_dup(&changes);

	init_metacommit_data(&metacommit);

	get_metacommit_from_command_line(state, &metacommit);

	ret = record_metacommit_withresult(repo, &chtable, &metacommit,
		state->change, state->options, err, &changes);

	for (i = 0; i < changes.nr; i++) {
		struct string_list_item *it = &changes.items[i];

		const char* name = lstrip_ref_components(it->string, 1);
		if (!name)
			die(_("Failed to remove `refs/` from %s"), it->string);

		if (it->util)
			fprintf(stdout, N_("Updated change %s\n"), name);
		else
			fprintf(stdout, N_("Created change %s\n"), name);
	}

	string_list_clear(&changes, 0);
	change_table_clear(&chtable);
	clear_metacommit_data(&metacommit);

	return ret;
}

static int change_update(int argc, const char **argv, const char* prefix)
{
	int result;
	int force = 0;
	int newchange = 0;
	struct strbuf err = STRBUF_INIT;
	struct update_state state;
	struct option options[] = {
		{ OPTION_CALLBACK, 'r', "replace", &state, N_("commit"),
			N_("marks the given commit as being obsolete"),
			0, update_option_parse_replace },
		{ OPTION_CALLBACK, 'o', "origin", &state, N_("commit"),
			N_("marks the given commit as being the origin of this commit"),
			0, update_option_parse_origin },
		OPT_BOOL('F', "force", &force,
			N_("overwrite an existing change of the same name")),
		OPT_STRING('c', "content", &state.content, N_("commit"),
				 N_("identifies the new content commit for the change")),
		OPT_STRING('g', "change", &state.change, N_("commit"),
				 N_("name of the change to update")),
		OPT_BOOL('n', "new", &newchange,
			N_("create a new change - do not append to any existing change")),
		OPT_END()
	};

	init_update_state(&state);

	argc = parse_options(argc, argv, prefix, options, builtin_update_usage, 0);

	if (force) state.options |= UPDATE_OPTION_FORCE;
	if (newchange) state.options |= UPDATE_OPTION_NOAPPEND;

	result = perform_update(the_repository, &state, &err);

	if (result < 0) {
		error("%s", err.buf);
		strbuf_release(&err);
	}

	clear_update_state(&state);

	return result;
}

int cmd_change(int argc, const char **argv, const char *prefix)
{
	/* No options permitted before subcommand currently */
	struct option options[] = {
		OPT_END()
	};
	int result = 1;

	argc = parse_options(argc, argv, prefix, options, builtin_change_usage,
		PARSE_OPT_STOP_AT_NON_OPTION);

	if (argc < 1)
		usage_with_options(builtin_change_usage, options);
	else if (!strcmp(argv[0], "list"))
		result = change_list(argc, argv, prefix);
	else if (!strcmp(argv[0], "update"))
		result = change_update(argc, argv, prefix);
	else {
		error(_("Unknown subcommand: %s"), argv[0]);
		usage_with_options(builtin_change_usage, options);
	}

	return result ? 1 : 0;
}

debug log:

solving 67d708dc8de ...
found 67d708dc8de in https://public-inbox.org/git/b83a79beeb456fa4c55aee5cfd204752a7b992c2.1663959325.git.gitgitgadget@gmail.com/
found b0e29e87ec9 in https://public-inbox.org/git/914028341842a4d57e02ec42a7426d3aa83640f9.1663959325.git.gitgitgadget@gmail.com/

applying [1/2] https://public-inbox.org/git/914028341842a4d57e02ec42a7426d3aa83640f9.1663959325.git.gitgitgadget@gmail.com/
diff --git a/builtin/change.c b/builtin/change.c
new file mode 100644
index 00000000000..b0e29e87ec9


applying [2/2] https://public-inbox.org/git/b83a79beeb456fa4c55aee5cfd204752a7b992c2.1663959325.git.gitgitgadget@gmail.com/
diff --git a/builtin/change.c b/builtin/change.c
index b0e29e87ec9..67d708dc8de 100644

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

index at:
100644 67d708dc8dec3b2a3ecbe5dbd44e7607745ab9ca	builtin/change.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).