baseline

yet another open-source distributed versioning control system
Log | Files | Refs

cmd-branch.c (3086B)


      1 /*
      2  * Copyright (c) 2014 Mohamed Aslan <maslan@sce.carleton.ca>
      3  *
      4  * Permission to use, copy, modify, and distribute this software for any
      5  * purpose with or without fee is hereby granted, provided that the above
      6  * copyright notice and this permission notice appear in all copies.
      7  *
      8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     15  */
     16 
     17 #include <sys/types.h>
     18 #include <sys/stat.h>
     19 #include <stdio.h>
     20 #include <stdlib.h> /* EXIT_FAILURE */
     21 #include <string.h> /* strstr(3) */
     22 #include <limits.h> /* PATH_MAX */
     23 #include <fcntl.h>  /* open(2) */
     24 #include <unistd.h> /* getcwd(3) */
     25 #include <err.h>    /* errx(3) */
     26 
     27 #include "defaults.h"
     28 #include "config.h"
     29 #include "session.h"
     30 #include "cmd.h"
     31 
     32 
     33 int
     34 cmd_branch(int argc, char **argv)
     35 {
     36 	char *branch = NULL;
     37 	char *head = NULL;
     38 	enum {
     39 		O_LISTCUR,
     40 		O_LISTALL,
     41 		O_CREATE,
     42 		O_SWITCH
     43 	} op = O_LISTCUR;
     44 	int ch, error = 0, exist = 0;
     45 	struct session s;
     46 
     47 	baseline_session_begin(&s, 0);
     48 
     49 	/* parse command line options */
     50 	while ((ch = getopt(argc, argv, "c:ls:")) != -1) {
     51 		switch (ch) {
     52 		case 'c':
     53 			op = O_CREATE;
     54 			branch = strdup(optarg);
     55 			break;
     56 		case 'l':
     57 			op = O_LISTALL;
     58 			break;
     59 		case 's':
     60 			op = O_SWITCH;
     61 			branch = strdup(optarg);
     62 			break;
     63 		default:
     64 			error = 1;
     65 			break;
     66 		}
     67 	}
     68 	argc -= optind;
     69 	argv += optind;
     70 
     71 	if (error) {
     72 		return EXIT_FAILURE;
     73 	}
     74 
     75 	switch (op) {
     76 	case O_CREATE:
     77 		/* get current branch's head */
     78 		if (s.db_ops->branch_get_head(s.db_ctx, s.branch, &head) == EXIT_FAILURE)
     79 			errx(EXIT_FAILURE, "error, failed to find branch head.");
     80 		if (head == NULL)
     81 			errx(EXIT_FAILURE, "error, branch \'%s\' does not contain any commits.", s.branch);
     82 		/* create a new branch from the current one */
     83 		if (s.db_ops->branch_create_from(s.db_ctx, branch, s.branch) == EXIT_FAILURE)
     84 			errx(EXIT_FAILURE, "error, failed to create branch \'%s\'.", branch);
     85 		break;
     86 	case O_LISTALL:
     87 		/* list all branches */
     88 		if (s.db_ops->branch_ls(s.db_ctx) == EXIT_FAILURE)
     89 			errx(EXIT_FAILURE, "error, failed to list branches.");
     90 		break;
     91 	case O_LISTCUR:
     92 		/* list current branch */
     93 		if (s.db_ops->branch_get_head(s.db_ctx, s.branch, &head) == EXIT_FAILURE)
     94 			errx(EXIT_FAILURE, "error, failed to find branch head.");
     95 		printf("current branch: %s\nhead: %s\n", s.branch, head);
     96 		free(head);
     97 		break;
     98 	case O_SWITCH:
     99 		if (s.db_ops->branch_if_exists(s.db_ctx, branch, &exist) == EXIT_FAILURE)
    100 			errx(EXIT_FAILURE, "error, failed to query branch.");
    101 		if (exist)
    102 			s.dc_ops->branch_set(s.dc_ctx, branch);
    103 		else
    104 			errx(EXIT_FAILURE, "branch \'%s\' does not exist.", branch);
    105 		break;
    106 	}
    107 
    108 	baseline_session_end(&s);
    109 	return EXIT_SUCCESS;
    110 }