git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
6c2fca12c14e32af44a82be1e341450d10e435a4 blob 7476 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
 
#include "cache.h"
#include "add-interactive.h"
#include "color.h"
#include "config.h"
#include "diffcore.h"
#include "revision.h"
#include "refs.h"

struct add_i_state {
	struct repository *r;
	int use_color;
	char header_color[COLOR_MAXLEN];
};

static void init_color(struct repository *r, struct add_i_state *s,
		       const char *slot_name, char *dst,
		       const char *default_color)
{
	char *key = xstrfmt("color.interactive.%s", slot_name);
	const char *value;

	if (!s->use_color)
		dst[0] = '\0';
	else if (repo_config_get_value(r, key, &value) ||
		 color_parse(value, dst))
		strlcpy(dst, default_color, COLOR_MAXLEN);

	free(key);
}

static int init_add_i_state(struct repository *r, struct add_i_state *s)
{
	const char *value;

	s->r = r;

	if (repo_config_get_value(r, "color.interactive", &value))
		s->use_color = -1;
	else
		s->use_color =
			git_config_colorbool("color.interactive", value);
	s->use_color = want_color(s->use_color);

	init_color(r, s, "header", s->header_color, GIT_COLOR_BOLD);

	return 0;
}

struct item {
	const char *name;
};

struct list_options {
	const char *header;
	void (*print_item)(int i, struct item *item, void *print_item_data);
	void *print_item_data;
};

static void list(struct item **list, size_t nr,
		 struct add_i_state *s, struct list_options *opts)
{
	int i;

	if (!nr)
		return;

	if (opts->header)
		color_fprintf_ln(stdout, s->header_color,
				 "%s", opts->header);

	for (i = 0; i < nr; i++) {
		opts->print_item(i, list[i], opts->print_item_data);
		putchar('\n');
	}
}

struct adddel {
	uintmax_t add, del;
	unsigned seen:1, binary:1;
};

struct file_list {
	struct file_item {
		struct item item;
		struct adddel index, worktree;
	} **file;
	size_t nr, alloc;
};

static void add_file_item(struct file_list *list, const char *name)
{
	struct file_item *item;

	FLEXPTR_ALLOC_STR(item, item.name, name);

	ALLOC_GROW(list->file, list->nr + 1, list->alloc);
	list->file[list->nr++] = item;
}

static void reset_file_list(struct file_list *list)
{
	size_t i;

	for (i = 0; i < list->nr; i++)
		free(list->file[i]);
	list->nr = 0;
}

static void release_file_list(struct file_list *list)
{
	reset_file_list(list);
	FREE_AND_NULL(list->file);
	list->alloc = 0;
}

static int file_item_cmp(const void *a, const void *b)
{
	const struct file_item * const *f1 = a;
	const struct file_item * const *f2 = b;

	return strcmp((*f1)->item.name, (*f2)->item.name);
}

struct pathname_entry {
	struct hashmap_entry ent;
	size_t index;
	char pathname[FLEX_ARRAY];
};

static int pathname_entry_cmp(const void *unused_cmp_data,
			      const void *entry, const void *entry_or_key,
			      const void *pathname)
{
	const struct pathname_entry *e1 = entry, *e2 = entry_or_key;

	return strcmp(e1->pathname,
		      pathname ? (const char *)pathname : e2->pathname);
}

struct collection_status {
	enum { FROM_WORKTREE = 0, FROM_INDEX = 1 } phase;

	const char *reference;

	struct file_list *list;
	struct hashmap file_map;
};

static void collect_changes_cb(struct diff_queue_struct *q,
			       struct diff_options *options,
			       void *data)
{
	struct collection_status *s = data;
	struct diffstat_t stat = { 0 };
	int i;

	if (!q->nr)
		return;

	compute_diffstat(options, &stat, q);

	for (i = 0; i < stat.nr; i++) {
		const char *name = stat.files[i]->name;
		int hash = strhash(name);
		struct pathname_entry *entry;
		size_t file_index;
		struct file_item *file;
		struct adddel *adddel;

		entry = hashmap_get_from_hash(&s->file_map, hash, name);
		if (entry)
			file_index = entry->index;
		else {
			FLEX_ALLOC_STR(entry, pathname, name);
			hashmap_entry_init(entry, hash);
			entry->index = file_index = s->list->nr;
			hashmap_add(&s->file_map, entry);

			add_file_item(s->list, name);
		}
		file = s->list->file[file_index];

		adddel = s->phase == FROM_INDEX ? &file->index : &file->worktree;
		adddel->seen = 1;
		adddel->add = stat.files[i]->added;
		adddel->del = stat.files[i]->deleted;
		if (stat.files[i]->is_binary)
			adddel->binary = 1;
	}
}

static int get_modified_files(struct repository *r, struct file_list *list,
			      const struct pathspec *ps)
{
	struct object_id head_oid;
	int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
					     &head_oid, NULL);
	struct collection_status s = { FROM_WORKTREE };

	if (repo_read_index_preload(r, ps, 0) < 0)
		return error(_("could not read index"));

	s.list = list;
	hashmap_init(&s.file_map, pathname_entry_cmp, NULL, 0);

	for (s.phase = FROM_WORKTREE; s.phase <= FROM_INDEX; s.phase++) {
		struct rev_info rev;
		struct setup_revision_opt opt = { 0 };

		opt.def = is_initial ?
			empty_tree_oid_hex() : oid_to_hex(&head_oid);

		init_revisions(&rev, NULL);
		setup_revisions(0, NULL, &rev, &opt);

		rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
		rev.diffopt.format_callback = collect_changes_cb;
		rev.diffopt.format_callback_data = &s;

		if (ps)
			copy_pathspec(&rev.prune_data, ps);

		if (s.phase == FROM_INDEX)
			run_diff_index(&rev, 1);
		else {
			rev.diffopt.flags.ignore_dirty_submodules = 1;
			run_diff_files(&rev, 0);
		}
	}
	hashmap_free(&s.file_map, 1);

	/* While the diffs are ordered already, we ran *two* diffs... */
	QSORT(list->file, list->nr, file_item_cmp);

	return 0;
}

static void populate_wi_changes(struct strbuf *buf,
				struct adddel *ad, const char *no_changes)
{
	if (ad->binary)
		strbuf_addstr(buf, _("binary"));
	else if (ad->seen)
		strbuf_addf(buf, "+%"PRIuMAX"/-%"PRIuMAX,
			    (uintmax_t)ad->add, (uintmax_t)ad->del);
	else
		strbuf_addstr(buf, no_changes);
}

struct print_file_item_data {
	const char *modified_fmt;
	struct strbuf buf, index, worktree;
};

static void print_file_item(int i, struct item *item,
			    void *print_file_item_data)
{
	struct file_item *c = (struct file_item *)item;
	struct print_file_item_data *d = print_file_item_data;

	strbuf_reset(&d->index);
	strbuf_reset(&d->worktree);
	strbuf_reset(&d->buf);

	populate_wi_changes(&d->worktree, &c->worktree, _("nothing"));
	populate_wi_changes(&d->index, &c->index, _("unchanged"));
	strbuf_addf(&d->buf, d->modified_fmt,
		    d->index.buf, d->worktree.buf, item->name);

	printf(" %2d: %s", i + 1, d->buf.buf);
}

static int run_status(struct add_i_state *s, const struct pathspec *ps,
		      struct file_list *files, struct list_options *opts)
{
	reset_file_list(files);

	if (get_modified_files(s->r, files, ps) < 0)
		return -1;

	if (files->nr)
		list((struct item **)files->file, files->nr, s, opts);
	putchar('\n');

	return 0;
}

int run_add_i(struct repository *r, const struct pathspec *ps)
{
	struct add_i_state s = { NULL };
	struct print_file_item_data print_file_item_data = {
		"%12s %12s %s", STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
	};
	struct list_options opts = {
		NULL, print_file_item, &print_file_item_data
	};
	struct strbuf header = STRBUF_INIT;
	struct file_list files = { NULL };
	int res = 0;

	if (init_add_i_state(r, &s))
		return error("could not parse `add -i` config");

	strbuf_addstr(&header, "      ");
	strbuf_addf(&header, print_file_item_data.modified_fmt,
		    _("staged"), _("unstaged"), _("path"));
	opts.header = header.buf;

	repo_refresh_and_write_index(r, REFRESH_QUIET, 1);
	if (run_status(&s, ps, &files, &opts) < 0)
		res = -1;

	release_file_list(&files);
	strbuf_release(&print_file_item_data.buf);
	strbuf_release(&print_file_item_data.index);
	strbuf_release(&print_file_item_data.worktree);
	strbuf_release(&header);

	return res;
}
debug log:

solving 6c2fca12c1 ...
found 6c2fca12c1 in https://public-inbox.org/git/83d92a9762b4c10d74d804a6104b5ae76330ca17.1557768471.git.gitgitgadget@gmail.com/ ||
	https://public-inbox.org/git/15f18f5b3ebe77a79886dbcdb7a900647be943dd.1563289115.git.gitgitgadget@gmail.com/
found 2dbf29dee2 in https://public-inbox.org/git/8cafc6ae8d2dd434f46751874074940f13412b9f.1557768471.git.gitgitgadget@gmail.com/ ||
	https://public-inbox.org/git/daff24074a604271d66e6a854ea44546b951f489.1563289115.git.gitgitgadget@gmail.com/
found 59b28011f7 in https://public-inbox.org/git/5e23c0756b5ee543437d0dbf4e9f685df6341bdf.1557768471.git.gitgitgadget@gmail.com/ ||
	https://public-inbox.org/git/b93b055ebeef98be11a1658453df6f85fed87347.1563289115.git.gitgitgadget@gmail.com/
found 482e458dc6 in git.git.git
preparing index
index prepared:
100644 482e458dc60f5c146d0192ab5564e9b8e668ca94	add-interactive.c

applying [1/6] https://public-inbox.org/git/5e23c0756b5ee543437d0dbf4e9f685df6341bdf.1557768471.git.gitgitgadget@gmail.com/
diff --git a/add-interactive.c b/add-interactive.c
index 482e458dc6..59b28011f7 100644

Checking patch add-interactive.c...
Applied patch add-interactive.c cleanly.

skipping https://public-inbox.org/git/b93b055ebeef98be11a1658453df6f85fed87347.1563289115.git.gitgitgadget@gmail.com/ for 59b28011f7
index at:
100644 59b28011f774320222bd4c031d5c472087859fd8	add-interactive.c

applying [2/6] https://public-inbox.org/git/8cafc6ae8d2dd434f46751874074940f13412b9f.1557768471.git.gitgitgadget@gmail.com/
diff --git a/add-interactive.c b/add-interactive.c
index 59b28011f7..2dbf29dee2 100644

Checking patch add-interactive.c...
Applied patch add-interactive.c cleanly.

skipping https://public-inbox.org/git/daff24074a604271d66e6a854ea44546b951f489.1563289115.git.gitgitgadget@gmail.com/ for 2dbf29dee2
index at:
100644 2dbf29dee235bbe87c69a2fdf5b991367067be5d	add-interactive.c

applying [3/6] https://public-inbox.org/git/83d92a9762b4c10d74d804a6104b5ae76330ca17.1557768471.git.gitgitgadget@gmail.com/
diff --git a/add-interactive.c b/add-interactive.c
index 2dbf29dee2..6c2fca12c1 100644

Checking patch add-interactive.c...
Applied patch add-interactive.c cleanly.

skipping https://public-inbox.org/git/15f18f5b3ebe77a79886dbcdb7a900647be943dd.1563289115.git.gitgitgadget@gmail.com/ for 6c2fca12c1
index at:
100644 6c2fca12c14e32af44a82be1e341450d10e435a4	add-interactive.c

git@vger.kernel.org list mirror (unofficial, one of many)

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 git git/ https://public-inbox.org/git \
		git@vger.kernel.org
	public-inbox-index git

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/inbox.comp.version-control.git
	nntp://ie5yzdi7fg72h7s4sdcztq5evakq23rdt33mfyfcddc5u3ndnw24ogqd.onion/inbox.comp.version-control.git
	nntp://4uok3hntl7oi7b4uf4rtfwefqeexfzil2w6kgk2jn5z2f764irre7byd.onion/inbox.comp.version-control.git
	nntp://news.gmane.io/gmane.comp.version-control.git
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for project(s) associated with this inbox:

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

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git