commit 51b1edc7be9a2071239ca69c89d5f35ad9a790c2
parent a0da5033f6cb2f417a9674fe2850a30ee93a6324
Author: Mohamed Aslan <maslan@sce.carleton.ca>
Date: Thu, 29 May 2014 03:45:39 -0400
by default branches are now created from the head of the current one
Diffstat:
5 files changed, 32 insertions(+), 2 deletions(-)
diff --git a/cmd-branch.c b/cmd-branch.c
@@ -69,18 +69,20 @@ cmd_branch(int argc, char **argv)
switch (op) {
case O_LISTCUR:
+ /* list current branch */
if (s.db_ops->branch_get_head(s.db_ctx, s.branch, &head) == EXIT_FAILURE)
errx(EXIT_FAILURE, "error, failed to find branch head.");
printf("current branch: %s\nhead: %s\n", s.branch, head);
free(head);
break;
case O_LISTALL:
+ /* list all branches */
if (s.db_ops->branch_ls(s.db_ctx) == EXIT_FAILURE)
errx(EXIT_FAILURE, "error, failed to list branches.");
break;
case O_CREATE:
- /* create a new branch */
- if (s.db_ops->branch_create(s.db_ctx, branch) == EXIT_FAILURE)
+ /* 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/cmd-checkout.c b/cmd-checkout.c
@@ -38,6 +38,7 @@ static int
checkout(struct session *s, const char *branch_name) {
int exist;
+ /* TODO: branch existence check should be moved to dircache */
if (s->db_ops->branch_if_exists(s->db_ctx, branch_name, &exist) == EXIT_FAILURE) {
return EXIT_FAILURE;
}
diff --git a/dircache-simple.c b/dircache-simple.c
@@ -187,6 +187,7 @@ simple_init(struct dircache_ctx *dc_ctx)
}
}
/* check if '.baseline/branch' file exists */
+ /* TODO: some of that code should be moved to cmd-init */
if(stat(branch_path, &s) == 0) {
if ((branch_fp = fopen(branch_path, "r")) == NULL) {
retval = EXIT_FAILURE;
diff --git a/objdb-fs.c b/objdb-fs.c
@@ -40,6 +40,7 @@ static int objdb_bl_dir_begin(struct objdb_ctx *, struct objdb_dirhandle **);
static int objdb_bl_dir_insert_object(struct objdb_ctx *, struct objdb_dirhandle *, const char *, const char *, enum objdb_type, mode_t);
static int objdb_bl_dir_end(struct objdb_ctx *, struct objdb_dirhandle *, char **);
static int objdb_bl_branch_create(struct objdb_ctx *, const char *);
+static int objdb_bl_branch_create_from(struct objdb_ctx *, const char *, const char *);
static int objdb_bl_branch_if_exists(struct objdb_ctx *, const char *, int *);
static int objdb_bl_branch_set_head(struct objdb_ctx *, const char *, const char *);
static int objdb_bl_branch_get_head(struct objdb_ctx *, const char *, char **);
@@ -59,6 +60,7 @@ static const struct objdb_ops baseline_objdb_ops = {
.dir_insert_object = objdb_bl_dir_insert_object,
.dir_end = objdb_bl_dir_end,
.branch_create = objdb_bl_branch_create,
+ .branch_create_from = objdb_bl_branch_create_from,
.branch_if_exists = objdb_bl_branch_if_exists,
.branch_set_head = objdb_bl_branch_set_head,
.branch_get_head = objdb_bl_branch_get_head,
@@ -375,6 +377,29 @@ ret:
}
static int
+objdb_bl_branch_create_from(struct objdb_ctx *ctx, const char *new_branch, const char *orig_branch)
+{
+ char *orig_head = NULL;
+ int retval = EXIT_FAILURE;
+
+ if (new_branch == NULL || orig_branch == NULL)
+ return EXIT_FAILURE;
+ /* get the original branch's name */
+ if (objdb_bl_branch_get_head(ctx, orig_branch, &orig_head) == EXIT_FAILURE)
+ goto ret;
+ /* create the new branch */
+ if (objdb_bl_branch_create(ctx, new_branch) == EXIT_FAILURE)
+ goto ret;
+ /* set the new branch's head */
+ if (objdb_bl_branch_set_head(ctx, new_branch, orig_head) == EXIT_FAILURE)
+ goto ret;
+ retval = EXIT_SUCCESS;
+ret:
+ free(orig_head);
+ return retval;
+}
+
+static int
objdb_bl_branch_if_exists(struct objdb_ctx *ctx, const char *branch_name, int *exist)
{
char *db_dir_name = NULL;
diff --git a/objdb.h b/objdb.h
@@ -53,6 +53,7 @@ struct objdb_ops {
int (*dir_end)(struct objdb_ctx *, struct objdb_dirhandle *, char **);
/* branch ops */
int (*branch_create)(struct objdb_ctx *, const char *);
+ int (*branch_create_from)(struct objdb_ctx *, const char *, const char *);
int (*branch_if_exists)(struct objdb_ctx *, const char *, int *);
int (*branch_set_head)(struct objdb_ctx *, const char *, const char *);
int (*branch_get_head)(struct objdb_ctx *, const char *, char **);