status.c 14.2 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7 8 9 10 11
 */

#include "common.h"
#include "git2.h"
#include "fileops.h"
#include "hash.h"
12 13
#include "vector.h"
#include "tree.h"
14
#include "status.h"
15
#include "git2/status.h"
16
#include "repository.h"
17
#include "ignore.h"
18
#include "index.h"
19

20 21 22
#include "git2/diff.h"
#include "diff.h"

23
static unsigned int index_delta2status(const git_diff_delta *head2idx)
24
{
25
	git_status_t st = GIT_STATUS_CURRENT;
26

27
	switch (head2idx->status) {
28 29 30 31 32 33 34 35 36 37
	case GIT_DELTA_ADDED:
	case GIT_DELTA_COPIED:
		st = GIT_STATUS_INDEX_NEW;
		break;
	case GIT_DELTA_DELETED:
		st = GIT_STATUS_INDEX_DELETED;
		break;
	case GIT_DELTA_MODIFIED:
		st = GIT_STATUS_INDEX_MODIFIED;
		break;
38 39
	case GIT_DELTA_RENAMED:
		st = GIT_STATUS_INDEX_RENAMED;
40

41
		if (!git_oid_equal(&head2idx->old_file.id, &head2idx->new_file.id))
42
			st |= GIT_STATUS_INDEX_MODIFIED;
43 44 45 46
		break;
	case GIT_DELTA_TYPECHANGE:
		st = GIT_STATUS_INDEX_TYPECHANGE;
		break;
47 48 49
	case GIT_DELTA_CONFLICTED:
		st = GIT_STATUS_CONFLICTED;
		break;
50 51 52 53 54 55 56
	default:
		break;
	}

	return st;
}

57
static unsigned int workdir_delta2status(
58
	git_diff *diff, git_diff_delta *idx2wd)
59
{
60
	git_status_t st = GIT_STATUS_CURRENT;
61

62
	switch (idx2wd->status) {
63
	case GIT_DELTA_ADDED:
64
	case GIT_DELTA_COPIED:
65 66 67
	case GIT_DELTA_UNTRACKED:
		st = GIT_STATUS_WT_NEW;
		break;
68 69 70
	case GIT_DELTA_UNREADABLE:
		st = GIT_STATUS_WT_UNREADABLE;
		break;
71 72 73 74 75 76 77 78 79
	case GIT_DELTA_DELETED:
		st = GIT_STATUS_WT_DELETED;
		break;
	case GIT_DELTA_MODIFIED:
		st = GIT_STATUS_WT_MODIFIED;
		break;
	case GIT_DELTA_IGNORED:
		st = GIT_STATUS_IGNORED;
		break;
80 81
	case GIT_DELTA_RENAMED:
		st = GIT_STATUS_WT_RENAMED;
82

83
		if (!git_oid_equal(&idx2wd->old_file.id, &idx2wd->new_file.id)) {
84 85 86
			/* if OIDs don't match, we might need to calculate them now to
			 * discern between RENAMED vs RENAMED+MODIFED
			 */
87
			if (git_oid_iszero(&idx2wd->old_file.id) &&
88 89
				diff->old_src == GIT_ITERATOR_TYPE_WORKDIR &&
				!git_diff__oid_for_file(
90 91
					&idx2wd->old_file.id, diff, idx2wd->old_file.path,
					idx2wd->old_file.mode, idx2wd->old_file.size))
92
			idx2wd->old_file.flags |= GIT_DIFF_FLAG_VALID_ID;
93

94
			if (git_oid_iszero(&idx2wd->new_file.id) &&
95 96
				diff->new_src == GIT_ITERATOR_TYPE_WORKDIR &&
				!git_diff__oid_for_file(
97 98
					&idx2wd->new_file.id, diff, idx2wd->new_file.path,
					idx2wd->new_file.mode, idx2wd->new_file.size))
99
				idx2wd->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;
100

101
			if (!git_oid_equal(&idx2wd->old_file.id, &idx2wd->new_file.id))
102 103
				st |= GIT_STATUS_WT_MODIFIED;
		}
104
		break;
105 106 107
	case GIT_DELTA_TYPECHANGE:
		st = GIT_STATUS_WT_TYPECHANGE;
		break;
108 109 110
	case GIT_DELTA_CONFLICTED:
		st = GIT_STATUS_CONFLICTED;
		break;
111 112 113 114 115 116 117
	default:
		break;
	}

	return st;
}

118
static bool status_is_included(
119
	git_status_list *status,
120 121
	git_diff_delta *head2idx,
	git_diff_delta *idx2wd)
122
{
123 124 125
	if (!(status->opts.flags & GIT_STATUS_OPT_EXCLUDE_SUBMODULES))
		return 1;

126
	/* if excluding submodules and this is a submodule everywhere */
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	if (head2idx) {
		if (head2idx->status != GIT_DELTA_ADDED &&
			head2idx->old_file.mode != GIT_FILEMODE_COMMIT)
			return 1;
		if (head2idx->status != GIT_DELTA_DELETED &&
			head2idx->new_file.mode != GIT_FILEMODE_COMMIT)
			return 1;
	}
	if (idx2wd) {
		if (idx2wd->status != GIT_DELTA_ADDED &&
			idx2wd->old_file.mode != GIT_FILEMODE_COMMIT)
			return 1;
		if (idx2wd->status != GIT_DELTA_DELETED &&
			idx2wd->new_file.mode != GIT_FILEMODE_COMMIT)
			return 1;
142 143
	}

144 145
	/* only get here if every valid mode is GIT_FILEMODE_COMMIT */
	return 0;
146 147
}

148
static git_status_t status_compute(
149
	git_status_list *status,
150 151 152
	git_diff_delta *head2idx,
	git_diff_delta *idx2wd)
{
153
	git_status_t st = GIT_STATUS_CURRENT;
154 155

	if (head2idx)
156
		st |= index_delta2status(head2idx);
157 158

	if (idx2wd)
159
		st |= workdir_delta2status(status->idx2wd, idx2wd);
160

161
	return st;
162 163 164 165 166
}

static int status_collect(
	git_diff_delta *head2idx,
	git_diff_delta *idx2wd,
167
	void *payload)
168
{
169
	git_status_list *status = payload;
170
	git_status_entry *status_entry;
171

172
	if (!status_is_included(status, head2idx, idx2wd))
173
		return 0;
174

175
	status_entry = git__malloc(sizeof(git_status_entry));
176
	GITERR_CHECK_ALLOC(status_entry);
177

178
	status_entry->status = status_compute(status, head2idx, idx2wd);
179 180 181
	status_entry->head_to_index = head2idx;
	status_entry->index_to_workdir = idx2wd;

182
	return git_vector_insert(&status->paired, status_entry);
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
GIT_INLINE(int) status_entry_cmp_base(
	const void *a,
	const void *b,
	int (*strcomp)(const char *a, const char *b))
{
	const git_status_entry *entry_a = a;
	const git_status_entry *entry_b = b;
	const git_diff_delta *delta_a, *delta_b;

	delta_a = entry_a->index_to_workdir ? entry_a->index_to_workdir :
		entry_a->head_to_index;
	delta_b = entry_b->index_to_workdir ? entry_b->index_to_workdir :
		entry_b->head_to_index;

	if (!delta_a && delta_b)
		return -1;
	if (delta_a && !delta_b)
		return 1;
	if (!delta_a && !delta_b)
		return 0;

	return strcomp(delta_a->new_file.path, delta_b->new_file.path);
}

static int status_entry_icmp(const void *a, const void *b)
{
	return status_entry_cmp_base(a, b, git__strcasecmp);
}

static int status_entry_cmp(const void *a, const void *b)
{
	return status_entry_cmp_base(a, b, git__strcmp);
}

static git_status_list *git_status_list_alloc(git_index *index)
220
{
221
	git_status_list *status = NULL;
222 223
	int (*entrycmp)(const void *a, const void *b);

224 225 226
	if (!(status = git__calloc(1, sizeof(git_status_list))))
		return NULL;

227
	entrycmp = index->ignore_case ? status_entry_icmp : status_entry_cmp;
228

229 230
	if (git_vector_init(&status->paired, 0, entrycmp) < 0) {
		git__free(status);
231
		return NULL;
232
	}
233

234
	return status;
235 236
}

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
static int status_validate_options(const git_status_options *opts)
{
	if (!opts)
		return 0;

	GITERR_CHECK_VERSION(opts, GIT_STATUS_OPTIONS_VERSION, "git_status_options");

	if (opts->show > GIT_STATUS_SHOW_WORKDIR_ONLY) {
		giterr_set(GITERR_INVALID, "Unknown status 'show' option");
		return -1;
	}

	if ((opts->flags & GIT_STATUS_OPT_NO_REFRESH) != 0 &&
		(opts->flags & GIT_STATUS_OPT_UPDATE_INDEX) != 0) {
		giterr_set(GITERR_INVALID, "Updating index from status "
			"is not allowed when index refresh is disabled");
		return -1;
	}

	return 0;
}

259 260 261 262 263
int git_status_list_new(
	git_status_list **out,
	git_repository *repo,
	const git_status_options *opts)
{
264
	git_index *index = NULL;
265
	git_status_list *status = NULL;
266
	git_diff_options diffopt = GIT_DIFF_OPTIONS_INIT;
267
	git_diff_find_options findopt = GIT_DIFF_FIND_OPTIONS_INIT;
268 269 270
	git_tree *head = NULL;
	git_status_show_t show =
		opts ? opts->show : GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
271
	int error = 0;
272
	unsigned int flags = opts ? opts->flags : GIT_STATUS_OPT_DEFAULTS;
273

274 275
	*out = NULL;

276 277
	if (status_validate_options(opts) < 0)
		return -1;
278

279 280
	if ((error = git_repository__ensure_not_bare(repo, "status")) < 0 ||
		(error = git_repository_index(&index, repo)) < 0)
281
		return error;
282

283
	/* if there is no HEAD, that's okay - we'll make an empty iterator */
284 285 286 287
	if ((error = git_repository_head_tree(&head, repo)) < 0) {
		if (error != GIT_ENOTFOUND && error != GIT_EUNBORNBRANCH)
			goto done;
		giterr_clear();
288
	}
289

290 291
	/* refresh index from disk unless prevented */
	if ((flags & GIT_STATUS_OPT_NO_REFRESH) == 0 &&
292
		git_index_read(index, false) < 0)
293 294
		giterr_clear();

295 296
	status = git_status_list_alloc(index);
	GITERR_CHECK_ALLOC(status);
297

298 299 300 301
	if (opts) {
		memcpy(&status->opts, opts, sizeof(git_status_options));
		memcpy(&diffopt.pathspec, &opts->pathspec, sizeof(diffopt.pathspec));
	}
302

303
	diffopt.flags = GIT_DIFF_INCLUDE_TYPECHANGE;
304
	findopt.flags = GIT_DIFF_FIND_FOR_UNTRACKED;
305

306
	if ((flags & GIT_STATUS_OPT_INCLUDE_UNTRACKED) != 0)
Russell Belfer committed
307
		diffopt.flags = diffopt.flags | GIT_DIFF_INCLUDE_UNTRACKED;
308
	if ((flags & GIT_STATUS_OPT_INCLUDE_IGNORED) != 0)
Russell Belfer committed
309
		diffopt.flags = diffopt.flags | GIT_DIFF_INCLUDE_IGNORED;
310
	if ((flags & GIT_STATUS_OPT_INCLUDE_UNMODIFIED) != 0)
Russell Belfer committed
311
		diffopt.flags = diffopt.flags | GIT_DIFF_INCLUDE_UNMODIFIED;
312
	if ((flags & GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS) != 0)
313
		diffopt.flags = diffopt.flags | GIT_DIFF_RECURSE_UNTRACKED_DIRS;
314
	if ((flags & GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH) != 0)
315
		diffopt.flags = diffopt.flags | GIT_DIFF_DISABLE_PATHSPEC_MATCH;
316
	if ((flags & GIT_STATUS_OPT_RECURSE_IGNORED_DIRS) != 0)
317
		diffopt.flags = diffopt.flags | GIT_DIFF_RECURSE_IGNORED_DIRS;
318
	if ((flags & GIT_STATUS_OPT_EXCLUDE_SUBMODULES) != 0)
319
		diffopt.flags = diffopt.flags | GIT_DIFF_IGNORE_SUBMODULES;
320 321
	if ((flags & GIT_STATUS_OPT_UPDATE_INDEX) != 0)
		diffopt.flags = diffopt.flags | GIT_DIFF_UPDATE_INDEX;
322 323
	if ((flags & GIT_STATUS_OPT_INCLUDE_UNREADABLE) != 0)
		diffopt.flags = diffopt.flags | GIT_DIFF_INCLUDE_UNREADABLE;
324 325
	if ((flags & GIT_STATUS_OPT_INCLUDE_UNREADABLE_AS_UNTRACKED) != 0)
		diffopt.flags = diffopt.flags | GIT_DIFF_INCLUDE_UNREADABLE_AS_UNTRACKED;
326

327
	if ((flags & GIT_STATUS_OPT_RENAMES_FROM_REWRITES) != 0)
328 329 330 331
		findopt.flags = findopt.flags |
			GIT_DIFF_FIND_AND_BREAK_REWRITES |
			GIT_DIFF_FIND_RENAMES_FROM_REWRITES |
			GIT_DIFF_BREAK_REWRITES_FOR_RENAMES_ONLY;
332

333
	if (show != GIT_STATUS_SHOW_WORKDIR_ONLY) {
334
		if ((error = git_diff_tree_to_index(
335
				&status->head2idx, repo, head, index, &diffopt)) < 0)
336
			goto done;
337

338
		if ((flags & GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX) != 0 &&
339
			(error = git_diff_find_similar(status->head2idx, &findopt)) < 0)
340
			goto done;
341
	}
342

343
	if (show != GIT_STATUS_SHOW_INDEX_ONLY) {
344
		if ((error = git_diff_index_to_workdir(
345
				&status->idx2wd, repo, index, &diffopt)) < 0) {
346
			goto done;
347
		}
348

349
		if ((flags & GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR) != 0 &&
350
			(error = git_diff_find_similar(status->idx2wd, &findopt)) < 0)
351
			goto done;
352
	}
353

354 355 356 357
	error = git_diff__paired_foreach(
		status->head2idx, status->idx2wd, status_collect, status);
	if (error < 0)
		goto done;
358

359 360 361 362 363 364 365 366 367 368
	if (flags & GIT_STATUS_OPT_SORT_CASE_SENSITIVELY)
		git_vector_set_cmp(&status->paired, status_entry_cmp);
	if (flags & GIT_STATUS_OPT_SORT_CASE_INSENSITIVELY)
		git_vector_set_cmp(&status->paired, status_entry_icmp);

	if ((flags &
		 (GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
		  GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR |
		  GIT_STATUS_OPT_SORT_CASE_SENSITIVELY |
		  GIT_STATUS_OPT_SORT_CASE_INSENSITIVELY)) != 0)
369
		git_vector_sort(&status->paired);
370

371 372 373 374 375
done:
	if (error < 0) {
		git_status_list_free(status);
		status = NULL;
	}
376

377
	*out = status;
378

379
	git_tree_free(head);
380
	git_index_free(index);
381

382 383 384
	return error;
}

385
size_t git_status_list_entrycount(git_status_list *status)
386
{
387
	assert(status);
388

389
	return status->paired.length;
390 391
}

392
const git_status_entry *git_status_byindex(git_status_list *status, size_t i)
393
{
394
	assert(status);
395

396
	return git_vector_get(&status->paired, i);
397 398
}

399
void git_status_list_free(git_status_list *status)
400
{
401
	if (status == NULL)
402 403
		return;

404 405
	git_diff_free(status->head2idx);
	git_diff_free(status->idx2wd);
406

407
	git_vector_free_deep(&status->paired);
408

409 410
	git__memzero(status, sizeof(*status));
	git__free(status);
411 412 413 414 415 416 417 418
}

int git_status_foreach_ext(
	git_repository *repo,
	const git_status_options *opts,
	git_status_cb cb,
	void *payload)
{
419
	git_status_list *status;
420 421 422 423
	const git_status_entry *status_entry;
	size_t i;
	int error = 0;

424
	if ((error = git_status_list_new(&status, repo, opts)) < 0) {
425
		return error;
426
	}
427

428
	git_vector_foreach(&status->paired, i, status_entry) {
429 430 431 432
		const char *path = status_entry->head_to_index ?
			status_entry->head_to_index->old_file.path :
			status_entry->index_to_workdir->old_file.path;

433
		if ((error = cb(path, status_entry->status, payload)) != 0) {
434
			giterr_set_after_callback(error);
435
			break;
436
		}
437 438
	}

439
	git_status_list_free(status);
Russell Belfer committed
440

441
	return error;
442 443
}

444
int git_status_foreach(git_repository *repo, git_status_cb cb, void *payload)
445
{
446
	return git_status_foreach_ext(repo, NULL, cb, payload);
447 448
}

449
struct status_file_info {
450
	char *expected;
451 452
	unsigned int count;
	unsigned int status;
453
	int fnm_flags;
454
	int ambiguous;
455 456
};

457
static int get_one_status(const char *path, unsigned int status, void *data)
458
{
459
	struct status_file_info *sfi = data;
460
	int (*strcomp)(const char *a, const char *b);
461

462 463
	sfi->count++;
	sfi->status = status;
464

465 466
	strcomp = (sfi->fnm_flags & FNM_CASEFOLD) ? git__strcasecmp : git__strcmp;

467
	if (sfi->count > 1 ||
468 469
		(strcomp(sfi->expected, path) != 0 &&
		 p_fnmatch(sfi->expected, path, sfi->fnm_flags) != 0))
470
	{
471
		sfi->ambiguous = true;
472
		return GIT_EAMBIGUOUS; /* giterr_set will be done by caller */
473
	}
474

475
	return 0;
476 477
}

478
int git_status_file(
479 480 481
	unsigned int *status_flags,
	git_repository *repo,
	const char *path)
482
{
483
	int error;
484 485
	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
	struct status_file_info sfi = {0};
486
	git_index *index;
487

488 489
	assert(status_flags && repo && path);

490 491 492
	if ((error = git_repository_index__weakptr(&index, repo)) < 0)
		return error;

493
	if ((sfi.expected = git__strdup(path)) == NULL)
494
		return -1;
495 496
	if (index->ignore_case)
		sfi.fnm_flags = FNM_CASEFOLD;
497

498
	opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
499
	opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED |
500
		GIT_STATUS_OPT_RECURSE_IGNORED_DIRS |
501 502
		GIT_STATUS_OPT_INCLUDE_UNTRACKED |
		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS |
503 504
		GIT_STATUS_OPT_INCLUDE_UNMODIFIED |
		GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH;
505 506
	opts.pathspec.count = 1;
	opts.pathspec.strings = &sfi.expected;
507

508
	error = git_status_foreach_ext(repo, &opts, get_one_status, &sfi);
509

510 511 512
	if (error < 0 && sfi.ambiguous) {
		giterr_set(GITERR_INVALID,
			"Ambiguous path '%s' given to git_status_file", sfi.expected);
513
		error = GIT_EAMBIGUOUS;
514
	}
515

516
	if (!error && !sfi.count) {
517 518 519
		giterr_set(GITERR_INVALID,
			"Attempt to get status of nonexistent file '%s'", path);
		error = GIT_ENOTFOUND;
520 521
	}

522
	*status_flags = sfi.status;
523

524
	git__free(sfi.expected);
525

526
	return error;
527
}
528

529
int git_status_should_ignore(
530 531 532
	int *ignored,
	git_repository *repo,
	const char *path)
Russell Belfer committed
533
{
534
	return git_ignore_path_is_ignored(ignored, repo, path);
Russell Belfer committed
535 536
}

537
int git_status_init_options(git_status_options *opts, unsigned int version)
538
{
539 540
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_status_options, GIT_STATUS_OPTIONS_INIT);
541 542 543 544 545 546
	return 0;
}

int git_status_list_get_perfdata(
	git_diff_perfdata *out, const git_status_list *status)
{
547 548
	assert(out);
	GITERR_CHECK_VERSION(out, GIT_DIFF_PERFDATA_VERSION, "git_diff_perfdata");
549 550 551 552 553 554 555 556 557 558 559 560 561 562

	out->stat_calls = 0;
	out->oid_calculations = 0;

	if (status->head2idx) {
		out->stat_calls += status->head2idx->perf.stat_calls;
		out->oid_calculations += status->head2idx->perf.oid_calculations;
	}
	if (status->idx2wd) {
		out->stat_calls += status->idx2wd->perf.stat_calls;
		out->oid_calculations += status->idx2wd->perf.oid_calculations;
	}

	return 0;
563
}
564