void test_core_path__13_cannot_prettify_a_non_existing_file(void) { git_buf p = GIT_BUF_INIT; cl_assert_equal_b(git_path_exists(NON_EXISTING_FILEPATH), false); cl_assert_equal_i(GIT_ENOTFOUND, git_path_prettify(&p, NON_EXISTING_FILEPATH, NULL)); cl_assert_equal_i(GIT_ENOTFOUND, git_path_prettify(&p, NON_EXISTING_FILEPATH "/so-do-i", NULL)); git_buf_dispose(&p); }
const char *cl_git_sandbox_path(int is_dir, ...) { const char *path = NULL; static char _temp[GIT_PATH_MAX]; git_buf buf = GIT_BUF_INIT; va_list arg; cl_git_pass(git_buf_sets(&buf, clar_sandbox_path())); va_start(arg, is_dir); while ((path = va_arg(arg, const char *)) != NULL) { cl_git_pass(git_buf_joinpath(&buf, buf.ptr, path)); } va_end(arg); cl_git_pass(git_path_prettify(&buf, buf.ptr, NULL)); if (is_dir) git_path_to_dir(&buf); /* make sure we won't truncate */ cl_assert(git_buf_len(&buf) < sizeof(_temp)); git_buf_copy_cstr(_temp, sizeof(_temp), &buf); git_buf_dispose(&buf); return _temp; }
void test_attr_ignore__expand_tilde_to_homedir(void) { git_buf path = GIT_BUF_INIT; git_config *cfg; assert_is_ignored(false, "example.global_with_tilde"); /* construct fake home with fake global excludes */ cl_must_pass(p_mkdir("home", 0777)); cl_git_pass(git_path_prettify(&path, "home", NULL)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); cl_git_mkfile("home/globalexcludes", "# found me\n*.global_with_tilde\n"); cl_git_pass(git_repository_config(&cfg, g_repo)); cl_git_pass(git_config_set_string(cfg, "core.excludesfile", "~/globalexcludes")); git_config_free(cfg); git_attr_cache_flush(g_repo); /* must reset to pick up change */ assert_is_ignored(true, "example.global_with_tilde"); cl_git_pass(git_futils_rmdir_r("home", NULL, GIT_RMDIR_REMOVE_FILES)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL)); git_buf_free(&path); }
int git_path_prettify_dir(git_buf *path_out, const char *path, const char *base) { int error = git_path_prettify(path_out, path, base); if (error == GIT_SUCCESS) error = git_path_to_dir(path_out); return error; }
void test_repo_config__initialize(void) { cl_fixture_sandbox("empty_standard_repo"); cl_git_pass(cl_rename( "empty_standard_repo/.gitted", "empty_standard_repo/.git")); git_buf_clear(&path); cl_must_pass(p_mkdir("alternate", 0777)); cl_git_pass(git_path_prettify(&path, "alternate", NULL)); }
void test_config_global__initialize(void) { git_buf path = GIT_BUF_INIT; cl_git_pass(git_futils_mkdir_r("home", 0777)); cl_git_pass(git_path_prettify(&path, "home", NULL)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); cl_git_pass(git_futils_mkdir_r("xdg/git", 0777)); cl_git_pass(git_path_prettify(&path, "xdg/git", NULL)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr)); cl_git_pass(git_futils_mkdir_r("etc", 0777)); cl_git_pass(git_path_prettify(&path, "etc", NULL)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr)); git_buf_free(&path); }
void test_core_env__0(void) { static char *home_values[] = { "fake_home", "fáke_hõme", /* all in latin-1 supplement */ "fĀke_Ĥome", /* latin extended */ "fακε_hοmέ", /* having fun with greek */ "faงe_นome", /* now I have no idea, but thai characters */ "f\xe1\x9cx80ke_\xe1\x9c\x91ome", /* tagalog characters */ "\xe1\xb8\x9fẢke_hoṁe", /* latin extended additional */ "\xf0\x9f\x98\x98\xf0\x9f\x98\x82", /* emoticons */ NULL }; git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT; char **val; char *check; for (val = home_values; *val != NULL; val++) { if (p_mkdir(*val, 0777) == 0) { /* if we can't make the directory, let's just assume * we are on a filesystem that doesn't support the * characters in question and skip this test... */ cl_git_pass(git_path_prettify(&path, *val, NULL)); #ifdef GIT_WIN32 cl_git_pass(cl_setenv("USERPROFILE", path.ptr)); /* do a quick check that it was set correctly */ check = cl_getenv("USERPROFILE"); cl_assert_equal_s(path.ptr, check); git__free(check); #else cl_git_pass(cl_setenv("HOME", path.ptr)); /* do a quick check that it was set correctly */ check = cl_getenv("HOME"); cl_assert_equal_s(path.ptr, check); #endif cl_git_pass(git_buf_puts(&path, "/testfile")); cl_git_mkfile(path.ptr, "find me"); cl_git_pass(git_futils_find_global_file(&found, "testfile")); } } git_buf_free(&path); git_buf_free(&found); }
int git_blob_create_fromdisk(git_oid *oid, git_repository *repo, const char *path) { int error; git_buf full_path = GIT_BUF_INIT; if ((error = git_path_prettify(&full_path, path, NULL)) < 0) { git_buf_free(&full_path); return error; } error = blob_create_internal(oid, repo, git_buf_cstr(&full_path), git_buf_cstr(&full_path), true); git_buf_free(&full_path); return error; }
void cl_fake_home(void) { git_buf path = GIT_BUF_INIT; cl_git_pass(git_libgit2_opts( GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &path)); _cl_restore_home = git_buf_detach(&path); cl_set_cleanup(cl_fake_home_cleanup, NULL); if (!git_path_exists("home")) cl_must_pass(p_mkdir("home", 0777)); cl_git_pass(git_path_prettify(&path, "home", NULL)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); git_buf_free(&path); }
static void init_linked_repo(const char *path, const char *alternate) { git_buf_clear(&destpath); git_buf_clear(&filepath); cl_git_pass(git_repository_init(&repo, path, 1)); cl_git_pass(git_path_prettify(&destpath, alternate, NULL)); cl_git_pass(git_buf_joinpath(&destpath, destpath.ptr, "objects")); cl_git_pass(git_buf_joinpath(&filepath, git_repository_path(repo), "objects/info")); cl_git_pass(git_futils_mkdir(filepath.ptr, NULL, 0755, GIT_MKDIR_PATH)); cl_git_pass(git_buf_joinpath(&filepath, filepath.ptr , "alternates")); cl_git_pass(git_filebuf_open(&file, git_buf_cstr(&filepath), 0)); git_filebuf_printf(&file, "%s\n", git_buf_cstr(&destpath)); cl_git_pass(git_filebuf_commit(&file, 0644)); git_repository_free(repo); }
void test_config_read__path(void) { git_config *cfg; git_buf path = GIT_BUF_INIT; git_buf old_path = GIT_BUF_INIT; git_buf home_path = GIT_BUF_INIT; git_buf expected_path = GIT_BUF_INIT; cl_git_pass(p_mkdir("fakehome", 0777)); cl_git_pass(git_path_prettify(&home_path, "fakehome", NULL)); cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &old_path)); cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, home_path.ptr)); cl_git_mkfile("./testconfig", "[some]\n path = ~/somefile"); cl_git_pass(git_path_join_unrooted(&expected_path, "somefile", home_path.ptr, NULL)); cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig")); cl_git_pass(git_config_get_path(&path, cfg, "some.path")); cl_assert_equal_s(expected_path.ptr, path.ptr); git_buf_dispose(&path); cl_git_mkfile("./testconfig", "[some]\n path = ~/"); cl_git_pass(git_path_join_unrooted(&expected_path, "", home_path.ptr, NULL)); cl_git_pass(git_config_get_path(&path, cfg, "some.path")); cl_assert_equal_s(expected_path.ptr, path.ptr); git_buf_dispose(&path); cl_git_mkfile("./testconfig", "[some]\n path = ~"); cl_git_pass(git_buf_sets(&expected_path, home_path.ptr)); cl_git_pass(git_config_get_path(&path, cfg, "some.path")); cl_assert_equal_s(expected_path.ptr, path.ptr); git_buf_dispose(&path); cl_git_mkfile("./testconfig", "[some]\n path = ~user/foo"); cl_git_fail(git_config_get_path(&path, cfg, "some.path")); cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, old_path.ptr)); git_buf_dispose(&old_path); git_buf_dispose(&home_path); git_buf_dispose(&expected_path); git_config_free(cfg); }
int git_blob_create_fromdisk(git_oid *oid, git_repository *repo, const char *path) { int error; git_buf full_path = GIT_BUF_INIT; const char *workdir, *hintpath; if ((error = git_path_prettify(&full_path, path, NULL)) < 0) { git_buf_free(&full_path); return error; } hintpath = git_buf_cstr(&full_path); workdir = git_repository_workdir(repo); if (workdir && !git__prefixcmp(hintpath, workdir)) hintpath += strlen(workdir); error = blob_create_internal( oid, repo, git_buf_cstr(&full_path), hintpath, true); git_buf_free(&full_path); return error; }
void test_repo_open__no_config(void) { git_buf path = GIT_BUF_INIT; git_repository *repo; git_config *config; cl_fixture_sandbox("empty_standard_repo"); cl_git_pass(cl_rename( "empty_standard_repo/.gitted", "empty_standard_repo/.git")); /* remove local config */ cl_git_pass(git_futils_rmdir_r( "empty_standard_repo/.git/config", NULL, GIT_RMDIR_REMOVE_FILES)); /* isolate from system level configs */ cl_must_pass(p_mkdir("alternate", 0777)); cl_git_pass(git_path_prettify(&path, "alternate", NULL)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr)); git_buf_free(&path); cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); cl_git_pass(git_repository_config(&config, repo)); cl_git_pass(git_config_set_string(config, "test.set", "42")); git_config_free(config); git_repository_free(repo); cl_fixture_cleanup("empty_standard_repo"); cl_sandbox_set_search_path_defaults(); }
void test_repo_init__relative_gitdir_2(void) { git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT; git_buf dot_git_content = GIT_BUF_INIT; git_buf full_path = GIT_BUF_INIT; cl_git_pass(git_path_prettify(&full_path, ".", NULL)); cl_git_pass(git_buf_joinpath(&full_path, full_path.ptr, "root/b/c_wd")); opts.workdir_path = full_path.ptr; 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)); git_buf_free(&full_path); 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"); }
/* rewrite gitmodules -> .gitmodules * rewrite the empty or relative urls inside each module * rename the .gitted directory inside any submodule to .git */ void rewrite_gitmodules(const char *workdir) { git_buf in_f = GIT_BUF_INIT, out_f = GIT_BUF_INIT, path = GIT_BUF_INIT; FILE *in, *out; char line[256]; cl_git_pass(git_buf_joinpath(&in_f, workdir, "gitmodules")); cl_git_pass(git_buf_joinpath(&out_f, workdir, ".gitmodules")); cl_assert((in = fopen(in_f.ptr, "rb")) != NULL); cl_assert((out = fopen(out_f.ptr, "wb")) != NULL); while (fgets(line, sizeof(line), in) != NULL) { char *scan = line; while (*scan == ' ' || *scan == '\t') scan++; /* rename .gitted -> .git in submodule directories */ if (git__prefixcmp(scan, "path =") == 0) { scan += strlen("path ="); while (*scan == ' ') scan++; git_buf_joinpath(&path, workdir, scan); git_buf_rtrim(&path); git_buf_joinpath(&path, path.ptr, ".gitted"); if (!git_buf_oom(&path) && p_access(path.ptr, F_OK) == 0) { git_buf_joinpath(&out_f, workdir, scan); git_buf_rtrim(&out_f); git_buf_joinpath(&out_f, out_f.ptr, ".git"); if (!git_buf_oom(&out_f)) p_rename(path.ptr, out_f.ptr); } } /* copy non-"url =" lines verbatim */ if (git__prefixcmp(scan, "url =") != 0) { fputs(line, out); continue; } /* convert relative URLs in "url =" lines */ scan += strlen("url ="); while (*scan == ' ') scan++; if (*scan == '.') { git_buf_joinpath(&path, workdir, scan); git_buf_rtrim(&path); } else if (!*scan || *scan == '\n') { git_buf_joinpath(&path, workdir, "../testrepo.git"); } else { fputs(line, out); continue; } git_path_prettify(&path, path.ptr, NULL); git_buf_putc(&path, '\n'); cl_assert(!git_buf_oom(&path)); fwrite(line, scan - line, sizeof(char), out); fputs(path.ptr, out); } fclose(in); fclose(out); cl_must_pass(p_unlink(in_f.ptr)); git_buf_free(&in_f); git_buf_free(&out_f); git_buf_free(&path); }
int git_path_prettify_dir(git_buf *path_out, const char *path, const char *base) { int error = git_path_prettify(path_out, path, base); return (error < 0) ? error : git_path_to_dir(path_out); }
static int find_repo( git_buf *repo_path, git_buf *parent_path, const char *start_path, uint32_t flags, const char *ceiling_dirs) { int error; git_buf path = GIT_BUF_INIT; struct stat st; dev_t initial_device = 0; bool try_with_dot_git = ((flags & GIT_REPOSITORY_OPEN_BARE) != 0); int ceiling_offset; git_buf_free(repo_path); if ((error = git_path_prettify(&path, start_path, NULL)) < 0) return error; ceiling_offset = find_ceiling_dir_offset(path.ptr, ceiling_dirs); if (!try_with_dot_git && (error = git_buf_joinpath(&path, path.ptr, DOT_GIT)) < 0) return error; while (!error && !git_buf_len(repo_path)) { if (p_stat(path.ptr, &st) == 0) { /* check that we have not crossed device boundaries */ if (initial_device == 0) initial_device = st.st_dev; else if (st.st_dev != initial_device && (flags & GIT_REPOSITORY_OPEN_CROSS_FS) == 0) break; if (S_ISDIR(st.st_mode)) { if (valid_repository_path(&path)) { git_path_to_dir(&path); git_buf_set(repo_path, path.ptr, path.size); break; } } else if (S_ISREG(st.st_mode)) { git_buf repo_link = GIT_BUF_INIT; if (!(error = read_gitfile(&repo_link, path.ptr))) { if (valid_repository_path(&repo_link)) git_buf_swap(repo_path, &repo_link); git_buf_free(&repo_link); break; } git_buf_free(&repo_link); } } /* move up one directory level */ if (git_path_dirname_r(&path, path.ptr) < 0) { error = -1; break; } if (try_with_dot_git) { /* if we tried original dir with and without .git AND either hit * directory ceiling or NO_SEARCH was requested, then be done. */ if (path.ptr[ceiling_offset] == '\0' || (flags & GIT_REPOSITORY_OPEN_NO_SEARCH) != 0) break; /* otherwise look first for .git item */ error = git_buf_joinpath(&path, path.ptr, DOT_GIT); } try_with_dot_git = !try_with_dot_git; } if (!error && parent_path && !(flags & GIT_REPOSITORY_OPEN_BARE)) { if (!git_buf_len(repo_path)) git_buf_clear(parent_path); else { git_path_dirname_r(parent_path, path.ptr); git_path_to_dir(parent_path); } if (git_buf_oom(parent_path)) return -1; } git_buf_free(&path); if (!git_buf_len(repo_path) && !error) { giterr_set(GITERR_REPOSITORY, "Could not find repository from '%s'", start_path); error = GIT_ENOTFOUND; } return error; }