commit 59ece79d29ce555391953bc053f934b5b1ec15ff from: Stefan Sperling date: Mon Feb 12 21:13:27 2018 UTC make struct got_object_id opaque commit - 908b01940959be5e3b25e6e352b4bdcb2a49f65c commit + 59ece79d29ce555391953bc053f934b5b1ec15ff blob - 5543f39b3fadb56aa44361f6af19d4e979ec45a5 blob + 63ff8ec7e3a9660476e5885e90590b8b63bc52bc --- include/got_error.h +++ include/got_error.h @@ -38,6 +38,7 @@ #define GOT_ERR_BAD_DELTA_CHAIN 20 #define GOT_ERR_BAD_DELTA 21 #define GOT_ERR_COMPRESSION 22 +#define GOT_ERR_BAD_OBJ_ID_STR 23 static const struct got_error { int code; @@ -64,8 +65,9 @@ static const struct got_error { { GOT_ERR_NOT_IMPL, "feature not implemented" }, { GOT_ERR_OBJ_NOT_PACKED,"object is not packed" }, { GOT_ERR_BAD_DELTA_CHAIN,"bad delta chain" }, - { GOT_ERR_BAD_DELTA ,"bad delta" }, - { GOT_ERR_COMPRESSION,"compression failed" }, + { GOT_ERR_BAD_DELTA, "bad delta" }, + { GOT_ERR_COMPRESSION, "compression failed" }, + { GOT_ERR_BAD_OBJ_ID_STR,"bad object id string" }, }; const struct got_error * got_error(int code); blob - 12c26ba164146ecbebea3eb36396764616cc5da2 blob + 1e490d07749d5bc65262db22d608334fc7dc81f3 --- include/got_object.h +++ include/got_object.h @@ -14,9 +14,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -struct got_object_id { - u_int8_t sha1[SHA1_DIGEST_LENGTH]; -}; +struct got_object_id; struct got_blob_object; @@ -24,7 +22,7 @@ struct got_tree_entry { SIMPLEQ_ENTRY(got_tree_entry) entry; mode_t mode; char *name; - struct got_object_id id; + struct got_object_id *id; }; struct got_tree_object { @@ -34,13 +32,13 @@ struct got_tree_object { struct got_parent_id { SIMPLEQ_ENTRY(got_parent_id) entry; - struct got_object_id id; + struct got_object_id *id; }; SIMPLEQ_HEAD(got_parent_id_list, got_parent_id); struct got_commit_object { - struct got_object_id tree_id; + struct got_object_id *tree_id; unsigned int nparents; SIMPLEQ_HEAD(, got_parent_id) parent_ids; char *author; @@ -60,6 +58,7 @@ struct got_object; struct got_repository; char *got_object_id_str(struct got_object_id *, char *, size_t); +const struct got_error *got_parse_object_id(struct got_object_id **, const char *); int got_object_id_cmp(struct got_object_id *, struct got_object_id *); int got_object_get_type(struct got_object *); const struct got_error *got_object_open(struct got_object **, blob - 0d417198dabeffa3677bed2341c49fdefa698b55 blob + a2cb581be5f9c483aa6a1c74a7158b0c0003403a --- lib/diff.c +++ lib/diff.c @@ -366,21 +366,21 @@ diff_entry_old_new(struct got_tree_entry *te1, struct te2 = match_entry_by_name(te1, tree2); if (te2 == NULL) { if (S_ISDIR(te1->mode)) - return diff_deleted_tree(&te1->id, repo, outfile); - return diff_deleted_blob(&te1->id, repo, outfile); + return diff_deleted_tree(te1->id, repo, outfile); + return diff_deleted_blob(te1->id, repo, outfile); } if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) { - if (got_object_id_cmp(&te1->id, &te2->id) != 0) - return diff_modified_tree(&te1->id, &te2->id, repo, + if (got_object_id_cmp(te1->id, te2->id) != 0) + return diff_modified_tree(te1->id, te2->id, repo, outfile); } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) { - if (got_object_id_cmp(&te1->id, &te2->id) != 0) - return diff_modified_blob(&te1->id, &te2->id, repo, + if (got_object_id_cmp(te1->id, te2->id) != 0) + return diff_modified_blob(te1->id, te2->id, repo, outfile); } - return diff_kind_mismatch(&te1->id, &te2->id, outfile); + return diff_kind_mismatch(te1->id, te2->id, outfile); } static const struct got_error * @@ -395,8 +395,8 @@ diff_entry_new_old(struct got_tree_entry *te2, struct return NULL; if (S_ISDIR(te2->mode)) - return diff_added_tree(&te2->id, repo, outfile); - return diff_added_blob(&te2->id, repo, outfile); + return diff_added_tree(te2->id, repo, outfile); + return diff_added_blob(te2->id, repo, outfile); } const struct got_error * blob - 9e483cc3b205fa699fa54d75cc474e3980cd8383 blob + e9db3c6742df9df3e1e59370a935408eefd16704 --- lib/object.c +++ lib/object.c @@ -30,8 +30,8 @@ #include "got_object.h" #include "got_repository.h" #include "got_sha1.h" -#include "pack.h" #include "delta.h" +#include "pack.h" #include "zb.h" #include "object.h" @@ -56,8 +56,23 @@ char * got_object_id_str(struct got_object_id *id, char *buf, size_t size) { return got_sha1_digest_to_str(id->sha1, buf, size); +} + +const struct got_error * +got_parse_object_id(struct got_object_id **id, const char *buf) +{ + *id = calloc(1, sizeof(**id)); + if (*id == NULL) + return got_error(GOT_ERR_NO_MEM); + if (!got_parse_sha1_digest((*id)->sha1, buf)) { + free(*id); + *id = NULL; + return got_error(GOT_ERR_BAD_OBJ_ID_STR); + } + return NULL; } + int got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2) { @@ -281,7 +296,7 @@ commit_object_valid(struct got_commit_object *commit) n = 0; for (i = 0; i < SHA1_DIGEST_LENGTH; i++) { - if (commit->tree_id.sha1[i] == 0) + if (commit->tree_id->sha1[i] == 0) n++; } if (n == SHA1_DIGEST_LENGTH) @@ -300,7 +315,13 @@ parse_commit_object(struct got_commit_object **commit, *commit = calloc(1, sizeof(**commit)); if (*commit == NULL) + return got_error(GOT_ERR_NO_MEM); + (*commit)->tree_id = calloc(1, sizeof(*(*commit)->tree_id)); + if ((*commit)->tree_id == NULL) { + free(*commit); + *commit = NULL; return got_error(GOT_ERR_NO_MEM); + } SIMPLEQ_INIT(&(*commit)->parent_ids); @@ -312,7 +333,7 @@ parse_commit_object(struct got_commit_object **commit, goto done; } s += tlen; - if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) { + if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) { err = got_error(GOT_ERR_BAD_OBJ_DATA); goto done; } @@ -338,9 +359,17 @@ parse_commit_object(struct got_commit_object **commit, err = got_error(GOT_ERR_NO_MEM); goto done; } - s += tlen; - if (!got_parse_sha1_digest(pid->id.sha1, s)) { + pid->id = calloc(1, sizeof(*pid->id)); + if (pid->id == NULL) { + free(pid); + err = got_error(GOT_ERR_NO_MEM); + goto done; + } + s += tlen; + if (!got_parse_sha1_digest(pid->id->sha1, s)) { err = got_error(GOT_ERR_BAD_OBJ_DATA); + free(pid->id); + free(pid); goto done; } SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry); @@ -406,14 +435,17 @@ parse_commit_object(struct got_commit_object **commit, goto done; } done: - if (err) + if (err) { got_object_commit_close(*commit); + *commit = NULL; + } return err; } static void tree_entry_close(struct got_tree_entry *te) { + free(te->id); free(te->name); free(te); } @@ -428,17 +460,26 @@ parse_tree_entry(struct got_tree_entry **te, size_t *e *te = calloc(1, sizeof(**te)); if (*te == NULL) + return got_error(GOT_ERR_NO_MEM); + + (*te)->id = calloc(1, sizeof(*(*te)->id)); + if ((*te)->id == NULL) { + free(*te); + *te = NULL; return got_error(GOT_ERR_NO_MEM); + } *elen = strlen(buf) + 1; if (*elen > maxlen) { free(*te); + *te = NULL; return got_error(GOT_ERR_BAD_OBJ_DATA); } space = strchr(buf, ' '); if (space == NULL) { free(*te); + *te = NULL; return got_error(GOT_ERR_BAD_OBJ_DATA); } while (*p != ' ') { @@ -457,11 +498,13 @@ parse_tree_entry(struct got_tree_entry **te, size_t *e goto done; } buf += strlen(buf) + 1; - memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH); + memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH); *elen += SHA1_DIGEST_LENGTH; done: - if (err) + if (err) { tree_entry_close(*te); + *te = NULL; + } return err; } @@ -608,9 +651,11 @@ got_object_commit_close(struct got_commit_object *comm while (!SIMPLEQ_EMPTY(&commit->parent_ids)) { pid = SIMPLEQ_FIRST(&commit->parent_ids); SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry); + free(pid->id); free(pid); } + free(commit->tree_id); free(commit->author); free(commit->committer); free(commit->logmsg); blob - 9b27d1b8f5cb6507171a8a3db2a4a98bce3e0562 blob + 156901f0339a8945664abed5e3624a696e2ea8b5 --- lib/object.h +++ lib/object.h @@ -14,6 +14,10 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +struct got_object_id { + u_int8_t sha1[SHA1_DIGEST_LENGTH]; +}; + struct got_object { int type; int flags; blob - 189b5641714068ed3ea3bc6d13b1f765956a582d blob + 348ae1a580907c62f979bb861cdde189a32ca3af --- lib/pack.h +++ lib/pack.h @@ -88,7 +88,7 @@ struct got_packfile_object_data { /* If object is of type GOT_OBJ_TYPE_REF_DELTA. */ struct got_packfile_object_data_ref_delta { - struct got_object_id id; + uint8_t sha1[SHA1_DIGEST_LENGTH]; uint8_t *delta_data; /* compressed */ }; blob - 9392f6678ce431df86878842a37ac009b1e1a171 blob + 67a327f5fa5f50aeaea6665fa38f8b692e726ea4 --- lib/refs.c +++ lib/refs.c @@ -31,8 +31,10 @@ #include "got_sha1.h" #include "path.h" +#include "delta.h" +#include "zb.h" +#include "object.h" - static const struct got_error * parse_symref(struct got_reference **ref, const char *name, const char *line) { blob - 0b04bd618d455a241730c9202119f379a6d102ce blob + 0dac4a0e1383ef0e7db9adee92a2dbed8d1e67ec --- regress/repository/repository_test.c +++ regress/repository/repository_test.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "got_error.h" #include "got_object.h" @@ -30,7 +31,6 @@ #include "got_repository.h" #include "got_sha1.h" #include "got_diff.h" -#include "unistd.h" #define GOT_REPO_PATH "../../../" @@ -61,7 +61,7 @@ print_parent_commits(struct got_commit_object *commit, struct got_object *obj; SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) { - err = got_object_open(&obj, repo, &pid->id); + err = got_object_open(&obj, repo, pid->id); if (err != NULL) return err; if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) @@ -92,15 +92,15 @@ print_tree_object(struct got_object *obj, char *parent if (!S_ISDIR(te->mode)) { test_printf("%s %s/%s\n", - got_object_id_str(&te->id, hex, sizeof(hex)), + got_object_id_str(te->id, hex, sizeof(hex)), parent, te->name); continue; } test_printf("%s %s/%s\n", - got_object_id_str(&te->id, hex, sizeof(hex)), + got_object_id_str(te->id, hex, sizeof(hex)), parent, te->name); - err = got_object_open(&treeobj, repo, &te->id); + err = got_object_open(&treeobj, repo, te->id); if (err != NULL) break; @@ -144,17 +144,17 @@ print_commit_object(struct got_object *obj, struct got return err; test_printf("tree: %s\n", - got_object_id_str(&commit->tree_id, buf, sizeof(buf))); + got_object_id_str(commit->tree_id, buf, sizeof(buf))); test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s"); SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) { test_printf("%s\n", - got_object_id_str(&pid->id, buf, sizeof(buf))); + got_object_id_str(pid->id, buf, sizeof(buf))); } test_printf("author: %s\n", commit->author); test_printf("committer: %s\n", commit->committer); test_printf("log: %s\n", commit->logmsg); - err = got_object_open(&treeobj, repo, &commit->tree_id); + err = got_object_open(&treeobj, repo, commit->tree_id); if (err != NULL) return err; if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) { @@ -212,19 +212,21 @@ repo_read_tree(const char *repo_path) const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690"; const struct got_error *err; struct got_repository *repo; - struct got_object_id id; + struct got_object_id *id; struct got_object *obj; char hex[SHA1_DIGEST_STRING_LENGTH]; int i; size_t len; - if (!got_parse_sha1_digest(id.sha1, tree_sha1)) + err = got_parse_object_id(&id, tree_sha1); + if (err != NULL) return 0; err = got_repo_open(&repo, repo_path); if (err != NULL || repo == NULL) return 0; - err = got_object_open(&obj, repo, &id); + err = got_object_open(&obj, repo, id); + free(id); if (err != NULL || obj == NULL) return 0; if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) @@ -243,20 +245,22 @@ repo_read_blob(const char *repo_path) const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3"; const struct got_error *err; struct got_repository *repo; - struct got_object_id id; + struct got_object_id *id; struct got_object *obj; struct got_blob_object *blob; char hex[SHA1_DIGEST_STRING_LENGTH]; int i; size_t len; - if (!got_parse_sha1_digest(id.sha1, blob_sha1)) + err = got_parse_object_id(&id, blob_sha1); + if (err != NULL) return 0; err = got_repo_open(&repo, repo_path); if (err != NULL || repo == NULL) return 0; - err = got_object_open(&obj, repo, &id); + err = got_object_open(&obj, repo, id); + free(id); if (err != NULL || obj == NULL) return 0; if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) @@ -290,8 +294,8 @@ repo_diff_blob(const char *repo_path) const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf"; const struct got_error *err; struct got_repository *repo; - struct got_object_id id1; - struct got_object_id id2; + struct got_object_id *id1; + struct got_object_id *id2; struct got_object *obj1; struct got_object *obj2; struct got_blob_object *blob1; @@ -301,21 +305,26 @@ repo_diff_blob(const char *repo_path) size_t len; FILE *outfile; - if (!got_parse_sha1_digest(id1.sha1, blob1_sha1)) + err = got_parse_object_id(&id1, blob1_sha1); + if (err != NULL) return 0; - if (!got_parse_sha1_digest(id2.sha1, blob2_sha1)) + + err = got_parse_object_id(&id2, blob2_sha1); + if (err != NULL) return 0; err = got_repo_open(&repo, repo_path); if (err != NULL || repo == NULL) return 0; - err = got_object_open(&obj1, repo, &id1); + err = got_object_open(&obj1, repo, id1); + free(id1); if (err != NULL || obj1 == NULL) return 0; if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) return 0; - err = got_object_open(&obj2, repo, &id2); + err = got_object_open(&obj2, repo, id2); + free(id2); if (err != NULL || obj2 == NULL) return 0; if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) @@ -354,8 +363,8 @@ repo_diff_tree(const char *repo_path) const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3"; const struct got_error *err; struct got_repository *repo; - struct got_object_id id1; - struct got_object_id id2; + struct got_object_id *id1; + struct got_object_id *id2; struct got_object *obj1; struct got_object *obj2; struct got_tree_object *tree1; @@ -365,21 +374,26 @@ repo_diff_tree(const char *repo_path) size_t len; FILE *outfile; - if (!got_parse_sha1_digest(id1.sha1, tree1_sha1)) + err = got_parse_object_id(&id1, tree1_sha1); + if (err != NULL) return 0; - if (!got_parse_sha1_digest(id2.sha1, tree2_sha1)) + + err = got_parse_object_id(&id2, tree2_sha1); + if (err != NULL) return 0; err = got_repo_open(&repo, repo_path); if (err != NULL || repo == NULL) return 0; - err = got_object_open(&obj1, repo, &id1); + err = got_object_open(&obj1, repo, id1); + free(id1); if (err != NULL || obj1 == NULL) return 0; if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE) return 0; - err = got_object_open(&obj2, repo, &id2); + err = got_object_open(&obj2, repo, id2); + free(id2); if (err != NULL || obj2 == NULL) return 0; if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)