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; }
static int filter_apply( git_filter *self, void **payload, /* may be read and/or set */ git_buf *to, const git_buf *from, const git_filter_source *src) { struct filter_filter *ffs = (struct filter_filter *)self; git_config *config; git_buf configKey = GIT_BUF_INIT; int isRequired = FALSE; int error; const char *cmd = NULL; git_buf cmdBuf = GIT_BUF_INIT; wchar_t *wide_cmd; COMMAND_HANDLE commandHandle = COMMAND_HANDLE_INIT; git_buf errBuf = GIT_BUF_INIT; DWORD exitCode; if (!*payload) return GIT_PASSTHROUGH; if (git_repository_config__weakptr(&config, git_filter_source_repo(src))) return -1; git_buf_join3(&configKey, '.', "filter", *payload, "required"); if (git_buf_oom(&configKey)) { giterr_set_oom(); return -1; } error = git_config_get_bool(&isRequired, config, configKey.ptr); git_buf_free(&configKey); if (error && error != GIT_ENOTFOUND) return -1; git_buf_join(&configKey, '.', "filter", *payload); if (git_filter_source_mode(src) == GIT_FILTER_SMUDGE) { git_buf_puts(&configKey, ".smudge"); } else { git_buf_puts(&configKey, ".clean"); } if (git_buf_oom(&configKey)) { giterr_set_oom(); return -1; } error = git_config_get_string(&cmd, config, configKey.ptr); git_buf_free(&configKey); if (error && error != GIT_ENOTFOUND) return -1; if (error == GIT_ENOTFOUND) { if (isRequired) return -1; return GIT_PASSTHROUGH; } git_buf_puts(&cmdBuf, cmd); if (git_buf_oom(&cmdBuf)) { giterr_set_oom(); return -1; } if (expandPerCentF(&cmdBuf, git_filter_source_path(src))) return -1; if (ffs->shexepath) { // build params for sh.exe git_buf shParams = GIT_BUF_INIT; git_buf_puts(&shParams, " -c \""); git_buf_text_puts_escaped(&shParams, cmdBuf.ptr, "\"\\", "\\"); git_buf_puts(&shParams, "\""); if (git_buf_oom(&shParams)) { git_buf_free(&cmdBuf); giterr_set_oom(); return -1; } git_buf_swap(&shParams, &cmdBuf); git_buf_free(&shParams); } if (git__utf8_to_16_alloc(&wide_cmd, cmdBuf.ptr) < 0) { git_buf_free(&cmdBuf); giterr_set_oom(); return -1; } git_buf_free(&cmdBuf); if (ffs->shexepath) { // build cmd, i.e. shexepath + params size_t len = wcslen(ffs->shexepath) + wcslen(wide_cmd) + 1; wchar_t *tmp = git__calloc(len, sizeof(wchar_t)); if (!tmp) { git__free(wide_cmd); giterr_set_oom(); return -1; } wcscat_s(tmp, len, ffs->shexepath); wcscat_s(tmp, len, wide_cmd); git__free(wide_cmd); wide_cmd = tmp; } commandHandle.errBuf = &errBuf; if (command_start(wide_cmd, &commandHandle, ffs->pEnv)) { git__free(wide_cmd); if (isRequired) return -1; return GIT_PASSTHROUGH; } git__free(wide_cmd); if (commmand_start_stdout_reading_thread(&commandHandle, to)) { command_close(&commandHandle); return -1; } if (command_write_gitbuf(&commandHandle, from)) { DWORD exitCode = command_close(&commandHandle); if (exitCode) setProcessError(exitCode, &errBuf); git_buf_free(&errBuf); if (isRequired) return -1; return GIT_PASSTHROUGH; } command_close_stdin(&commandHandle); if (command_wait_stdout_reading_thread(&commandHandle)) { DWORD exitCode = command_close(&commandHandle); if (exitCode) setProcessError(exitCode, &errBuf); git_buf_free(&errBuf); if (isRequired) return -1; return GIT_PASSTHROUGH; } exitCode = command_close(&commandHandle); if (exitCode) { if (isRequired) { setProcessError(exitCode, &errBuf); git_buf_free(&errBuf); return -1; } git_buf_free(&errBuf); return GIT_PASSTHROUGH; } git_buf_free(&errBuf); return 0; }
void test_core_buffer__join3(void) { git_buf a = GIT_BUF_INIT; cl_git_pass(git_buf_join3(&a, '/', "test", "string", "join")); cl_assert_equal_s("test/string/join", a.ptr); cl_git_pass(git_buf_join3(&a, '/', "test/", "string", "join")); cl_assert_equal_s("test/string/join", a.ptr); cl_git_pass(git_buf_join3(&a, '/', "test/", "/string", "join")); cl_assert_equal_s("test/string/join", a.ptr); cl_git_pass(git_buf_join3(&a, '/', "test/", "/string/", "join")); cl_assert_equal_s("test/string/join", a.ptr); cl_git_pass(git_buf_join3(&a, '/', "test/", "/string/", "/join")); cl_assert_equal_s("test/string/join", a.ptr); cl_git_pass(git_buf_join3(&a, '/', "", "string", "join")); cl_assert_equal_s("string/join", a.ptr); cl_git_pass(git_buf_join3(&a, '/', "", "string/", "join")); cl_assert_equal_s("string/join", a.ptr); cl_git_pass(git_buf_join3(&a, '/', "", "string/", "/join")); cl_assert_equal_s("string/join", a.ptr); cl_git_pass(git_buf_join3(&a, '/', "string", "", "join")); cl_assert_equal_s("string/join", a.ptr); cl_git_pass(git_buf_join3(&a, '/', "string/", "", "join")); cl_assert_equal_s("string/join", a.ptr); cl_git_pass(git_buf_join3(&a, '/', "string/", "", "/join")); cl_assert_equal_s("string/join", a.ptr); git_buf_free(&a); }