submodule.c 38.7 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
static kh_inline khint_t str_hash_no_trailing_slash(const char *s)
47
{
48
	khint_t h;
49

50
	for (h = 0; *s; ++s)
51
		if (s[1] != '\0' || *s != '/')
52
			h = (h << 5) - h + *s;
53

54
	return h;
55 56
}

57
static kh_inline int str_equal_no_trailing_slash(const char *a, const char *b)
58
{
59 60
	size_t alen = a ? strlen(a) : 0;
	size_t blen = b ? strlen(b) : 0;
61

62
	if (alen > 0 && a[alen - 1] == '/')
63
		alen--;
64
	if (blen > 0 && b[blen - 1] == '/')
65 66
		blen--;

67
	return (alen == blen && strncmp(a, b, alen) == 0);
68 69
}

Russell Belfer committed
70 71 72 73
__KHASH_IMPL(
	str, static kh_inline, const char *, void *, 1,
	str_hash_no_trailing_slash, str_equal_no_trailing_slash);

74
static int load_submodule_config(git_repository *repo);
Ben Straub committed
75
static git_config_backend *open_gitmodules(git_repository *, bool, const git_oid *);
76 77
static int lookup_head_remote(git_buf *url, git_repository *repo);
static int submodule_get(git_submodule **, git_repository *, const char *, const char *);
78
static int submodule_load_from_config(const git_config_entry *, void *);
79 80
static int submodule_load_from_wd_lite(git_submodule *, const char *, void *);
static int submodule_update_config(git_submodule *, const char *, const char *, bool, bool);
81 82
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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

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);
}

/*
 * PUBLIC APIS
 */

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

	assert(repo && name);

110
	if ((error = load_submodule_config(repo)) < 0)
Russell Belfer committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
		return error;

	pos = git_strmap_lookup_index(repo->submodules, name);

	if (!git_strmap_valid_index(repo->submodules, pos)) {
		error = GIT_ENOTFOUND;

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

			if (git_buf_joinpath(&path, git_repository_workdir(repo), name) < 0)
				return -1;

			if (git_path_contains_dir(&path, DOT_GIT))
				error = GIT_EEXISTS;

			git_buf_free(&path);
		}

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

Russell Belfer committed
135 136 137 138 139 140 141 142
		return error;
	}

	if (sm_ptr)
		*sm_ptr = git_strmap_value_at(repo->submodules, pos);

	return 0;
}
143

Russell Belfer committed
144 145 146 147 148 149 150 151
int git_submodule_foreach(
	git_repository *repo,
	int (*callback)(git_submodule *sm, const char *name, void *payload),
	void *payload)
{
	int error;
	git_submodule *sm;
	git_vector seen = GIT_VECTOR_INIT;
152
	git_vector_set_cmp(&seen, submodule_cmp);
Russell Belfer committed
153 154 155

	assert(repo && callback);

156
	if ((error = load_submodule_config(repo)) < 0)
Russell Belfer committed
157 158 159 160 161 162 163
		return error;

	git_strmap_foreach_value(repo->submodules, sm, {
		/* Usually the following will not come into play - it just prevents
		 * us from issuing a callback twice for a submodule where the name
		 * and path are not the same.
		 */
164
		if (GIT_REFCOUNT_VAL(sm) > 1) {
165
			if (git_vector_bsearch(NULL, &seen, sm) != GIT_ENOTFOUND)
Russell Belfer committed
166 167 168 169 170
				continue;
			if ((error = git_vector_insert(&seen, sm)) < 0)
				break;
		}

171
		if (callback(sm, sm->name, payload)) {
Russell Belfer committed
172
			giterr_clear();
173
			error = GIT_EUSER;
Russell Belfer committed
174
			break;
175
		}
Russell Belfer committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	});

	git_vector_free(&seen);

	return error;
}

void git_submodule_config_free(git_repository *repo)
{
	git_strmap *smcfg;
	git_submodule *sm;

	assert(repo);

	smcfg = repo->submodules;
	repo->submodules = NULL;

	if (smcfg == NULL)
		return;

196
	git_strmap_foreach_value(smcfg, sm, { git_submodule_free(sm); });
Russell Belfer committed
197 198 199 200 201 202 203 204 205 206 207
	git_strmap_free(smcfg);
}

int git_submodule_add_setup(
	git_submodule **submodule,
	git_repository *repo,
	const char *url,
	const char *path,
	int use_gitlink)
{
	int error = 0;
Ben Straub committed
208
	git_config_backend *mods = NULL;
Russell Belfer committed
209 210
	git_submodule *sm;
	git_buf name = GIT_BUF_INIT, real_url = GIT_BUF_INIT;
211
	git_repository_init_options initopt = GIT_REPOSITORY_INIT_OPTIONS_INIT;
Russell Belfer committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
	git_repository *subrepo = NULL;

	assert(repo && url && path);

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

	if (git_submodule_lookup(&sm, repo, path) < 0)
		giterr_clear();
	else {
		giterr_set(GITERR_SUBMODULE,
			"Attempt to add a submodule that already exists");
		return GIT_EEXISTS;
	}

	/* resolve parameters */

	if (url[0] == '.' && (url[1] == '/' || (url[1] == '.' && url[2] == '/'))) {
		if (!(error = lookup_head_remote(&real_url, repo)))
			error = git_path_apply_relative(&real_url, url);
	} else if (strchr(url, ':') != NULL || url[0] == '/') {
		error = git_buf_sets(&real_url, url);
	} else {
		giterr_set(GITERR_SUBMODULE, "Invalid format for submodule URL");
		error = -1;
	}
	if (error)
		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 */

	if ((mods = open_gitmodules(repo, true, NULL)) == NULL) {
		giterr_set(GITERR_SUBMODULE,
			"Adding submodules to a bare repository is not supported (for now)");
		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;

	/* 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/
	 */

	initopt.flags = GIT_REPOSITORY_INIT_MKPATH |
		GIT_REPOSITORY_INIT_NO_REINIT;
	initopt.origin_url = real_url.ptr;

	if (git_path_exists(name.ptr) &&
		git_path_contains(&name, DOT_GIT))
	{
		/* repo appears to already exist - reinit? */
	}
	else if (use_gitlink) {
		git_buf repodir = GIT_BUF_INIT;

		error = git_buf_join_n(
			&repodir, '/', 3, git_repository_path(repo), "modules", path);
		if (error < 0)
			goto cleanup;

		initopt.workdir_path = name.ptr;
		initopt.flags |= GIT_REPOSITORY_INIT_NO_DOTGIT_DIR;

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

		git_buf_free(&repodir);
	}
	else {
		error = git_repository_init_ext(&subrepo, name.ptr, &initopt);
	}
	if (error < 0)
		goto cleanup;

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

313 314 315
	if (!(error = submodule_get(&sm, repo, path, NULL)) &&
		!(error = git_submodule_reload(sm)))
		error = git_submodule_init(sm, false);
Russell Belfer committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

cleanup:
	if (submodule != NULL)
		*submodule = !error ? sm : NULL;

	if (mods != NULL)
		git_config_file_free(mods);
	git_repository_free(subrepo);
	git_buf_free(&real_url);
	git_buf_free(&name);

	return error;
}

int git_submodule_add_finalize(git_submodule *sm)
{
	int error;
	git_index *index;

	assert(sm);

337
	if ((error = git_repository_index__weakptr(&index, sm->repo)) < 0 ||
338
		(error = git_index_add_bypath(index, GIT_MODULES_FILE)) < 0)
Russell Belfer committed
339 340
		return error;

341
	return git_submodule_add_to_index(sm, true);
Russell Belfer committed
342 343
}

344
int git_submodule_add_to_index(git_submodule *sm, int write_index)
Russell Belfer committed
345 346
{
	int error;
347
	git_repository *sm_repo = NULL;
Russell Belfer committed
348 349 350 351 352 353 354 355
	git_index *index;
	git_buf path = GIT_BUF_INIT;
	git_commit *head;
	git_index_entry entry;
	struct stat st;

	assert(sm);

356 357 358
	/* force reload of wd OID by git_submodule_open */
	sm->flags = sm->flags & ~GIT_SUBMODULE_STATUS__WD_OID_VALID;

359
	if ((error = git_repository_index__weakptr(&index, sm->repo)) < 0 ||
Russell Belfer committed
360
		(error = git_buf_joinpath(
361
			&path, git_repository_workdir(sm->repo), sm->path)) < 0 ||
Russell Belfer committed
362 363 364 365 366 367 368 369 370 371
		(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;
	}
372 373

	memset(&entry, 0, sizeof(entry));
374
	entry.path = sm->path;
375 376
	git_index_entry__init_from_stat(
		&entry, &st, !(git_index_caps(index) & GIT_INDEXCAP_NO_FILEMODE));
Russell Belfer committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396

	/* 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;
	}
	git_oid_cpy(&entry.oid, &sm->wd_oid);

	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);

397
	/* add it */
Edward Thomson committed
398
	error = git_index_add(index, &entry);
Russell Belfer committed
399

400 401 402 403 404 405 406 407
	/* 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
408 409 410 411 412 413
cleanup:
	git_repository_free(sm_repo);
	git_buf_free(&path);
	return error;
}

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
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;
}

Russell Belfer committed
432 433 434
int git_submodule_save(git_submodule *submodule)
{
	int error = 0;
Ben Straub committed
435
	git_config_backend *mods;
Russell Belfer committed
436
	git_buf key = GIT_BUF_INIT;
437
	const char *val;
Russell Belfer committed
438 439 440

	assert(submodule);

441
	mods = open_gitmodules(submodule->repo, true, NULL);
Russell Belfer committed
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
	if (!mods) {
		giterr_set(GITERR_SUBMODULE,
			"Adding submodules to a bare repository is not supported (for now)");
		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;

	if (!(error = submodule_config_key_trunc_puts(&key, "update")) &&
462
		(val = git_submodule_update_to_str(submodule->update)) != NULL)
Russell Belfer committed
463 464 465 466 467
		error = git_config_file_set_string(mods, key.ptr, val);
	if (error < 0)
		goto cleanup;

	if (!(error = submodule_config_key_trunc_puts(&key, "ignore")) &&
468
		(val = git_submodule_ignore_to_str(submodule->ignore)) != NULL)
Russell Belfer committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
		error = git_config_file_set_string(mods, key.ptr, val);
	if (error < 0)
		goto cleanup;

	if ((error = submodule_config_key_trunc_puts(
			&key, "fetchRecurseSubmodules")) < 0 ||
		(error = git_config_file_set_string(
			mods, key.ptr, submodule->fetch_recurse ? "true" : "false")) < 0)
		goto cleanup;

	/* update internal defaults */

	submodule->ignore_default = submodule->ignore;
	submodule->update_default = submodule->update;
	submodule->flags |= GIT_SUBMODULE_STATUS_IN_CONFIG;

cleanup:
	if (mods != NULL)
		git_config_file_free(mods);
	git_buf_free(&key);

	return error;
}

git_repository *git_submodule_owner(git_submodule *submodule)
{
	assert(submodule);
496
	return submodule->repo;
Russell Belfer committed
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
}

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;
}

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;
}

529
const git_oid *git_submodule_index_id(git_submodule *submodule)
Russell Belfer committed
530 531 532 533 534 535 536 537 538
{
	assert(submodule);

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

539
const git_oid *git_submodule_head_id(git_submodule *submodule)
Russell Belfer committed
540 541 542 543 544 545 546 547 548
{
	assert(submodule);

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

549
const git_oid *git_submodule_wd_id(git_submodule *submodule)
Russell Belfer committed
550 551 552
{
	assert(submodule);

553
	/* load unless we think we have a valid oid */
Russell Belfer committed
554 555 556 557
	if (!(submodule->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID)) {
		git_repository *subrepo;

		/* calling submodule open grabs the HEAD OID if possible */
558
		if (!git_submodule_open_bare(&subrepo, submodule))
Russell Belfer committed
559
			git_repository_free(subrepo);
560 561
		else
			giterr_clear();
Russell Belfer committed
562 563 564 565 566 567 568 569 570 571 572
	}

	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);
573 574
	return (submodule->ignore < GIT_SUBMODULE_IGNORE_NONE) ?
		GIT_SUBMODULE_IGNORE_NONE : submodule->ignore;
Russell Belfer committed
575 576 577 578 579 580 581 582 583
}

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

	assert(submodule);

584
	if (ignore == GIT_SUBMODULE_IGNORE_RESET)
Russell Belfer committed
585 586 587 588 589 590 591 592 593 594
		ignore = submodule->ignore_default;

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

git_submodule_update_t git_submodule_update(git_submodule *submodule)
{
	assert(submodule);
595 596
	return (submodule->update < GIT_SUBMODULE_UPDATE_CHECKOUT) ?
		GIT_SUBMODULE_UPDATE_CHECKOUT : submodule->update;
Russell Belfer committed
597 598 599 600 601 602 603 604 605
}

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

	assert(submodule);

606
	if (update == GIT_SUBMODULE_UPDATE_RESET)
Russell Belfer committed
607 608 609 610 611 612 613
		update = submodule->update_default;

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

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
int git_submodule_fetch_recurse_submodules(
	git_submodule *submodule)
{
	assert(submodule);
	return submodule->fetch_recurse;
}

int git_submodule_set_fetch_recurse_submodules(
	git_submodule *submodule,
	int fetch_recurse_submodules)
{
	int old;

	assert(submodule);

	old = submodule->fetch_recurse;
	submodule->fetch_recurse = (fetch_recurse_submodules != 0);
	return old;
}

Russell Belfer committed
634 635 636
int git_submodule_init(git_submodule *submodule, int overwrite)
{
	int error;
637
	const char *val;
Russell Belfer committed
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653

	/* write "submodule.NAME.url" */

	if (!submodule->url) {
		giterr_set(GITERR_SUBMODULE,
			"No URL configured for submodule '%s'", submodule->name);
		return -1;
	}

	error = submodule_update_config(
		submodule, "url", submodule->url, overwrite != 0, false);
	if (error < 0)
		return error;

	/* write "submodule.NAME.update" if not default */

654 655 656 657
	val = (submodule->update == GIT_SUBMODULE_UPDATE_CHECKOUT) ?
		NULL : git_submodule_update_to_str(submodule->update);
	error = submodule_update_config(
		submodule, "update", val, (overwrite != 0), false);
Russell Belfer committed
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675

	return error;
}

int git_submodule_sync(git_submodule *submodule)
{
	if (!submodule->url) {
		giterr_set(GITERR_SUBMODULE,
			"No URL configured for submodule '%s'", submodule->name);
		return -1;
	}

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

	return submodule_update_config(
		submodule, "url", submodule->url, true, true);
}

676 677
static int git_submodule__open(
	git_repository **subrepo, git_submodule *sm, bool bare)
Russell Belfer committed
678 679 680
{
	int error;
	git_buf path = GIT_BUF_INIT;
681 682
	unsigned int flags = GIT_REPOSITORY_OPEN_NO_SEARCH;
	const char *wd;
Russell Belfer committed
683

684
	assert(sm && subrepo);
Russell Belfer committed
685

686 687 688
	if (git_repository__ensure_not_bare(
			sm->repo, "open submodule repository") < 0)
		return GIT_EBAREREPO;
Russell Belfer committed
689

690
	wd = git_repository_workdir(sm->repo);
Russell Belfer committed
691

692 693
	if (git_buf_joinpath(&path, wd, sm->path) < 0 ||
		git_buf_joinpath(&path, path.ptr, DOT_GIT) < 0)
Russell Belfer committed
694 695
		return -1;

696 697 698 699
	sm->flags = sm->flags &
		~(GIT_SUBMODULE_STATUS_IN_WD |
		  GIT_SUBMODULE_STATUS__WD_OID_VALID |
		  GIT_SUBMODULE_STATUS__WD_SCANNED);
Russell Belfer committed
700

701 702
	if (bare)
		flags |= GIT_REPOSITORY_OPEN_BARE;
Russell Belfer committed
703

704
	error = git_repository_open_ext(subrepo, path.ptr, flags, wd);
705

706 707 708 709
	/* if we opened the submodule successfully, grab HEAD OID, etc. */
	if (!error) {
		sm->flags |= GIT_SUBMODULE_STATUS_IN_WD |
			GIT_SUBMODULE_STATUS__WD_SCANNED;
710

711 712 713 714 715 716 717 718 719
		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" */
720

721 722 723
		if (git_path_isdir(path.ptr))
			sm->flags |= GIT_SUBMODULE_STATUS__WD_SCANNED;
	}
724

725
	git_buf_free(&path);
726

727 728
	return error;
}
729

730 731 732 733
int git_submodule_open_bare(git_repository **subrepo, git_submodule *sm)
{
	return git_submodule__open(subrepo, sm, true);
}
Russell Belfer committed
734

735 736 737
int git_submodule_open(git_repository **subrepo, git_submodule *sm)
{
	return git_submodule__open(subrepo, sm, false);
Russell Belfer committed
738 739 740 741 742
}

int git_submodule_reload_all(git_repository *repo)
{
	assert(repo);
743 744
	git_submodule_config_free(repo);
	return load_submodule_config(repo);
Russell Belfer committed
745 746
}

747 748
static void submodule_update_from_index_entry(
	git_submodule *sm, const git_index_entry *ie)
Russell Belfer committed
749
{
750
	bool already_found = (sm->flags & GIT_SUBMODULE_STATUS_IN_INDEX) != 0;
Russell Belfer committed
751

752 753 754 755 756 757 758 759
	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
			git_oid_cpy(&sm->index_oid, &ie->oid);
Russell Belfer committed
760

761 762 763 764 765 766 767 768 769
		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
770

771
	if (git_repository_index__weakptr(&index, sm->repo) < 0)
Russell Belfer committed
772 773
		return -1;

774
	sm->flags = sm->flags &
775 776 777
		~(GIT_SUBMODULE_STATUS_IN_INDEX |
		  GIT_SUBMODULE_STATUS__INDEX_OID_VALID);

778 779
	if (!(ie = git_index_get_bypath(index, sm->path, 0)))
		return 0;
Russell Belfer committed
780

781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
	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
796
	}
797
}
Russell Belfer committed
798

799 800 801
static int submodule_update_head(git_submodule *submodule)
{
	git_tree *head = NULL;
nulltoken committed
802
	git_tree_entry *te = NULL;
Russell Belfer committed
803

804 805 806
	submodule->flags = submodule->flags &
		~(GIT_SUBMODULE_STATUS_IN_HEAD |
		  GIT_SUBMODULE_STATUS__HEAD_OID_VALID);
Russell Belfer committed
807

808 809 810 811 812 813
	/* 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
814

nulltoken committed
815
	git_tree_entry_free(te);
816 817 818
	git_tree_free(head);
	return 0;
}
819

820 821 822 823
int git_submodule_reload(git_submodule *submodule)
{
	int error = 0;
	git_config_backend *mods;
Russell Belfer committed
824

825
	assert(submodule);
Russell Belfer committed
826

827
	/* refresh index data */
Russell Belfer committed
828

829 830 831 832 833 834 835
	if (submodule_update_index(submodule) < 0)
		return -1;

	/* refresh HEAD tree data */

	if (submodule_update_head(submodule) < 0)
		return -1;
Russell Belfer committed
836 837 838

	/* refresh config data */

839 840
	mods = open_gitmodules(submodule->repo, false, NULL);
	if (mods != NULL) {
Russell Belfer committed
841 842 843
		git_buf path = GIT_BUF_INIT;

		git_buf_sets(&path, "submodule\\.");
844
		git_buf_text_puts_escape_regex(&path, submodule->name);
Russell Belfer committed
845 846 847 848 849 850
		git_buf_puts(&path, ".*");

		if (git_buf_oom(&path))
			error = -1;
		else
			error = git_config_file_foreach_match(
851
				mods, path.ptr, submodule_load_from_config, submodule->repo);
Russell Belfer committed
852 853

		git_buf_free(&path);
854
		git_config_file_free(mods);
Russell Belfer committed
855 856
	}

857 858 859 860 861 862 863 864 865 866
	if (error < 0)
		return error;

	/* refresh wd data */

	submodule->flags = submodule->flags &
		~(GIT_SUBMODULE_STATUS_IN_WD | GIT_SUBMODULE_STATUS__WD_OID_VALID);

	error = submodule_load_from_wd_lite(submodule, submodule->path, NULL);

Russell Belfer committed
867 868 869
	return error;
}

870 871
static void submodule_copy_oid_maybe(
	git_oid *tgt, const git_oid *src, bool valid)
Russell Belfer committed
872
{
873 874 875 876 877 878 879
	if (tgt) {
		if (valid)
			memcpy(tgt, src, sizeof(*tgt));
		else
			memset(tgt, 0, sizeof(*tgt));
	}
}
880

881 882 883 884 885 886 887 888 889 890
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
891

892
	if (ign < GIT_SUBMODULE_IGNORE_NONE)
893
		ign = sm->ignore;
894

895 896 897 898
	/* only return location info if ignore == all */
	if (ign == GIT_SUBMODULE_IGNORE_ALL) {
		*out_status = (sm->flags & GIT_SUBMODULE_STATUS__IN_FLAGS);
		return 0;
899
	}
Russell Belfer committed
900

901 902 903 904 905 906 907
	/* 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
908

909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
	/* 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
939 940
}

941
int git_submodule_status(unsigned int *status, git_submodule *sm)
942
{
943
	assert(status && sm);
944

945
	return git_submodule__status(status, NULL, NULL, NULL, sm, 0);
946
}
947

948 949 950 951 952 953
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);
954 955 956
}


Russell Belfer committed
957 958 959 960 961
/*
 * INTERNAL FUNCTIONS
 */

static git_submodule *submodule_alloc(git_repository *repo, const char *name)
962
{
963
	size_t namelen;
964
	git_submodule *sm;
965

966
	if (!name || !(namelen = strlen(name))) {
967
		giterr_set(GITERR_SUBMODULE, "Invalid submodule name");
968 969 970
		return NULL;
	}

971
	sm = git__calloc(1, sizeof(git_submodule));
972
	if (sm == NULL)
973
		return NULL;
974

975 976 977 978 979
	sm->name = sm->path = git__strdup(name);
	if (!sm->name) {
		git__free(sm);
		return NULL;
	}
Russell Belfer committed
980

981
	GIT_REFCOUNT_INC(sm);
982 983 984 985
	sm->ignore = sm->ignore_default = GIT_SUBMODULE_IGNORE_NONE;
	sm->update = sm->update_default = GIT_SUBMODULE_UPDATE_CHECKOUT;
	sm->repo   = repo;

Russell Belfer committed
986 987 988
	return sm;
}

989
static void submodule_release(git_submodule *sm)
Russell Belfer committed
990 991 992 993
{
	if (!sm)
		return;

994 995 996 997 998 999 1000
	if (sm->path != sm->name)
		git__free(sm->path);
	git__free(sm->name);
	git__free(sm->url);
	git__memzero(sm, sizeof(*sm));
	git__free(sm);
}
Russell Belfer committed
1001

1002 1003 1004 1005 1006
void git_submodule_free(git_submodule *sm)
{
	if (!sm)
		return;
	GIT_REFCOUNT_DEC(sm, submodule_release);
Russell Belfer committed
1007 1008
}

1009 1010 1011 1012 1013
static int submodule_get(
	git_submodule **sm_ptr,
	git_repository *repo,
	const char *name,
	const char *alternate)
Russell Belfer committed
1014 1015 1016 1017
{
	git_strmap *smcfg = repo->submodules;
	khiter_t pos;
	git_submodule *sm;
1018
	int error;
Russell Belfer committed
1019

1020
	assert(repo && name);
Russell Belfer committed
1021

1022
	pos = git_strmap_lookup_index(smcfg, name);
Russell Belfer committed
1023

1024 1025
	if (!git_strmap_valid_index(smcfg, pos) && alternate)
		pos = git_strmap_lookup_index(smcfg, alternate);
Russell Belfer committed
1026

1027 1028
	if (!git_strmap_valid_index(smcfg, pos)) {
		sm = submodule_alloc(repo, name);
1029
		GITERR_CHECK_ALLOC(sm);
Russell Belfer committed
1030

1031 1032 1033
		/* insert value at name - if another thread beats us to it, then use
		 * their record and release our own.
		 */
1034
		pos = kh_put(str, smcfg, sm->name, &error);
1035 1036

		if (error < 0) {
1037
			git_submodule_free(sm);
1038 1039
			sm = NULL;
		} else if (error == 0) {
1040
			git_submodule_free(sm);
1041 1042 1043 1044 1045 1046
			sm = git_strmap_value_at(smcfg, pos);
		} else {
			git_strmap_set_value_at(smcfg, pos, sm);
		}
	} else {
		sm = git_strmap_value_at(smcfg, pos);
Russell Belfer committed
1047 1048
	}

1049
	*sm_ptr = sm;
Russell Belfer committed
1050

1051
	return (sm != NULL) ? 0 : -1;
1052 1053
}

1054 1055 1056 1057 1058
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;
1059 1060
}

1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
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;
}

Russell Belfer committed
1089
static int submodule_load_from_config(
1090
	const git_config_entry *entry, void *data)
1091
{
Russell Belfer committed
1092 1093
	git_repository *repo = data;
	git_strmap *smcfg = repo->submodules;
1094
	const char *namestart, *property, *alternate = NULL;
1095
	const char *key = entry->name, *value = entry->value;
1096 1097 1098
	git_buf name = GIT_BUF_INIT;
	git_submodule *sm;
	bool is_path;
1099
	int error = 0;
1100 1101 1102 1103 1104 1105

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

	namestart = key + strlen("submodule.");
	property  = strrchr(namestart, '.');
1106 1107

	if (!property || (property == namestart))
1108
		return 0;
1109

1110
	property++;
Russell Belfer committed
1111
	is_path = (strcasecmp(property, "path") == 0);
1112 1113 1114 1115

	if (git_buf_set(&name, namestart, property - namestart - 1) < 0)
		return -1;

1116 1117 1118 1119
	if (submodule_get(&sm, repo, name.ptr, is_path ? value : NULL) < 0) {
		git_buf_free(&name);
		return -1;
	}
1120

Russell Belfer committed
1121 1122
	sm->flags |= GIT_SUBMODULE_STATUS_IN_CONFIG;

1123 1124 1125
	/* Only from config might we get differing names & paths.  If so, then
	 * update the submodule and insert under the alternative key.
	 */
1126

1127 1128 1129
	/* TODO: if case insensitive filesystem, then the following strcmps
	 * should be strcasecmp
	 */
1130

1131 1132 1133 1134 1135 1136
	if (strcmp(sm->name, name.ptr) != 0) {
		alternate = sm->name = git_buf_detach(&name);
	} else if (is_path && value && strcmp(sm->path, value) != 0) {
		alternate = sm->path = git__strdup(value);
		if (!sm->path)
			error = -1;
1137
	}
1138 1139 1140
	if (alternate) {
		void *old_sm = NULL;
		git_strmap_insert2(smcfg, alternate, sm, old_sm, error);
1141

1142
		if (error >= 0)
1143
			GIT_REFCOUNT_INC(sm); /* inserted under a new key */
1144 1145 1146

		/* if we replaced an old module under this key, release the old one */
		if (old_sm && ((git_submodule *)old_sm) != sm) {
1147
			git_submodule_free(old_sm);
1148 1149
			/* TODO: log warning about multiple submodules with same path */
		}
1150 1151
	}

1152 1153 1154 1155
	git_buf_free(&name);
	if (error < 0)
		return error;

Russell Belfer committed
1156 1157 1158 1159
	/* 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)
	 */

1160 1161 1162 1163
	if (is_path)
		return 0;

	/* copy other properties into submodule entry */
Russell Belfer committed
1164
	if (strcasecmp(property, "url") == 0) {
1165 1166 1167
		git__free(sm->url);
		sm->url = NULL;

Russell Belfer committed
1168
		if (value != NULL && (sm->url = git__strdup(value)) == NULL)
1169
			return -1;
1170
	}
Russell Belfer committed
1171
	else if (strcasecmp(property, "update") == 0) {
1172 1173 1174
		if (git_submodule_parse_update(&sm->update, value) < 0)
			return -1;
		sm->update_default = sm->update;
1175
	}
Russell Belfer committed
1176
	else if (strcasecmp(property, "fetchRecurseSubmodules") == 0) {
1177 1178
		if (git__parse_bool(&sm->fetch_recurse, value) < 0)
			return submodule_config_error("fetchRecurseSubmodules", value);
1179
	}
Russell Belfer committed
1180
	else if (strcasecmp(property, "ignore") == 0) {
1181 1182 1183
		if (git_submodule_parse_ignore(&sm->ignore, value) < 0)
			return -1;
		sm->ignore_default = sm->ignore;
1184 1185 1186 1187 1188 1189
	}
	/* ignore other unknown submodule properties */

	return 0;
}

Russell Belfer committed
1190 1191 1192 1193 1194 1195 1196 1197
static int submodule_load_from_wd_lite(
	git_submodule *sm, const char *name, void *payload)
{
	git_buf path = GIT_BUF_INIT;

	GIT_UNUSED(name);
	GIT_UNUSED(payload);

1198 1199 1200 1201
	if (git_repository_is_bare(sm->repo))
		return 0;

	if (git_buf_joinpath(&path, git_repository_workdir(sm->repo), sm->path) < 0)
Russell Belfer committed
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
		return -1;

	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;
}

static int load_submodule_config_from_index(
	git_repository *repo, git_oid *gitmodules_oid)
1217 1218
{
	int error;
1219
	git_index *index;
Russell Belfer committed
1220 1221
	git_iterator *i;
	const git_index_entry *entry;
1222

1223
	if ((error = git_repository_index__weakptr(&index, repo)) < 0 ||
1224
		(error = git_iterator_for_index(&i, index, 0, NULL, NULL)) < 0)
Russell Belfer committed
1225
		return error;
1226

1227
	while (!(error = git_iterator_advance(&entry, i))) {
1228 1229
		khiter_t pos = git_strmap_lookup_index(repo->submodules, entry->path);
		git_submodule *sm;
1230

1231 1232
		if (git_strmap_valid_index(repo->submodules, pos)) {
			sm = git_strmap_value_at(repo->submodules, pos);
1233

1234 1235 1236 1237 1238 1239 1240 1241 1242
			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)) {
			if (!submodule_get(&sm, repo, entry->path, NULL))
				submodule_update_from_index_entry(sm, entry);
		} else if (strcmp(entry->path, GIT_MODULES_FILE) == 0)
			git_oid_cpy(gitmodules_oid, &entry->oid);
1243 1244
	}

1245 1246 1247
	if (error == GIT_ITEROVER)
		error = 0;

Russell Belfer committed
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
	git_iterator_free(i);

	return error;
}

static int load_submodule_config_from_head(
	git_repository *repo, git_oid *gitmodules_oid)
{
	int error;
	git_tree *head;
	git_iterator *i;
	const git_index_entry *entry;

1261 1262 1263 1264 1265
	/* if we can't look up current head, then there's no submodule in it */
	if (git_repository_head_tree(&head, repo) < 0) {
		giterr_clear();
		return 0;
	}
Russell Belfer committed
1266

1267
	if ((error = git_iterator_for_tree(&i, head, 0, NULL, NULL)) < 0) {
Russell Belfer committed
1268 1269 1270 1271
		git_tree_free(head);
		return error;
	}

1272
	while (!(error = git_iterator_advance(&entry, i))) {
1273 1274
		khiter_t pos = git_strmap_lookup_index(repo->submodules, entry->path);
		git_submodule *sm;
Russell Belfer committed
1275

1276 1277
		if (git_strmap_valid_index(repo->submodules, pos)) {
			sm = git_strmap_value_at(repo->submodules, pos);
1278

1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
			if (S_ISGITLINK(entry->mode))
				submodule_update_from_head_data(
					sm, entry->mode, &entry->oid);
			else
				sm->flags |= GIT_SUBMODULE_STATUS__HEAD_NOT_SUBMODULE;
		} else if (S_ISGITLINK(entry->mode)) {
			if (!submodule_get(&sm, repo, entry->path, NULL))
				submodule_update_from_head_data(
					sm, entry->mode, &entry->oid);
		} else if (strcmp(entry->path, GIT_MODULES_FILE) == 0 &&
				   git_oid_iszero(gitmodules_oid)) {
			git_oid_cpy(gitmodules_oid, &entry->oid);
1291
		}
Russell Belfer committed
1292 1293
	}

1294 1295 1296
	if (error == GIT_ITEROVER)
		error = 0;

Russell Belfer committed
1297 1298 1299 1300 1301 1302
	git_iterator_free(i);
	git_tree_free(head);

	return error;
}

Ben Straub committed
1303
static git_config_backend *open_gitmodules(
Russell Belfer committed
1304 1305 1306 1307 1308 1309
	git_repository *repo,
	bool okay_to_create,
	const git_oid *gitmodules_oid)
{
	const char *workdir = git_repository_workdir(repo);
	git_buf path = GIT_BUF_INIT;
Ben Straub committed
1310
	git_config_backend *mods = NULL;
Russell Belfer committed
1311 1312 1313 1314 1315 1316 1317 1318

	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)
1319
				mods = NULL;
Russell Belfer committed
1320
			/* open should only fail here if the file is malformed */
1321
			else if (git_config_file_open(mods, GIT_CONFIG_LEVEL_LOCAL) < 0) {
Russell Belfer committed
1322 1323 1324
				git_config_file_free(mods);
				mods = NULL;
			}
1325 1326 1327
		}
	}

Russell Belfer committed
1328 1329 1330 1331 1332 1333 1334 1335
	if (!mods && gitmodules_oid && !git_oid_iszero(gitmodules_oid)) {
		/* TODO: Retrieve .gitmodules content from ODB */

		/* Should we actually do this?  Core git does not, but it means you
		 * can't really get much information about submodules on bare repos.
		 */
	}

1336 1337
	git_buf_free(&path);

Russell Belfer committed
1338 1339 1340
	return mods;
}

1341
static int load_submodule_config(git_repository *repo)
Russell Belfer committed
1342 1343 1344 1345
{
	int error;
	git_oid gitmodules_oid;
	git_buf path = GIT_BUF_INIT;
Ben Straub committed
1346
	git_config_backend *mods = NULL;
Russell Belfer committed
1347

1348
	if (repo->submodules)
Russell Belfer committed
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
		return 0;

	memset(&gitmodules_oid, 0, sizeof(gitmodules_oid));

	/* Submodule data is kept in a hashtable keyed by both name and path.
	 * These are usually the same, but that is not guaranteed.
	 */
	if (!repo->submodules) {
		repo->submodules = git_strmap_alloc();
		GITERR_CHECK_ALLOC(repo->submodules);
1359 1360
	}

Russell Belfer committed
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
	/* add submodule information from index */

	if ((error = load_submodule_config_from_index(repo, &gitmodules_oid)) < 0)
		goto cleanup;

	/* add submodule information from HEAD */

	if ((error = load_submodule_config_from_head(repo, &gitmodules_oid)) < 0)
		goto cleanup;

	/* add submodule information from .gitmodules */

	if ((mods = open_gitmodules(repo, false, &gitmodules_oid)) != NULL)
		error = git_config_file_foreach(mods, submodule_load_from_config, repo);

	if (error != 0)
		goto cleanup;

	/* shallow scan submodules in work tree */
1380

Russell Belfer committed
1381 1382
	if (!git_repository_is_bare(repo))
		error = git_submodule_foreach(repo, submodule_load_from_wd_lite, NULL);
1383 1384

cleanup:
Russell Belfer committed
1385 1386
	git_buf_free(&path);

1387 1388
	if (mods != NULL)
		git_config_file_free(mods);
Russell Belfer committed
1389

1390
	if (error)
Russell Belfer committed
1391 1392
		git_submodule_config_free(repo);

1393 1394 1395
	return error;
}

Russell Belfer committed
1396
static int lookup_head_remote(git_buf *url, git_repository *repo)
1397
{
Russell Belfer committed
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
	int error;
	git_config *cfg;
	git_reference *head = NULL, *remote = NULL;
	const char *tgt, *scan;
	git_buf key = GIT_BUF_INIT;

	/* 1. resolve HEAD -> refs/heads/BRANCH
	 * 2. lookup config branch.BRANCH.remote -> ORIGIN
	 * 3. lookup remote.ORIGIN.url
	 */
1408

Russell Belfer committed
1409 1410
	if ((error = git_repository_config__weakptr(&cfg, repo)) < 0)
		return error;
1411

Russell Belfer committed
1412 1413 1414 1415 1416 1417
	if (git_reference_lookup(&head, repo, GIT_HEAD_FILE) < 0) {
		giterr_set(GITERR_SUBMODULE,
			"Cannot resolve relative URL when HEAD cannot be resolved");
		error = GIT_ENOTFOUND;
		goto cleanup;
	}
1418

Russell Belfer committed
1419 1420 1421 1422 1423 1424 1425
	if (git_reference_type(head) != GIT_REF_SYMBOLIC) {
		giterr_set(GITERR_SUBMODULE,
			"Cannot resolve relative URL when HEAD is not symbolic");
		error = GIT_ENOTFOUND;
		goto cleanup;
	}

1426
	if ((error = git_branch_upstream(&remote, head)) < 0)
Russell Belfer committed
1427 1428 1429 1430 1431
		goto cleanup;

	/* remote should refer to something like refs/remotes/ORIGIN/BRANCH */

	if (git_reference_type(remote) != GIT_REF_SYMBOLIC ||
1432
		git__prefixcmp(git_reference_symbolic_target(remote), GIT_REFS_REMOTES_DIR) != 0)
Russell Belfer committed
1433 1434 1435 1436 1437 1438 1439
	{
		giterr_set(GITERR_SUBMODULE,
			"Cannot resolve relative URL when HEAD is not symbolic");
		error = GIT_ENOTFOUND;
		goto cleanup;
	}

1440
	scan = tgt = git_reference_symbolic_target(remote) + strlen(GIT_REFS_REMOTES_DIR);
Russell Belfer committed
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
	while (*scan && (*scan != '/' || (scan > tgt && scan[-1] != '\\')))
		scan++; /* find non-escaped slash to end ORIGIN name */

	error = git_buf_printf(&key, "remote.%.*s.url", (int)(scan - tgt), tgt);
	if (error < 0)
		goto cleanup;

	if ((error = git_config_get_string(&tgt, cfg, key.ptr)) < 0)
		goto cleanup;

	error = git_buf_sets(url, tgt);

cleanup:
	git_buf_free(&key);
	git_reference_free(head);
	git_reference_free(remote);

	return error;
1459 1460
}

Russell Belfer committed
1461 1462 1463 1464 1465 1466
static int submodule_update_config(
	git_submodule *submodule,
	const char *attr,
	const char *value,
	bool overwrite,
	bool only_existing)
1467
{
Russell Belfer committed
1468 1469 1470 1471 1472 1473 1474
	int error;
	git_config *config;
	git_buf key = GIT_BUF_INIT;
	const char *old = NULL;

	assert(submodule);

1475
	error = git_repository_config__weakptr(&config, submodule->repo);
Russell Belfer committed
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
	if (error < 0)
		return error;

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

	if (git_config_get_string(&old, config, key.ptr) < 0)
		giterr_clear();

	if (!old && only_existing)
		goto cleanup;
	if (old && !overwrite)
		goto cleanup;
	if ((!old && !value) || (old && value && strcmp(old, value) == 0))
		goto cleanup;

	if (!value)
Ben Straub committed
1494
		error = git_config_delete_entry(config, key.ptr);
Russell Belfer committed
1495 1496 1497 1498 1499 1500
	else
		error = git_config_set_string(config, key.ptr, value);

cleanup:
	git_buf_free(&key);
	return error;
1501 1502
}

1503
static void submodule_get_index_status(unsigned int *status, git_submodule *sm)
Russell Belfer committed
1504
{
1505 1506
	const git_oid *head_oid  = git_submodule_head_id(sm);
	const git_oid *index_oid = git_submodule_index_id(sm);
Russell Belfer committed
1507

1508 1509
	*status = *status & ~GIT_SUBMODULE_STATUS__INDEX_FLAGS;

1510 1511 1512 1513 1514 1515 1516 1517
	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
1518 1519
}

1520 1521 1522 1523 1524
static void submodule_get_wd_status(
	unsigned int *status,
	git_submodule *sm,
	git_repository *sm_repo,
	git_submodule_ignore_t ign)
1525
{
1526 1527 1528 1529 1530 1531
	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;
	git_diff_options opt = GIT_DIFF_OPTIONS_INIT;
	git_diff_list *diff;
1532

1533
	*status = *status & ~GIT_SUBMODULE_STATUS__WD_FLAGS;
Russell Belfer committed
1534

1535 1536 1537
	if (!index_oid) {
		if (wd_oid)
			*status |= GIT_SUBMODULE_STATUS_WD_ADDED;
Russell Belfer committed
1538
	}
1539 1540 1541 1542 1543 1544
	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
1545
	}
1546 1547
	else if (!git_oid_equal(index_oid, wd_oid))
		*status |= GIT_SUBMODULE_STATUS_WD_MODIFIED;
Russell Belfer committed
1548

1549 1550 1551
	/* if we have no repo, then we're done */
	if (!sm_repo)
		return;
1552

1553 1554 1555 1556
	/* 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).
	 */
1557

1558 1559
	if (ign == GIT_SUBMODULE_IGNORE_NONE)
		opt.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
Russell Belfer committed
1560

1561
	/* if we don't have an unborn head, check diff with index */
1562 1563 1564 1565 1566 1567 1568
	if (git_repository_head_tree(&sm_head, sm_repo) < 0)
		giterr_clear();
	else {
		/* perform head to index diff on submodule */
		if (git_diff_tree_to_index(&diff, sm_repo, sm_head, NULL, &opt) < 0)
			giterr_clear();
		else {
1569
			if (git_diff_num_deltas(diff) > 0)
1570 1571 1572 1573
				*status |= GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED;
			git_diff_list_free(diff);
			diff = NULL;
		}
Russell Belfer committed
1574

1575
		git_tree_free(sm_head);
1576
	}
1577

1578 1579 1580 1581 1582 1583
	/* perform index-to-workdir diff on submodule */
	if (git_diff_index_to_workdir(&diff, sm_repo, NULL, &opt) < 0)
		giterr_clear();
	else {
		size_t untracked =
			git_diff_num_deltas_of_type(diff, GIT_DELTA_UNTRACKED);
1584

1585 1586
		if (untracked > 0)
			*status |= GIT_SUBMODULE_STATUS_WD_UNTRACKED;
1587

1588 1589
		if (git_diff_num_deltas(diff) != untracked)
			*status |= GIT_SUBMODULE_STATUS_WD_WD_MODIFIED;
Russell Belfer committed
1590

1591 1592
		git_diff_list_free(diff);
		diff = NULL;
1593
	}
1594
}