baseline

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

helper.c (2670B)


      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 <stdio.h>
     18 #include <stdlib.h>
     19 #include <string.h>
     20 #include <sys/stat.h>
     21 
     22 #include "helper.h"
     23 #include "config.h"
     24 
     25 /* TODO: add support for multiple parents */
     26 struct commit *
     27 baseline_helper_commit_build(struct dircache_ctx *dc_ctx, const char *dir_id, const char *parent_head, const char *msg_file)
     28 {
     29         char *line = NULL;
     30         const char *user_name, *user_email;
     31         size_t size = 0;
     32         struct stat s;
     33         struct commit *comm;
     34         FILE *mfp;
     35 
     36 	comm = baseline_commit_new();
     37         if ((user_name = baseline_config_get_val("username")) == NULL)
     38 		return NULL;
     39         if ((user_email = baseline_config_get_val("useremail")) == NULL)
     40 		return NULL;
     41         if (msg_file == NULL)
     42                 return NULL;
     43         if (stat(msg_file, &s) == -1)
     44                 return NULL;
     45         comm->dir = strdup(dir_id);
     46         comm->author.name = strdup(user_name);
     47         comm->author.email = strdup(user_email);
     48 	if ((comm->author.timestamp = time(NULL)) == (time_t)-1)
     49 		return NULL;
     50         comm->committer.name = strdup(user_name);
     51         comm->committer.email = strdup(user_email);
     52 	if ((comm->committer.timestamp = time(NULL)) == (time_t)-1)
     53 		return NULL;
     54         if (parent_head != NULL) {
     55                 comm->parents[0] = strdup(parent_head);
     56                 comm->n_parents = 1;
     57         }
     58         else {
     59                 comm->n_parents = 0;
     60         }
     61         /* commit message */
     62         if ((mfp = fopen(msg_file, "r")) == NULL)
     63                 return NULL;
     64         /* FIXME: loading the whole message file in memory */
     65 	/* use calloc() so that we can safely call strlcat() afterwards */
     66         if ((comm->message = (char *)calloc(1, s.st_size)) == NULL)
     67                 return NULL;
     68         while (getline(&line, &size, mfp) != -1)
     69                 strlcat(comm->message, line, s.st_size);
     70         fclose(mfp);
     71 	return comm;
     72 }
     73