status.c 7 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 14
#include "vector.h"
#include "tree.h"
#include "git2/status.h"
15
#include "repository.h"
16
#include "ignore.h"
17

18 19
#include "git2/diff.h"
#include "diff.h"
20
#include "diff_output.h"
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

static unsigned int index_delta2status(git_delta_t index_status)
{
	unsigned int st = GIT_STATUS_CURRENT;

	switch (index_status) {
	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;
37 38 39 40 41 42
	case GIT_DELTA_RENAMED:
		st = GIT_STATUS_INDEX_RENAMED;
		break;
	case GIT_DELTA_TYPECHANGE:
		st = GIT_STATUS_INDEX_TYPECHANGE;
		break;
43 44 45 46 47 48 49 50 51 52 53 54 55 56
	default:
		break;
	}

	return st;
}

static unsigned int workdir_delta2status(git_delta_t workdir_status)
{
	unsigned int st = GIT_STATUS_CURRENT;

	switch (workdir_status) {
	case GIT_DELTA_ADDED:
	case GIT_DELTA_RENAMED:
57
	case GIT_DELTA_COPIED:
58 59 60 61 62 63 64 65 66 67 68 69
	case GIT_DELTA_UNTRACKED:
		st = GIT_STATUS_WT_NEW;
		break;
	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;
70 71 72
	case GIT_DELTA_TYPECHANGE:
		st = GIT_STATUS_WT_TYPECHANGE;
		break;
73 74 75 76 77 78 79
	default:
		break;
	}

	return st;
}

80
typedef struct {
81
	git_status_cb cb;
82
	void *payload;
83 84 85
} status_user_callback;

static int status_invoke_cb(
86
	git_diff_delta *i2h, git_diff_delta *w2i, void *payload)
87
{
88
	status_user_callback *usercb = payload;
89 90 91 92 93 94 95 96 97 98 99 100
	const char *path = NULL;
	unsigned int status = 0;

	if (w2i) {
		path = w2i->old_file.path;
		status |= workdir_delta2status(w2i->status);
	}
	if (i2h) {
		path = i2h->old_file.path;
		status |= index_delta2status(i2h->status);
	}

101
	return usercb->cb(path, status, usercb->payload);
102 103
}

104 105
int git_status_foreach_ext(
	git_repository *repo,
106
	const git_status_options *opts,
107
	git_status_cb cb,
108
	void *payload)
109
{
110
	int err = 0;
111
	git_diff_options diffopt = GIT_DIFF_OPTIONS_INIT;
112 113 114 115
	git_diff_list *idx2head = NULL, *wd2idx = NULL;
	git_tree *head = NULL;
	git_status_show_t show =
		opts ? opts->show : GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
116
	status_user_callback usercb;
117 118 119

	assert(show <= GIT_STATUS_SHOW_INDEX_THEN_WORKDIR);

Ben Straub committed
120
	GITERR_CHECK_VERSION(opts, GIT_STATUS_OPTIONS_VERSION, "git_status_options");
121

122 123 124 125
	if (show != GIT_STATUS_SHOW_INDEX_ONLY &&
		(err = git_repository__ensure_not_bare(repo, "status")) < 0)
		return err;

126 127 128 129
	/* if there is no HEAD, that's okay - we'll make an empty iterator */
	if (((err = git_repository_head_tree(&head, repo)) < 0) &&
		!(err == GIT_ENOTFOUND || err == GIT_EORPHANEDHEAD))
			return err;
130

131 132
	memcpy(&diffopt.pathspec, &opts->pathspec, sizeof(diffopt.pathspec));

133
	diffopt.flags = GIT_DIFF_INCLUDE_TYPECHANGE;
134

Russell Belfer committed
135 136 137 138 139 140
	if ((opts->flags & GIT_STATUS_OPT_INCLUDE_UNTRACKED) != 0)
		diffopt.flags = diffopt.flags | GIT_DIFF_INCLUDE_UNTRACKED;
	if ((opts->flags & GIT_STATUS_OPT_INCLUDE_IGNORED) != 0)
		diffopt.flags = diffopt.flags | GIT_DIFF_INCLUDE_IGNORED;
	if ((opts->flags & GIT_STATUS_OPT_INCLUDE_UNMODIFIED) != 0)
		diffopt.flags = diffopt.flags | GIT_DIFF_INCLUDE_UNMODIFIED;
141 142
	if ((opts->flags & GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS) != 0)
		diffopt.flags = diffopt.flags | GIT_DIFF_RECURSE_UNTRACKED_DIRS;
143 144
	if ((opts->flags & GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH) != 0)
		diffopt.flags = diffopt.flags | GIT_DIFF_DISABLE_PATHSPEC_MATCH;
Russell Belfer committed
145
	/* TODO: support EXCLUDE_SUBMODULES flag */
146

147
	if (show != GIT_STATUS_SHOW_WORKDIR_ONLY &&
148
		(err = git_diff_tree_to_index(&idx2head, repo, head, NULL, &diffopt)) < 0)
149 150 151
		goto cleanup;

	if (show != GIT_STATUS_SHOW_INDEX_ONLY &&
152
		(err = git_diff_index_to_workdir(&wd2idx, repo, NULL, &diffopt)) < 0)
153 154
		goto cleanup;

155
	usercb.cb = cb;
156
	usercb.payload = payload;
157

158
	if (show == GIT_STATUS_SHOW_INDEX_THEN_WORKDIR) {
159 160 161 162
		if ((err = git_diff__paired_foreach(
				 idx2head, NULL, status_invoke_cb, &usercb)) < 0)
			goto cleanup;

163 164 165 166
		git_diff_list_free(idx2head);
		idx2head = NULL;
	}

167
	err = git_diff__paired_foreach(idx2head, wd2idx, status_invoke_cb, &usercb);
168 169 170 171 172

cleanup:
	git_tree_free(head);
	git_diff_list_free(idx2head);
	git_diff_list_free(wd2idx);
173

Russell Belfer committed
174 175 176
	if (err == GIT_EUSER)
		giterr_clear();

177 178 179 180 181
	return err;
}

int git_status_foreach(
	git_repository *repo,
182
	git_status_cb callback,
183 184
	void *payload)
{
185
	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
186

Russell Belfer committed
187
	opts.show  = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
188
	opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED |
189 190
		GIT_STATUS_OPT_INCLUDE_UNTRACKED |
		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
191 192 193 194

	return git_status_foreach_ext(repo, &opts, callback, payload);
}

195
struct status_file_info {
196
	char *expected;
197 198
	unsigned int count;
	unsigned int status;
199
	int fnm_flags;
200
	int ambiguous;
201 202
};

203
static int get_one_status(const char *path, unsigned int status, void *data)
204
{
205
	struct status_file_info *sfi = data;
206
	int (*strcomp)(const char *a, const char *b);
207

208 209
	sfi->count++;
	sfi->status = status;
210

211 212
	strcomp = (sfi->fnm_flags & FNM_CASEFOLD) ? git__strcasecmp : git__strcmp;

213
	if (sfi->count > 1 ||
214 215
		(strcomp(sfi->expected, path) != 0 &&
		 p_fnmatch(sfi->expected, path, sfi->fnm_flags) != 0))
216
	{
217
		sfi->ambiguous = true;
218
		return GIT_EAMBIGUOUS;
219
	}
220

221
	return 0;
222 223
}

224
int git_status_file(
225 226 227
	unsigned int *status_flags,
	git_repository *repo,
	const char *path)
228
{
229
	int error;
230 231
	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
	struct status_file_info sfi = {0};
232
	git_index *index;
233

234 235
	assert(status_flags && repo && path);

236 237 238
	if ((error = git_repository_index__weakptr(&index, repo)) < 0)
		return error;

239
	if ((sfi.expected = git__strdup(path)) == NULL)
240
		return -1;
241 242
	if (index->ignore_case)
		sfi.fnm_flags = FNM_CASEFOLD;
243

244 245 246 247 248 249 250
	opts.show  = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
	opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED |
		GIT_STATUS_OPT_INCLUDE_UNTRACKED |
		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS |
		GIT_STATUS_OPT_INCLUDE_UNMODIFIED;
	opts.pathspec.count = 1;
	opts.pathspec.strings = &sfi.expected;
251

252
	error = git_status_foreach_ext(repo, &opts, get_one_status, &sfi);
253

254 255 256
	if (error < 0 && sfi.ambiguous) {
		giterr_set(GITERR_INVALID,
			"Ambiguous path '%s' given to git_status_file", sfi.expected);
257
		error = GIT_EAMBIGUOUS;
258
	}
259

260
	if (!error && !sfi.count) {
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
		git_buf full = GIT_BUF_INIT;

		/* if the file actually exists and we still did not get a callback
		 * for it, then it must be contained inside an ignored directory, so
		 * mark it as such instead of generating an error.
		 */
		if (!git_buf_joinpath(&full, git_repository_workdir(repo), path) &&
			git_path_exists(full.ptr))
			sfi.status = GIT_STATUS_IGNORED;
		else {
			giterr_set(GITERR_INVALID,
				"Attempt to get status of nonexistent file '%s'", path);
			error = GIT_ENOTFOUND;
		}

		git_buf_free(&full);
277 278
	}

279
	*status_flags = sfi.status;
280

281
	git__free(sfi.expected);
282

283
	return error;
284
}
285

286
int git_status_should_ignore(
287 288 289
	int *ignored,
	git_repository *repo,
	const char *path)
Russell Belfer committed
290
{
291
	return git_ignore_path_is_ignored(ignored, repo, path);
Russell Belfer committed
292 293
}