예제 #1
0
파일: status.c 프로젝트: benqian/repobuild
void test_submodule_status__ignore_untracked(void)
{
	unsigned int status;
	git_submodule *sm;
	git_buf path = GIT_BUF_INIT;
	git_submodule_ignore_t ign = GIT_SUBMODULE_IGNORE_UNTRACKED;

	cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "sm_unchanged"));
	cl_git_pass(git_futils_rmdir_r(git_buf_cstr(&path), NULL, GIT_RMDIR_REMOVE_FILES));

	cl_git_pass(git_submodule_foreach(g_repo, set_sm_ignore, &ign));

	cl_git_fail(git_submodule_lookup(&sm, g_repo, "not-submodule"));

	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_index"));
	cl_git_pass(git_submodule_status(&status, sm));
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED) != 0);

	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
	cl_git_pass(git_submodule_status(&status, sm));
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0);

	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_file"));
	cl_git_pass(git_submodule_status(&status, sm));
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_WD_MODIFIED) != 0);

	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_untracked_file"));
	cl_git_pass(git_submodule_status(&status, sm));
	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));

	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_missing_commits"));
	cl_git_pass(git_submodule_status(&status, sm));
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0);

	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_added_and_uncommited"));
	cl_git_pass(git_submodule_status(&status, sm));
	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_ADDED) != 0);

	/* removed sm_unchanged for deleted workdir */
	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged"));
	cl_git_pass(git_submodule_status(&status, sm));
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_DELETED) != 0);

	/* now mkdir sm_unchanged to test uninitialized */
	cl_git_pass(git_futils_mkdir(git_buf_cstr(&path), NULL, 0755, 0));
	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged"));
	cl_git_pass(git_submodule_reload(sm));
	cl_git_pass(git_submodule_status(&status, sm));
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) != 0);

	/* update sm_changed_head in index */
	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
	cl_git_pass(git_submodule_add_to_index(sm, true));
	/* reload is not needed because add_to_index updates the submodule data */
	cl_git_pass(git_submodule_status(&status, sm));
	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_MODIFIED) != 0);

	git_buf_free(&path);
}
예제 #2
0
void test_submodule_modify__edit_and_save(void)
{
	git_submodule *sm1, *sm2;
	char *old_url;
	git_submodule_ignore_t old_ignore;
	git_submodule_update_t old_update;
	git_repository *r2;
	int old_fetchrecurse;

	cl_git_pass(git_submodule_lookup(&sm1, g_repo, "sm_changed_head"));

	old_url = git__strdup(git_submodule_url(sm1));

	/* modify properties of submodule */
	cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL));
	old_ignore = git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_UNTRACKED);
	old_update = git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_REBASE);
	old_fetchrecurse = git_submodule_set_fetch_recurse_submodules(sm1, 1);

	cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1));
	cl_assert_equal_i(
		(int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm1));
	cl_assert_equal_i(
		(int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update(sm1));
	cl_assert_equal_i(1, git_submodule_fetch_recurse_submodules(sm1));

	/* revert without saving (and confirm setters return old value) */
	cl_git_pass(git_submodule_set_url(sm1, old_url));
	cl_assert_equal_i(
		(int)GIT_SUBMODULE_IGNORE_UNTRACKED,
		(int)git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_DEFAULT));
	cl_assert_equal_i(
		(int)GIT_SUBMODULE_UPDATE_REBASE,
		(int)git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_DEFAULT));
	cl_assert_equal_i(
		1, git_submodule_set_fetch_recurse_submodules(sm1, old_fetchrecurse));

	/* check that revert was successful */
	cl_assert_equal_s(old_url, git_submodule_url(sm1));
	cl_assert_equal_i((int)old_ignore, (int)git_submodule_ignore(sm1));
	cl_assert_equal_i((int)old_update, (int)git_submodule_update(sm1));
	cl_assert_equal_i(
		old_fetchrecurse, git_submodule_fetch_recurse_submodules(sm1));

	/* modify properties of submodule (again) */
	cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL));
	git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_UNTRACKED);
	git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_REBASE);
	git_submodule_set_fetch_recurse_submodules(sm1, 1);

	/* call save */
	cl_git_pass(git_submodule_save(sm1));

	/* attempt to "revert" values */
	git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_DEFAULT);
	git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_DEFAULT);

	/* but ignore and update should NOT revert because the DEFAULT
	 * should now be the newly saved value...
	 */
	cl_assert_equal_i(
		(int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm1));
	cl_assert_equal_i(
		(int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update(sm1));
	cl_assert_equal_i(1, git_submodule_fetch_recurse_submodules(sm1));

	/* call reload and check that the new values are loaded */
	cl_git_pass(git_submodule_reload(sm1));

	cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1));
	cl_assert_equal_i(
		(int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm1));
	cl_assert_equal_i(
		(int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update(sm1));
	cl_assert_equal_i(1, git_submodule_fetch_recurse_submodules(sm1));

	/* open a second copy of the repo and compare submodule */
	cl_git_pass(git_repository_open(&r2, "submod2"));
	cl_git_pass(git_submodule_lookup(&sm2, r2, "sm_changed_head"));

	cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm2));
	cl_assert_equal_i(
		(int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm2));
	cl_assert_equal_i(
		(int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update(sm2));
	cl_assert_equal_i(1, git_submodule_fetch_recurse_submodules(sm2));

	git_repository_free(r2);
	git__free(old_url);
}
예제 #3
0
파일: status.c 프로젝트: benqian/repobuild
void test_submodule_status__ignore_none(void)
{
	unsigned int status;
	git_submodule *sm;
	git_buf path = GIT_BUF_INIT;

	cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "sm_unchanged"));
	cl_git_pass(git_futils_rmdir_r(git_buf_cstr(&path), NULL, GIT_RMDIR_REMOVE_FILES));

	cl_assert_equal_i(GIT_ENOTFOUND,
		git_submodule_lookup(&sm, g_repo, "just_a_dir"));
	cl_assert_equal_i(GIT_EEXISTS,
		git_submodule_lookup(&sm, g_repo, "not-submodule"));
	cl_assert_equal_i(GIT_EEXISTS,
		git_submodule_lookup(&sm, g_repo, "not"));

	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_index"));
	cl_git_pass(git_submodule_status(&status, sm));
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED) != 0);

	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
	cl_git_pass(git_submodule_status(&status, sm));
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0);

	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_file"));
	cl_git_pass(git_submodule_status(&status, sm));
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_WD_MODIFIED) != 0);

	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_untracked_file"));
	cl_git_pass(git_submodule_status(&status, sm));
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNTRACKED) != 0);

	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_missing_commits"));
	cl_git_pass(git_submodule_status(&status, sm));
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0);

	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_added_and_uncommited"));
	cl_git_pass(git_submodule_status(&status, sm));
	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_ADDED) != 0);

	/* removed sm_unchanged for deleted workdir */
	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged"));
	cl_git_pass(git_submodule_status(&status, sm));
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_DELETED) != 0);

	/* now mkdir sm_unchanged to test uninitialized */
	cl_git_pass(git_futils_mkdir(git_buf_cstr(&path), NULL, 0755, 0));
	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged"));
	cl_git_pass(git_submodule_reload(sm));
	cl_git_pass(git_submodule_status(&status, sm));
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) != 0);

	/* update sm_changed_head in index */
	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
	cl_git_pass(git_submodule_add_to_index(sm, true));
	/* reload is not needed because add_to_index updates the submodule data */
	cl_git_pass(git_submodule_status(&status, sm));
	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_MODIFIED) != 0);

	/* remove sm_changed_head from index */
	{
		git_index *index;
		size_t pos;

		cl_git_pass(git_repository_index(&index, g_repo));
		cl_assert(!git_index_find(&pos, index, "sm_changed_head"));
		cl_git_pass(git_index_remove(index, "sm_changed_head", 0));
		cl_git_pass(git_index_write(index));

		git_index_free(index);
	}

	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
	cl_git_pass(git_submodule_reload(sm));
	cl_git_pass(git_submodule_status(&status, sm));
	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_DELETED) != 0);

	git_buf_free(&path);
}
예제 #4
0
파일: submodule.c 프로젝트: jeoyjng/libgit2
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;
	unsigned int location;
	git_submodule *sm;

	assert(repo && name);

	if ((error = submodule_alloc(&sm, repo, name)) < 0)
		return error;

	if ((error = git_submodule_reload(sm, false)) < 0) {
		git_submodule_free(sm);
		return error;
	}

	if ((error = git_submodule_location(&location, sm)) < 0) {
		git_submodule_free(sm);
		return error;
	}

	/* If it's not configured or we're looking by path  */
	if (location == 0 || location == GIT_SUBMODULE_STATUS_IN_WD) {
		git_config_backend *mods;
		const char *pattern = "submodule\\..*\\.path";
		git_buf path = GIT_BUF_INIT;
		fbp_data data = { NULL, NULL };

		git_buf_puts(&path, name);
		while (path.ptr[path.size-1] == '/') {
			path.ptr[--path.size] = '\0';
		}
		data.path = path.ptr;

		mods = open_gitmodules(repo, GITMODULES_EXISTING);

		if (mods)
			error = git_config_file_foreach_match(mods, pattern, find_by_path, &data);

		git_config_file_free(mods);

		if (error < 0) {
			git_submodule_free(sm);
			return error;
		}

		if (data.name) {
			git__free(sm->name);
			sm->name = data.name;
			sm->path = git_buf_detach(&path);

			/* Try to load again with the right name */
			if ((error = git_submodule_reload(sm, false)) < 0) {
				git_submodule_free(sm);
				return error;
			}
		}

		git_buf_free(&path);
	}

	if ((error = git_submodule_location(&location, sm)) < 0) {
		git_submodule_free(sm);
		return error;
	}

	/* If we still haven't found it, do the WD check */
	if (location == 0 || location == GIT_SUBMODULE_STATUS_IN_WD) {
		git_submodule_free(sm);
		error = GIT_ENOTFOUND;

		/* If it's not configured, we still check if there's a repo at the path */
		if (git_repository_workdir(repo)) {
			git_buf path = GIT_BUF_INIT;
			if (git_buf_join3(&path,
					  '/', git_repository_workdir(repo), name, DOT_GIT) < 0)
				return -1;

			if (git_path_exists(path.ptr))
				error = GIT_EEXISTS;

			git_buf_free(&path);
		}

		submodule_set_lookup_error(error, name);
		return error;
	}

	if (out)
		*out = sm;
	else
		git_submodule_free(sm);

	return 0;
}
예제 #5
0
void test_submodule_modify__edit_and_save(void)
{
	git_submodule *sm1, *sm2;
	char *old_url, *old_branch;
	git_submodule_ignore_t old_ignore;
	git_submodule_update_t old_update;
	git_repository *r2;
	git_submodule_recurse_t old_fetchrecurse;

	cl_git_pass(git_submodule_lookup(&sm1, g_repo, "sm_changed_head"));

	old_url = git__strdup(git_submodule_url(sm1));
	old_branch = NULL;

	/* modify properties of submodule */
	cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL));
	cl_git_pass(git_submodule_set_branch(sm1, SM_LIBGIT2_BRANCH));
	old_ignore = git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_UNTRACKED);
	old_update = git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_REBASE);
	old_fetchrecurse = git_submodule_set_fetch_recurse_submodules(
		sm1, GIT_SUBMODULE_RECURSE_YES);

	cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1));
	cl_assert_equal_s(SM_LIBGIT2_BRANCH, git_submodule_branch(sm1));
	cl_assert_equal_i(
		GIT_SUBMODULE_IGNORE_UNTRACKED, git_submodule_ignore(sm1));
	cl_assert_equal_i(
		GIT_SUBMODULE_UPDATE_REBASE, git_submodule_update_strategy(sm1));
	cl_assert_equal_i(
		GIT_SUBMODULE_RECURSE_YES, git_submodule_fetch_recurse_submodules(sm1));

	/* revert without saving (and confirm setters return old value) */
	cl_git_pass(git_submodule_set_url(sm1, old_url));
	cl_git_pass(git_submodule_set_branch(sm1, old_branch));
	cl_assert_equal_i(
		GIT_SUBMODULE_IGNORE_UNTRACKED,
		git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_RESET));
	cl_assert_equal_i(
		GIT_SUBMODULE_UPDATE_REBASE,
		git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_RESET));
	cl_assert_equal_i(
		GIT_SUBMODULE_RECURSE_YES, git_submodule_set_fetch_recurse_submodules(
			sm1, GIT_SUBMODULE_RECURSE_RESET));

	/* check that revert was successful */
	cl_assert_equal_s(old_url, git_submodule_url(sm1));
	cl_assert_equal_s(old_branch, git_submodule_branch(sm1));
	cl_assert_equal_i((int)old_ignore, (int)git_submodule_ignore(sm1));
	cl_assert_equal_i((int)old_update, (int)git_submodule_update_strategy(sm1));
	cl_assert_equal_i(
		old_fetchrecurse, git_submodule_fetch_recurse_submodules(sm1));

	/* modify properties of submodule (again) */
	cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL));
	cl_git_pass(git_submodule_set_branch(sm1, SM_LIBGIT2_BRANCH));
	git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_UNTRACKED);
	git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_REBASE);
	git_submodule_set_fetch_recurse_submodules(sm1, GIT_SUBMODULE_RECURSE_YES);

	/* call save */
	cl_git_pass(git_submodule_save(sm1));

	/* attempt to "revert" values */
	git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_RESET);
	git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_RESET);

	/* but ignore and update should NOT revert because the RESET
	 * should now be the newly saved value...
	 */
	cl_assert_equal_i(
		(int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm1));
	cl_assert_equal_i(
		(int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update_strategy(sm1));
	cl_assert_equal_i(GIT_SUBMODULE_RECURSE_YES, git_submodule_fetch_recurse_submodules(sm1));

	/* call reload and check that the new values are loaded */
	cl_git_pass(git_submodule_reload(sm1, 0));

	cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1));
	cl_assert_equal_s(SM_LIBGIT2_BRANCH, git_submodule_branch(sm1));
	cl_assert_equal_i(
		(int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm1));
	cl_assert_equal_i(
		(int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update_strategy(sm1));
	cl_assert_equal_i(GIT_SUBMODULE_RECURSE_YES, git_submodule_fetch_recurse_submodules(sm1));

	/* unset branch again and verify that the property is deleted in config */
	cl_git_pass(git_submodule_set_branch(sm1, NULL));
	cl_git_pass(git_submodule_save(sm1));
	cl_git_pass(git_submodule_reload(sm1, 0));
	cl_assert_equal_s(NULL, git_submodule_branch(sm1));

	/* open a second copy of the repo and compare submodule */
	cl_git_pass(git_repository_open(&r2, "submod2"));
	cl_git_pass(git_submodule_lookup(&sm2, r2, "sm_changed_head"));

	cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm2));
	cl_assert_equal_i(
		GIT_SUBMODULE_IGNORE_UNTRACKED, git_submodule_ignore(sm2));
	cl_assert_equal_i(
		GIT_SUBMODULE_UPDATE_REBASE, git_submodule_update_strategy(sm2));
	cl_assert_equal_i(
		GIT_SUBMODULE_RECURSE_NO, git_submodule_fetch_recurse_submodules(sm2));

	/* set fetchRecurseSubmodules on-demand */
	cl_git_pass(git_submodule_reload(sm1, 0));
	git_submodule_set_fetch_recurse_submodules(sm1, GIT_SUBMODULE_RECURSE_ONDEMAND);
	cl_assert_equal_i(
		GIT_SUBMODULE_RECURSE_ONDEMAND, git_submodule_fetch_recurse_submodules(sm1));
	/* call save */
	cl_git_pass(git_submodule_save(sm1));
	cl_git_pass(git_submodule_reload(sm1, 0));
	cl_assert_equal_i(
		GIT_SUBMODULE_RECURSE_ONDEMAND, git_submodule_fetch_recurse_submodules(sm1));

	git_submodule_free(sm1);
	git_submodule_free(sm2);
	git_repository_free(r2);
	git__free(old_url);
}