commit 78591b77bb44d0907cd974b37d534629bc63b6dc
parent 51b1edc7be9a2071239ca69c89d5f35ad9a790c2
Author: Mohamed Aslan <maslan@sce.carleton.ca>
Date: Thu, 29 May 2014 04:30:50 -0400
add support for commit parent (single)
Diffstat:
3 files changed, 19 insertions(+), 6 deletions(-)
diff --git a/cmd-branch.c b/cmd-branch.c
@@ -81,7 +81,7 @@ cmd_branch(int argc, char **argv)
errx(EXIT_FAILURE, "error, failed to list branches.");
break;
case O_CREATE:
- /* create a new branch from the current one*/
+ /* create a new branch from the current one */
if (s.db_ops->branch_create_from(s.db_ctx, branch, s.branch) == EXIT_FAILURE)
errx(EXIT_FAILURE, "error, failed to create branch \'%s\'.", branch);
break;
diff --git a/dircache-simple.c b/dircache-simple.c
@@ -359,8 +359,9 @@ simple_gen_dindex(struct dircache_ctx *dc_ctx, char **didx)
return EXIT_SUCCESS;
}
+/* TODO: support commits with multiple parents */
static int
-simple_gen_commit(struct dircache_ctx *dc_ctx, const char *dir_id, const char *msg_file, char **commit_file)
+simple_gen_commit(struct dircache_ctx *dc_ctx, const char *dir_id, const char *parent_head, const char *msg_file, char **commit_file)
{
char *dc_path;
char *cmt_path;
@@ -396,6 +397,8 @@ simple_gen_commit(struct dircache_ctx *dc_ctx, const char *dir_id, const char *m
return EXIT_FAILURE;
}
fprintf(cfp, "dir %s\n", dir_id);
+ if (parent_head != NULL)
+ fprintf(cfp, "parent %s\n", parent_head);
fprintf(cfp, "author %s <%s>\n", user_name, user_email);
fprintf(cfp, "committer %s <%s>\n", user_name, user_email);
while (getline(&msg, &size, mfp) != -1) {
@@ -431,7 +434,7 @@ simple_get_branch(struct dircache_ctx *dc_ctx, char **branch_name)
static int
simple_commit(struct dircache_ctx *dc_ctx, const char *msgfile)
{
- char *branch;
+ char *cur_branch, *cur_head;
char *objid, *path, *paths[2], tmp_objid[1024];
char *dircache_path, *didx_path;
struct objdb_dirhandle *handle;
@@ -518,15 +521,21 @@ simple_commit(struct dircache_ctx *dc_ctx, const char *msgfile)
if (mkdir(dircache_path, S_IRUSR | S_IWUSR | S_IXUSR) == -1) {
return EXIT_FAILURE;
}
+ /* get commit's parent */
+ if (simple_get_branch(dc_ctx, &cur_branch) == EXIT_FAILURE)
+ return EXIT_FAILURE;
+ if (dc_ctx->db_ops->branch_get_head(dc_ctx->db_ctx, cur_branch, &cur_head) == EXIT_FAILURE)
+ return EXIT_FAILURE;
/* create commit */
- if (simple_gen_commit(dc_ctx, objid, msgfile, &path) == EXIT_FAILURE)
+ if (simple_gen_commit(dc_ctx, objid, cur_head, msgfile, &path) == EXIT_FAILURE)
return EXIT_FAILURE;
if (dc_ctx->db_ops->insert_from_file(dc_ctx->db_ctx, T_COMMIT, path, &objid) == EXIT_FAILURE)
return EXIT_FAILURE;
printf("commit id: %s\n", objid);
unlink(path);
- simple_get_branch(dc_ctx, &branch);
- dc_ctx->db_ops->branch_set_head(dc_ctx->db_ctx, branch, objid);
+ dc_ctx->db_ops->branch_set_head(dc_ctx->db_ctx, cur_branch, objid);
+ free(cur_branch);
+ free(cur_head);
free(path);
free(objid);
return EXIT_SUCCESS;
diff --git a/objdb-fs.c b/objdb-fs.c
@@ -461,12 +461,16 @@ objdb_bl_branch_get_head(struct objdb_ctx *ctx, const char *branch_name, char **
int retval = EXIT_SUCCESS;
size_t size = 0;
FILE *head_fp;
+
+ if (ctx == NULL || branch_name == NULL || head_objid == NULL)
+ return EXIT_FAILURE;
db_dir_name = get_objdb_dir(ctx);
asprintf(&branch_head, "%s/branches/%s/head", db_dir_name, branch_name);
if ((head_fp = fopen(branch_head, "r")) == NULL) {
retval = EXIT_FAILURE;
goto ret;
}
+ *head_objid = NULL;
if (getline(head_objid, &size, head_fp) == -1) {
/* assuming the head file is empty */
/* FIXME: check for errors */