void test_reset_hard__resetting_culls_empty_directories(void) { git_buf subdir_path = GIT_BUF_INIT; git_buf subfile_path = GIT_BUF_INIT; git_buf newdir_path = GIT_BUF_INIT; cl_git_pass(git_buf_joinpath(&newdir_path, git_repository_workdir(repo), "newdir/")); cl_git_pass(git_buf_joinpath(&subfile_path, git_buf_cstr(&newdir_path), "with/nested/file.txt")); cl_git_pass(git_futils_mkpath2file(git_buf_cstr(&subfile_path), 0755)); cl_git_mkfile(git_buf_cstr(&subfile_path), "all anew...\n"); cl_git_pass(git_buf_joinpath(&subdir_path, git_repository_workdir(repo), "subdir/")); cl_assert(git_path_isdir(git_buf_cstr(&subdir_path)) == true); retrieve_target_from_oid(&target, repo, "0017bd4ab1ec30440b17bae1680cff124ab5f1f6"); cl_git_pass(git_reset(repo, target, GIT_RESET_HARD)); cl_assert(git_path_isdir(git_buf_cstr(&subdir_path)) == false); cl_assert(git_path_isdir(git_buf_cstr(&newdir_path)) == false); git_buf_free(&subdir_path); git_buf_free(&subfile_path); git_buf_free(&newdir_path); }
static void assert_sm_valid(git_repository *parent, git_repository *child, const char *sm_name) { git_buf expected = GIT_BUF_INIT, actual = GIT_BUF_INIT; /* assert working directory */ cl_git_pass(git_buf_joinpath(&expected, git_repository_workdir(parent), sm_name)); cl_git_pass(git_path_prettify_dir(&expected, expected.ptr, NULL)); cl_git_pass(git_buf_sets(&actual, git_repository_workdir(child))); cl_git_pass(git_path_prettify_dir(&actual, actual.ptr, NULL)); cl_assert_equal_s(expected.ptr, actual.ptr); git_buf_clear(&expected); git_buf_clear(&actual); /* assert common directory */ cl_git_pass(git_buf_joinpath(&expected, git_repository_commondir(parent), "modules")); cl_git_pass(git_buf_joinpath(&expected, expected.ptr, sm_name)); cl_git_pass(git_path_prettify_dir(&expected, expected.ptr, NULL)); cl_git_pass(git_buf_sets(&actual, git_repository_commondir(child))); cl_git_pass(git_path_prettify_dir(&actual, actual.ptr, NULL)); cl_assert_equal_s(expected.ptr, actual.ptr); /* assert git directory */ cl_git_pass(git_buf_sets(&actual, git_repository_path(child))); cl_git_pass(git_path_prettify_dir(&actual, actual.ptr, NULL)); cl_assert_equal_s(expected.ptr, actual.ptr); git_buf_dispose(&expected); git_buf_dispose(&actual); }
void test_repo_setters__setting_a_workdir_turns_a_bare_repository_into_a_standard_one(void) { cl_assert(git_repository_is_bare(repo) == 1); cl_assert(git_repository_workdir(repo) == NULL); cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", false)); cl_assert(git_repository_workdir(repo) != NULL); cl_assert(git_repository_is_bare(repo) == 0); }
void test_repo_open__standard_empty_repo_through_workdir(void) { git_repository *repo = cl_git_sandbox_init("empty_standard_repo"); cl_assert(git_repository_path(repo) != NULL); cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0); cl_assert(git_repository_workdir(repo) != NULL); cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0); }
void test_repo_open__standard_empty_repo_through_gitdir(void) { git_repository *repo; cl_git_pass(git_repository_open(&repo, cl_fixture("empty_standard_repo/.gitted"))); cl_assert(git_repository_path(repo) != NULL); cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0); cl_assert(git_repository_workdir(repo) != NULL); cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0); git_repository_free(repo); }
void test_repo_open__failures(void) { git_repository *base, *repo; git_buf ceiling = GIT_BUF_INIT; base = cl_git_sandbox_init("attr"); cl_git_pass(git_buf_sets(&ceiling, git_repository_workdir(base))); /* fail with no searching */ cl_git_fail(git_repository_open(&repo, "attr/sub")); cl_git_fail(git_repository_open_ext( &repo, "attr/sub", GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)); /* fail with ceiling too low */ cl_git_pass(git_buf_joinpath(&ceiling, ceiling.ptr, "sub")); cl_git_fail(git_repository_open_ext(&repo, "attr/sub", 0, ceiling.ptr)); /* fail with no repo */ cl_git_pass(p_mkdir("alternate", 0777)); cl_git_pass(p_mkdir("alternate/.git", 0777)); cl_git_fail(git_repository_open_ext(&repo, "alternate", 0, NULL)); cl_git_fail(git_repository_open_ext(&repo, "alternate/.git", 0, NULL)); git_buf_free(&ceiling); }
/* * Note: this test was flaky prior to fixing #4101 -- run it several * times to get a failure. The issues is that whether the fast * (stat-only) codepath is used inside stash's diff operation depends * on whether files are "racily clean", and there doesn't seem to be * an easy way to force the exact required state. */ void test_stash_save__untracked_regression(void) { git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; const char *paths[] = {"what", "where", "how", "why"}; git_reference *head; git_commit *head_commit; git_buf untracked_dir; const char* workdir = git_repository_workdir(repo); git_buf_init(&untracked_dir, 0); git_buf_printf(&untracked_dir, "%sz", workdir); cl_assert(!p_mkdir(untracked_dir.ptr, 0777)); cl_git_pass(git_repository_head(&head, repo)); cl_git_pass(git_reference_peel((git_object **)&head_commit, head, GIT_OBJ_COMMIT)); opts.checkout_strategy = GIT_CHECKOUT_FORCE; opts.paths.strings = (char **)paths; opts.paths.count = 4; cl_git_pass(git_checkout_tree(repo, (git_object*)head_commit, &opts)); cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT)); assert_commit_message_contains("refs/stash", "WIP on master"); git_reference_free(head); git_commit_free(head_commit); git_buf_dispose(&untracked_dir); }
void test_submodule_init__relative_url(void) { git_submodule *sm; git_config *cfg; git_buf absolute_url = GIT_BUF_INIT; const char *config_url; g_repo = setup_fixture_submodule_simple(); cl_assert(git_path_dirname_r(&absolute_url, git_repository_workdir(g_repo)) > 0); cl_git_pass(git_buf_joinpath(&absolute_url, absolute_url.ptr, "testrepo.git")); cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo")); /* verify that the .gitmodules is set with an absolute path*/ cl_assert_equal_s("../testrepo.git", git_submodule_url(sm)); /* init and verify that absolute path is written to .git/config */ cl_git_pass(git_submodule_init(sm, false)); cl_git_pass(git_repository_config_snapshot(&cfg, g_repo)); cl_git_pass(git_config_get_string(&config_url, cfg, "submodule.testrepo.url")); cl_assert_equal_s(absolute_url.ptr, config_url); git_buf_free(&absolute_url); git_config_free(cfg); git_submodule_free(sm); }
void test_diff_workdir__submodules(void) { const char *a_commit = "873585b94bdeabccea991ea5e3ec1a277895b698"; git_tree *a; git_diff_options opts = {0}; git_diff_list *diff = NULL; diff_expects exp; g_repo = cl_git_sandbox_init("submod2"); cl_fixture_sandbox("submod2_target"); p_rename("submod2_target/.gitted", "submod2_target/.git"); rewrite_gitmodules(git_repository_workdir(g_repo)); p_rename("submod2/not_submodule/.gitted", "submod2/not_submodule/.git"); cl_fixture_cleanup("submod2_target"); a = resolve_commit_oid_to_tree(g_repo, a_commit); opts.flags = GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_RECURSE_UNTRACKED_DIRS | GIT_DIFF_INCLUDE_UNTRACKED_CONTENT; cl_git_pass(git_diff_workdir_to_tree(g_repo, &opts, a, &diff)); /* diff_print(stderr, diff); */ /* essentially doing: git diff 873585b94bdeabccea991ea5e3ec1a277895b698 */ memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); /* the following differs from "git diff 873585" by one "untracked" file * because the diff list includes the "not_submodule/" directory which * is not displayed in the text diff. */ cl_assert_equal_i(10, exp.files); cl_assert_equal_i(0, exp.file_adds); cl_assert_equal_i(0, exp.file_dels); cl_assert_equal_i(1, exp.file_mods); cl_assert_equal_i(0, exp.file_ignored); cl_assert_equal_i(9, exp.file_untracked); /* the following numbers match "git diff 873585" exactly */ cl_assert_equal_i(9, exp.hunks); cl_assert_equal_i(33, exp.lines); cl_assert_equal_i(2, exp.line_ctxt); cl_assert_equal_i(30, exp.line_adds); cl_assert_equal_i(1, exp.line_dels); git_diff_list_free(diff); git_tree_free(a); }
static void setup_race(void) { git_buf path = GIT_BUF_INIT; git_index *index; git_index_entry *entry; struct stat st; /* Make sure we do have a timestamp */ cl_git_pass(git_repository_index__weakptr(&index, g_repo)); cl_git_pass(git_index_write(index)); cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "A")); cl_git_mkfile(path.ptr, "A"); cl_git_pass(git_index_add_bypath(index, "A")); cl_git_mkfile(path.ptr, "B"); cl_git_pass(git_index_write(index)); cl_git_mkfile(path.ptr, ""); cl_git_pass(p_stat(path.ptr, &st)); cl_assert(entry = (git_index_entry *)git_index_get_bypath(index, "A", 0)); /* force a race */ entry->mtime.seconds = (int32_t)st.st_mtime; entry->mtime.nanoseconds = (int32_t)st.st_mtime_nsec; git_buf_dispose(&path); }
/* this test is equivalent to t18-status.c:statuscb2 */ void test_status_worktree__purged_worktree(void) { status_entry_counts counts; git_repository *repo = cl_git_sandbox_init("status"); git_buf workdir = GIT_BUF_INIT; /* first purge the contents of the worktree */ cl_git_pass(git_buf_sets(&workdir, git_repository_workdir(repo))); cl_git_pass(git_path_direach(&workdir, remove_file_cb, NULL)); git_buf_free(&workdir); /* now get status */ memset(&counts, 0x0, sizeof(status_entry_counts)); counts.expected_entry_count = entry_count2; counts.expected_paths = entry_paths2; counts.expected_statuses = entry_statuses2; cl_git_pass( git_status_foreach(repo, cb_status__normal, &counts) ); cl_assert_equal_i(counts.expected_entry_count, counts.entry_count); cl_assert_equal_i(0, counts.wrong_status_flags_count); cl_assert_equal_i(0, counts.wrong_sorted_path); }
static void ensure_repository_init( const char *working_directory, int is_bare, const char *expected_path_repository, const char *expected_working_directory) { const char *workdir; cl_assert(!git_path_isdir(working_directory)); cl_git_pass(git_repository_init(&_repo, working_directory, is_bare)); workdir = git_repository_workdir(_repo); if (workdir != NULL || expected_working_directory != NULL) { cl_assert( git__suffixcmp(workdir, expected_working_directory) == 0 ); } cl_assert( git__suffixcmp(git_repository_path(_repo), expected_path_repository) == 0 ); cl_assert(git_repository_is_bare(_repo) == is_bare); #ifdef GIT_WIN32 if (!is_bare) { DWORD fattrs = GetFileAttributes(git_repository_path(_repo)); cl_assert((fattrs & FILE_ATTRIBUTE_HIDDEN) != 0); } #endif cl_assert(git_repository_is_empty(_repo)); }
static void add_submodule_with_commit(const char *name) { git_submodule *sm; git_repository *smrepo; git_index *idx; git_buf p = GIT_BUF_INIT; cl_git_pass(git_submodule_add_setup(&sm, g_repo, "https://github.com/libgit2/libgit2.git", name, 1)); assert_submodule_exists(g_repo, name); cl_git_pass(git_submodule_open(&smrepo, sm)); cl_git_pass(git_repository_index(&idx, smrepo)); cl_git_pass(git_buf_joinpath(&p, git_repository_workdir(smrepo), "file")); cl_git_mkfile(p.ptr, "new file"); git_buf_free(&p); cl_git_pass(git_index_add_bypath(idx, "file")); cl_git_pass(git_index_write(idx)); git_index_free(idx); cl_repo_commit_from_index(NULL, smrepo, NULL, 0, "initial commit"); git_repository_free(smrepo); cl_git_pass(git_submodule_add_finalize(sm)); git_submodule_free(sm); }
static int diff_file_content_load_workdir(git_diff_file_content *fc) { int error = 0; git_buf path = GIT_BUF_INIT; if (fc->file->mode == GIT_FILEMODE_COMMIT) return diff_file_content_commit_to_str(fc, true); if (fc->file->mode == GIT_FILEMODE_TREE) return 0; if (git_buf_joinpath( &path, git_repository_workdir(fc->repo), fc->file->path) < 0) return -1; if (S_ISLNK(fc->file->mode)) error = diff_file_content_load_workdir_symlink(fc, &path); else error = diff_file_content_load_workdir_file(fc, &path); /* once data is loaded, update OID if we didn't have it previously */ if (!error && (fc->file->flags & GIT_DIFF_FLAG_VALID_OID) == 0) { error = git_odb_hash( &fc->file->oid, fc->map.data, fc->map.len, GIT_OBJ_BLOB); fc->file->flags |= GIT_DIFF_FLAG_VALID_OID; } git_buf_free(&path); return error; }
void test_repo_init__relative_gitdir(void) { git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT; git_buf dot_git_content = GIT_BUF_INIT; opts.workdir_path = "../c_wd"; opts.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_RELATIVE_GITLINK | GIT_REPOSITORY_INIT_NO_DOTGIT_DIR; /* make the directory first, then it should succeed */ cl_git_pass(git_repository_init_ext(&_repo, "root/b/my_repository", &opts)); cl_assert(!git__suffixcmp(git_repository_workdir(_repo), "root/b/c_wd/")); cl_assert(!git__suffixcmp(git_repository_path(_repo), "root/b/my_repository/")); cl_assert(!git_repository_is_bare(_repo)); cl_assert(git_repository_is_empty(_repo)); /* Verify that the gitlink and worktree entries are relative */ /* Verify worktree */ assert_config_entry_value(_repo, "core.worktree", "../c_wd/"); /* Verify gitlink */ cl_git_pass(git_futils_readbuffer(&dot_git_content, "root/b/c_wd/.git")); cl_assert_equal_s("gitdir: ../my_repository/", dot_git_content.ptr); git_buf_free(&dot_git_content); cleanup_repository("root"); }
void test_submodule_status__untracked_dirs_containing_ignored_files(void) { git_buf path = GIT_BUF_INIT; unsigned int status, expected; git_submodule *sm; cl_git_pass(git_buf_joinpath(&path, git_repository_path(g_repo), "modules/sm_unchanged/info/exclude")); cl_git_append2file(git_buf_cstr(&path), "\n*.ignored\n"); cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "sm_unchanged/directory")); cl_git_pass(git_futils_mkdir(git_buf_cstr(&path), NULL, 0755, 0)); cl_git_pass(git_buf_joinpath(&path, git_buf_cstr(&path), "i_am.ignored")); cl_git_mkfile(git_buf_cstr(&path), "ignored this file, please\n"); cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); cl_git_pass(git_submodule_status(&status, sm)); cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); expected = GIT_SUBMODULE_STATUS_IN_HEAD | GIT_SUBMODULE_STATUS_IN_INDEX | GIT_SUBMODULE_STATUS_IN_CONFIG | GIT_SUBMODULE_STATUS_IN_WD; cl_assert(status == expected); git_buf_free(&path); }
bool CGitStatus::GetRepoRootInternal(const wstring& path, wstring& repoRoot_out, git_buf &buf, git_repository *&repo) { // This is temporary. CMutexLock mutexLock(&m_mutex); SecureZeroMemory(&buf, sizeof(git_buf)); wstring_convert<codecvt_utf8<wchar_t>> converter; string cpath = converter.to_bytes(path); if (git_repository_discover(&buf, cpath.c_str(), 0, NULL)) { Logger::LogWarning(_T("Unable to find git repository")); return false; } if (git_repository_open(&repo, buf.ptr)) { Logger::LogError(_T("Unable to open repository")); git_buf_free(&buf); return false; } const char* workDir = git_repository_workdir(repo); if (workDir == nullptr) { Logger::LogWarning(_T("No working directory. Probably bare repository.")); git_buf_free(&buf); return false; } repoRoot_out = MyUtils::NormalizePath(converter.from_bytes(workDir)); return true; }
static void setup_uptodate_files(void) { git_buf path = GIT_BUF_INIT; git_index *index; const git_index_entry *a_entry; git_index_entry new_entry = {{0}}; cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "A")); cl_git_mkfile(path.ptr, "A"); /* Put 'A' into the index */ cl_git_pass(git_index_add_bypath(index, "A")); cl_assert((a_entry = git_index_get_bypath(index, "A", 0))); /* Put 'B' into the index */ new_entry.path = "B"; new_entry.mode = GIT_FILEMODE_BLOB; git_oid_cpy(&new_entry.id, &a_entry->id); cl_git_pass(git_index_add(index, &new_entry)); /* Put 'C' into the index */ new_entry.path = "C"; new_entry.mode = GIT_FILEMODE_BLOB; cl_git_pass(git_index_add_frombuffer(index, &new_entry, "hello!\n", 7)); git_index_free(index); git_buf_dispose(&path); }
void test_index_racy__diff(void) { git_index *index; git_diff *diff; git_buf path = GIT_BUF_INIT; cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "A")); cl_git_mkfile(path.ptr, "A"); /* Put 'A' into the index */ cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_index_add_bypath(index, "A")); cl_git_pass(git_index_write(index)); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL)); cl_assert_equal_i(0, git_diff_num_deltas(diff)); git_diff_free(diff); /* Change its contents quickly, so we get the same timestamp */ cl_git_mkfile(path.ptr, "B"); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL)); cl_assert_equal_i(1, git_diff_num_deltas(diff)); git_index_free(index); git_diff_free(diff); git_buf_dispose(&path); }
void test_rebase_submodule__init_untracked(void) { git_rebase *rebase; git_reference *branch_ref, *upstream_ref; git_annotated_commit *branch_head, *upstream_head; git_buf untracked_path = GIT_BUF_INIT; FILE *fp; git_submodule *submodule; cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/asparagus")); cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/master")); cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref)); cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref)); cl_git_pass(git_submodule_lookup(&submodule, repo, "my-submodule")); cl_git_pass(git_submodule_update(submodule, 1, NULL)); git_buf_printf(&untracked_path, "%s/my-submodule/untracked", git_repository_workdir(repo)); fp = fopen(git_buf_cstr(&untracked_path), "w"); fprintf(fp, "An untracked file in a submodule should not block a rebase\n"); fclose(fp); git_buf_free(&untracked_path); cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, NULL)); git_submodule_free(submodule); git_annotated_commit_free(branch_head); git_annotated_commit_free(upstream_head); git_reference_free(branch_ref); git_reference_free(upstream_ref); git_rebase_free(rebase); }
void test_online_clone__can_checkout_a_cloned_repo(void) { git_buf path = GIT_BUF_INIT; git_reference *head; bool checkout_progress_cb_was_called = false, fetch_progress_cb_was_called = false; g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; g_options.checkout_opts.progress_cb = &checkout_progress; g_options.checkout_opts.progress_payload = &checkout_progress_cb_was_called; g_options.fetch_opts.callbacks.transfer_progress = &fetch_progress; g_options.fetch_opts.callbacks.payload = &fetch_progress_cb_was_called; cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options)); cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt")); cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&path))); cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD")); cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head)); cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head)); cl_assert_equal_i(true, checkout_progress_cb_was_called); cl_assert_equal_i(true, fetch_progress_cb_was_called); git_reference_free(head); git_buf_free(&path); }
static const char *repository_base(git_repository *repo) { if (git_repository_is_bare(repo)) return git_repository_path(repo); return git_repository_workdir(repo); }
static void setup_submodules(void) { g_repo = cl_git_sandbox_init("submodules"); cl_fixture_sandbox("testrepo.git"); rewrite_gitmodules(git_repository_workdir(g_repo)); p_rename("submodules/testrepo/.gitted", "submodules/testrepo/.git"); }
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); }
void test_repo_open__bare_empty_repo(void) { git_repository *repo = cl_git_sandbox_init("empty_bare.git"); cl_assert(git_repository_path(repo) != NULL); cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0); cl_assert(git_repository_workdir(repo) == NULL); }
static void assert_workdir_matches_tree( git_repository *repo, const git_oid *id, const char *root, bool recurse) { git_object *obj; git_tree *tree; size_t i, max_i; git_buf path = GIT_BUF_INIT; if (!root) root = git_repository_workdir(repo); cl_assert(root); cl_git_pass(git_object_lookup(&obj, repo, id, GIT_OBJ_ANY)); cl_git_pass(git_object_peel((git_object **)&tree, obj, GIT_OBJ_TREE)); git_object_free(obj); max_i = git_tree_entrycount(tree); for (i = 0; i < max_i; ++i) { const git_tree_entry *te = git_tree_entry_byindex(tree, i); cl_assert(te); cl_git_pass(git_buf_joinpath(&path, root, git_tree_entry_name(te))); switch (git_tree_entry_type(te)) { case GIT_OBJ_COMMIT: assert_dir_exists(path.ptr); break; case GIT_OBJ_TREE: assert_dir_exists(path.ptr); if (recurse) assert_workdir_matches_tree( repo, git_tree_entry_id(te), path.ptr, true); break; case GIT_OBJ_BLOB: switch (git_tree_entry_filemode(te)) { case GIT_FILEMODE_BLOB: case GIT_FILEMODE_BLOB_EXECUTABLE: assert_file_exists(path.ptr); /* because of cross-platform, don't confirm exec bit yet */ break; case GIT_FILEMODE_LINK: cl_assert_(git_path_exists(path.ptr), path.ptr); /* because of cross-platform, don't confirm link yet */ break; default: cl_assert(false); /* really?! */ } break; default: cl_assert(false); /* really?!! */ } } git_tree_free(tree); git_buf_free(&path); }
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); if ((error = load_submodule_config(repo)) < 0) 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); } giterr_set(GITERR_SUBMODULE, (error == GIT_ENOTFOUND) ? "No submodule named '%s'" : "Submodule '%s' has not been added yet", name); return error; } if (sm_ptr) *sm_ptr = git_strmap_value_at(repo->submodules, pos); return 0; }
void test_repo_new__has_nothing(void) { git_repository *repo; cl_git_pass(git_repository_new(&repo)); cl_assert_equal_b(true, git_repository_is_bare(repo)); cl_assert_equal_p(NULL, git_repository_path(repo)); cl_assert_equal_p(NULL, git_repository_workdir(repo)); git_repository_free(repo); }
void test_repo_open__gitlinked(void) { /* need to have both repo dir and workdir set up correctly */ git_repository *repo = cl_git_sandbox_init("empty_standard_repo"); git_repository *repo2; make_gitlink_dir("alternate", "gitdir: ../empty_standard_repo/.git"); cl_git_pass(git_repository_open(&repo2, "alternate")); cl_assert(git_repository_path(repo2) != NULL); cl_assert_(git__suffixcmp(git_repository_path(repo2), "empty_standard_repo/.git/") == 0, git_repository_path(repo2)); cl_assert_equal_s(git_repository_path(repo), git_repository_path(repo2)); cl_assert(git_repository_workdir(repo2) != NULL); cl_assert_(git__suffixcmp(git_repository_workdir(repo2), "alternate/") == 0, git_repository_workdir(repo2)); git_repository_free(repo2); }
static void env_pass_(const char *path, const char *file, int line) { git_repository *repo; cl_git_expect(git_repository_open_ext(NULL, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), 0, file, line); cl_git_expect(git_repository_open_ext(&repo, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), 0, file, line); cl_assert_at_line(git__suffixcmp(git_repository_path(repo), "attr/.git/") == 0, file, line); cl_assert_at_line(git__suffixcmp(git_repository_workdir(repo), "attr/") == 0, file, line); cl_assert_at_line(!git_repository_is_bare(repo), file, line); git_repository_free(repo); }