Skip to content

Commit ef8a6c6

Browse files
hanwengitster
authored andcommitted
reftable: utility functions
This commit provides basic utility classes for the reftable library. Signed-off-by: Han-Wen Nienhuys <[email protected]> Helped-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8900447 commit ef8a6c6

16 files changed

+570
-7
lines changed

Makefile

+22-3
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ TEST_BUILTINS_OBJS += test-read-cache.o
743743
TEST_BUILTINS_OBJS += test-read-graph.o
744744
TEST_BUILTINS_OBJS += test-read-midx.o
745745
TEST_BUILTINS_OBJS += test-ref-store.o
746+
TEST_BUILTINS_OBJS += test-reftable.o
746747
TEST_BUILTINS_OBJS += test-regex.o
747748
TEST_BUILTINS_OBJS += test-repository.o
748749
TEST_BUILTINS_OBJS += test-revision-walking.o
@@ -821,6 +822,8 @@ TEST_SHELL_PATH = $(SHELL_PATH)
821822

822823
LIB_FILE = libgit.a
823824
XDIFF_LIB = xdiff/lib.a
825+
REFTABLE_LIB = reftable/libreftable.a
826+
REFTABLE_TEST_LIB = reftable/libreftable_test.a
824827

825828
GENERATED_H += command-list.h
826829
GENERATED_H += config-list.h
@@ -1195,7 +1198,7 @@ THIRD_PARTY_SOURCES += compat/regex/%
11951198
THIRD_PARTY_SOURCES += sha1collisiondetection/%
11961199
THIRD_PARTY_SOURCES += sha1dc/%
11971200

1198-
GITLIBS = common-main.o $(LIB_FILE) $(XDIFF_LIB)
1201+
GITLIBS = common-main.o $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB)
11991202
EXTLIBS =
12001203

12011204
GIT_USER_AGENT = git/$(GIT_VERSION)
@@ -2446,7 +2449,15 @@ XDIFF_OBJS += xdiff/xutils.o
24462449
.PHONY: xdiff-objs
24472450
xdiff-objs: $(XDIFF_OBJS)
24482451

2452+
REFTABLE_OBJS += reftable/basics.o
2453+
REFTABLE_OBJS += reftable/error.o
2454+
REFTABLE_OBJS += reftable/publicbasics.o
2455+
2456+
REFTABLE_TEST_OBJS += reftable/test_framework.o
2457+
REFTABLE_TEST_OBJS += reftable/basics_test.o
2458+
24492459
TEST_OBJS := $(patsubst %$X,%.o,$(TEST_PROGRAMS)) $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS))
2460+
24502461
.PHONY: test-objs
24512462
test-objs: $(TEST_OBJS)
24522463

@@ -2462,6 +2473,8 @@ OBJECTS += $(PROGRAM_OBJS)
24622473
OBJECTS += $(TEST_OBJS)
24632474
OBJECTS += $(XDIFF_OBJS)
24642475
OBJECTS += $(FUZZ_OBJS)
2476+
OBJECTS += $(REFTABLE_OBJS) $(REFTABLE_TEST_OBJS)
2477+
24652478
ifndef NO_CURL
24662479
OBJECTS += http.o http-walker.o remote-curl.o
24672480
endif
@@ -2612,6 +2625,12 @@ $(LIB_FILE): $(LIB_OBJS)
26122625
$(XDIFF_LIB): $(XDIFF_OBJS)
26132626
$(QUIET_AR)$(AR) $(ARFLAGS) $@ $^
26142627

2628+
$(REFTABLE_LIB): $(REFTABLE_OBJS)
2629+
$(QUIET_AR)$(RM) $@ && $(AR) $(ARFLAGS) $@ $^
2630+
2631+
$(REFTABLE_TEST_LIB): $(REFTABLE_TEST_OBJS)
2632+
$(QUIET_AR)$(RM) $@ && $(AR) $(ARFLAGS) $@ $^
2633+
26152634
export DEFAULT_EDITOR DEFAULT_PAGER
26162635

26172636
Documentation/GIT-EXCLUDED-PROGRAMS: FORCE
@@ -2904,7 +2923,7 @@ perf: all
29042923

29052924
t/helper/test-tool$X: $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS))
29062925

2907-
t/helper/test-%$X: t/helper/test-%.o GIT-LDFLAGS $(GITLIBS)
2926+
t/helper/test-%$X: t/helper/test-%.o GIT-LDFLAGS $(GITLIBS) $(REFTABLE_TEST_LIB)
29082927
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(filter %.a,$^) $(LIBS)
29092928

29102929
check-sha1:: t/helper/test-tool$X
@@ -3234,7 +3253,7 @@ cocciclean:
32343253
clean: profile-clean coverage-clean cocciclean
32353254
$(RM) *.res
32363255
$(RM) $(OBJECTS)
3237-
$(RM) $(LIB_FILE) $(XDIFF_LIB)
3256+
$(RM) $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB) $(REFTABLE_TEST_LIB)
32383257
$(RM) $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) git$X
32393258
$(RM) $(TEST_PROGRAMS)
32403259
$(RM) $(FUZZ_PROGRAMS)

contrib/buildsystems/CMakeLists.txt

+12-2
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,12 @@ parse_makefile_for_sources(libxdiff_SOURCES "XDIFF_OBJS")
640640
list(TRANSFORM libxdiff_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
641641
add_library(xdiff STATIC ${libxdiff_SOURCES})
642642

643+
#reftable
644+
parse_makefile_for_sources(reftable_SOURCES "REFTABLE_OBJS")
645+
646+
list(TRANSFORM reftable_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
647+
add_library(reftable STATIC ${reftable_SOURCES})
648+
643649
if(WIN32)
644650
if(NOT MSVC)#use windres when compiling with gcc and clang
645651
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res
@@ -662,7 +668,7 @@ endif()
662668
#link all required libraries to common-main
663669
add_library(common-main OBJECT ${CMAKE_SOURCE_DIR}/common-main.c)
664670

665-
target_link_libraries(common-main libgit xdiff ${ZLIB_LIBRARIES})
671+
target_link_libraries(common-main libgit xdiff reftable ${ZLIB_LIBRARIES})
666672
if(Intl_FOUND)
667673
target_link_libraries(common-main ${Intl_LIBRARIES})
668674
endif()
@@ -902,11 +908,15 @@ if(BUILD_TESTING)
902908
add_executable(test-fake-ssh ${CMAKE_SOURCE_DIR}/t/helper/test-fake-ssh.c)
903909
target_link_libraries(test-fake-ssh common-main)
904910

911+
#reftable-tests
912+
parse_makefile_for_sources(test-reftable_SOURCES "REFTABLE_TEST_OBJS")
913+
list(TRANSFORM test-reftable_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
914+
905915
#test-tool
906916
parse_makefile_for_sources(test-tool_SOURCES "TEST_BUILTINS_OBJS")
907917

908918
list(TRANSFORM test-tool_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/t/helper/")
909-
add_executable(test-tool ${CMAKE_SOURCE_DIR}/t/helper/test-tool.c ${test-tool_SOURCES})
919+
add_executable(test-tool ${CMAKE_SOURCE_DIR}/t/helper/test-tool.c ${test-tool_SOURCES} ${test-reftable_SOURCES})
910920
target_link_libraries(test-tool common-main)
911921

912922
set_target_properties(test-fake-ssh test-tool

contrib/buildsystems/Generators/Vcxproj.pm

+10-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ sub createProject {
7777
my $libs_release = "\n ";
7878
my $libs_debug = "\n ";
7979
if (!$static_library) {
80-
$libs_release = join(";", sort(grep /^(?!libgit\.lib|xdiff\/lib\.lib|vcs-svn\/lib\.lib)/, @{$$build_structure{"$prefix${name}_LIBS"}}));
80+
$libs_release = join(";", sort(grep /^(?!libgit\.lib|xdiff\/lib\.lib|vcs-svn\/lib\.lib|reftable\/libreftable\.lib)/, @{$$build_structure{"$prefix${name}_LIBS"}}));
8181
$libs_debug = $libs_release;
8282
$libs_debug =~ s/zlib\.lib/zlibd\.lib/g;
8383
$libs_debug =~ s/libexpat\.lib/libexpatd\.lib/g;
@@ -232,6 +232,7 @@ EOM
232232
EOM
233233
if (!$static_library || $target =~ 'vcs-svn' || $target =~ 'xdiff') {
234234
my $uuid_libgit = $$build_structure{"LIBS_libgit_GUID"};
235+
my $uuid_libreftable = $$build_structure{"LIBS_reftable/libreftable_GUID"};
235236
my $uuid_xdiff_lib = $$build_structure{"LIBS_xdiff/lib_GUID"};
236237

237238
print F << "EOM";
@@ -241,6 +242,14 @@ EOM
241242
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
242243
</ProjectReference>
243244
EOM
245+
if (!($name =~ /xdiff|libreftable/)) {
246+
print F << "EOM";
247+
<ProjectReference Include="$cdup\\reftable\\libreftable\\libreftable.vcxproj">
248+
<Project>$uuid_libreftable</Project>
249+
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
250+
</ProjectReference>
251+
EOM
252+
}
244253
if (!($name =~ 'xdiff')) {
245254
print F << "EOM";
246255
<ProjectReference Include="$cdup\\xdiff\\lib\\xdiff_lib.vcxproj">

reftable/basics.c

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
Copyright 2020 Google LLC
3+
4+
Use of this source code is governed by a BSD-style
5+
license that can be found in the LICENSE file or at
6+
https://developers.google.com/open-source/licenses/bsd
7+
*/
8+
9+
#include "basics.h"
10+
11+
void put_be24(uint8_t *out, uint32_t i)
12+
{
13+
out[0] = (uint8_t)((i >> 16) & 0xff);
14+
out[1] = (uint8_t)((i >> 8) & 0xff);
15+
out[2] = (uint8_t)(i & 0xff);
16+
}
17+
18+
uint32_t get_be24(uint8_t *in)
19+
{
20+
return (uint32_t)(in[0]) << 16 | (uint32_t)(in[1]) << 8 |
21+
(uint32_t)(in[2]);
22+
}
23+
24+
void put_be16(uint8_t *out, uint16_t i)
25+
{
26+
out[0] = (uint8_t)((i >> 8) & 0xff);
27+
out[1] = (uint8_t)(i & 0xff);
28+
}
29+
30+
int binsearch(size_t sz, int (*f)(size_t k, void *args), void *args)
31+
{
32+
size_t lo = 0;
33+
size_t hi = sz;
34+
35+
/* Invariants:
36+
*
37+
* (hi == sz) || f(hi) == true
38+
* (lo == 0 && f(0) == true) || fi(lo) == false
39+
*/
40+
while (hi - lo > 1) {
41+
size_t mid = lo + (hi - lo) / 2;
42+
43+
if (f(mid, args))
44+
hi = mid;
45+
else
46+
lo = mid;
47+
}
48+
49+
if (lo)
50+
return hi;
51+
52+
return f(0, args) ? 0 : 1;
53+
}
54+
55+
void free_names(char **a)
56+
{
57+
char **p;
58+
if (!a) {
59+
return;
60+
}
61+
for (p = a; *p; p++) {
62+
reftable_free(*p);
63+
}
64+
reftable_free(a);
65+
}
66+
67+
int names_length(char **names)
68+
{
69+
char **p = names;
70+
for (; *p; p++) {
71+
/* empty */
72+
}
73+
return p - names;
74+
}
75+
76+
void parse_names(char *buf, int size, char ***namesp)
77+
{
78+
char **names = NULL;
79+
size_t names_cap = 0;
80+
size_t names_len = 0;
81+
82+
char *p = buf;
83+
char *end = buf + size;
84+
while (p < end) {
85+
char *next = strchr(p, '\n');
86+
if (next && next < end) {
87+
*next = 0;
88+
} else {
89+
next = end;
90+
}
91+
if (p < next) {
92+
if (names_len == names_cap) {
93+
names_cap = 2 * names_cap + 1;
94+
names = reftable_realloc(
95+
names, names_cap * sizeof(*names));
96+
}
97+
names[names_len++] = xstrdup(p);
98+
}
99+
p = next + 1;
100+
}
101+
102+
names = reftable_realloc(names, (names_len + 1) * sizeof(*names));
103+
names[names_len] = NULL;
104+
*namesp = names;
105+
}
106+
107+
int names_equal(char **a, char **b)
108+
{
109+
int i = 0;
110+
for (; a[i] && b[i]; i++) {
111+
if (strcmp(a[i], b[i])) {
112+
return 0;
113+
}
114+
}
115+
116+
return a[i] == b[i];
117+
}
118+
119+
int common_prefix_size(struct strbuf *a, struct strbuf *b)
120+
{
121+
int p = 0;
122+
for (; p < a->len && p < b->len; p++) {
123+
if (a->buf[p] != b->buf[p])
124+
break;
125+
}
126+
127+
return p;
128+
}

reftable/basics.h

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2020 Google LLC
3+
4+
Use of this source code is governed by a BSD-style
5+
license that can be found in the LICENSE file or at
6+
https://developers.google.com/open-source/licenses/bsd
7+
*/
8+
9+
#ifndef BASICS_H
10+
#define BASICS_H
11+
12+
/*
13+
* miscellaneous utilities that are not provided by Git.
14+
*/
15+
16+
#include "system.h"
17+
18+
/* Bigendian en/decoding of integers */
19+
20+
void put_be24(uint8_t *out, uint32_t i);
21+
uint32_t get_be24(uint8_t *in);
22+
void put_be16(uint8_t *out, uint16_t i);
23+
24+
/*
25+
* find smallest index i in [0, sz) at which f(i) is true, assuming
26+
* that f is ascending. Return sz if f(i) is false for all indices.
27+
*
28+
* Contrary to bsearch(3), this returns something useful if the argument is not
29+
* found.
30+
*/
31+
int binsearch(size_t sz, int (*f)(size_t k, void *args), void *args);
32+
33+
/*
34+
* Frees a NULL terminated array of malloced strings. The array itself is also
35+
* freed.
36+
*/
37+
void free_names(char **a);
38+
39+
/* parse a newline separated list of names. `size` is the length of the buffer,
40+
* without terminating '\0'. Empty names are discarded. */
41+
void parse_names(char *buf, int size, char ***namesp);
42+
43+
/* compares two NULL-terminated arrays of strings. */
44+
int names_equal(char **a, char **b);
45+
46+
/* returns the array size of a NULL-terminated array of strings. */
47+
int names_length(char **names);
48+
49+
/* Allocation routines; they invoke the functions set through
50+
* reftable_set_alloc() */
51+
void *reftable_malloc(size_t sz);
52+
void *reftable_realloc(void *p, size_t sz);
53+
void reftable_free(void *p);
54+
void *reftable_calloc(size_t sz);
55+
56+
/* Find the longest shared prefix size of `a` and `b` */
57+
struct strbuf;
58+
int common_prefix_size(struct strbuf *a, struct strbuf *b);
59+
60+
#endif

0 commit comments

Comments
 (0)