Skip to content

Commit

Permalink
Merge pull request #18 from jacquesg/sqlite-rot
Browse files Browse the repository at this point in the history
Repair SQLite backend bitrot
  • Loading branch information
Edward Thomson authored Jan 17, 2017
2 parents bf0831e + d9dec3a commit b2b968f
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions sqlite/sqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <assert.h>
#include <string.h>
#include <git2.h>
#include <git2/odb_backend.h>
#include <git2/sys/odb_backend.h>
#include <sqlite3.h>

#define GIT2_TABLE_NAME "git2_odb"
Expand Down Expand Up @@ -54,7 +54,7 @@ int sqlite_backend__read_header(size_t *len_p, git_otype *type_p, git_odb_backen
*type_p = (git_otype)sqlite3_column_int(backend->st_read_header, 0);
*len_p = (size_t)sqlite3_column_int(backend->st_read_header, 1);
assert(sqlite3_step(backend->st_read_header) == SQLITE_DONE);
error = GIT_SUCCESS;
error = GIT_OK;
} else {
error = GIT_ENOTFOUND;
}
Expand All @@ -81,10 +81,11 @@ int sqlite_backend__read(void **data_p, size_t *len_p, git_otype *type_p, git_od
*data_p = malloc(*len_p);

if (*data_p == NULL) {
error = GIT_ENOMEM;
giterr_set_oom();
error = GIT_ERROR;
} else {
memcpy(*data_p, sqlite3_column_blob(backend->st_read, 2), *len_p);
error = GIT_SUCCESS;
error = GIT_OK;
}

assert(sqlite3_step(backend->st_read) == SQLITE_DONE);
Expand All @@ -98,17 +99,17 @@ int sqlite_backend__read(void **data_p, size_t *len_p, git_otype *type_p, git_od
}

int sqlite_backend__read_prefix(git_oid *out_oid, void **data_p, size_t *len_p, git_otype *type_p, git_odb_backend *_backend,
const git_oid *short_oid, unsigned int len) {
const git_oid *short_oid, size_t len) {
if (len >= GIT_OID_HEXSZ) {
/* Just match the full identifier */
int error = sqlite_backend__read(data_p, len_p, type_p, _backend, short_oid);
if (error == GIT_SUCCESS)
if (error == GIT_OK)
git_oid_cpy(out_oid, short_oid);

return error;
} else if (len < GIT_OID_HEXSZ) {
return GIT_ENOTIMPLEMENTED;
}
/* not implemented (yet) */
return GIT_ERROR;
}

int sqlite_backend__exists(git_odb_backend *_backend, const git_oid *oid)
Expand All @@ -133,7 +134,7 @@ int sqlite_backend__exists(git_odb_backend *_backend, const git_oid *oid)
}


int sqlite_backend__write(git_oid *id, git_odb_backend *_backend, const void *data, size_t len, git_otype type)
int sqlite_backend__write(git_odb_backend *_backend, const git_oid *id, const void *data, size_t len, git_otype type)
{
int error;
sqlite_backend *backend;
Expand All @@ -142,9 +143,6 @@ int sqlite_backend__write(git_oid *id, git_odb_backend *_backend, const void *da

backend = (sqlite_backend *)_backend;

if ((error = git_odb_hash(id, data, len, type)) < 0)
return error;

error = SQLITE_ERROR;

if (sqlite3_bind_text(backend->st_write, 1, (char *)id->id, 20, SQLITE_TRANSIENT) == SQLITE_OK &&
Expand All @@ -155,7 +153,7 @@ int sqlite_backend__write(git_oid *id, git_odb_backend *_backend, const void *da
}

sqlite3_reset(backend->st_write);
return (error == SQLITE_DONE) ? GIT_SUCCESS : GIT_ERROR;
return (error == SQLITE_DONE) ? GIT_OK : GIT_ERROR;
}


Expand Down Expand Up @@ -185,7 +183,7 @@ static int create_table(sqlite3 *db)
if (sqlite3_exec(db, sql_creat, NULL, NULL, NULL) != SQLITE_OK)
return GIT_ERROR;

return GIT_SUCCESS;
return GIT_OK;
}

static int init_db(sqlite3 *db)
Expand All @@ -207,7 +205,7 @@ static int init_db(sqlite3 *db)

case SQLITE_ROW:
/* the table was found */
error = GIT_SUCCESS;
error = GIT_OK;
break;

default:
Expand Down Expand Up @@ -239,7 +237,7 @@ static int init_statements(sqlite_backend *backend)
if (sqlite3_prepare_v2(backend->db, sql_write, -1, &backend->st_write, NULL) != SQLITE_OK)
return GIT_ERROR;

return GIT_SUCCESS;
return GIT_OK;
}

int git_odb_backend_sqlite(git_odb_backend **backend_out, const char *sqlite_db)
Expand All @@ -248,8 +246,10 @@ int git_odb_backend_sqlite(git_odb_backend **backend_out, const char *sqlite_db)
int error;

backend = calloc(1, sizeof(sqlite_backend));
if (backend == NULL)
return GIT_ENOMEM;
if (backend == NULL) {
giterr_set_oom();
return GIT_ERROR;
}

if (sqlite3_open(sqlite_db, &backend->db) != SQLITE_OK)
goto cleanup;
Expand All @@ -262,6 +262,7 @@ int git_odb_backend_sqlite(git_odb_backend **backend_out, const char *sqlite_db)
if (error < 0)
goto cleanup;

backend->parent.version = GIT_ODB_BACKEND_VERSION;
backend->parent.read = &sqlite_backend__read;
backend->parent.read_prefix = &sqlite_backend__read_prefix;
backend->parent.read_header = &sqlite_backend__read_header;
Expand All @@ -270,7 +271,7 @@ int git_odb_backend_sqlite(git_odb_backend **backend_out, const char *sqlite_db)
backend->parent.free = &sqlite_backend__free;

*backend_out = (git_odb_backend *)backend;
return GIT_SUCCESS;
return GIT_OK;

cleanup:
sqlite_backend__free((git_odb_backend *)backend);
Expand Down

0 comments on commit b2b968f

Please sign in to comment.