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

#include "common.h"
#include "git2/config.h"
10
#include "git2/sys/config.h"
11 12 13
#include "git2/types.h"
#include "git2/index.h"
#include "buffer.h"
14
#include "buf_text.h"
15 16 17 18 19
#include "vector.h"
#include "posix.h"
#include "config_file.h"
#include "config.h"
#include "repository.h"
Russell Belfer committed
20 21 22
#include "submodule.h"
#include "tree.h"
#include "iterator.h"
23 24
#include "path.h"
#include "index.h"
Russell Belfer committed
25 26

#define GIT_MODULES_FILE ".gitmodules"
27

28 29 30
static git_cvar_map _sm_update_map[] = {
	{GIT_CVAR_STRING, "checkout", GIT_SUBMODULE_UPDATE_CHECKOUT},
	{GIT_CVAR_STRING, "rebase", GIT_SUBMODULE_UPDATE_REBASE},
Russell Belfer committed
31 32
	{GIT_CVAR_STRING, "merge", GIT_SUBMODULE_UPDATE_MERGE},
	{GIT_CVAR_STRING, "none", GIT_SUBMODULE_UPDATE_NONE},
33 34
	{GIT_CVAR_FALSE, NULL, GIT_SUBMODULE_UPDATE_NONE},
	{GIT_CVAR_TRUE, NULL, GIT_SUBMODULE_UPDATE_CHECKOUT},
35 36
};

37
static git_cvar_map _sm_ignore_map[] = {
Russell Belfer committed
38
	{GIT_CVAR_STRING, "none", GIT_SUBMODULE_IGNORE_NONE},
39
	{GIT_CVAR_STRING, "untracked", GIT_SUBMODULE_IGNORE_UNTRACKED},
Russell Belfer committed
40 41
	{GIT_CVAR_STRING, "dirty", GIT_SUBMODULE_IGNORE_DIRTY},
	{GIT_CVAR_STRING, "all", GIT_SUBMODULE_IGNORE_ALL},
42 43
	{GIT_CVAR_FALSE, NULL, GIT_SUBMODULE_IGNORE_NONE},
	{GIT_CVAR_TRUE, NULL, GIT_SUBMODULE_IGNORE_ALL},
44 45
};

46 47 48 49 50 51
static git_cvar_map _sm_recurse_map[] = {
	{GIT_CVAR_STRING, "on-demand", GIT_SUBMODULE_RECURSE_ONDEMAND},
	{GIT_CVAR_FALSE, NULL, GIT_SUBMODULE_RECURSE_NO},
	{GIT_CVAR_TRUE, NULL, GIT_SUBMODULE_RECURSE_YES},
};

52 53 54 55 56 57 58 59 60 61
enum {
	CACHE_OK = 0,
	CACHE_REFRESH = 1,
	CACHE_FLUSH = 2
};
enum {
	GITMODULES_EXISTING = 0,
	GITMODULES_CREATE = 1,
};

62
static kh_inline khint_t str_hash_no_trailing_slash(const char *s)
63
{
64
	khint_t h;
65

66
	for (h = 0; *s; ++s)
67
		if (s[1] != '\0' || *s != '/')
68
			h = (h << 5) - h + *s;
69

70
	return h;
71 72
}

73
static kh_inline int str_equal_no_trailing_slash(const char *a, const char *b)
74
{
75 76
	size_t alen = a ? strlen(a) : 0;
	size_t blen = b ? strlen(b) : 0;
77

78
	if (alen > 0 && a[alen - 1] == '/')
79
		alen--;
80
	if (blen > 0 && b[blen - 1] == '/')
81 82
		blen--;

83
	return (alen == blen && strncmp(a, b, alen) == 0);
84 85
}

Russell Belfer committed
86 87 88 89
__KHASH_IMPL(
	str, static kh_inline, const char *, void *, 1,
	str_hash_no_trailing_slash, str_equal_no_trailing_slash);

90
static int submodule_cache_init(git_repository *repo, int refresh);
91 92
static void submodule_cache_free(git_submodule_cache *cache);

93
static git_config_backend *open_gitmodules(git_submodule_cache *, int gitmod);
94 95
static int get_url_base(git_buf *url, git_repository *repo);
static int lookup_head_remote_key(git_buf *remote_key, git_repository *repo);
96
static int submodule_get(git_submodule **, git_submodule_cache *, const char *, const char *);
97
static int submodule_load_from_config(const git_config_entry *, void *);
98
static int submodule_load_from_wd_lite(git_submodule *);
99 100
static void submodule_get_index_status(unsigned int *, git_submodule *);
static void submodule_get_wd_status(unsigned int *, git_submodule *, git_repository *, git_submodule_ignore_t);
Russell Belfer committed
101 102 103 104 105 106 107 108 109 110 111 112 113

static int submodule_cmp(const void *a, const void *b)
{
	return strcmp(((git_submodule *)a)->name, ((git_submodule *)b)->name);
}

static int submodule_config_key_trunc_puts(git_buf *key, const char *suffix)
{
	ssize_t idx = git_buf_rfind(key, '.');
	git_buf_truncate(key, (size_t)(idx + 1));
	return git_buf_puts(key, suffix);
}

114 115 116
/* lookup submodule or return ENOTFOUND if it doesn't exist */
static int submodule_lookup(
	git_submodule **out,
117
	git_submodule_cache *cache,
118 119 120 121 122 123 124
	const char *name,
	const char *alternate)
{
	khiter_t pos;

	/* lock cache */

125
	pos = git_strmap_lookup_index(cache->submodules, name);
126

127 128
	if (!git_strmap_valid_index(cache->submodules, pos) && alternate)
		pos = git_strmap_lookup_index(cache->submodules, alternate);
129

130
	if (!git_strmap_valid_index(cache->submodules, pos)) {
131 132 133 134 135
		/* unlock cache */
		return GIT_ENOTFOUND; /* don't set error - caller will cope */
	}

	if (out != NULL) {
136
		git_submodule *sm = git_strmap_value_at(cache->submodules, pos);
137 138 139 140 141 142 143 144 145
		GIT_REFCOUNT_INC(sm);
		*out = sm;
	}

	/* unlock cache */

	return 0;
}

146 147 148 149 150 151 152 153 154 155 156 157
/* clear a set of flags on all submodules */
static void submodule_cache_clear_flags(
	git_submodule_cache *cache, uint32_t mask)
{
	git_submodule *sm;
	uint32_t inverted_mask = ~mask;

	git_strmap_foreach_value(cache->submodules, sm, {
		sm->flags &= inverted_mask;
	});
}

Russell Belfer committed
158 159 160 161
/*
 * PUBLIC APIS
 */

162 163 164 165
bool git_submodule__is_submodule(git_repository *repo, const char *name)
{
	git_strmap *map;

166
	if (submodule_cache_init(repo, CACHE_OK) < 0) {
167 168 169 170
		giterr_clear();
		return false;
	}

171
	if (!repo->_submodules || !(map = repo->_submodules->submodules))
172 173 174 175 176
		return false;

	return git_strmap_valid_index(map, git_strmap_lookup_index(map, name));
}

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
static void submodule_set_lookup_error(int error, const char *name)
{
	if (!error)
		return;

	giterr_set(GITERR_SUBMODULE, (error == GIT_ENOTFOUND) ?
		"No submodule named '%s'" :
		"Submodule '%s' has not been added yet", name);
}

int git_submodule__lookup(
	git_submodule **out, /* NULL if user only wants to test existence */
	git_repository *repo,
	const char *name)    /* trailing slash is allowed */
{
	int error;

	assert(repo && name);

196
	if ((error = submodule_cache_init(repo, CACHE_OK)) < 0)
197 198 199 200 201 202 203 204
		return error;

	if ((error = submodule_lookup(out, repo->_submodules, name, NULL)) < 0)
		submodule_set_lookup_error(error, name);

	return error;
}

Russell Belfer committed
205
int git_submodule_lookup(
206
	git_submodule **out, /* NULL if user only wants to test existence */
Russell Belfer committed
207
	git_repository *repo,
208
	const char *name)    /* trailing slash is allowed */
Russell Belfer committed
209 210 211 212 213
{
	int error;

	assert(repo && name);

214
	if ((error = submodule_cache_init(repo, CACHE_REFRESH)) < 0)
Russell Belfer committed
215 216
		return error;

217
	if ((error = submodule_lookup(out, repo->_submodules, name, NULL)) < 0) {
Russell Belfer committed
218 219 220 221 222

		/* check if a plausible submodule exists at path */
		if (git_repository_workdir(repo)) {
			git_buf path = GIT_BUF_INIT;

223 224
			if (git_buf_join3(&path,
					'/', git_repository_workdir(repo), name, DOT_GIT) < 0)
Russell Belfer committed
225 226
				return -1;

227
			if (git_path_exists(path.ptr))
Russell Belfer committed
228 229 230 231 232
				error = GIT_EEXISTS;

			git_buf_free(&path);
		}

233
		submodule_set_lookup_error(error, name);
Russell Belfer committed
234 235
	}

236
	return error;
Russell Belfer committed
237
}
238

239 240 241 242 243
static void submodule_free_dup(void *sm)
{
	git_submodule_free(sm);
}

Russell Belfer committed
244 245 246 247 248 249
int git_submodule_foreach(
	git_repository *repo,
	int (*callback)(git_submodule *sm, const char *name, void *payload),
	void *payload)
{
	int error;
250
	size_t i;
Russell Belfer committed
251
	git_submodule *sm;
252 253
	git_submodule_cache *cache;
	git_vector snapshot = GIT_VECTOR_INIT;
Russell Belfer committed
254 255 256

	assert(repo && callback);

257
	if ((error = submodule_cache_init(repo, CACHE_REFRESH)) < 0)
Russell Belfer committed
258 259
		return error;

260 261 262 263 264 265 266 267 268 269 270 271
	cache = repo->_submodules;

	if (git_mutex_lock(&cache->lock) < 0) {
		giterr_set(GITERR_OS, "Unable to acquire lock on submodule cache");
		return -1;
	}

	if (!(error = git_vector_init(
			&snapshot, kh_size(cache->submodules), submodule_cmp))) {

		git_strmap_foreach_value(cache->submodules, sm, {
			if ((error = git_vector_insert(&snapshot, sm)) < 0)
Russell Belfer committed
272
				break;
273 274 275 276 277
			GIT_REFCOUNT_INC(sm);
		});
	}

	git_mutex_unlock(&cache->lock);
Russell Belfer committed
278

279 280 281 282 283 284
	if (error < 0)
		goto done;

	git_vector_uniq(&snapshot, submodule_free_dup);

	git_vector_foreach(&snapshot, i, sm) {
285
		if ((error = callback(sm, sm->name, payload)) != 0) {
286
			giterr_set_after_callback(error);
Russell Belfer committed
287
			break;
288
		}
289
	}
Russell Belfer committed
290

291 292 293 294
done:
	git_vector_foreach(&snapshot, i, sm)
		git_submodule_free(sm);
	git_vector_free(&snapshot);
Russell Belfer committed
295 296 297 298

	return error;
}

299
void git_submodule_cache_free(git_repository *repo)
Russell Belfer committed
300
{
301
	git_submodule_cache *cache;
Russell Belfer committed
302 303 304

	assert(repo);

305 306
	if ((cache = git__swap(repo->_submodules, NULL)) != NULL)
		submodule_cache_free(cache);
Russell Belfer committed
307 308
}

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
static int submodule_repo_init(
	git_repository **out,
	git_repository *parent_repo,
	const char *path,
	const char *url,
	bool use_gitlink)
{
	int error = 0;
	git_buf workdir = GIT_BUF_INIT, repodir = GIT_BUF_INIT;
	git_repository_init_options initopt = GIT_REPOSITORY_INIT_OPTIONS_INIT;
	git_repository *subrepo = NULL;

	error = git_buf_joinpath(&workdir, git_repository_workdir(parent_repo), path);
	if (error < 0)
		goto cleanup;

	initopt.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_NO_REINIT;
	initopt.origin_url = url;

	/* init submodule repository and add origin remote as needed */

	/* New style: sub-repo goes in <repo-dir>/modules/<name>/ with a
	 * gitlink in the sub-repo workdir directory to that repository
	 *
	 * Old style: sub-repo goes directly into repo/<name>/.git/
	 */
	 if (use_gitlink) {
		error = git_buf_join3(
			&repodir, '/', git_repository_path(parent_repo), "modules", path);
		if (error < 0)
			goto cleanup;

		initopt.workdir_path = workdir.ptr;
		initopt.flags |=
			GIT_REPOSITORY_INIT_NO_DOTGIT_DIR |
			GIT_REPOSITORY_INIT_RELATIVE_GITLINK;

		error = git_repository_init_ext(&subrepo, repodir.ptr, &initopt);
	} else
		error = git_repository_init_ext(&subrepo, workdir.ptr, &initopt);

cleanup:
	git_buf_free(&workdir);
	git_buf_free(&repodir);

	*out = subrepo;

	return error;
}

Russell Belfer committed
359
int git_submodule_add_setup(
360
	git_submodule **out,
Russell Belfer committed
361 362 363 364 365 366
	git_repository *repo,
	const char *url,
	const char *path,
	int use_gitlink)
{
	int error = 0;
Ben Straub committed
367
	git_config_backend *mods = NULL;
368
	git_submodule *sm = NULL;
Russell Belfer committed
369 370 371 372 373 374 375
	git_buf name = GIT_BUF_INIT, real_url = GIT_BUF_INIT;
	git_repository *subrepo = NULL;

	assert(repo && url && path);

	/* see if there is already an entry for this submodule */

376
	if (git_submodule_lookup(NULL, repo, path) < 0)
Russell Belfer committed
377 378 379
		giterr_clear();
	else {
		giterr_set(GITERR_SUBMODULE,
380
			"Attempt to add submodule '%s' that already exists", path);
Russell Belfer committed
381 382 383 384
		return GIT_EEXISTS;
	}

	/* resolve parameters */
385
	if ((error = git_submodule_resolve_url(&real_url, repo, url)) < 0)
Russell Belfer committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
		goto cleanup;

	/* validate and normalize path */

	if (git__prefixcmp(path, git_repository_workdir(repo)) == 0)
		path += strlen(git_repository_workdir(repo));

	if (git_path_root(path) >= 0) {
		giterr_set(GITERR_SUBMODULE, "Submodule path must be a relative path");
		error = -1;
		goto cleanup;
	}

	/* update .gitmodules */

401
	if (!(mods = open_gitmodules(repo->_submodules, GITMODULES_CREATE))) {
Russell Belfer committed
402
		giterr_set(GITERR_SUBMODULE,
403
			"Adding submodules to a bare repository is not supported");
Russell Belfer committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
		return -1;
	}

	if ((error = git_buf_printf(&name, "submodule.%s.path", path)) < 0 ||
		(error = git_config_file_set_string(mods, name.ptr, path)) < 0)
		goto cleanup;

	if ((error = submodule_config_key_trunc_puts(&name, "url")) < 0 ||
		(error = git_config_file_set_string(mods, name.ptr, real_url.ptr)) < 0)
		goto cleanup;

	git_buf_clear(&name);

	/* init submodule repository and add origin remote as needed */

	error = git_buf_joinpath(&name, git_repository_workdir(repo), path);
	if (error < 0)
		goto cleanup;

423 424
	/* if the repo does not already exist, then init a new repo and add it.
	 * Otherwise, just add the existing repo.
Russell Belfer committed
425
	 */
426 427 428
	if (!(git_path_exists(name.ptr) &&
		git_path_contains(&name, DOT_GIT))) {
		if ((error = submodule_repo_init(&subrepo, repo, path, real_url.ptr, use_gitlink)) < 0)
Russell Belfer committed
429 430 431 432 433
			goto cleanup;
	}

	/* add submodule to hash and "reload" it */

434 435 436 437 438 439
	if (git_mutex_lock(&repo->_submodules->lock) < 0) {
		giterr_set(GITERR_OS, "Unable to acquire lock on submodule cache");
		error = -1;
		goto cleanup;
	}

440
	if (!(error = submodule_get(&sm, repo->_submodules, path, NULL)) &&
441
		!(error = git_submodule_reload(sm, false)))
442
		error = git_submodule_init(sm, false);
Russell Belfer committed
443

444 445
	git_mutex_unlock(&repo->_submodules->lock);

Russell Belfer committed
446
cleanup:
447 448 449 450 451 452
	if (error && sm) {
		git_submodule_free(sm);
		sm = NULL;
	}
	if (out != NULL)
		*out = sm;
Russell Belfer committed
453

454
	git_config_file_free(mods);
Russell Belfer committed
455 456 457 458 459 460 461
	git_repository_free(subrepo);
	git_buf_free(&real_url);
	git_buf_free(&name);

	return error;
}

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
int git_submodule_repo_init(
	git_repository **out,
	const git_submodule *sm,
	int use_gitlink)
{
	int error;
	git_repository *sub_repo = NULL;

	assert(out && sm);

	error = submodule_repo_init(&sub_repo, sm->repo, sm->path, sm->url, use_gitlink);

	*out = sub_repo;

	return error;
}

Russell Belfer committed
479 480 481 482 483 484 485
int git_submodule_add_finalize(git_submodule *sm)
{
	int error;
	git_index *index;

	assert(sm);

486
	if ((error = git_repository_index__weakptr(&index, sm->repo)) < 0 ||
487
		(error = git_index_add_bypath(index, GIT_MODULES_FILE)) < 0)
Russell Belfer committed
488 489
		return error;

490
	return git_submodule_add_to_index(sm, true);
Russell Belfer committed
491 492
}

493
int git_submodule_add_to_index(git_submodule *sm, int write_index)
Russell Belfer committed
494 495
{
	int error;
496
	git_repository *sm_repo = NULL;
Russell Belfer committed
497 498 499 500 501 502 503 504
	git_index *index;
	git_buf path = GIT_BUF_INIT;
	git_commit *head;
	git_index_entry entry;
	struct stat st;

	assert(sm);

505 506 507
	/* force reload of wd OID by git_submodule_open */
	sm->flags = sm->flags & ~GIT_SUBMODULE_STATUS__WD_OID_VALID;

508
	if ((error = git_repository_index__weakptr(&index, sm->repo)) < 0 ||
Russell Belfer committed
509
		(error = git_buf_joinpath(
510
			&path, git_repository_workdir(sm->repo), sm->path)) < 0 ||
Russell Belfer committed
511 512 513 514 515 516 517 518 519 520
		(error = git_submodule_open(&sm_repo, sm)) < 0)
		goto cleanup;

	/* read stat information for submodule working directory */
	if (p_stat(path.ptr, &st) < 0) {
		giterr_set(GITERR_SUBMODULE,
			"Cannot add submodule without working directory");
		error = -1;
		goto cleanup;
	}
521 522

	memset(&entry, 0, sizeof(entry));
523
	entry.path = sm->path;
524 525
	git_index_entry__init_from_stat(
		&entry, &st, !(git_index_caps(index) & GIT_INDEXCAP_NO_FILEMODE));
Russell Belfer committed
526 527 528 529 530 531 532 533

	/* calling git_submodule_open will have set sm->wd_oid if possible */
	if ((sm->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID) == 0) {
		giterr_set(GITERR_SUBMODULE,
			"Cannot add submodule without HEAD to index");
		error = -1;
		goto cleanup;
	}
534
	git_oid_cpy(&entry.id, &sm->wd_oid);
Russell Belfer committed
535 536 537 538 539 540 541 542 543 544 545

	if ((error = git_commit_lookup(&head, sm_repo, &sm->wd_oid)) < 0)
		goto cleanup;

	entry.ctime.seconds = git_commit_time(head);
	entry.ctime.nanoseconds = 0;
	entry.mtime.seconds = git_commit_time(head);
	entry.mtime.nanoseconds = 0;

	git_commit_free(head);

546
	/* add it */
Edward Thomson committed
547
	error = git_index_add(index, &entry);
Russell Belfer committed
548

549 550 551 552 553 554 555 556
	/* write it, if requested */
	if (!error && write_index) {
		error = git_index_write(index);

		if (!error)
			git_oid_cpy(&sm->index_oid, &sm->wd_oid);
	}

Russell Belfer committed
557 558 559 560 561 562
cleanup:
	git_repository_free(sm_repo);
	git_buf_free(&path);
	return error;
}

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
const char *git_submodule_ignore_to_str(git_submodule_ignore_t ignore)
{
	int i;
	for (i = 0; i < (int)ARRAY_SIZE(_sm_ignore_map); ++i)
		if (_sm_ignore_map[i].map_value == ignore)
			return _sm_ignore_map[i].str_match;
	return NULL;
}

const char *git_submodule_update_to_str(git_submodule_update_t update)
{
	int i;
	for (i = 0; i < (int)ARRAY_SIZE(_sm_update_map); ++i)
		if (_sm_update_map[i].map_value == update)
			return _sm_update_map[i].str_match;
	return NULL;
}

581 582 583 584 585 586 587 588 589
const char *git_submodule_recurse_to_str(git_submodule_recurse_t recurse)
{
	int i;
	for (i = 0; i < (int)ARRAY_SIZE(_sm_recurse_map); ++i)
		if (_sm_recurse_map[i].map_value == recurse)
			return _sm_recurse_map[i].str_match;
	return NULL;
}

Russell Belfer committed
590 591 592
int git_submodule_save(git_submodule *submodule)
{
	int error = 0;
Ben Straub committed
593
	git_config_backend *mods;
Russell Belfer committed
594
	git_buf key = GIT_BUF_INIT;
595
	const char *val;
Russell Belfer committed
596 597 598

	assert(submodule);

599
	mods = open_gitmodules(submodule->repo->_submodules, GITMODULES_CREATE);
Russell Belfer committed
600 601
	if (!mods) {
		giterr_set(GITERR_SUBMODULE,
602
			"Adding submodules to a bare repository is not supported");
Russell Belfer committed
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
		return -1;
	}

	if ((error = git_buf_printf(&key, "submodule.%s.", submodule->name)) < 0)
		goto cleanup;

	/* save values for path, url, update, ignore, fetchRecurseSubmodules */

	if ((error = submodule_config_key_trunc_puts(&key, "path")) < 0 ||
		(error = git_config_file_set_string(mods, key.ptr, submodule->path)) < 0)
		goto cleanup;

	if ((error = submodule_config_key_trunc_puts(&key, "url")) < 0 ||
		(error = git_config_file_set_string(mods, key.ptr, submodule->url)) < 0)
		goto cleanup;

619 620 621 622
	if ((error = submodule_config_key_trunc_puts(&key, "branch")) < 0 ||
		(error = git_config_file_set_string(mods, key.ptr, submodule->branch)) < 0)
		goto cleanup;

Russell Belfer committed
623
	if (!(error = submodule_config_key_trunc_puts(&key, "update")) &&
624
		(val = git_submodule_update_to_str(submodule->update)) != NULL)
Russell Belfer committed
625 626 627 628 629
		error = git_config_file_set_string(mods, key.ptr, val);
	if (error < 0)
		goto cleanup;

	if (!(error = submodule_config_key_trunc_puts(&key, "ignore")) &&
630
		(val = git_submodule_ignore_to_str(submodule->ignore)) != NULL)
Russell Belfer committed
631 632 633 634
		error = git_config_file_set_string(mods, key.ptr, val);
	if (error < 0)
		goto cleanup;

635 636 637 638
	if (!(error = submodule_config_key_trunc_puts(&key, "fetchRecurseSubmodules")) &&
		(val = git_submodule_recurse_to_str(submodule->fetch_recurse)) != NULL)
		error = git_config_file_set_string(mods, key.ptr, val);
	if (error < 0)
Russell Belfer committed
639 640 641 642 643 644
		goto cleanup;

	/* update internal defaults */

	submodule->ignore_default = submodule->ignore;
	submodule->update_default = submodule->update;
645
	submodule->fetch_recurse_default = submodule->fetch_recurse;
Russell Belfer committed
646 647 648
	submodule->flags |= GIT_SUBMODULE_STATUS_IN_CONFIG;

cleanup:
649
	git_config_file_free(mods);
Russell Belfer committed
650 651 652 653 654 655 656 657
	git_buf_free(&key);

	return error;
}

git_repository *git_submodule_owner(git_submodule *submodule)
{
	assert(submodule);
658
	return submodule->repo;
Russell Belfer committed
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
}

const char *git_submodule_name(git_submodule *submodule)
{
	assert(submodule);
	return submodule->name;
}

const char *git_submodule_path(git_submodule *submodule)
{
	assert(submodule);
	return submodule->path;
}

const char *git_submodule_url(git_submodule *submodule)
{
	assert(submodule);
	return submodule->url;
}

679 680 681 682
int git_submodule_resolve_url(git_buf *out, git_repository *repo, const char *url)
{
	int error = 0;

683 684 685
	assert(out && repo && url);

	git_buf_sanitize(out);
Carlos Martín Nieto committed
686

687
	if (git_path_is_relative(url)) {
688
		if (!(error = get_url_base(out, repo)))
689 690 691 692 693 694 695 696 697 698 699
			error = git_path_apply_relative(out, url);
	} else if (strchr(url, ':') != NULL || url[0] == '/') {
		error = git_buf_sets(out, url);
	} else {
		giterr_set(GITERR_SUBMODULE, "Invalid format for submodule URL");
		error = -1;
	}

	return error;
}

700 701 702 703 704 705
const char *git_submodule_branch(git_submodule *submodule)
{
	assert(submodule);
	return submodule->branch;
}

Russell Belfer committed
706 707 708 709 710 711 712 713 714 715 716 717
int git_submodule_set_url(git_submodule *submodule, const char *url)
{
	assert(submodule && url);

	git__free(submodule->url);

	submodule->url = git__strdup(url);
	GITERR_CHECK_ALLOC(submodule->url);

	return 0;
}

718
const git_oid *git_submodule_index_id(git_submodule *submodule)
Russell Belfer committed
719 720 721 722 723 724 725 726 727
{
	assert(submodule);

	if (submodule->flags & GIT_SUBMODULE_STATUS__INDEX_OID_VALID)
		return &submodule->index_oid;
	else
		return NULL;
}

728
const git_oid *git_submodule_head_id(git_submodule *submodule)
Russell Belfer committed
729 730 731 732 733 734 735 736 737
{
	assert(submodule);

	if (submodule->flags & GIT_SUBMODULE_STATUS__HEAD_OID_VALID)
		return &submodule->head_oid;
	else
		return NULL;
}

738
const git_oid *git_submodule_wd_id(git_submodule *submodule)
Russell Belfer committed
739 740 741
{
	assert(submodule);

742
	/* load unless we think we have a valid oid */
Russell Belfer committed
743 744 745 746
	if (!(submodule->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID)) {
		git_repository *subrepo;

		/* calling submodule open grabs the HEAD OID if possible */
747
		if (!git_submodule_open_bare(&subrepo, submodule))
Russell Belfer committed
748
			git_repository_free(subrepo);
749 750
		else
			giterr_clear();
Russell Belfer committed
751 752 753 754 755 756 757 758 759 760 761
	}

	if (submodule->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID)
		return &submodule->wd_oid;
	else
		return NULL;
}

git_submodule_ignore_t git_submodule_ignore(git_submodule *submodule)
{
	assert(submodule);
762 763
	return (submodule->ignore < GIT_SUBMODULE_IGNORE_NONE) ?
		GIT_SUBMODULE_IGNORE_NONE : submodule->ignore;
Russell Belfer committed
764 765 766 767 768 769 770 771 772
}

git_submodule_ignore_t git_submodule_set_ignore(
	git_submodule *submodule, git_submodule_ignore_t ignore)
{
	git_submodule_ignore_t old;

	assert(submodule);

773
	if (ignore == GIT_SUBMODULE_IGNORE_RESET)
Russell Belfer committed
774 775 776 777 778 779 780 781 782 783
		ignore = submodule->ignore_default;

	old = submodule->ignore;
	submodule->ignore = ignore;
	return old;
}

git_submodule_update_t git_submodule_update(git_submodule *submodule)
{
	assert(submodule);
784 785
	return (submodule->update < GIT_SUBMODULE_UPDATE_CHECKOUT) ?
		GIT_SUBMODULE_UPDATE_CHECKOUT : submodule->update;
Russell Belfer committed
786 787 788 789 790 791 792 793 794
}

git_submodule_update_t git_submodule_set_update(
	git_submodule *submodule, git_submodule_update_t update)
{
	git_submodule_update_t old;

	assert(submodule);

795
	if (update == GIT_SUBMODULE_UPDATE_RESET)
Russell Belfer committed
796 797 798 799 800 801 802
		update = submodule->update_default;

	old = submodule->update;
	submodule->update = update;
	return old;
}

803
git_submodule_recurse_t git_submodule_fetch_recurse_submodules(
804 805 806 807 808 809
	git_submodule *submodule)
{
	assert(submodule);
	return submodule->fetch_recurse;
}

810
git_submodule_recurse_t git_submodule_set_fetch_recurse_submodules(
811
	git_submodule *submodule,
812
	git_submodule_recurse_t fetch_recurse_submodules)
813
{
814
	git_submodule_recurse_t old;
815 816 817

	assert(submodule);

818 819 820
	if (fetch_recurse_submodules == GIT_SUBMODULE_RECURSE_RESET)
		fetch_recurse_submodules = submodule->fetch_recurse_default;

821
	old = submodule->fetch_recurse;
822
	submodule->fetch_recurse = fetch_recurse_submodules;
823 824 825
	return old;
}

826
int git_submodule_init(git_submodule *sm, int overwrite)
Russell Belfer committed
827 828
{
	int error;
829
	const char *val;
830 831
	git_buf key = GIT_BUF_INIT;
	git_config *cfg = NULL;
Russell Belfer committed
832

833
	if (!sm->url) {
Russell Belfer committed
834
		giterr_set(GITERR_SUBMODULE,
835
			"No URL configured for submodule '%s'", sm->name);
Russell Belfer committed
836 837 838
		return -1;
	}

839
	if ((error = git_repository_config(&cfg, sm->repo)) < 0)
Russell Belfer committed
840 841
		return error;

842 843 844 845 846 847 848
	/* write "submodule.NAME.url" */

	if ((error = git_buf_printf(&key, "submodule.%s.url", sm->name)) < 0 ||
		(error = git_config__update_entry(
			cfg, key.ptr, sm->url, overwrite != 0, false)) < 0)
		goto cleanup;

Russell Belfer committed
849 850
	/* write "submodule.NAME.update" if not default */

851 852 853 854 855 856 857 858 859 860 861 862 863
	val = (sm->update == GIT_SUBMODULE_UPDATE_CHECKOUT) ?
		NULL : git_submodule_update_to_str(sm->update);

	if ((error = git_buf_printf(&key, "submodule.%s.update", sm->name)) < 0 ||
		(error = git_config__update_entry(
			cfg, key.ptr, val, overwrite != 0, false)) < 0)
		goto cleanup;

	/* success */

cleanup:
	git_config_free(cfg);
	git_buf_free(&key);
Russell Belfer committed
864 865 866 867

	return error;
}

868
int git_submodule_sync(git_submodule *sm)
Russell Belfer committed
869
{
870 871 872
	int error = 0;
	git_config *cfg = NULL;
	git_buf key = GIT_BUF_INIT;
873
	git_repository *smrepo = NULL;
874 875

	if (!sm->url) {
Russell Belfer committed
876
		giterr_set(GITERR_SUBMODULE,
877
			"No URL configured for submodule '%s'", sm->name);
Russell Belfer committed
878 879 880 881 882
		return -1;
	}

	/* copy URL over to config only if it already exists */

883 884 885 886 887 888
	if (!(error = git_repository_config__weakptr(&cfg, sm->repo)) &&
		!(error = git_buf_printf(&key, "submodule.%s.url", sm->name)))
		error = git_config__update_entry(cfg, key.ptr, sm->url, true, true);

	/* if submodule exists in the working directory, update remote url */

889 890 891 892
	if (!error &&
		(sm->flags & GIT_SUBMODULE_STATUS_IN_WD) != 0 &&
		!(error = git_submodule_open(&smrepo, sm)))
	{
893 894 895 896 897
		git_buf remote_name = GIT_BUF_INIT;

		if ((error = git_repository_config__weakptr(&cfg, smrepo)) < 0)
			/* return error from reading submodule config */;
		else if ((error = lookup_head_remote_key(&remote_name, smrepo)) < 0) {
898
			giterr_clear();
899 900 901 902 903
			error = git_buf_sets(&key, "branch.origin.remote");
		} else {
			error = git_buf_join3(
				&key, '.', "branch", remote_name.ptr, "remote");
			git_buf_free(&remote_name);
904 905
		}

906 907
		if (!error)
			error = git_config__update_entry(cfg, key.ptr, sm->url, true, false);
908 909 910 911 912 913 914

		git_repository_free(smrepo);
	}

	git_buf_free(&key);

	return error;
Russell Belfer committed
915 916
}

917 918
static int git_submodule__open(
	git_repository **subrepo, git_submodule *sm, bool bare)
Russell Belfer committed
919 920 921
{
	int error;
	git_buf path = GIT_BUF_INIT;
922 923
	unsigned int flags = GIT_REPOSITORY_OPEN_NO_SEARCH;
	const char *wd;
Russell Belfer committed
924

925
	assert(sm && subrepo);
Russell Belfer committed
926

927 928 929
	if (git_repository__ensure_not_bare(
			sm->repo, "open submodule repository") < 0)
		return GIT_EBAREREPO;
Russell Belfer committed
930

931
	wd = git_repository_workdir(sm->repo);
Russell Belfer committed
932

933 934
	if (git_buf_joinpath(&path, wd, sm->path) < 0 ||
		git_buf_joinpath(&path, path.ptr, DOT_GIT) < 0)
Russell Belfer committed
935 936
		return -1;

937 938 939 940
	sm->flags = sm->flags &
		~(GIT_SUBMODULE_STATUS_IN_WD |
		  GIT_SUBMODULE_STATUS__WD_OID_VALID |
		  GIT_SUBMODULE_STATUS__WD_SCANNED);
Russell Belfer committed
941

942 943
	if (bare)
		flags |= GIT_REPOSITORY_OPEN_BARE;
Russell Belfer committed
944

945
	error = git_repository_open_ext(subrepo, path.ptr, flags, wd);
946

947 948 949 950
	/* if we opened the submodule successfully, grab HEAD OID, etc. */
	if (!error) {
		sm->flags |= GIT_SUBMODULE_STATUS_IN_WD |
			GIT_SUBMODULE_STATUS__WD_SCANNED;
951

952 953 954 955 956 957 958 959 960
		if (!git_reference_name_to_id(&sm->wd_oid, *subrepo, GIT_HEAD_FILE))
			sm->flags |= GIT_SUBMODULE_STATUS__WD_OID_VALID;
		else
			giterr_clear();
	} else if (git_path_exists(path.ptr)) {
		sm->flags |= GIT_SUBMODULE_STATUS__WD_SCANNED |
			GIT_SUBMODULE_STATUS_IN_WD;
	} else {
		git_buf_rtruncate_at_char(&path, '/'); /* remove "/.git" */
961

962 963 964
		if (git_path_isdir(path.ptr))
			sm->flags |= GIT_SUBMODULE_STATUS__WD_SCANNED;
	}
965

966
	git_buf_free(&path);
967

968 969
	return error;
}
970

971 972 973 974
int git_submodule_open_bare(git_repository **subrepo, git_submodule *sm)
{
	return git_submodule__open(subrepo, sm, true);
}
Russell Belfer committed
975

976 977 978
int git_submodule_open(git_repository **subrepo, git_submodule *sm)
{
	return git_submodule__open(subrepo, sm, false);
Russell Belfer committed
979 980
}

981
int git_submodule_reload_all(git_repository *repo, int force)
Russell Belfer committed
982
{
983
	return submodule_cache_init(repo, force ? CACHE_FLUSH : CACHE_REFRESH);
Russell Belfer committed
984 985
}

986 987
static void submodule_update_from_index_entry(
	git_submodule *sm, const git_index_entry *ie)
Russell Belfer committed
988
{
989
	bool already_found = (sm->flags & GIT_SUBMODULE_STATUS_IN_INDEX) != 0;
Russell Belfer committed
990

991 992 993 994 995 996 997
	if (!S_ISGITLINK(ie->mode)) {
		if (!already_found)
			sm->flags |= GIT_SUBMODULE_STATUS__INDEX_NOT_SUBMODULE;
	} else {
		if (already_found)
			sm->flags |= GIT_SUBMODULE_STATUS__INDEX_MULTIPLE_ENTRIES;
		else
998
			git_oid_cpy(&sm->index_oid, &ie->id);
Russell Belfer committed
999

1000 1001 1002 1003 1004 1005 1006 1007 1008
		sm->flags |= GIT_SUBMODULE_STATUS_IN_INDEX |
			GIT_SUBMODULE_STATUS__INDEX_OID_VALID;
	}
}

static int submodule_update_index(git_submodule *sm)
{
	git_index *index;
	const git_index_entry *ie;
Russell Belfer committed
1009

1010
	if (git_repository_index__weakptr(&index, sm->repo) < 0)
Russell Belfer committed
1011 1012
		return -1;

1013
	sm->flags = sm->flags &
1014 1015 1016
		~(GIT_SUBMODULE_STATUS_IN_INDEX |
		  GIT_SUBMODULE_STATUS__INDEX_OID_VALID);

1017 1018
	if (!(ie = git_index_get_bypath(index, sm->path, 0)))
		return 0;
Russell Belfer committed
1019

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
	submodule_update_from_index_entry(sm, ie);

	return 0;
}

static void submodule_update_from_head_data(
	git_submodule *sm, mode_t mode, const git_oid *id)
{
	if (!S_ISGITLINK(mode))
		sm->flags |= GIT_SUBMODULE_STATUS__HEAD_NOT_SUBMODULE;
	else {
		git_oid_cpy(&sm->head_oid, id);

		sm->flags |= GIT_SUBMODULE_STATUS_IN_HEAD |
			GIT_SUBMODULE_STATUS__HEAD_OID_VALID;
Russell Belfer committed
1035
	}
1036
}
Russell Belfer committed
1037

1038 1039 1040
static int submodule_update_head(git_submodule *submodule)
{
	git_tree *head = NULL;
nulltoken committed
1041
	git_tree_entry *te = NULL;
Russell Belfer committed
1042

1043 1044 1045
	submodule->flags = submodule->flags &
		~(GIT_SUBMODULE_STATUS_IN_HEAD |
		  GIT_SUBMODULE_STATUS__HEAD_OID_VALID);
Russell Belfer committed
1046

1047 1048 1049 1050 1051 1052
	/* if we can't look up file in current head, then done */
	if (git_repository_head_tree(&head, submodule->repo) < 0 ||
		git_tree_entry_bypath(&te, head, submodule->path) < 0)
		giterr_clear();
	else
		submodule_update_from_head_data(submodule, te->attr, &te->oid);
Russell Belfer committed
1053

nulltoken committed
1054
	git_tree_entry_free(te);
1055 1056 1057
	git_tree_free(head);
	return 0;
}
1058

1059

1060
int git_submodule_reload(git_submodule *sm, int force)
1061 1062 1063
{
	int error = 0;
	git_config_backend *mods;
1064
	git_submodule_cache *cache;
Russell Belfer committed
1065

1066 1067
	GIT_UNUSED(force);

1068 1069 1070
	assert(sm);

	cache = sm->repo->_submodules;
Russell Belfer committed
1071

1072
	/* refresh index data */
1073
	if ((error = submodule_update_index(sm)) < 0)
1074
		return error;
1075 1076

	/* refresh HEAD tree data */
1077
	if ((error = submodule_update_head(sm)) < 0)
1078
		return error;
Russell Belfer committed
1079

1080
	/* done if bare */
1081
	if (git_repository_is_bare(sm->repo))
1082 1083
		return error;

Russell Belfer committed
1084
	/* refresh config data */
1085
	mods = open_gitmodules(cache, GITMODULES_EXISTING);
1086
	if (mods != NULL) {
Russell Belfer committed
1087 1088 1089
		git_buf path = GIT_BUF_INIT;

		git_buf_sets(&path, "submodule\\.");
1090
		git_buf_text_puts_escape_regex(&path, sm->name);
Russell Belfer committed
1091 1092 1093 1094
		git_buf_puts(&path, ".*");

		if (git_buf_oom(&path))
			error = -1;
1095
		else
Russell Belfer committed
1096
			error = git_config_file_foreach_match(
1097
				mods, path.ptr, submodule_load_from_config, cache);
Russell Belfer committed
1098 1099

		git_buf_free(&path);
1100
		git_config_file_free(mods);
Russell Belfer committed
1101

1102 1103 1104
		if (error < 0)
			return error;
	}
1105 1106

	/* refresh wd data */
1107 1108 1109
	sm->flags &=
		~(GIT_SUBMODULE_STATUS_IN_WD | GIT_SUBMODULE_STATUS__WD_OID_VALID |
		  GIT_SUBMODULE_STATUS__WD_FLAGS);
1110

1111
	return submodule_load_from_wd_lite(sm);
Russell Belfer committed
1112 1113
}

1114 1115
static void submodule_copy_oid_maybe(
	git_oid *tgt, const git_oid *src, bool valid)
Russell Belfer committed
1116
{
1117 1118 1119 1120 1121 1122 1123
	if (tgt) {
		if (valid)
			memcpy(tgt, src, sizeof(*tgt));
		else
			memset(tgt, 0, sizeof(*tgt));
	}
}
1124

1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
int git_submodule__status(
	unsigned int *out_status,
	git_oid *out_head_id,
	git_oid *out_index_id,
	git_oid *out_wd_id,
	git_submodule *sm,
	git_submodule_ignore_t ign)
{
	unsigned int status;
	git_repository *smrepo = NULL;
Russell Belfer committed
1135

1136
	if (ign < GIT_SUBMODULE_IGNORE_NONE)
1137
		ign = sm->ignore;
1138

1139 1140 1141 1142
	/* only return location info if ignore == all */
	if (ign == GIT_SUBMODULE_IGNORE_ALL) {
		*out_status = (sm->flags & GIT_SUBMODULE_STATUS__IN_FLAGS);
		return 0;
1143
	}
Russell Belfer committed
1144

1145 1146 1147 1148 1149 1150 1151
	/* refresh the index OID */
	if (submodule_update_index(sm) < 0)
		return -1;

	/* refresh the HEAD OID */
	if (submodule_update_head(sm) < 0)
		return -1;
Russell Belfer committed
1152

1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
	/* for ignore == dirty, don't scan the working directory */
	if (ign == GIT_SUBMODULE_IGNORE_DIRTY) {
		/* git_submodule_open_bare will load WD OID data */
		if (git_submodule_open_bare(&smrepo, sm) < 0)
			giterr_clear();
		else
			git_repository_free(smrepo);
		smrepo = NULL;
	} else if (git_submodule_open(&smrepo, sm) < 0) {
		giterr_clear();
		smrepo = NULL;
	}

	status = GIT_SUBMODULE_STATUS__CLEAR_INTERNAL(sm->flags);

	submodule_get_index_status(&status, sm);
	submodule_get_wd_status(&status, sm, smrepo, ign);

	git_repository_free(smrepo);

	*out_status = status;

	submodule_copy_oid_maybe(out_head_id, &sm->head_oid,
		(sm->flags & GIT_SUBMODULE_STATUS__HEAD_OID_VALID) != 0);
	submodule_copy_oid_maybe(out_index_id, &sm->index_oid,
		(sm->flags & GIT_SUBMODULE_STATUS__INDEX_OID_VALID) != 0);
	submodule_copy_oid_maybe(out_wd_id, &sm->wd_oid,
		(sm->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID) != 0);

	return 0;
Russell Belfer committed
1183 1184
}

1185
int git_submodule_status(unsigned int *status, git_submodule *sm)
1186
{
1187
	assert(status && sm);
1188

1189
	return git_submodule__status(status, NULL, NULL, NULL, sm, 0);
1190
}
1191

1192 1193 1194 1195 1196 1197
int git_submodule_location(unsigned int *location, git_submodule *sm)
{
	assert(location && sm);

	return git_submodule__status(
		location, NULL, NULL, NULL, sm, GIT_SUBMODULE_IGNORE_ALL);
1198 1199 1200
}


Russell Belfer committed
1201 1202 1203 1204
/*
 * INTERNAL FUNCTIONS
 */

1205 1206
static int submodule_alloc(
	git_submodule **out, git_submodule_cache *cache, const char *name)
1207
{
1208
	size_t namelen;
1209
	git_submodule *sm;
1210

1211
	if (!name || !(namelen = strlen(name))) {
1212
		giterr_set(GITERR_SUBMODULE, "Invalid submodule name");
1213
		return -1;
1214 1215
	}

1216
	sm = git__calloc(1, sizeof(git_submodule));
1217
	GITERR_CHECK_ALLOC(sm);
1218

1219 1220 1221
	sm->name = sm->path = git__strdup(name);
	if (!sm->name) {
		git__free(sm);
1222
		return -1;
1223
	}
Russell Belfer committed
1224

1225
	GIT_REFCOUNT_INC(sm);
1226 1227
	sm->ignore = sm->ignore_default = GIT_SUBMODULE_IGNORE_NONE;
	sm->update = sm->update_default = GIT_SUBMODULE_UPDATE_CHECKOUT;
1228
	sm->fetch_recurse = sm->fetch_recurse_default = GIT_SUBMODULE_RECURSE_NO;
1229
	sm->repo   = cache->repo;
1230
	sm->branch = NULL;
1231

1232 1233
	*out = sm;
	return 0;
Russell Belfer committed
1234 1235
}

1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
static void submodule_cache_remove_item(
	git_submodule_cache *cache,
	git_submodule *item,
	bool free_after_remove)
{
	git_strmap *map;
	const char *name, *alt;

	if (!cache || !(map = cache->submodules) || !item)
		return;

	name = item->name;
	alt  = (item->path != item->name) ? item->path : NULL;

	for (; name; name = alt, alt = NULL) {
		khiter_t pos = git_strmap_lookup_index(map, name);
		git_submodule *found;

		if (!git_strmap_valid_index(map, pos))
			continue;

		found = git_strmap_value_at(map, pos);

		if (found != item)
			continue;

		git_strmap_set_value_at(map, pos, NULL);
		git_strmap_delete_at(map, pos);

		if (free_after_remove)
			git_submodule_free(found);
	}
}

1270
static void submodule_release(git_submodule *sm)
Russell Belfer committed
1271 1272 1273 1274
{
	if (!sm)
		return;

1275 1276
	if (sm->repo) {
		git_submodule_cache *cache = sm->repo->_submodules;
1277
		sm->repo = NULL;
1278
		submodule_cache_remove_item(cache, sm, false);
1279 1280
	}

1281 1282 1283 1284
	if (sm->path != sm->name)
		git__free(sm->path);
	git__free(sm->name);
	git__free(sm->url);
1285
	git__free(sm->branch);
1286 1287 1288
	git__memzero(sm, sizeof(*sm));
	git__free(sm);
}
Russell Belfer committed
1289

1290 1291 1292 1293 1294
void git_submodule_free(git_submodule *sm)
{
	if (!sm)
		return;
	GIT_REFCOUNT_DEC(sm, submodule_release);
Russell Belfer committed
1295 1296
}

1297
static int submodule_get(
1298
	git_submodule **out,
1299
	git_submodule_cache *cache,
1300 1301
	const char *name,
	const char *alternate)
Russell Belfer committed
1302
{
1303
	int error = 0;
Russell Belfer committed
1304 1305 1306
	khiter_t pos;
	git_submodule *sm;

1307
	pos = git_strmap_lookup_index(cache->submodules, name);
Russell Belfer committed
1308

1309 1310
	if (!git_strmap_valid_index(cache->submodules, pos) && alternate)
		pos = git_strmap_lookup_index(cache->submodules, alternate);
Russell Belfer committed
1311

1312 1313 1314
	if (!git_strmap_valid_index(cache->submodules, pos)) {
		if ((error = submodule_alloc(&sm, cache, name)) < 0)
			return error;
Russell Belfer committed
1315

1316 1317 1318
		/* insert value at name - if another thread beats us to it, then use
		 * their record and release our own.
		 */
1319
		pos = kh_put(str, cache->submodules, sm->name, &error);
1320

1321 1322 1323
		if (error < 0)
			goto done;
		else if (error == 0) {
1324
			git_submodule_free(sm);
1325
			sm = git_strmap_value_at(cache->submodules, pos);
1326
		} else {
1327
			error = 0;
1328
			git_strmap_set_value_at(cache->submodules, pos, sm);
1329 1330
		}
	} else {
1331
		sm = git_strmap_value_at(cache->submodules, pos);
Russell Belfer committed
1332 1333
	}

1334 1335 1336 1337 1338 1339 1340
done:
	if (error < 0)
		git_submodule_free(sm);
	else if (out) {
		GIT_REFCOUNT_INC(sm);
		*out = sm;
	}
Russell Belfer committed
1341

1342
	return error;
1343 1344
}

1345 1346 1347 1348 1349
static int submodule_config_error(const char *property, const char *value)
{
	giterr_set(GITERR_INVALID,
		"Invalid value for submodule '%s' property: '%s'", property, value);
	return -1;
1350 1351
}

1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
int git_submodule_parse_ignore(git_submodule_ignore_t *out, const char *value)
{
	int val;

	if (git_config_lookup_map_value(
			&val, _sm_ignore_map, ARRAY_SIZE(_sm_ignore_map), value) < 0) {
		*out = GIT_SUBMODULE_IGNORE_NONE;
		return submodule_config_error("ignore", value);
	}

	*out = (git_submodule_ignore_t)val;
	return 0;
}

int git_submodule_parse_update(git_submodule_update_t *out, const char *value)
{
	int val;

	if (git_config_lookup_map_value(
			&val, _sm_update_map, ARRAY_SIZE(_sm_update_map), value) < 0) {
		*out = GIT_SUBMODULE_UPDATE_CHECKOUT;
		return submodule_config_error("update", value);
	}

	*out = (git_submodule_update_t)val;
	return 0;
}

1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
int git_submodule_parse_recurse(git_submodule_recurse_t *out, const char *value)
{
	int val;

	if (git_config_lookup_map_value(
			&val, _sm_recurse_map, ARRAY_SIZE(_sm_recurse_map), value) < 0) {
		*out = GIT_SUBMODULE_RECURSE_YES;
		return submodule_config_error("recurse", value);
	}

	*out = (git_submodule_recurse_t)val;
	return 0;
}

Russell Belfer committed
1394
static int submodule_load_from_config(
1395
	const git_config_entry *entry, void *payload)
1396
{
1397
	git_submodule_cache *cache = payload;
1398
	const char *namestart, *property;
1399
	const char *key = entry->name, *value = entry->value, *path;
1400
	char *alternate = NULL, *replaced = NULL;
1401
	git_buf name = GIT_BUF_INIT;
1402
	git_submodule *sm = NULL;
1403
	int error = 0;
1404 1405 1406 1407 1408 1409

	if (git__prefixcmp(key, "submodule.") != 0)
		return 0;

	namestart = key + strlen("submodule.");
	property  = strrchr(namestart, '.');
1410 1411

	if (!property || (property == namestart))
1412
		return 0;
1413

1414
	property++;
1415
	path = !strcasecmp(property, "path") ? value : NULL;
1416

1417
	if ((error = git_buf_set(&name, namestart, property - namestart - 1)) < 0 ||
1418
		(error = submodule_get(&sm, cache, name.ptr, path)) < 0)
1419
		goto done;
1420

Russell Belfer committed
1421 1422
	sm->flags |= GIT_SUBMODULE_STATUS_IN_CONFIG;

1423 1424 1425
	/* Only from config might we get differing names & paths.  If so, then
	 * update the submodule and insert under the alternative key.
	 */
1426

1427 1428 1429
	/* TODO: if case insensitive filesystem, then the following strcmps
	 * should be strcasecmp
	 */
1430

1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
	if (strcmp(sm->name, name.ptr) != 0) { /* name changed */
		if (!strcmp(sm->path, name.ptr)) { /* already set as path */
			replaced = sm->name;
			sm->name = sm->path;
		} else {
			if (sm->name != sm->path)
				replaced = sm->name;
			alternate = sm->name = git_buf_detach(&name);
		}
	}
	else if (path && strcmp(path, sm->path) != 0) { /* path changed */
		if (!strcmp(sm->name, value)) { /* already set as name */
			replaced = sm->path;
			sm->path = sm->name;
		} else {
			if (sm->path != sm->name)
				replaced = sm->path;
			if ((alternate = git__strdup(value)) == NULL) {
				error = -1;
				goto done;
			}
			sm->path = alternate;
1453
		}
1454
	}
1455

1456 1457 1458 1459 1460 1461 1462 1463
	/* Deregister under name being replaced */
	if (replaced) {
		git_strmap_delete(cache->submodules, replaced);
		git_submodule_free(sm);
		git__free(replaced);
	}

	/* Insert under alternate key */
1464 1465
	if (alternate) {
		void *old_sm = NULL;
1466
		git_strmap_insert2(cache->submodules, alternate, sm, old_sm, error);
1467

1468 1469
		if (error < 0)
			goto done;
1470 1471 1472 1473
		if (error > 0)
			error = 0;

		GIT_REFCOUNT_INC(sm); /* increase refcount for new key */
1474 1475 1476

		/* if we replaced an old module under this key, release the old one */
		if (old_sm && ((git_submodule *)old_sm) != sm) {
1477
			git_submodule_free(old_sm);
1478 1479
			/* TODO: log warning about multiple submodules with same path */
		}
1480 1481
	}

Russell Belfer committed
1482 1483 1484 1485
	/* TODO: Look up path in index and if it is present but not a GITLINK
	 * then this should be deleted (at least to match git's behavior)
	 */

1486
	if (path)
1487
		goto done;
1488 1489

	/* copy other properties into submodule entry */
Russell Belfer committed
1490
	if (strcasecmp(property, "url") == 0) {
1491 1492 1493
		git__free(sm->url);
		sm->url = NULL;

1494 1495 1496 1497
		if (value != NULL && (sm->url = git__strdup(value)) == NULL) {
			error = -1;
			goto done;
		}
1498
	}
1499 1500 1501 1502 1503 1504 1505 1506 1507
	else if (strcasecmp(property, "branch") == 0) {
		git__free(sm->branch);
		sm->branch = NULL;

		if (value != NULL && (sm->branch = git__strdup(value)) == NULL) {
			error = -1;
			goto done;
		}
	}
Russell Belfer committed
1508
	else if (strcasecmp(property, "update") == 0) {
1509 1510
		if ((error = git_submodule_parse_update(&sm->update, value)) < 0)
			goto done;
1511
		sm->update_default = sm->update;
1512
	}
Russell Belfer committed
1513
	else if (strcasecmp(property, "fetchRecurseSubmodules") == 0) {
1514 1515
		if ((error = git_submodule_parse_recurse(&sm->fetch_recurse, value)) < 0)
			goto done;
1516
		sm->fetch_recurse_default = sm->fetch_recurse;
1517
	}
Russell Belfer committed
1518
	else if (strcasecmp(property, "ignore") == 0) {
1519 1520
		if ((error = git_submodule_parse_ignore(&sm->ignore, value)) < 0)
			goto done;
1521
		sm->ignore_default = sm->ignore;
1522 1523 1524
	}
	/* ignore other unknown submodule properties */

1525
done:
1526
	git_submodule_free(sm); /* offset refcount inc from submodule_get() */
1527
	git_buf_free(&name);
1528
	return error;
1529 1530
}

1531
static int submodule_load_from_wd_lite(git_submodule *sm)
Russell Belfer committed
1532 1533 1534
{
	git_buf path = GIT_BUF_INIT;

1535
	if (git_buf_joinpath(&path, git_repository_workdir(sm->repo), sm->path) < 0)
1536
		return -1;
Russell Belfer committed
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547

	if (git_path_isdir(path.ptr))
		sm->flags |= GIT_SUBMODULE_STATUS__WD_SCANNED;

	if (git_path_contains(&path, DOT_GIT))
		sm->flags |= GIT_SUBMODULE_STATUS_IN_WD;

	git_buf_free(&path);
	return 0;
}

1548 1549
static int submodule_cache_refresh_from_index(
	git_submodule_cache *cache, git_index *idx)
1550 1551
{
	int error;
Russell Belfer committed
1552 1553
	git_iterator *i;
	const git_index_entry *entry;
1554

1555
	if ((error = git_iterator_for_index(&i, idx, 0, NULL, NULL)) < 0)
Russell Belfer committed
1556
		return error;
1557

1558
	while (!(error = git_iterator_advance(&entry, i))) {
1559
		khiter_t pos = git_strmap_lookup_index(cache->submodules, entry->path);
1560
		git_submodule *sm;
1561

1562 1563
		if (git_strmap_valid_index(cache->submodules, pos)) {
			sm = git_strmap_value_at(cache->submodules, pos);
1564

1565 1566 1567 1568 1569
			if (S_ISGITLINK(entry->mode))
				submodule_update_from_index_entry(sm, entry);
			else
				sm->flags |= GIT_SUBMODULE_STATUS__INDEX_NOT_SUBMODULE;
		} else if (S_ISGITLINK(entry->mode)) {
1570
			if (!submodule_get(&sm, cache, entry->path, NULL)) {
1571
				submodule_update_from_index_entry(sm, entry);
1572 1573
				git_submodule_free(sm);
			}
1574
		}
1575 1576
	}

1577 1578 1579
	if (error == GIT_ITEROVER)
		error = 0;

Russell Belfer committed
1580 1581 1582 1583 1584
	git_iterator_free(i);

	return error;
}

1585 1586
static int submodule_cache_refresh_from_head(
	git_submodule_cache *cache, git_tree *head)
Russell Belfer committed
1587 1588 1589 1590 1591
{
	int error;
	git_iterator *i;
	const git_index_entry *entry;

1592
	if ((error = git_iterator_for_tree(&i, head, 0, NULL, NULL)) < 0)
Russell Belfer committed
1593 1594
		return error;

1595
	while (!(error = git_iterator_advance(&entry, i))) {
1596
		khiter_t pos = git_strmap_lookup_index(cache->submodules, entry->path);
1597
		git_submodule *sm;
Russell Belfer committed
1598

1599 1600
		if (git_strmap_valid_index(cache->submodules, pos)) {
			sm = git_strmap_value_at(cache->submodules, pos);
1601

1602
			if (S_ISGITLINK(entry->mode))
1603
				submodule_update_from_head_data(sm, entry->mode, &entry->id);
1604 1605 1606
			else
				sm->flags |= GIT_SUBMODULE_STATUS__HEAD_NOT_SUBMODULE;
		} else if (S_ISGITLINK(entry->mode)) {
1607
			if (!submodule_get(&sm, cache, entry->path, NULL)) {
1608
				submodule_update_from_head_data(
1609
					sm, entry->mode, &entry->id);
1610 1611
				git_submodule_free(sm);
			}
1612
		}
Russell Belfer committed
1613 1614
	}

1615 1616 1617
	if (error == GIT_ITEROVER)
		error = 0;

Russell Belfer committed
1618 1619 1620 1621 1622
	git_iterator_free(i);

	return error;
}

Ben Straub committed
1623
static git_config_backend *open_gitmodules(
1624
	git_submodule_cache *cache,
1625
	int okay_to_create)
Russell Belfer committed
1626
{
1627
	const char *workdir = git_repository_workdir(cache->repo);
Russell Belfer committed
1628
	git_buf path = GIT_BUF_INIT;
Ben Straub committed
1629
	git_config_backend *mods = NULL;
Russell Belfer committed
1630 1631 1632 1633 1634 1635 1636 1637

	if (workdir != NULL) {
		if (git_buf_joinpath(&path, workdir, GIT_MODULES_FILE) != 0)
			return NULL;

		if (okay_to_create || git_path_isfile(path.ptr)) {
			/* git_config_file__ondisk should only fail if OOM */
			if (git_config_file__ondisk(&mods, path.ptr) < 0)
1638
				mods = NULL;
Russell Belfer committed
1639
			/* open should only fail here if the file is malformed */
1640
			else if (git_config_file_open(mods, GIT_CONFIG_LEVEL_LOCAL) < 0) {
Russell Belfer committed
1641 1642 1643
				git_config_file_free(mods);
				mods = NULL;
			}
1644 1645 1646
		}
	}

1647 1648
	git_buf_free(&path);

Russell Belfer committed
1649 1650 1651
	return mods;
}

1652
static void submodule_cache_free(git_submodule_cache *cache)
Russell Belfer committed
1653
{
1654
	git_submodule *sm;
Russell Belfer committed
1655

1656 1657
	if (!cache)
		return;
Russell Belfer committed
1658

1659 1660 1661 1662 1663
	git_strmap_foreach_value(cache->submodules, sm, {
		sm->repo = NULL; /* disconnect from repo */
		git_submodule_free(sm);
	});
	git_strmap_free(cache->submodules);
Russell Belfer committed
1664

1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
	git_buf_free(&cache->gitmodules_path);
	git_mutex_free(&cache->lock);
	git__free(cache);
}

static int submodule_cache_alloc(
	git_submodule_cache **out, git_repository *repo)
{
	git_submodule_cache *cache = git__calloc(1, sizeof(git_submodule_cache));
	GITERR_CHECK_ALLOC(cache);

	if (git_mutex_init(&cache->lock) < 0) {
		giterr_set(GITERR_OS, "Unable to initialize submodule cache lock");
		git__free(cache);
		return -1;
	}

1682
	if (git_strmap_alloc(&cache->submodules) < 0) {
1683 1684
		submodule_cache_free(cache);
		return -1;
1685 1686
	}

1687 1688 1689 1690 1691 1692 1693
	cache->repo = repo;
	git_buf_init(&cache->gitmodules_path, 0);

	*out = cache;
	return 0;
}

1694
static int submodule_cache_refresh(git_submodule_cache *cache, int refresh)
1695
{
1696
	int error = 0, update_index, update_head, update_gitmod;
1697 1698
	git_index *idx = NULL;
	git_tree *head = NULL;
1699
	const char *wd = NULL;
1700
	git_buf path = GIT_BUF_INIT;
1701 1702 1703
	git_submodule *sm;
	git_config_backend *mods = NULL;
	uint32_t mask;
1704

1705
	if (!cache || !cache->repo || !refresh)
1706 1707
		return 0;

1708 1709 1710 1711
	if (git_mutex_lock(&cache->lock) < 0) {
		giterr_set(GITERR_OS, "Unable to acquire lock on submodule cache");
		return -1;
	}
1712

1713
	/* get sources that we will need to check */
1714

1715 1716 1717 1718
	if (git_repository_index(&idx, cache->repo) < 0)
		giterr_clear();
	if (git_repository_head_tree(&head, cache->repo) < 0)
		giterr_clear();
Russell Belfer committed
1719

1720 1721 1722
	wd = git_repository_workdir(cache->repo);
	if (wd && (error = git_buf_joinpath(&path, wd, GIT_MODULES_FILE)) < 0)
		goto cleanup;
1723

1724
	/* check for invalidation */
1725

1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
	if (refresh == CACHE_FLUSH)
		update_index = update_head = update_gitmod = true;
	else {
		update_index =
			!idx || git_index__changed_relative_to(idx, &cache->index_stamp);
		update_head =
			!head || !git_oid_equal(&cache->head_id, git_tree_id(head));

		update_gitmod = (wd != NULL) ?
			git_futils_filestamp_check(&cache->gitmodules_stamp, path.ptr) :
			(cache->gitmodules_stamp.mtime != 0);
1737
	}
Russell Belfer committed
1738

1739
	/* clear submodule flags that are to be refreshed */
Russell Belfer committed
1740

1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
	mask = 0;
	if (!idx || update_index)
		mask |= GIT_SUBMODULE_STATUS_IN_INDEX |
			GIT_SUBMODULE_STATUS__INDEX_FLAGS |
			GIT_SUBMODULE_STATUS__INDEX_OID_VALID |
			GIT_SUBMODULE_STATUS__INDEX_MULTIPLE_ENTRIES;
	if (!head || update_head)
		mask |= GIT_SUBMODULE_STATUS_IN_HEAD |
			GIT_SUBMODULE_STATUS__HEAD_OID_VALID;
	if (update_gitmod)
		mask |= GIT_SUBMODULE_STATUS_IN_CONFIG;
	if (mask != 0)
		mask |= GIT_SUBMODULE_STATUS_IN_WD |
			GIT_SUBMODULE_STATUS__WD_SCANNED |
			GIT_SUBMODULE_STATUS__WD_FLAGS |
			GIT_SUBMODULE_STATUS__WD_OID_VALID;
1757 1758
	else
		goto cleanup; /* nothing to do */
1759

1760 1761 1762 1763 1764 1765 1766
	submodule_cache_clear_flags(cache, mask);

	/* add back submodule information from index */

	if (idx && update_index) {
		if ((error = submodule_cache_refresh_from_index(cache, idx)) < 0)
			goto cleanup;
1767

1768 1769
		git_futils_filestamp_set(
			&cache->index_stamp, git_index__filestamp(idx));
1770
	}
Russell Belfer committed
1771

1772
	/* add submodule information from HEAD */
Russell Belfer committed
1773

1774 1775 1776
	if (head && update_head) {
		if ((error = submodule_cache_refresh_from_head(cache, head)) < 0)
			goto cleanup;
1777

1778 1779
		git_oid_cpy(&cache->head_id, git_tree_id(head));
	}
Russell Belfer committed
1780

1781
	/* add submodule information from .gitmodules */
1782

1783 1784 1785
	if (wd && update_gitmod > 0) {
		if ((mods = open_gitmodules(cache, false)) != NULL &&
			(error = git_config_file_foreach(
1786 1787 1788 1789
				mods, submodule_load_from_config, cache)) < 0)
			goto cleanup;
	}

1790
	/* shallow scan submodules in work tree as needed */
1791

1792
	if (wd && mask != 0) {
1793
		git_strmap_foreach_value(cache->submodules, sm, {
1794 1795 1796
			submodule_load_from_wd_lite(sm);
		});
	}
1797

1798 1799 1800
	/* remove submodules that no longer exist */

	git_strmap_foreach_value(cache->submodules, sm, {
1801 1802 1803 1804 1805 1806
		/* purge unless in HEAD, index, or .gitmodules; no sm for wd only */
		if (sm != NULL &&
			!(sm->flags &
			 (GIT_SUBMODULE_STATUS_IN_HEAD |
			  GIT_SUBMODULE_STATUS_IN_INDEX |
			  GIT_SUBMODULE_STATUS_IN_CONFIG)))
1807 1808 1809
			submodule_cache_remove_item(cache, sm, true);
	});

1810
cleanup:
1811
	git_config_file_free(mods);
Russell Belfer committed
1812

1813 1814
	/* TODO: if we got an error, mark submodule config as invalid? */

1815 1816 1817 1818 1819 1820
	git_mutex_unlock(&cache->lock);

	git_index_free(idx);
	git_tree_free(head);
	git_buf_free(&path);

1821 1822 1823
	return error;
}

1824
static int submodule_cache_init(git_repository *repo, int cache_refresh)
1825 1826 1827 1828 1829 1830
{
	int error = 0;
	git_submodule_cache *cache = NULL;

	/* if submodules already exist, just refresh as requested */
	if (repo->_submodules)
1831
		return submodule_cache_refresh(repo->_submodules, cache_refresh);
1832 1833 1834

	/* otherwise create a new cache, load it, and atomically swap it in */
	if (!(error = submodule_cache_alloc(&cache, repo)) &&
1835
		!(error = submodule_cache_refresh(cache, CACHE_FLUSH)))
1836 1837 1838 1839 1840
		cache = git__compare_and_swap(&repo->_submodules, NULL, cache);

	/* might have raced with another thread to set cache, so free if needed */
	if (cache)
		submodule_cache_free(cache);
Russell Belfer committed
1841

1842 1843 1844
	return error;
}

Russell Belfer committed
1845 1846
/* Lookup name of remote of the local tracking branch HEAD points to */
static int lookup_head_remote_key(git_buf *remote_name, git_repository *repo)
1847
{
Russell Belfer committed
1848
	int error;
Russell Belfer committed
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
	git_reference *head = NULL;
	git_buf upstream_name = GIT_BUF_INIT;

	/* lookup and dereference HEAD */
	if ((error = git_repository_head(&head, repo)) < 0)
		return error;

	/* lookup remote tracking branch of HEAD */
	if (!(error = git_branch_upstream_name(
			&upstream_name, repo, git_reference_name(head))))
	{
		/* lookup remote of remote tracking branch */
		error = git_branch_remote_name(remote_name, repo, upstream_name.ptr);

		git_buf_free(&upstream_name);
1864
	}
Russell Belfer committed
1865

Russell Belfer committed
1866
	git_reference_free(head);
1867

1868 1869
	return error;
}
1870

Russell Belfer committed
1871 1872
/* Lookup the remote of the local tracking branch HEAD points to */
static int lookup_head_remote(git_remote **remote, git_repository *repo)
1873 1874
{
	int error;
Russell Belfer committed
1875
	git_buf remote_name = GIT_BUF_INIT;
1876

Russell Belfer committed
1877 1878 1879
	/* lookup remote of remote tracking branch name */
	if (!(error = lookup_head_remote_key(&remote_name, repo)))
		error = git_remote_load(remote, repo, remote_name.ptr);
1880

Russell Belfer committed
1881
	git_buf_free(&remote_name);
Russell Belfer committed
1882

1883 1884
	return error;
}
Russell Belfer committed
1885

Russell Belfer committed
1886 1887
/* Lookup remote, either from HEAD or fall back on origin */
static int lookup_default_remote(git_remote **remote, git_repository *repo)
1888
{
Russell Belfer committed
1889
	int error = lookup_head_remote(remote, repo);
Russell Belfer committed
1890

Russell Belfer committed
1891 1892 1893
	/* if that failed, use 'origin' instead */
	if (error == GIT_ENOTFOUND)
		error = git_remote_load(remote, repo, "origin");
Russell Belfer committed
1894

Russell Belfer committed
1895 1896 1897 1898 1899
	if (error == GIT_ENOTFOUND)
		giterr_set(
			GITERR_SUBMODULE,
			"Cannot get default remote for submodule - no local tracking "
			"branch for HEAD and origin does not exist");
Russell Belfer committed
1900 1901

	return error;
1902 1903
}

Russell Belfer committed
1904
static int get_url_base(git_buf *url, git_repository *repo)
1905 1906
{
	int error;
Russell Belfer committed
1907
	git_remote *remote = NULL;
1908

Russell Belfer committed
1909 1910 1911 1912 1913 1914 1915 1916 1917
	if (!(error = lookup_default_remote(&remote, repo))) {
		error = git_buf_sets(url, git_remote_url(remote));
		git_remote_free(remote);
	}
	else if (error == GIT_ENOTFOUND) {
		/* if repository does not have a default remote, use workdir instead */
		giterr_clear();
		error = git_buf_sets(url, git_repository_workdir(repo));
	}
1918 1919 1920 1921

	return error;
}

1922
static void submodule_get_index_status(unsigned int *status, git_submodule *sm)
Russell Belfer committed
1923
{
1924 1925
	const git_oid *head_oid  = git_submodule_head_id(sm);
	const git_oid *index_oid = git_submodule_index_id(sm);
Russell Belfer committed
1926

1927 1928
	*status = *status & ~GIT_SUBMODULE_STATUS__INDEX_FLAGS;

1929 1930 1931 1932 1933 1934 1935 1936
	if (!head_oid) {
		if (index_oid)
			*status |= GIT_SUBMODULE_STATUS_INDEX_ADDED;
	}
	else if (!index_oid)
		*status |= GIT_SUBMODULE_STATUS_INDEX_DELETED;
	else if (!git_oid_equal(head_oid, index_oid))
		*status |= GIT_SUBMODULE_STATUS_INDEX_MODIFIED;
Russell Belfer committed
1937 1938
}

1939

1940 1941 1942 1943 1944
static void submodule_get_wd_status(
	unsigned int *status,
	git_submodule *sm,
	git_repository *sm_repo,
	git_submodule_ignore_t ign)
1945
{
1946 1947 1948 1949
	const git_oid *index_oid = git_submodule_index_id(sm);
	const git_oid *wd_oid =
		(sm->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID) ? &sm->wd_oid : NULL;
	git_tree *sm_head = NULL;
1950
	git_index *index = NULL;
1951
	git_diff_options opt = GIT_DIFF_OPTIONS_INIT;
1952
	git_diff *diff;
1953

1954
	*status = *status & ~GIT_SUBMODULE_STATUS__WD_FLAGS;
Russell Belfer committed
1955

1956 1957 1958
	if (!index_oid) {
		if (wd_oid)
			*status |= GIT_SUBMODULE_STATUS_WD_ADDED;
Russell Belfer committed
1959
	}
1960 1961 1962 1963 1964 1965
	else if (!wd_oid) {
		if ((sm->flags & GIT_SUBMODULE_STATUS__WD_SCANNED) != 0 &&
			(sm->flags & GIT_SUBMODULE_STATUS_IN_WD) == 0)
			*status |= GIT_SUBMODULE_STATUS_WD_UNINITIALIZED;
		else
			*status |= GIT_SUBMODULE_STATUS_WD_DELETED;
Russell Belfer committed
1966
	}
1967 1968
	else if (!git_oid_equal(index_oid, wd_oid))
		*status |= GIT_SUBMODULE_STATUS_WD_MODIFIED;
Russell Belfer committed
1969

1970 1971 1972
	/* if we have no repo, then we're done */
	if (!sm_repo)
		return;
1973

1974 1975 1976 1977
	/* the diffs below could be optimized with an early termination
	 * option to the git_diff functions, but for now this is sufficient
	 * (and certainly no worse that what core git does).
	 */
1978

1979 1980
	if (ign == GIT_SUBMODULE_IGNORE_NONE)
		opt.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
Russell Belfer committed
1981

1982 1983
	(void)git_repository_index__weakptr(&index, sm_repo);

1984
	/* if we don't have an unborn head, check diff with index */
1985 1986 1987 1988
	if (git_repository_head_tree(&sm_head, sm_repo) < 0)
		giterr_clear();
	else {
		/* perform head to index diff on submodule */
1989
		if (git_diff_tree_to_index(&diff, sm_repo, sm_head, index, &opt) < 0)
1990 1991
			giterr_clear();
		else {
1992
			if (git_diff_num_deltas(diff) > 0)
1993
				*status |= GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED;
1994
			git_diff_free(diff);
1995 1996
			diff = NULL;
		}
Russell Belfer committed
1997

1998
		git_tree_free(sm_head);
1999
	}
2000

2001
	/* perform index-to-workdir diff on submodule */
2002
	if (git_diff_index_to_workdir(&diff, sm_repo, index, &opt) < 0)
2003 2004 2005 2006
		giterr_clear();
	else {
		size_t untracked =
			git_diff_num_deltas_of_type(diff, GIT_DELTA_UNTRACKED);
2007

2008 2009
		if (untracked > 0)
			*status |= GIT_SUBMODULE_STATUS_WD_UNTRACKED;
2010

2011 2012
		if (git_diff_num_deltas(diff) != untracked)
			*status |= GIT_SUBMODULE_STATUS_WD_WD_MODIFIED;
Russell Belfer committed
2013

2014
		git_diff_free(diff);
2015
		diff = NULL;
2016
	}
2017
}