git

Форк
0
/
reflog-walk.c 
383 строки · 9.0 Кб
1
#define USE_THE_REPOSITORY_VARIABLE
2

3
#include "git-compat-util.h"
4
#include "commit.h"
5
#include "refs.h"
6
#include "diff.h"
7
#include "repository.h"
8
#include "revision.h"
9
#include "string-list.h"
10
#include "reflog-walk.h"
11

12
struct complete_reflogs {
13
	char *ref;
14
	char *short_ref;
15
	struct reflog_info {
16
		struct object_id ooid, noid;
17
		char *email;
18
		timestamp_t timestamp;
19
		int tz;
20
		char *message;
21
	} *items;
22
	int nr, alloc;
23
};
24

25
static int read_one_reflog(struct object_id *ooid, struct object_id *noid,
26
		const char *email, timestamp_t timestamp, int tz,
27
		const char *message, void *cb_data)
28
{
29
	struct complete_reflogs *array = cb_data;
30
	struct reflog_info *item;
31

32
	ALLOC_GROW(array->items, array->nr + 1, array->alloc);
33
	item = array->items + array->nr;
34
	oidcpy(&item->ooid, ooid);
35
	oidcpy(&item->noid, noid);
36
	item->email = xstrdup(email);
37
	item->timestamp = timestamp;
38
	item->tz = tz;
39
	item->message = xstrdup(message);
40
	array->nr++;
41
	return 0;
42
}
43

44
static void free_complete_reflog(struct complete_reflogs *array)
45
{
46
	int i;
47

48
	if (!array)
49
		return;
50

51
	for (i = 0; i < array->nr; i++) {
52
		free(array->items[i].email);
53
		free(array->items[i].message);
54
	}
55
	free(array->items);
56
	free(array->ref);
57
	free(array->short_ref);
58
	free(array);
59
}
60

61
static void complete_reflogs_clear(void *util, const char *str UNUSED)
62
{
63
	struct complete_reflogs *array = util;
64
	free_complete_reflog(array);
65
}
66

67
static struct complete_reflogs *read_complete_reflog(const char *ref)
68
{
69
	struct complete_reflogs *reflogs =
70
		xcalloc(1, sizeof(struct complete_reflogs));
71
	reflogs->ref = xstrdup(ref);
72
	refs_for_each_reflog_ent(get_main_ref_store(the_repository), ref,
73
				 read_one_reflog, reflogs);
74
	if (reflogs->nr == 0) {
75
		const char *name;
76
		void *name_to_free;
77
		name = name_to_free = refs_resolve_refdup(get_main_ref_store(the_repository),
78
							  ref,
79
							  RESOLVE_REF_READING,
80
							  NULL, NULL);
81
		if (name) {
82
			refs_for_each_reflog_ent(get_main_ref_store(the_repository),
83
						 name, read_one_reflog,
84
						 reflogs);
85
			free(name_to_free);
86
		}
87
	}
88
	if (reflogs->nr == 0) {
89
		char *refname = xstrfmt("refs/%s", ref);
90
		refs_for_each_reflog_ent(get_main_ref_store(the_repository),
91
					 refname, read_one_reflog, reflogs);
92
		if (reflogs->nr == 0) {
93
			free(refname);
94
			refname = xstrfmt("refs/heads/%s", ref);
95
			refs_for_each_reflog_ent(get_main_ref_store(the_repository),
96
						 refname, read_one_reflog,
97
						 reflogs);
98
		}
99
		free(refname);
100
	}
101
	return reflogs;
102
}
103

104
static int get_reflog_recno_by_time(struct complete_reflogs *array,
105
	timestamp_t timestamp)
106
{
107
	int i;
108
	for (i = array->nr - 1; i >= 0; i--)
109
		if (timestamp >= array->items[i].timestamp)
110
			return i;
111
	return -1;
112
}
113

114
struct commit_reflog {
115
	int recno;
116
	enum selector_type {
117
		SELECTOR_NONE,
118
		SELECTOR_INDEX,
119
		SELECTOR_DATE
120
	} selector;
121
	struct complete_reflogs *reflogs;
122
};
123

124
struct reflog_walk_info {
125
	struct commit_reflog **logs;
126
	size_t nr, alloc;
127
	struct string_list complete_reflogs;
128
	struct commit_reflog *last_commit_reflog;
129
};
130

131
void init_reflog_walk(struct reflog_walk_info **info)
132
{
133
	CALLOC_ARRAY(*info, 1);
134
	(*info)->complete_reflogs.strdup_strings = 1;
135
}
136

137
void reflog_walk_info_release(struct reflog_walk_info *info)
138
{
139
	size_t i;
140

141
	if (!info)
142
		return;
143

144
	for (i = 0; i < info->nr; i++)
145
		free(info->logs[i]);
146
	string_list_clear_func(&info->complete_reflogs,
147
			       complete_reflogs_clear);
148
	free(info->logs);
149
	free(info);
150
}
151

152
int add_reflog_for_walk(struct reflog_walk_info *info,
153
		struct commit *commit, const char *name)
154
{
155
	timestamp_t timestamp = 0;
156
	int recno = -1;
157
	struct string_list_item *item;
158
	struct complete_reflogs *reflogs;
159
	char *branch, *at = strchr(name, '@');
160
	struct commit_reflog *commit_reflog;
161
	enum selector_type selector = SELECTOR_NONE;
162

163
	if (commit->object.flags & UNINTERESTING)
164
		die("cannot walk reflogs for %s", name);
165

166
	branch = xstrdup(name);
167
	if (at && at[1] == '{') {
168
		char *ep;
169
		branch[at - name] = '\0';
170
		recno = strtoul(at + 2, &ep, 10);
171
		if (*ep != '}') {
172
			recno = -1;
173
			timestamp = approxidate(at + 2);
174
			selector = SELECTOR_DATE;
175
		}
176
		else
177
			selector = SELECTOR_INDEX;
178
	} else
179
		recno = 0;
180

181
	item = string_list_lookup(&info->complete_reflogs, branch);
182
	if (item)
183
		reflogs = item->util;
184
	else {
185
		if (*branch == '\0') {
186
			free(branch);
187
			branch = refs_resolve_refdup(get_main_ref_store(the_repository),
188
						     "HEAD", 0, NULL, NULL);
189
			if (!branch)
190
				die("no current branch");
191

192
		}
193
		reflogs = read_complete_reflog(branch);
194
		if (!reflogs || reflogs->nr == 0) {
195
			char *b;
196
			int ret = repo_dwim_log(the_repository, branch, strlen(branch),
197
						NULL, &b);
198
			if (ret > 1)
199
				free(b);
200
			else if (ret == 1) {
201
				free_complete_reflog(reflogs);
202
				free(branch);
203
				branch = b;
204
				reflogs = read_complete_reflog(branch);
205
			}
206
		}
207
		if (!reflogs || reflogs->nr == 0) {
208
			free_complete_reflog(reflogs);
209
			free(branch);
210
			return -1;
211
		}
212
		string_list_insert(&info->complete_reflogs, branch)->util
213
			= reflogs;
214
	}
215
	free(branch);
216

217
	CALLOC_ARRAY(commit_reflog, 1);
218
	if (recno < 0) {
219
		commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
220
		if (commit_reflog->recno < 0) {
221
			free(commit_reflog);
222
			return -1;
223
		}
224
	} else
225
		commit_reflog->recno = reflogs->nr - recno - 1;
226
	commit_reflog->selector = selector;
227
	commit_reflog->reflogs = reflogs;
228

229
	ALLOC_GROW(info->logs, info->nr + 1, info->alloc);
230
	info->logs[info->nr++] = commit_reflog;
231

232
	return 0;
233
}
234

235
void get_reflog_selector(struct strbuf *sb,
236
			 struct reflog_walk_info *reflog_info,
237
			 struct date_mode dmode, int force_date,
238
			 int shorten)
239
{
240
	struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
241
	struct reflog_info *info;
242
	const char *printed_ref;
243

244
	if (!commit_reflog)
245
		return;
246

247
	if (shorten) {
248
		if (!commit_reflog->reflogs->short_ref)
249
			commit_reflog->reflogs->short_ref
250
				= refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
251
							       commit_reflog->reflogs->ref,
252
							       0);
253
		printed_ref = commit_reflog->reflogs->short_ref;
254
	} else {
255
		printed_ref = commit_reflog->reflogs->ref;
256
	}
257

258
	strbuf_addf(sb, "%s@{", printed_ref);
259
	if (commit_reflog->selector == SELECTOR_DATE ||
260
	    (commit_reflog->selector == SELECTOR_NONE && force_date)) {
261
		info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
262
		strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
263
	} else {
264
		strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
265
			    - 2 - commit_reflog->recno);
266
	}
267

268
	strbuf_addch(sb, '}');
269
}
270

271
void get_reflog_message(struct strbuf *sb,
272
			struct reflog_walk_info *reflog_info)
273
{
274
	struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
275
	struct reflog_info *info;
276
	size_t len;
277

278
	if (!commit_reflog)
279
		return;
280

281
	info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
282
	len = strlen(info->message);
283
	if (len > 0)
284
		len--; /* strip away trailing newline */
285
	strbuf_add(sb, info->message, len);
286
}
287

288
const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
289
{
290
	struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
291
	struct reflog_info *info;
292

293
	if (!commit_reflog)
294
		return NULL;
295

296
	info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
297
	return info->email;
298
}
299

300
timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)
301
{
302
	struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
303
	struct reflog_info *info;
304

305
	if (!commit_reflog)
306
		return 0;
307

308
	info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
309
	return info->timestamp;
310
}
311

312
void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
313
			 struct date_mode dmode, int force_date)
314
{
315
	if (reflog_info && reflog_info->last_commit_reflog) {
316
		struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
317
		struct reflog_info *info;
318
		struct strbuf selector = STRBUF_INIT;
319

320
		info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
321
		get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
322
		if (oneline) {
323
			printf("%s: %s", selector.buf, info->message);
324
		}
325
		else {
326
			printf("Reflog: %s (%s)\nReflog message: %s",
327
			       selector.buf, info->email, info->message);
328
		}
329

330
		strbuf_release(&selector);
331
	}
332
}
333

334
int reflog_walk_empty(struct reflog_walk_info *info)
335
{
336
	return !info || !info->nr;
337
}
338

339
static struct commit *next_reflog_commit(struct commit_reflog *log)
340
{
341
	for (; log->recno >= 0; log->recno--) {
342
		struct reflog_info *entry = &log->reflogs->items[log->recno];
343
		struct object *obj = parse_object(the_repository,
344
						  &entry->noid);
345

346
		if (obj && obj->type == OBJ_COMMIT)
347
			return (struct commit *)obj;
348
	}
349
	return NULL;
350
}
351

352
static timestamp_t log_timestamp(struct commit_reflog *log)
353
{
354
	return log->reflogs->items[log->recno].timestamp;
355
}
356

357
struct commit *next_reflog_entry(struct reflog_walk_info *walk)
358
{
359
	struct commit_reflog *best = NULL;
360
	struct commit *best_commit = NULL;
361
	size_t i;
362

363
	for (i = 0; i < walk->nr; i++) {
364
		struct commit_reflog *log = walk->logs[i];
365
		struct commit *commit = next_reflog_commit(log);
366

367
		if (!commit)
368
			continue;
369

370
		if (!best || log_timestamp(log) > log_timestamp(best)) {
371
			best = log;
372
			best_commit = commit;
373
		}
374
	}
375

376
	if (best) {
377
		best->recno--;
378
		walk->last_commit_reflog = best;
379
		return best_commit;
380
	}
381

382
	return NULL;
383
}
384

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.