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_free(&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_free(&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_free(&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_free(&old_path); git_buf_free(&home_path); git_buf_free(&expected_path); git_config_free(cfg); }
int git_path_find_dir(git_buf *dir, const char *path, const char *base) { int error = git_path_join_unrooted(dir, path, base, NULL); if (!error) { char buf[GIT_PATH_MAX]; if (p_realpath(dir->ptr, buf) != NULL) error = git_buf_sets(dir, buf); } /* call dirname if this is not a directory */ if (!error) /* && git_path_isdir(dir->ptr) == false) */ error = (git_path_dirname_r(dir, dir->ptr) < 0) ? -1 : 0; if (!error) error = git_path_to_dir(dir); return error; }
static void check_tree_entry( git_iterator *i, const char *oid, const char *oid_p, const char *oid_pp, const char *oid_ppp) { const git_index_entry *ie; const git_tree_entry *te; const git_tree *tree; git_buf path = GIT_BUF_INIT; cl_git_pass(git_iterator_current_tree_entry(&te, i)); cl_assert(te); cl_assert(git_oid_streq(te->oid, oid) == 0); cl_git_pass(git_iterator_current(&ie, i)); cl_git_pass(git_buf_sets(&path, ie->path)); if (oid_p) { git_buf_rtruncate_at_char(&path, '/'); cl_git_pass(git_iterator_current_parent_tree(&tree, i, path.ptr)); cl_assert(tree); cl_assert(git_oid_streq(git_tree_id(tree), oid_p) == 0); } if (oid_pp) { git_buf_rtruncate_at_char(&path, '/'); cl_git_pass(git_iterator_current_parent_tree(&tree, i, path.ptr)); cl_assert(tree); cl_assert(git_oid_streq(git_tree_id(tree), oid_pp) == 0); } if (oid_ppp) { git_buf_rtruncate_at_char(&path, '/'); cl_git_pass(git_iterator_current_parent_tree(&tree, i, path.ptr)); cl_assert(tree); cl_assert(git_oid_streq(git_tree_id(tree), oid_ppp) == 0); } git_buf_free(&path); }
/* test detach/attach data */ void test_core_buffer__7(void) { const char *fun = "This is fun"; git_buf a = GIT_BUF_INIT; char *b = NULL; git_buf_sets(&a, "foo"); cl_assert(git_buf_oom(&a) == 0); cl_assert_equal_s("foo", git_buf_cstr(&a)); b = git_buf_detach(&a); cl_assert_equal_s("foo", b); cl_assert_equal_s("", a.ptr); git__free(b); b = git_buf_detach(&a); cl_assert_equal_s(NULL, b); cl_assert_equal_s("", a.ptr); git_buf_free(&a); b = git__strdup(fun); git_buf_attach(&a, b, 0); cl_assert_equal_s(fun, a.ptr); cl_assert(a.size == strlen(fun)); cl_assert(a.asize == strlen(fun) + 1); git_buf_free(&a); b = git__strdup(fun); git_buf_attach(&a, b, strlen(fun) + 1); cl_assert_equal_s(fun, a.ptr); cl_assert(a.size == strlen(fun)); cl_assert(a.asize == strlen(fun) + 1); git_buf_free(&a); }
int git_path_prettify(git_buf *path_out, const char *path, const char *base) { int error = GIT_SUCCESS; char buf[GIT_PATH_MAX]; git_buf_clear(path_out); /* construct path if needed */ if (base != NULL && git_path_root(path) < 0) { if ((error = git_buf_joinpath(path_out, base, path)) < GIT_SUCCESS) return error; path = path_out->ptr; } if (path == NULL || p_realpath(path, buf) == NULL) error = GIT_EOSERR; else error = git_buf_sets(path_out, buf); return error; }
int cl_git_remove_placeholders(const char *directory_path, const char *filename) { int error; remove_data data; git_buf buffer = GIT_BUF_INIT; if (git_path_isdir(directory_path) == false) return -1; if (git_buf_sets(&buffer, directory_path) < 0) return -1; data.filename = filename; data.filename_len = strlen(filename); error = remove_placeholders_recurs(&data, &buffer); git_buf_free(&buffer); return error; }
static void check_buf_append( const char* data_a, const char* data_b, const char* expected_data, size_t expected_size, size_t expected_asize) { git_buf tgt = GIT_BUF_INIT; git_buf_sets(&tgt, data_a); cl_assert(git_buf_oom(&tgt) == 0); git_buf_puts(&tgt, data_b); cl_assert(git_buf_oom(&tgt) == 0); cl_assert_equal_s(expected_data, git_buf_cstr(&tgt)); cl_assert(tgt.size == expected_size); if (expected_asize > 0) cl_assert(tgt.asize == expected_asize); git_buf_free(&tgt); }
static int reference_matches_remote_head( const char *reference_name, void *payload) { struct head_info *head_info = (struct head_info *)payload; git_oid oid; /* TODO: Should we guard against references * which name doesn't start with refs/heads/ ? */ /* Stop looking if we've already found a match */ if (git_buf_len(&head_info->branchname) > 0) return 0; if (git_reference_name_to_oid( &oid, head_info->repo, reference_name) < 0) { /* TODO: How to handle not found references? */ return -1; } if (git_oid_cmp(&head_info->remote_head_oid, &oid) == 0) { /* Determine the local reference name from the remote tracking one */ if (git_refspec_transform_l( &head_info->branchname, head_info->refspec, reference_name) < 0) return -1; if (git_buf_sets( &head_info->branchname, git_buf_cstr(&head_info->branchname) + strlen(GIT_REFS_HEADS_DIR)) < 0) return -1; } return 0; }
static void check_buf_append_abc( const char* buf_a, const char* buf_b, const char* buf_c, const char* expected_ab, const char* expected_abc, const char* expected_abca, const char* expected_abcab, const char* expected_abcabc) { git_buf buf = GIT_BUF_INIT; git_buf_sets(&buf, buf_a); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s(buf_a, git_buf_cstr(&buf)); git_buf_puts(&buf, buf_b); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s(expected_ab, git_buf_cstr(&buf)); git_buf_puts(&buf, buf_c); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s(expected_abc, git_buf_cstr(&buf)); git_buf_puts(&buf, buf_a); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s(expected_abca, git_buf_cstr(&buf)); git_buf_puts(&buf, buf_b); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s(expected_abcab, git_buf_cstr(&buf)); git_buf_puts(&buf, buf_c); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s(expected_abcabc, git_buf_cstr(&buf)); git_buf_free(&buf); }
/*********************************************************** * * PACKED BACKEND PUBLIC API * * Implement the git_odb_backend API calls * ***********************************************************/ static int pack_backend__refresh(git_odb_backend *backend_) { int error; struct stat st; git_buf path = GIT_BUF_INIT; struct pack_backend *backend = (struct pack_backend *)backend_; if (backend->pack_folder == NULL) return 0; if (p_stat(backend->pack_folder, &st) < 0 || !S_ISDIR(st.st_mode)) return git_odb__error_notfound("failed to refresh packfiles", NULL); git_buf_sets(&path, backend->pack_folder); /* reload all packs */ error = git_path_direach(&path, 0, packfile_load__cb, backend); git_buf_free(&path); git_vector_sort(&backend->packs); return error; }
void test_core_buffer__rfind_variants(void) { git_buf a = GIT_BUF_INIT; ssize_t len; cl_git_pass(git_buf_sets(&a, "/this/is/it/")); len = (ssize_t)git_buf_len(&a); cl_assert(git_buf_rfind(&a, '/') == len - 1); cl_assert(git_buf_rfind_next(&a, '/') == len - 4); cl_assert(git_buf_rfind(&a, 'i') == len - 3); cl_assert(git_buf_rfind_next(&a, 'i') == len - 3); cl_assert(git_buf_rfind(&a, 'h') == 2); cl_assert(git_buf_rfind_next(&a, 'h') == 2); cl_assert(git_buf_rfind(&a, 'q') == -1); cl_assert(git_buf_rfind_next(&a, 'q') == -1); git_buf_free(&a); }
void test_core_path__11_walkup(void) { git_buf p = GIT_BUF_INIT; char *expect[] = { "/a/b/c/d/e/", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL, "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL, "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL, "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL, "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", NULL, "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", NULL, "this is a path", NULL, "///a///b///c///d///e///", "///a///b///c///d///", "///a///b///c///", "///a///b///", "///a///", "///", NULL, NULL }; char *root[] = { NULL, NULL, "/", "", "/a/b", "/a/b/", NULL, NULL, NULL }; int i, j; check_walkup_info info; info.expect = expect; for (i = 0, j = 0; expect[i] != NULL; i++, j++) { git_buf_sets(&p, expect[i]); info.expect_idx = i; cl_git_pass( git_path_walk_up(&p, root[j], check_one_walkup_step, &info) ); cl_assert_equal_s(p.ptr, expect[i]); /* skip to next run of expectations */ while (expect[i] != NULL) i++; } git_buf_free(&p); }
int git_config_backend_from_string(git_config_backend **out, const char *cfg) { config_memory_backend *backend; backend = git__calloc(1, sizeof(config_memory_backend)); GITERR_CHECK_ALLOC(backend); if (git_config_entries_new(&backend->entries) < 0) { git__free(backend); return -1; } if (git_buf_sets(&backend->cfg, cfg) < 0) { git_config_entries_free(backend->entries); git__free(backend); return -1; } backend->parent.version = GIT_CONFIG_BACKEND_VERSION; backend->parent.readonly = 1; backend->parent.open = config_memory_open; backend->parent.get = config_memory_get; backend->parent.set = config_memory_set; backend->parent.set_multivar = config_memory_set_multivar; backend->parent.del = config_memory_delete; backend->parent.del_multivar = config_memory_delete_multivar; backend->parent.iterator = config_memory_iterator; backend->parent.lock = config_memory_lock; backend->parent.unlock = config_memory_unlock; backend->parent.snapshot = config_memory_snapshot; backend->parent.free = config_memory_free; *out = (git_config_backend *)backend; return 0; }
int git_path_join_unrooted( git_buf *path_out, const char *path, const char *base, ssize_t *root_at) { int error, root; assert(path && path_out); root = git_path_root(path); if (base != NULL && root < 0) { error = git_buf_joinpath(path_out, base, path); if (root_at) *root_at = (ssize_t)strlen(base); } else { error = git_buf_sets(path_out, path); if (root_at) *root_at = (root < 0) ? 0 : (ssize_t)root; } return error; }
int git_futils_dirs_set(git_futils_dir_t which, const char *search_path) { const char *expand_path = NULL; git_buf merge = GIT_BUF_INIT; GITERR_CHECK_ERROR(git_futils_check_selector(which)); if (search_path != NULL) expand_path = strstr(search_path, PATH_MAGIC); /* init with default if not yet done and needed (ignoring error) */ if ((!search_path || expand_path) && !git_buf_len(&git_futils__dirs[which])) git_futils__dir_guess[which](&git_futils__dirs[which]); /* if $PATH is not referenced, then just set the path */ if (!expand_path) return git_buf_sets(&git_futils__dirs[which], search_path); /* otherwise set to join(before $PATH, old value, after $PATH) */ if (expand_path > search_path) git_buf_set(&merge, search_path, expand_path - search_path); if (git_buf_len(&git_futils__dirs[which])) git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR, merge.ptr, git_futils__dirs[which].ptr); expand_path += strlen(PATH_MAGIC); if (*expand_path) git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR, merge.ptr, expand_path); git_buf_swap(&git_futils__dirs[which], &merge); git_buf_free(&merge); return git_buf_oom(&git_futils__dirs[which]) ? -1 : 0; }
int git_reset( git_repository *repo, git_object *target, git_reset_t reset_type, git_checkout_options *checkout_opts, git_signature *signature, const char *log_message) { git_object *commit = NULL; git_index *index = NULL; git_tree *tree = NULL; int error = 0; git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; git_buf log_message_buf = GIT_BUF_INIT; assert(repo && target); if (checkout_opts) opts = *checkout_opts; if (git_object_owner(target) != repo) { giterr_set(GITERR_OBJECT, "%s - The given target does not belong to this repository.", ERROR_MSG); return -1; } if (reset_type != GIT_RESET_SOFT && (error = git_repository__ensure_not_bare(repo, reset_type == GIT_RESET_MIXED ? "reset mixed" : "reset hard")) < 0) return error; if ((error = git_object_peel(&commit, target, GIT_OBJ_COMMIT)) < 0 || (error = git_repository_index(&index, repo)) < 0 || (error = git_commit_tree(&tree, (git_commit *)commit)) < 0) goto cleanup; if (reset_type == GIT_RESET_SOFT && (git_repository_state(repo) == GIT_REPOSITORY_STATE_MERGE || git_index_has_conflicts(index))) { giterr_set(GITERR_OBJECT, "%s (soft) in the middle of a merge.", ERROR_MSG); error = GIT_EUNMERGED; goto cleanup; } if (log_message) git_buf_sets(&log_message_buf, log_message); else git_buf_sets(&log_message_buf, "reset: moving"); /* move HEAD to the new target */ if ((error = git_reference__update_terminal(repo, GIT_HEAD_FILE, git_object_id(commit), signature, git_buf_cstr(&log_message_buf))) < 0) goto cleanup; if (reset_type == GIT_RESET_HARD) { /* overwrite working directory with HEAD */ opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_SKIP_UNMERGED; if ((error = git_checkout_tree(repo, (git_object *)tree, &opts)) < 0) goto cleanup; } if (reset_type > GIT_RESET_SOFT) { /* reset index to the target content */ if ((error = git_index_read_tree(index, tree)) < 0 || (error = git_index_write(index)) < 0) goto cleanup; if ((error = git_repository_state_cleanup(repo)) < 0) { giterr_set(GITERR_INDEX, "%s - failed to clean up merge data", ERROR_MSG); goto cleanup; } } cleanup: git_object_free(commit); git_index_free(index); git_tree_free(tree); git_buf_free(&log_message_buf); return error; }
void test_core_path__11_walkup(void) { git_buf p = GIT_BUF_INIT; char *expect[] = { /* 1 */ "/a/b/c/d/e/", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL, /* 2 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL, /* 3 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL, /* 4 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL, /* 5 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", NULL, /* 6 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", NULL, /* 7 */ "this_is_a_path", "", NULL, /* 8 */ "this_is_a_path/", "", NULL, /* 9 */ "///a///b///c///d///e///", "///a///b///c///d///", "///a///b///c///", "///a///b///", "///a///", "///", NULL, /* 10 */ "a/b/c/", "a/b/", "a/", "", NULL, /* 11 */ "a/b/c", "a/b/", "a/", "", NULL, /* 12 */ "a/b/c/", "a/b/", "a/", NULL, /* 13 */ "", NULL, /* 14 */ "/", NULL, /* 15 */ NULL }; char *root[] = { /* 1 */ NULL, /* 2 */ NULL, /* 3 */ "/", /* 4 */ "", /* 5 */ "/a/b", /* 6 */ "/a/b/", /* 7 */ NULL, /* 8 */ NULL, /* 9 */ NULL, /* 10 */ NULL, /* 11 */ NULL, /* 12 */ "a/", /* 13 */ NULL, /* 14 */ NULL, }; int i, j; check_walkup_info info; info.expect = expect; info.cancel_after = -1; for (i = 0, j = 0; expect[i] != NULL; i++, j++) { git_buf_sets(&p, expect[i]); info.expect_idx = i; cl_git_pass( git_path_walk_up(&p, root[j], check_one_walkup_step, &info) ); cl_assert_equal_s(p.ptr, expect[i]); cl_assert(expect[info.expect_idx] == NULL); i = info.expect_idx; } git_buf_dispose(&p); }
void test_core_path__15_resolve_relative(void) { git_buf buf = GIT_BUF_INIT; assert_resolve_relative(&buf, "", ""); assert_resolve_relative(&buf, "", "."); assert_resolve_relative(&buf, "", "./"); assert_resolve_relative(&buf, "..", ".."); assert_resolve_relative(&buf, "../", "../"); assert_resolve_relative(&buf, "..", "./.."); assert_resolve_relative(&buf, "../", "./../"); assert_resolve_relative(&buf, "../", "../."); assert_resolve_relative(&buf, "../", ".././"); assert_resolve_relative(&buf, "../..", "../.."); assert_resolve_relative(&buf, "../../", "../../"); assert_resolve_relative(&buf, "/", "/"); assert_resolve_relative(&buf, "/", "/."); assert_resolve_relative(&buf, "", "a/.."); assert_resolve_relative(&buf, "", "a/../"); assert_resolve_relative(&buf, "", "a/../."); assert_resolve_relative(&buf, "/a", "/a"); assert_resolve_relative(&buf, "/a/", "/a/."); assert_resolve_relative(&buf, "/", "/a/../"); assert_resolve_relative(&buf, "/", "/a/../."); assert_resolve_relative(&buf, "/", "/a/.././"); assert_resolve_relative(&buf, "a", "a"); assert_resolve_relative(&buf, "a/", "a/"); assert_resolve_relative(&buf, "a/", "a/."); assert_resolve_relative(&buf, "a/", "a/./"); assert_resolve_relative(&buf, "a/b", "a//b"); assert_resolve_relative(&buf, "a/b/c", "a/b/c"); assert_resolve_relative(&buf, "b/c", "./b/c"); assert_resolve_relative(&buf, "a/c", "a/./c"); assert_resolve_relative(&buf, "a/b/", "a/b/."); assert_resolve_relative(&buf, "/a/b/c", "///a/b/c"); assert_resolve_relative(&buf, "/", "////"); assert_resolve_relative(&buf, "/a", "///a"); assert_resolve_relative(&buf, "/", "///."); assert_resolve_relative(&buf, "/", "///a/.."); assert_resolve_relative(&buf, "../../path", "../../test//../././path"); assert_resolve_relative(&buf, "../d", "a/b/../../../c/../d"); cl_git_pass(git_buf_sets(&buf, "/..")); cl_git_fail(git_path_resolve_relative(&buf, 0)); cl_git_pass(git_buf_sets(&buf, "/./..")); cl_git_fail(git_path_resolve_relative(&buf, 0)); cl_git_pass(git_buf_sets(&buf, "/.//..")); cl_git_fail(git_path_resolve_relative(&buf, 0)); cl_git_pass(git_buf_sets(&buf, "/../.")); cl_git_fail(git_path_resolve_relative(&buf, 0)); cl_git_pass(git_buf_sets(&buf, "/../.././../a")); cl_git_fail(git_path_resolve_relative(&buf, 0)); cl_git_pass(git_buf_sets(&buf, "////..")); cl_git_fail(git_path_resolve_relative(&buf, 0)); /* things that start with Windows network paths */ #ifdef GIT_WIN32 assert_resolve_relative(&buf, "//a/b/c", "//a/b/c"); assert_resolve_relative(&buf, "//a/", "//a/b/.."); assert_resolve_relative(&buf, "//a/b/c", "//a/Q/../b/x/y/../../c"); cl_git_pass(git_buf_sets(&buf, "//a/b/../..")); cl_git_fail(git_path_resolve_relative(&buf, 0)); #else assert_resolve_relative(&buf, "/a/b/c", "//a/b/c"); assert_resolve_relative(&buf, "/a/", "//a/b/.."); assert_resolve_relative(&buf, "/a/b/c", "//a/Q/../b/x/y/../../c"); assert_resolve_relative(&buf, "/", "//a/b/../.."); #endif git_buf_dispose(&buf); }
/* Inspired from https://github.com/git/git/blob/f06d47e7e0d9db709ee204ed13a8a7486149f494/refs.c#L36-100 */ int git_reference__normalize_name( git_buf *buf, const char *name, unsigned int flags) { const char *current; int segment_len, segments_count = 0, error = GIT_EINVALIDSPEC; unsigned int process_flags; bool normalize = (buf != NULL); bool validate = (flags & GIT_REF_FORMAT__VALIDATION_DISABLE) == 0; #ifdef GIT_USE_ICONV git_path_iconv_t ic = GIT_PATH_ICONV_INIT; #endif assert(name); process_flags = flags; current = (char *)name; if (validate && *current == '/') goto cleanup; if (normalize) git_buf_clear(buf); #ifdef GIT_USE_ICONV if ((flags & GIT_REF_FORMAT__PRECOMPOSE_UNICODE) != 0) { size_t namelen = strlen(current); if ((error = git_path_iconv_init_precompose(&ic)) < 0 || (error = git_path_iconv(&ic, ¤t, &namelen)) < 0) goto cleanup; error = GIT_EINVALIDSPEC; } #endif if (!validate) { git_buf_sets(buf, current); error = git_buf_oom(buf) ? -1 : 0; goto cleanup; } while (true) { segment_len = ensure_segment_validity(current); if (segment_len < 0) { if ((process_flags & GIT_REF_FORMAT_REFSPEC_PATTERN) && current[0] == '*' && (current[1] == '\0' || current[1] == '/')) { /* Accept one wildcard as a full refname component. */ process_flags &= ~GIT_REF_FORMAT_REFSPEC_PATTERN; segment_len = 1; } else goto cleanup; } if (segment_len > 0) { if (normalize) { size_t cur_len = git_buf_len(buf); git_buf_joinpath(buf, git_buf_cstr(buf), current); git_buf_truncate(buf, cur_len + segment_len + (segments_count ? 1 : 0)); if (git_buf_oom(buf)) { error = -1; goto cleanup; } } segments_count++; } /* No empty segment is allowed when not normalizing */ if (segment_len == 0 && !normalize) goto cleanup; if (current[segment_len] == '\0') break; current += segment_len + 1; } /* A refname can not be empty */ if (segment_len == 0 && segments_count == 0) goto cleanup; /* A refname can not end with "." */ if (current[segment_len - 1] == '.') goto cleanup; /* A refname can not end with "/" */ if (current[segment_len - 1] == '/') goto cleanup; if ((segments_count == 1 ) && !(flags & GIT_REF_FORMAT_ALLOW_ONELEVEL)) goto cleanup; if ((segments_count == 1 ) && !(flags & GIT_REF_FORMAT_REFSPEC_SHORTHAND) && !(is_all_caps_and_underscore(name, (size_t)segment_len) || ((flags & GIT_REF_FORMAT_REFSPEC_PATTERN) && !strcmp("*", name)))) goto cleanup; if ((segments_count > 1) && (is_all_caps_and_underscore(name, strchr(name, '/') - name))) goto cleanup; error = 0; cleanup: if (error == GIT_EINVALIDSPEC) giterr_set( GITERR_REFERENCE, "the given reference name '%s' is not valid", name); if (error && normalize) git_buf_free(buf); #ifdef GIT_USE_ICONV git_path_iconv_clear(&ic); #endif return error; }
/* more thorough test of concatenation options */ void test_core_buffer__2(void) { git_buf buf = GIT_BUF_INIT; int i; char data[128]; cl_assert(buf.size == 0); /* this must be safe to do */ git_buf_free(&buf); cl_assert(buf.size == 0); cl_assert(buf.asize == 0); /* empty buffer should be empty string */ cl_assert_equal_s("", git_buf_cstr(&buf)); cl_assert(buf.size == 0); /* cl_assert(buf.asize == 0); -- should not assume what git_buf does */ /* free should set us back to the beginning */ git_buf_free(&buf); cl_assert(buf.size == 0); cl_assert(buf.asize == 0); /* add letter */ git_buf_putc(&buf, '+'); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s("+", git_buf_cstr(&buf)); /* add letter again */ git_buf_putc(&buf, '+'); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s("++", git_buf_cstr(&buf)); /* let's try that a few times */ for (i = 0; i < 16; ++i) { git_buf_putc(&buf, '+'); cl_assert(git_buf_oom(&buf) == 0); } cl_assert_equal_s("++++++++++++++++++", git_buf_cstr(&buf)); git_buf_free(&buf); /* add data */ git_buf_put(&buf, "xo", 2); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s("xo", git_buf_cstr(&buf)); /* add letter again */ git_buf_put(&buf, "xo", 2); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s("xoxo", git_buf_cstr(&buf)); /* let's try that a few times */ for (i = 0; i < 16; ++i) { git_buf_put(&buf, "xo", 2); cl_assert(git_buf_oom(&buf) == 0); } cl_assert_equal_s("xoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxo", git_buf_cstr(&buf)); git_buf_free(&buf); /* set to string */ git_buf_sets(&buf, test_string); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s(test_string, git_buf_cstr(&buf)); /* append string */ git_buf_puts(&buf, test_string); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s(test_string_x2, git_buf_cstr(&buf)); /* set to string again (should overwrite - not append) */ git_buf_sets(&buf, test_string); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s(test_string, git_buf_cstr(&buf)); /* test clear */ git_buf_clear(&buf); cl_assert_equal_s("", git_buf_cstr(&buf)); git_buf_free(&buf); /* test extracting data into buffer */ git_buf_puts(&buf, REP4("0123456789")); cl_assert(git_buf_oom(&buf) == 0); git_buf_copy_cstr(data, sizeof(data), &buf); cl_assert_equal_s(REP4("0123456789"), data); git_buf_copy_cstr(data, 11, &buf); cl_assert_equal_s("0123456789", data); git_buf_copy_cstr(data, 3, &buf); cl_assert_equal_s("01", data); git_buf_copy_cstr(data, 1, &buf); cl_assert_equal_s("", data); git_buf_copy_cstr(data, sizeof(data), &buf); cl_assert_equal_s(REP4("0123456789"), data); git_buf_sets(&buf, REP256("x")); git_buf_copy_cstr(data, sizeof(data), &buf); /* since sizeof(data) == 128, only 127 bytes should be copied */ cl_assert_equal_s(REP4(REP16("x")) REP16("x") REP16("x") REP16("x") "xxxxxxxxxxxxxxx", data); git_buf_free(&buf); git_buf_copy_cstr(data, sizeof(data), &buf); cl_assert_equal_s("", data); }
void test_core_buffer__lf_and_crlf_conversions(void) { git_buf src = GIT_BUF_INIT, tgt = GIT_BUF_INIT; /* LF source */ git_buf_sets(&src, "lf\nlf\nlf\nlf\n"); cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src)); check_buf("lf\r\nlf\r\nlf\r\nlf\r\n", tgt); cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src)); check_buf(src.ptr, tgt); git_buf_sets(&src, "\nlf\nlf\nlf\nlf\nlf"); cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src)); check_buf("\r\nlf\r\nlf\r\nlf\r\nlf\r\nlf", tgt); cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src)); check_buf(src.ptr, tgt); /* CRLF source */ git_buf_sets(&src, "crlf\r\ncrlf\r\ncrlf\r\ncrlf\r\n"); cl_git_fail_with(GIT_PASSTHROUGH, git_buf_text_lf_to_crlf(&tgt, &src)); cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src)); check_buf("crlf\ncrlf\ncrlf\ncrlf\n", tgt); git_buf_sets(&src, "\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf"); cl_git_fail_with(GIT_PASSTHROUGH, git_buf_text_lf_to_crlf(&tgt, &src)); cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src)); check_buf("\ncrlf\ncrlf\ncrlf\ncrlf\ncrlf", tgt); /* CRLF in LF text */ git_buf_sets(&src, "\nlf\nlf\ncrlf\r\nlf\nlf\ncrlf\r\n"); cl_git_fail_with(GIT_PASSTHROUGH, git_buf_text_lf_to_crlf(&tgt, &src)); cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src)); check_buf("\nlf\nlf\ncrlf\nlf\nlf\ncrlf\n", tgt); /* LF in CRLF text */ git_buf_sets(&src, "\ncrlf\r\ncrlf\r\nlf\ncrlf\r\ncrlf"); cl_git_fail_with(GIT_PASSTHROUGH, git_buf_text_lf_to_crlf(&tgt, &src)); cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src)); check_buf("\ncrlf\ncrlf\nlf\ncrlf\ncrlf", tgt); /* bare CR test */ git_buf_sets(&src, "\rcrlf\r\nlf\nlf\ncr\rcrlf\r\nlf\ncr\r"); cl_git_fail_with(GIT_PASSTHROUGH, git_buf_text_lf_to_crlf(&tgt, &src)); cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src)); check_buf("\rcrlf\nlf\nlf\ncr\rcrlf\nlf\ncr\r", tgt); git_buf_sets(&src, "\rcr\r"); cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src)); check_buf(src.ptr, tgt); cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src)); check_buf("\rcr\r", tgt); git_buf_free(&src); git_buf_free(&tgt); /* blob correspondence tests */ git_buf_sets(&src, ALL_CRLF_TEXT_RAW); cl_git_fail_with(GIT_PASSTHROUGH, git_buf_text_lf_to_crlf(&tgt, &src)); cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src)); check_buf(ALL_CRLF_TEXT_AS_LF, tgt); git_buf_free(&src); git_buf_free(&tgt); git_buf_sets(&src, ALL_LF_TEXT_RAW); cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src)); check_buf(ALL_LF_TEXT_AS_CRLF, tgt); cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src)); check_buf(ALL_LF_TEXT_AS_LF, tgt); git_buf_free(&src); git_buf_free(&tgt); git_buf_sets(&src, MORE_CRLF_TEXT_RAW); cl_git_fail_with(GIT_PASSTHROUGH, git_buf_text_lf_to_crlf(&tgt, &src)); cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src)); check_buf(MORE_CRLF_TEXT_AS_LF, tgt); git_buf_free(&src); git_buf_free(&tgt); git_buf_sets(&src, MORE_LF_TEXT_RAW); cl_git_fail_with(GIT_PASSTHROUGH, git_buf_text_lf_to_crlf(&tgt, &src)); cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src)); check_buf(MORE_LF_TEXT_AS_LF, tgt); git_buf_free(&src); git_buf_free(&tgt); }
void test_core_buffer__similarity_metric(void) { git_hashsig *a, *b; git_buf buf = GIT_BUF_INIT; int sim; /* in the first case, we compare data to itself and expect 100% match */ cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1)); cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); cl_assert_equal_i(100, git_hashsig_compare(a, b)); git_hashsig_free(a); git_hashsig_free(b); /* if we change just a single byte, how much does that change magnify? */ cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1)); cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); cl_git_pass(git_buf_sets(&buf, "000\n001\n002\n003\n004\n005\n006\n007\n008\n009\n" \ "010\n011\n012\n013\n014\n015\n016\n017\n018\n019\n" \ "x020x\n021\n022\n023\n024\n025\n026\n027\n028\n029\n" \ "030\n031\n032\n033\n034\n035\n036\n037\n038\n039\n" \ "040\n041\n042\n043\n044\n045\n046\n047\n048\n049\n" )); cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); sim = git_hashsig_compare(a, b); cl_assert_in_range(95, sim, 100); /* expect >95% similarity */ git_hashsig_free(a); git_hashsig_free(b); /* let's try comparing data to a superset of itself */ cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1)); cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1 "050\n051\n052\n053\n054\n055\n056\n057\n058\n059\n")); cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); sim = git_hashsig_compare(a, b); /* 20% lines added ~= 10% lines changed */ cl_assert_in_range(85, sim, 95); /* expect similarity around 90% */ git_hashsig_free(a); git_hashsig_free(b); /* what if we keep about half the original data and add half new */ cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1)); cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); cl_git_pass(git_buf_sets(&buf, "000\n001\n002\n003\n004\n005\n006\n007\n008\n009\n" \ "010\n011\n012\n013\n014\n015\n016\n017\n018\n019\n" \ "020x\n021\n022\n023\n024\n" \ "x25\nx26\nx27\nx28\nx29\n" \ "x30\nx31\nx32\nx33\nx34\nx35\nx36\nx37\nx38\nx39\n" \ "x40\nx41\nx42\nx43\nx44\nx45\nx46\nx47\nx48\nx49\n" )); cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); sim = git_hashsig_compare(a, b); /* 50% lines changed */ cl_assert_in_range(40, sim, 60); /* expect in the 40-60% similarity range */ git_hashsig_free(a); git_hashsig_free(b); /* lastly, let's check that we can hash file content as well */ cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1)); cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); cl_git_pass(git_futils_mkdir("scratch", NULL, 0755, GIT_MKDIR_PATH)); cl_git_mkfile("scratch/testdata", SIMILARITY_TEST_DATA_1); cl_git_pass(git_hashsig_create_fromfile( &b, "scratch/testdata", GIT_HASHSIG_NORMAL)); cl_assert_equal_i(100, git_hashsig_compare(a, b)); git_hashsig_free(a); git_hashsig_free(b); git_buf_free(&buf); git_futils_rmdir_r("scratch", NULL, GIT_RMDIR_REMOVE_FILES); }
int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats) { git_mwindow *w = NULL; unsigned int i, long_offsets = 0, left; int error; struct git_pack_idx_header hdr; git_buf filename = GIT_BUF_INIT; struct entry *entry; git_oid trailer_hash, file_hash; git_hash_ctx ctx; git_filebuf index_file = {0}; void *packfile_trailer; if (git_hash_ctx_init(&ctx) < 0) return -1; /* Test for this before resolve_deltas(), as it plays with idx->off */ if (idx->off < idx->pack->mwf.size - 20) { giterr_set(GITERR_INDEXER, "Unexpected data at the end of the pack"); return -1; } packfile_trailer = git_mwindow_open(&idx->pack->mwf, &w, idx->pack->mwf.size - GIT_OID_RAWSZ, GIT_OID_RAWSZ, &left); if (packfile_trailer == NULL) { git_mwindow_close(&w); goto on_error; } /* Compare the packfile trailer as it was sent to us and what we calculated */ git_oid_fromraw(&file_hash, (unsigned char*) packfile_trailer); git_mwindow_close(&w); git_hash_final(&trailer_hash, &idx->trailer); if (git_oid_cmp(&file_hash, &trailer_hash)) { giterr_set(GITERR_INDEXER, "packfile trailer mismatch"); return -1; } /* Freeze the number of deltas */ stats->total_deltas = stats->total_objects - stats->indexed_objects; if ((error = resolve_deltas(idx, stats)) < 0) return error; if (stats->indexed_objects != stats->total_objects) { giterr_set(GITERR_INDEXER, "early EOF"); return -1; } if (stats->local_objects > 0) { if (update_header_and_rehash(idx, stats) < 0) return -1; git_hash_final(&trailer_hash, &idx->trailer); write_at(idx, &trailer_hash, idx->pack->mwf.size - GIT_OID_RAWSZ, GIT_OID_RAWSZ); } git_vector_sort(&idx->objects); git_buf_sets(&filename, idx->pack->pack_name); git_buf_shorten(&filename, strlen("pack")); git_buf_puts(&filename, "idx"); if (git_buf_oom(&filename)) return -1; if (git_filebuf_open(&index_file, filename.ptr, GIT_FILEBUF_HASH_CONTENTS, idx->mode) < 0) goto on_error; /* Write out the header */ hdr.idx_signature = htonl(PACK_IDX_SIGNATURE); hdr.idx_version = htonl(2); git_filebuf_write(&index_file, &hdr, sizeof(hdr)); /* Write out the fanout table */ for (i = 0; i < 256; ++i) { uint32_t n = htonl(idx->fanout[i]); git_filebuf_write(&index_file, &n, sizeof(n)); } /* Write out the object names (SHA-1 hashes) */ git_vector_foreach(&idx->objects, i, entry, struct entry*) { git_filebuf_write(&index_file, &entry->oid, sizeof(git_oid)); git_hash_update(&ctx, &entry->oid, GIT_OID_RAWSZ); } git_hash_final(&idx->hash, &ctx); /* Write out the CRC32 values */ git_vector_foreach(&idx->objects, i, entry, struct entry*) { git_filebuf_write(&index_file, &entry->crc, sizeof(uint32_t)); } /* Write out the offsets */ git_vector_foreach(&idx->objects, i, entry, struct entry*) { uint32_t n; if (entry->offset == UINT32_MAX) n = htonl(0x80000000 | long_offsets++); else n = htonl(entry->offset); git_filebuf_write(&index_file, &n, sizeof(uint32_t)); } /* Write out the long offsets */ git_vector_foreach(&idx->objects, i, entry, struct entry*) { uint32_t split[2]; if (entry->offset != UINT32_MAX) continue; split[0] = htonl(entry->offset_long >> 32); split[1] = htonl(entry->offset_long & 0xffffffff); git_filebuf_write(&index_file, &split, sizeof(uint32_t) * 2); } /* Write out the packfile trailer to the index */ if (git_filebuf_write(&index_file, &trailer_hash, GIT_OID_RAWSZ) < 0) goto on_error; /* Write out the hash of the idx */ if (git_filebuf_hash(&trailer_hash, &index_file) < 0) goto on_error; git_filebuf_write(&index_file, &trailer_hash, sizeof(git_oid)); /* Figure out what the final name should be */ if (index_path(&filename, idx, ".idx") < 0) goto on_error; /* Commit file */ if (git_filebuf_commit_at(&index_file, filename.ptr) < 0) goto on_error; git_mwindow_free_all(&idx->pack->mwf); /* We need to close the descriptor here so Windows doesn't choke on commit_at */ if (p_close(idx->pack->mwf.fd) < 0) { giterr_set(GITERR_OS, "failed to close packfile"); goto on_error; } idx->pack->mwf.fd = -1; if (index_path(&filename, idx, ".pack") < 0) goto on_error; /* And don't forget to rename the packfile to its new place. */ p_rename(idx->pack->pack_name, git_buf_cstr(&filename)); git_buf_free(&filename); git_hash_ctx_cleanup(&ctx); return 0; on_error: git_mwindow_free_all(&idx->pack->mwf); git_filebuf_cleanup(&index_file); git_buf_free(&filename); git_hash_ctx_cleanup(&ctx); return -1; }
void test_core_buffer__similarity_metric_whitespace(void) { git_hashsig *a, *b; git_buf buf = GIT_BUF_INIT; int sim, i, j; git_hashsig_option_t opt; const char *tabbed = " for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\n" " separator = sep[s];\n" " expect = expect_values[s];\n" "\n" " for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\n" " for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\n" " git_buf_join(&buf, separator, a[i], b[j]);\n" " cl_assert_equal_s(*expect, buf.ptr);\n" " expect++;\n" " }\n" " }\n" " }\n"; const char *spaced = " for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\n" " separator = sep[s];\n" " expect = expect_values[s];\n" "\n" " for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\n" " for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\n" " git_buf_join(&buf, separator, a[i], b[j]);\n" " cl_assert_equal_s(*expect, buf.ptr);\n" " expect++;\n" " }\n" " }\n" " }\n"; const char *crlf_spaced2 = " for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\r\n" " separator = sep[s];\r\n" " expect = expect_values[s];\r\n" "\r\n" " for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\r\n" " for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\r\n" " git_buf_join(&buf, separator, a[i], b[j]);\r\n" " cl_assert_equal_s(*expect, buf.ptr);\r\n" " expect++;\r\n" " }\r\n" " }\r\n" " }\r\n"; const char *text[3] = { tabbed, spaced, crlf_spaced2 }; /* let's try variations of our own code with whitespace changes */ for (opt = GIT_HASHSIG_NORMAL; opt <= GIT_HASHSIG_SMART_WHITESPACE; ++opt) { for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) { cl_git_pass(git_buf_sets(&buf, text[i])); cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, opt)); cl_git_pass(git_buf_sets(&buf, text[j])); cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, opt)); sim = git_hashsig_compare(a, b); if (opt == GIT_HASHSIG_NORMAL) { if (i == j) cl_assert_equal_i(100, sim); else cl_assert_in_range(0, sim, 30); /* pretty different */ } else { cl_assert_equal_i(100, sim); } git_hashsig_free(a); git_hashsig_free(b); } } } git_buf_free(&buf); }