]> asedeno.scripts.mit.edu Git - git.git/blobdiff - merge-recursive.c
Use xmemdupz() in many places.
[git.git] / merge-recursive.c
index f7d1b84999d401887e4a4eba091d53f5fe7294c8..14b56c2f20651711727169b6d1f28df66bfe775d 100644 (file)
@@ -171,30 +171,6 @@ static void output_commit_title(struct commit *commit)
        }
 }
 
-static struct cache_entry *make_cache_entry(unsigned int mode,
-               const unsigned char *sha1, const char *path, int stage, int refresh)
-{
-       int size, len;
-       struct cache_entry *ce;
-
-       if (!verify_path(path))
-               return NULL;
-
-       len = strlen(path);
-       size = cache_entry_size(len);
-       ce = xcalloc(1, size);
-
-       hashcpy(ce->sha1, sha1);
-       memcpy(ce->name, path, len);
-       ce->ce_flags = create_ce_flags(len, stage);
-       ce->ce_mode = create_ce_mode(mode);
-
-       if (refresh)
-               return refresh_cache_entry(ce, 0);
-
-       return ce;
-}
-
 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
                const char *path, int stage, int refresh, int options)
 {
@@ -456,19 +432,15 @@ static int update_stages(const char *path, struct diff_filespec *o,
 
 static int remove_path(const char *name)
 {
-       int ret, len;
+       int ret;
        char *slash, *dirs;
 
        ret = unlink(name);
        if (ret)
                return ret;
-       len = strlen(name);
-       dirs = xmalloc(len+1);
-       memcpy(dirs, name, len);
-       dirs[len] = '\0';
+       dirs = xstrdup(name);
        while ((slash = strrchr(name, '/'))) {
                *slash = '\0';
-               len = slash - name;
                if (rmdir(name) != 0)
                        break;
        }
@@ -602,9 +574,7 @@ static void update_file_flags(const unsigned char *sha,
                        flush_buffer(fd, buf, size);
                        close(fd);
                } else if (S_ISLNK(mode)) {
-                       char *lnk = xmalloc(size + 1);
-                       memcpy(lnk, buf, size);
-                       lnk[size] = '\0';
+                       char *lnk = xmemdupz(buf, size);
                        mkdir_p(path, 0777);
                        unlink(path);
                        symlink(lnk, path);
@@ -677,6 +647,26 @@ struct ll_merge_driver {
 /*
  * Built-in low-levels
  */
+static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
+                          const char *path_unused,
+                          mmfile_t *orig,
+                          mmfile_t *src1, const char *name1,
+                          mmfile_t *src2, const char *name2,
+                          mmbuffer_t *result)
+{
+       /*
+        * The tentative merge result is "ours" for the final round,
+        * or common ancestor for an internal merge.  Still return
+        * "conflicted merge" status.
+        */
+       mmfile_t *stolen = index_only ? orig : src1;
+
+       result->ptr = stolen->ptr;
+       result->size = stolen->size;
+       stolen->ptr = NULL;
+       return 1;
+}
+
 static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
                        const char *path_unused,
                        mmfile_t *orig,
@@ -687,10 +677,15 @@ static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
        xpparam_t xpp;
 
        if (buffer_is_binary(orig->ptr, orig->size) ||
-                       buffer_is_binary(src1->ptr, src1->size) ||
-                       buffer_is_binary(src2->ptr, src2->size))
-               return error("Cannot merge binary files: %s vs. %s\n",
+           buffer_is_binary(src1->ptr, src1->size) ||
+           buffer_is_binary(src2->ptr, src2->size)) {
+               warning("Cannot merge binary files: %s vs. %s\n",
                        name1, name2);
+               return ll_binary_merge(drv_unused, path_unused,
+                                      orig, src1, name1,
+                                      src2, name2,
+                                      result);
+       }
 
        memset(&xpp, 0, sizeof(xpp));
        return xdl_merge(orig,
@@ -743,26 +738,6 @@ static int ll_union_merge(const struct ll_merge_driver *drv_unused,
        return 0;
 }
 
-static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
-                          const char *path_unused,
-                          mmfile_t *orig,
-                          mmfile_t *src1, const char *name1,
-                          mmfile_t *src2, const char *name2,
-                          mmbuffer_t *result)
-{
-       /*
-        * The tentative merge result is "ours" for the final round,
-        * or common ancestor for an internal merge.  Still return
-        * "conflicted merge" status.
-        */
-       mmfile_t *stolen = index_only ? orig : src1;
-
-       result->ptr = stolen->ptr;
-       result->size = stolen->size;
-       stolen->ptr = NULL;
-       return 1;
-}
-
 #define LL_BINARY_MERGE 0
 #define LL_TEXT_MERGE 1
 #define LL_UNION_MERGE 2
@@ -777,9 +752,7 @@ static void create_temp(mmfile_t *src, char *path)
        int fd;
 
        strcpy(path, ".merge_file_XXXXXX");
-       fd = mkstemp(path);
-       if (fd < 0)
-               die("unable to create temp-file");
+       fd = xmkstemp(path);
        if (write_in_full(fd, src->ptr, src->size) != src->size)
                die("unable to write temp-file");
        close(fd);
@@ -893,14 +866,9 @@ static int read_merge_config(const char *var, const char *value)
                if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
                        break;
        if (!fn) {
-               char *namebuf;
                fn = xcalloc(1, sizeof(struct ll_merge_driver));
-               namebuf = xmalloc(namelen + 1);
-               memcpy(namebuf, name, namelen);
-               namebuf[namelen] = 0;
-               fn->name = namebuf;
+               fn->name = xmemdupz(name, namelen);
                fn->fn = ll_ext_merge;
-               fn->next = NULL;
                *ll_user_merge_tail = fn;
                ll_user_merge_tail = &(fn->next);
        }