Skip to content

Commit

Permalink
Add reftable library
Browse files Browse the repository at this point in the history
Reftable is a new format for storing the ref database. It provides the
following benefits:

 * Simple and fast atomic ref transactions, including multiple refs and reflogs.
 * Compact storage of ref data.
 * Fast look ups of ref data.
 * Case-sensitive ref names on Windows/OSX, regardless of file system
 * Eliminates file/directory conflicts in ref names

Further context and motivation can be found in background reading:

* Spec: https://github.com/eclipse/jgit/blob/master/Documentation/technical/reftable.md

* Original discussion on JGit-dev:  https://www.eclipse.org/lists/jgit-dev/msg03389.html

* First design discussion on git@vger: https://public-inbox.org/git/CAJo=hJtTp2eA3z9wW9cHo-nA7kK40vVThqh6inXpbCcqfdMP9g@mail.gmail.com/

* Last design discussion on git@vger: https://public-inbox.org/git/CAJo=hJsZcAM9sipdVr7TMD-FD2V2W6_pvMQ791EGCDsDkQ033w@mail.gmail.com/

* First attempt at implementation: https://public-inbox.org/git/CAP8UFD0PPZSjBnxCA7ez91vBuatcHKQ+JUWvTD1iHcXzPBjPBg@mail.gmail.com/

* libgit2 support issue: https://github.com/libgit2/libgit2/issues

* GitLab support issue: https://gitlab.com/gitlab-org/git/issues/6

* go-git support issue: src-d/go-git#1059

Signed-off-by: Han-Wen Nienhuys <[email protected]>
  • Loading branch information
hanwen committed Feb 4, 2020
1 parent 1b01c73 commit eb0df10
Show file tree
Hide file tree
Showing 34 changed files with 6,268 additions and 0 deletions.
31 changes: 31 additions & 0 deletions reftable/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
BSD License

Copyright (c) 2020, Google LLC
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

* Neither the name of Google LLC nor the names of its contributors may
be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19 changes: 19 additions & 0 deletions reftable/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

The source code in this directory comes from https://github.com/google/reftable.

The VERSION file keeps track of the current version of the reftable library.

To update the library, do:

((cd reftable-repo && git fetch origin && git checkout origin/master ) ||
git clone https://github.com/google/reftable reftable-repo) && \
cp reftable-repo/c/*.[ch] reftable/ && \
cp reftable-repo/LICENSE reftable/ &&
git --git-dir reftable-repo/.git show --no-patch origin/master \
> reftable/VERSION && \
echo '/* empty */' > reftable/config.h
rm reftable/*_test.c reftable/test_framework.*
git add reftable/*.[ch]

Bugfixes should be accompanied by a test and applied to upstream project at
https://github.com/google/reftable.
5 changes: 5 additions & 0 deletions reftable/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
commit e54326f73d95bfe8b17f264c400f4c365dbd5e5e
Author: Han-Wen Nienhuys <[email protected]>
Date: Tue Feb 4 19:48:17 2020 +0100

C: PRI?MAX use; clang-format.
196 changes: 196 additions & 0 deletions reftable/basics.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/*
Copyright 2020 Google LLC
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd
*/

#include "basics.h"

#include "system.h"

void put_u24(byte *out, uint32_t i)
{
out[0] = (byte)((i >> 16) & 0xff);
out[1] = (byte)((i >> 8) & 0xff);
out[2] = (byte)((i)&0xff);
}

uint32_t get_u24(byte *in)
{
return (uint32_t)(in[0]) << 16 | (uint32_t)(in[1]) << 8 |
(uint32_t)(in[2]);
}

void put_u32(byte *out, uint32_t i)
{
out[0] = (byte)((i >> 24) & 0xff);
out[1] = (byte)((i >> 16) & 0xff);
out[2] = (byte)((i >> 8) & 0xff);
out[3] = (byte)((i)&0xff);
}

uint32_t get_u32(byte *in)
{
return (uint32_t)(in[0]) << 24 | (uint32_t)(in[1]) << 16 |
(uint32_t)(in[2]) << 8 | (uint32_t)(in[3]);
}

void put_u64(byte *out, uint64_t v)
{
int i = 0;
for (i = sizeof(uint64_t); i--;) {
out[i] = (byte)(v & 0xff);
v >>= 8;
}
}

uint64_t get_u64(byte *out)
{
uint64_t v = 0;
int i = 0;
for (i = 0; i < sizeof(uint64_t); i++) {
v = (v << 8) | (byte)(out[i] & 0xff);
}
return v;
}

void put_u16(byte *out, uint16_t i)
{
out[0] = (byte)((i >> 8) & 0xff);
out[1] = (byte)((i)&0xff);
}

uint16_t get_u16(byte *in)
{
return (uint32_t)(in[0]) << 8 | (uint32_t)(in[1]);
}

/*
find smallest index i in [0, sz) at which f(i) is true, assuming
that f is ascending. Return sz if f(i) is false for all indices.
*/
int binsearch(int sz, int (*f)(int k, void *args), void *args)
{
int lo = 0;
int hi = sz;

/* invariant: (hi == sz) || f(hi) == true
(lo == 0 && f(0) == true) || fi(lo) == false
*/
while (hi - lo > 1) {
int mid = lo + (hi - lo) / 2;

int val = f(mid, args);
if (val) {
hi = mid;
} else {
lo = mid;
}
}

if (lo == 0) {
if (f(0, args)) {
return 0;
} else {
return 1;
}
}

return hi;
}

void free_names(char **a)
{
char **p = a;
if (p == NULL) {
return;
}
while (*p) {
free(*p);
p++;
}
free(a);
}

int names_length(char **names)
{
int len = 0;
for (char **p = names; *p; p++) {
len++;
}
return len;
}

/* parse a newline separated list of names. Empty names are discarded. */
void parse_names(char *buf, int size, char ***namesp)
{
char **names = NULL;
int names_cap = 0;
int names_len = 0;

char *p = buf;
char *end = buf + size;
while (p < end) {
char *next = strchr(p, '\n');
if (next != NULL) {
*next = 0;
} else {
next = end;
}
if (p < next) {
if (names_len == names_cap) {
names_cap = 2 * names_cap + 1;
names = realloc(names,
names_cap * sizeof(char *));
}
names[names_len++] = strdup(p);
}
p = next + 1;
}

if (names_len == names_cap) {
names_cap = 2 * names_cap + 1;
names = realloc(names, names_cap * sizeof(char *));
}

names[names_len] = NULL;
*namesp = names;
}

int names_equal(char **a, char **b)
{
while (*a && *b) {
if (strcmp(*a, *b)) {
return 0;
}

a++;
b++;
}

return *a == *b;
}

const char *error_str(int err)
{
switch (err) {
case IO_ERROR:
return "I/O error";
case FORMAT_ERROR:
return "FORMAT_ERROR";
case NOT_EXIST_ERROR:
return "NOT_EXIST_ERROR";
case LOCK_ERROR:
return "LOCK_ERROR";
case API_ERROR:
return "API_ERROR";
case ZLIB_ERROR:
return "ZLIB_ERROR";
case -1:
return "general error";
default:
return "unknown error code";
}
}
37 changes: 37 additions & 0 deletions reftable/basics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2020 Google LLC
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd
*/

#ifndef BASICS_H
#define BASICS_H

#include "system.h"

#include "reftable.h"

#define true 1
#define false 0

void put_u24(byte *out, uint32_t i);
uint32_t get_u24(byte *in);

uint64_t get_u64(byte *in);
void put_u64(byte *out, uint64_t i);

void put_u32(byte *out, uint32_t i);
uint32_t get_u32(byte *in);

void put_u16(byte *out, uint16_t i);
uint16_t get_u16(byte *in);
int binsearch(int sz, int (*f)(int k, void *args), void *args);

void free_names(char **a);
void parse_names(char *buf, int size, char ***namesp);
int names_equal(char **a, char **b);
int names_length(char **names);

#endif
Loading

0 comments on commit eb0df10

Please sign in to comment.