git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
blob b0e29e87ec92ca8cc66c6d7519f5657f2707b180 5481 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
 
#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 update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
	NULL
};

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

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], "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 b0e29e87ec9 ...
found b0e29e87ec9 in https://public-inbox.org/git/914028341842a4d57e02ec42a7426d3aa83640f9.1663959325.git.gitgitgadget@gmail.com/

applying [1/1] 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

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

index at:
100644 b0e29e87ec92ca8cc66c6d7519f5657f2707b180	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).