/* what is the common non-wildcard prefix for all items in the pathspec */ char *git_pathspec_prefix(const git_strarray *pathspec) { git_buf prefix = GIT_BUF_INIT; const char *scan; if (!pathspec || !pathspec->count || git_buf_text_common_prefix(&prefix, pathspec) < 0) return NULL; /* diff prefix will only be leading non-wildcards */ for (scan = prefix.ptr; *scan; ++scan) { if (git__iswildcard(*scan) && (scan == prefix.ptr || (*(scan - 1) != '\\'))) break; } git_buf_truncate(&prefix, scan - prefix.ptr); if (prefix.size <= 0) { git_buf_free(&prefix); return NULL; } git_buf_text_unescape(&prefix); return git_buf_detach(&prefix); }
void test_core_buffer__11(void) { git_buf a = GIT_BUF_INIT; git_strarray t; char *t1[] = { "nothing", "in", "common" }; char *t2[] = { "something", "something else", "some other" }; char *t3[] = { "something", "some fun", "no fun" }; char *t4[] = { "happy", "happier", "happiest" }; char *t5[] = { "happiest", "happier", "happy" }; char *t6[] = { "no", "nope", "" }; char *t7[] = { "", "doesn't matter" }; t.strings = t1; t.count = 3; cl_git_pass(git_buf_text_common_prefix(&a, &t)); cl_assert_equal_s(a.ptr, ""); t.strings = t2; t.count = 3; cl_git_pass(git_buf_text_common_prefix(&a, &t)); cl_assert_equal_s(a.ptr, "some"); t.strings = t3; t.count = 3; cl_git_pass(git_buf_text_common_prefix(&a, &t)); cl_assert_equal_s(a.ptr, ""); t.strings = t4; t.count = 3; cl_git_pass(git_buf_text_common_prefix(&a, &t)); cl_assert_equal_s(a.ptr, "happ"); t.strings = t5; t.count = 3; cl_git_pass(git_buf_text_common_prefix(&a, &t)); cl_assert_equal_s(a.ptr, "happ"); t.strings = t6; t.count = 3; cl_git_pass(git_buf_text_common_prefix(&a, &t)); cl_assert_equal_s(a.ptr, ""); t.strings = t7; t.count = 3; cl_git_pass(git_buf_text_common_prefix(&a, &t)); cl_assert_equal_s(a.ptr, ""); git_buf_free(&a); }