Skip to content

Commit a6f6a9f

Browse files
authored
Merge pull request libgit2#6077 from libgit2/ethomson/strarray
buf: common_prefix takes a string array
2 parents 1369010 + 7e7cfe8 commit a6f6a9f

File tree

4 files changed

+16
-30
lines changed

4 files changed

+16
-30
lines changed

src/buffer.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -1201,25 +1201,26 @@ int git_buf_lf_to_crlf(git_buf *tgt, const git_buf *src)
12011201
return git_buf_put(tgt, scan, end - scan);
12021202
}
12031203

1204-
int git_buf_common_prefix(git_buf *buf, const git_strarray *strings)
1204+
int git_buf_common_prefix(git_buf *buf, char *const *const strings, size_t count)
12051205
{
12061206
size_t i;
12071207
const char *str, *pfx;
12081208

12091209
git_buf_clear(buf);
12101210

1211-
if (!strings || !strings->count)
1211+
if (!strings || !count)
12121212
return 0;
12131213

12141214
/* initialize common prefix to first string */
1215-
if (git_buf_sets(buf, strings->strings[0]) < 0)
1215+
if (git_buf_sets(buf, strings[0]) < 0)
12161216
return -1;
12171217

12181218
/* go through the rest of the strings, truncating to shared prefix */
1219-
for (i = 1; i < strings->count; ++i) {
1219+
for (i = 1; i < count; ++i) {
12201220

1221-
for (str = strings->strings[i], pfx = buf->ptr;
1222-
*str && *str == *pfx; str++, pfx++)
1221+
for (str = strings[i], pfx = buf->ptr;
1222+
*str && *str == *pfx;
1223+
str++, pfx++)
12231224
/* scanning */;
12241225

12251226
git_buf_truncate(buf, pfx - buf->ptr);

src/buffer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ extern int git_buf_lf_to_crlf(git_buf *tgt, const git_buf *src);
324324
*
325325
* Buffer will be set to empty if there is no common prefix
326326
*/
327-
extern int git_buf_common_prefix(git_buf *buf, const git_strarray *strs);
327+
extern int git_buf_common_prefix(git_buf *buf, char *const *const strings, size_t count);
328328

329329
/**
330330
* Check if a buffer begins with a UTF BOM

src/pathspec.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ char *git_pathspec_prefix(const git_strarray *pathspec)
2424
const char *scan;
2525

2626
if (!pathspec || !pathspec->count ||
27-
git_buf_common_prefix(&prefix, pathspec) < 0)
27+
git_buf_common_prefix(&prefix, pathspec->strings, pathspec->count) < 0)
2828
return NULL;
2929

3030
/* diff prefix will only be leading non-wildcards */

tests/core/buffer.c

+7-22
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,6 @@ void test_core_buffer__join3(void)
632632
void test_core_buffer__11(void)
633633
{
634634
git_buf a = GIT_BUF_INIT;
635-
git_strarray t;
636635
char *t1[] = { "nothing", "in", "common" };
637636
char *t2[] = { "something", "something else", "some other" };
638637
char *t3[] = { "something", "some fun", "no fun" };
@@ -641,39 +640,25 @@ void test_core_buffer__11(void)
641640
char *t6[] = { "no", "nope", "" };
642641
char *t7[] = { "", "doesn't matter" };
643642

644-
t.strings = t1;
645-
t.count = 3;
646-
cl_git_pass(git_buf_common_prefix(&a, &t));
643+
cl_git_pass(git_buf_common_prefix(&a, t1, 3));
647644
cl_assert_equal_s(a.ptr, "");
648645

649-
t.strings = t2;
650-
t.count = 3;
651-
cl_git_pass(git_buf_common_prefix(&a, &t));
646+
cl_git_pass(git_buf_common_prefix(&a, t2, 3));
652647
cl_assert_equal_s(a.ptr, "some");
653648

654-
t.strings = t3;
655-
t.count = 3;
656-
cl_git_pass(git_buf_common_prefix(&a, &t));
649+
cl_git_pass(git_buf_common_prefix(&a, t3, 3));
657650
cl_assert_equal_s(a.ptr, "");
658651

659-
t.strings = t4;
660-
t.count = 3;
661-
cl_git_pass(git_buf_common_prefix(&a, &t));
652+
cl_git_pass(git_buf_common_prefix(&a, t4, 3));
662653
cl_assert_equal_s(a.ptr, "happ");
663654

664-
t.strings = t5;
665-
t.count = 3;
666-
cl_git_pass(git_buf_common_prefix(&a, &t));
655+
cl_git_pass(git_buf_common_prefix(&a, t5, 3));
667656
cl_assert_equal_s(a.ptr, "happ");
668657

669-
t.strings = t6;
670-
t.count = 3;
671-
cl_git_pass(git_buf_common_prefix(&a, &t));
658+
cl_git_pass(git_buf_common_prefix(&a, t6, 3));
672659
cl_assert_equal_s(a.ptr, "");
673660

674-
t.strings = t7;
675-
t.count = 3;
676-
cl_git_pass(git_buf_common_prefix(&a, &t));
661+
cl_git_pass(git_buf_common_prefix(&a, t7, 3));
677662
cl_assert_equal_s(a.ptr, "");
678663

679664
git_buf_dispose(&a);

0 commit comments

Comments
 (0)