diff: allow turning on textconv explicitly for plumbing
[gitweb.git] / merge-recursive.c
index c426589d1172ddc6b693e117f9b175ad0e861425..a0c804c8171d021780680f11901680d4c7e89e0b 100644 (file)
@@ -16,9 +16,9 @@
 #include "string-list.h"
 #include "xdiff-interface.h"
 #include "ll-merge.h"
-#include "interpolate.h"
 #include "attr.h"
 #include "merge-recursive.h"
+#include "dir.h"
 
 static struct tree *shift_tree_object(struct tree *one, struct tree *two)
 {
@@ -35,18 +35,14 @@ static struct tree *shift_tree_object(struct tree *one, struct tree *two)
 }
 
 /*
- * A virtual commit has
- * - (const char *)commit->util set to the name, and
- * - *(int *)commit->object.sha1 set to the virtual id.
+ * A virtual commit has (const char *)commit->util set to the name.
  */
 
 struct commit *make_virtual_commit(struct tree *tree, const char *comment)
 {
        struct commit *commit = xcalloc(1, sizeof(struct commit));
-       static unsigned virtual_id = 1;
        commit->tree = tree;
        commit->util = (void*)comment;
-       *(int*)commit->object.sha1 = virtual_id++;
        /* avoid warnings */
        commit->object.parsed = 1;
        return commit;
@@ -77,21 +73,16 @@ struct stage_data
        unsigned processed:1;
 };
 
-static struct string_list current_file_set = {NULL, 0, 0, 1};
-static struct string_list current_directory_set = {NULL, 0, 0, 1};
-
-static struct strbuf obuf = STRBUF_INIT;
-
 static int show(struct merge_options *o, int v)
 {
        return (!o->call_depth && o->verbosity >= v) || o->verbosity >= 5;
 }
 
-static void flush_output(void)
+static void flush_output(struct merge_options *o)
 {
-       if (obuf.len) {
-               fputs(obuf.buf, stdout);
-               strbuf_reset(&obuf);
+       if (o->obuf.len) {
+               fputs(o->obuf.buf, stdout);
+               strbuf_reset(&o->obuf);
        }
 }
 
@@ -103,35 +94,35 @@ static void output(struct merge_options *o, int v, const char *fmt, ...)
        if (!show(o, v))
                return;
 
-       strbuf_grow(&obuf, o->call_depth * 2 + 2);
-       memset(obuf.buf + obuf.len, ' ', o->call_depth * 2);
-       strbuf_setlen(&obuf, obuf.len + o->call_depth * 2);
+       strbuf_grow(&o->obuf, o->call_depth * 2 + 2);
+       memset(o->obuf.buf + o->obuf.len, ' ', o->call_depth * 2);
+       strbuf_setlen(&o->obuf, o->obuf.len + o->call_depth * 2);
 
        va_start(ap, fmt);
-       len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap);
+       len = vsnprintf(o->obuf.buf + o->obuf.len, strbuf_avail(&o->obuf), fmt, ap);
        va_end(ap);
 
        if (len < 0)
                len = 0;
-       if (len >= strbuf_avail(&obuf)) {
-               strbuf_grow(&obuf, len + 2);
+       if (len >= strbuf_avail(&o->obuf)) {
+               strbuf_grow(&o->obuf, len + 2);
                va_start(ap, fmt);
-               len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap);
+               len = vsnprintf(o->obuf.buf + o->obuf.len, strbuf_avail(&o->obuf), fmt, ap);
                va_end(ap);
-               if (len >= strbuf_avail(&obuf)) {
+               if (len >= strbuf_avail(&o->obuf)) {
                        die("this should not happen, your snprintf is broken");
                }
        }
-       strbuf_setlen(&obuf, obuf.len + len);
-       strbuf_add(&obuf, "\n", 1);
+       strbuf_setlen(&o->obuf, o->obuf.len + len);
+       strbuf_add(&o->obuf, "\n", 1);
        if (!o->buffer_output)
-               flush_output();
+               flush_output(o);
 }
 
 static void output_commit_title(struct merge_options *o, struct commit *commit)
 {
        int i;
-       flush_output();
+       flush_output(o);
        for (i = o->call_depth; i--;)
                fputs("  ", stdout);
        if (commit->util)
@@ -234,25 +225,27 @@ static int save_files_dirs(const unsigned char *sha1,
 {
        int len = strlen(path);
        char *newpath = xmalloc(baselen + len + 1);
+       struct merge_options *o = context;
+
        memcpy(newpath, base, baselen);
        memcpy(newpath + baselen, path, len);
        newpath[baselen + len] = '\0';
 
        if (S_ISDIR(mode))
-               string_list_insert(newpath, &current_directory_set);
+               string_list_insert(newpath, &o->current_directory_set);
        else
-               string_list_insert(newpath, &current_file_set);
+               string_list_insert(newpath, &o->current_file_set);
        free(newpath);
 
        return READ_TREE_RECURSIVE;
 }
 
-static int get_files_dirs(struct tree *tree)
+static int get_files_dirs(struct merge_options *o, struct tree *tree)
 {
        int n;
-       if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs, NULL))
+       if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs, o))
                return 0;
-       n = current_file_set.nr + current_directory_set.nr;
+       n = o->current_file_set.nr + o->current_directory_set.nr;
        return n;
 }
 
@@ -399,24 +392,6 @@ static int update_stages(const char *path, struct diff_filespec *o,
        return 0;
 }
 
-static int remove_path(const char *name)
-{
-       int ret;
-       char *slash, *dirs;
-
-       ret = unlink(name);
-       if (ret)
-               return ret;
-       dirs = xstrdup(name);
-       while ((slash = strrchr(name, '/'))) {
-               *slash = '\0';
-               if (rmdir(name) != 0)
-                       break;
-       }
-       free(dirs);
-       return ret;
-}
-
 static int remove_file(struct merge_options *o, int clean,
                       const char *path, int no_wd)
 {
@@ -428,15 +403,13 @@ static int remove_file(struct merge_options *o, int clean,
                        return -1;
        }
        if (update_working_directory) {
-               unlink(path);
-               if (errno != ENOENT || errno != EISDIR)
+               if (remove_path(path) && errno != ENOENT)
                        return -1;
-               remove_path(path);
        }
        return 0;
 }
 
-static char *unique_path(const char *path, const char *branch)
+static char *unique_path(struct merge_options *o, const char *path, const char *branch)
 {
        char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1);
        int suffix = 0;
@@ -448,12 +421,12 @@ static char *unique_path(const char *path, const char *branch)
        for (; *p; ++p)
                if ('/' == *p)
                        *p = '_';
-       while (string_list_has_string(&current_file_set, newpath) ||
-              string_list_has_string(&current_directory_set, newpath) ||
+       while (string_list_has_string(&o->current_file_set, newpath) ||
+              string_list_has_string(&o->current_directory_set, newpath) ||
               lstat(newpath, &st) == 0)
                sprintf(p, "_%d", suffix++);
 
-       string_list_insert(newpath, &current_file_set);
+       string_list_insert(newpath, &o->current_file_set);
        return newpath;
 }
 
@@ -524,8 +497,7 @@ static void update_file_flags(struct merge_options *o,
                if (type != OBJ_BLOB)
                        die("blob expected for %s '%s'", sha1_to_hex(sha), path);
                if (S_ISREG(mode)) {
-                       struct strbuf strbuf;
-                       strbuf_init(&strbuf, 0);
+                       struct strbuf strbuf = STRBUF_INIT;
                        if (convert_to_working_tree(path, buf, size, &strbuf)) {
                                free(buf);
                                size = strbuf.len;
@@ -553,7 +525,8 @@ static void update_file_flags(struct merge_options *o,
                        char *lnk = xmemdupz(buf, size);
                        safe_create_leading_directories_const(path);
                        unlink(path);
-                       symlink(lnk, path);
+                       if (symlink(lnk, path))
+                               die("failed to symlink %s: %s", path, strerror(errno));
                        free(lnk);
                } else
                        die("do not know what to do with %06o %s '%s'",
@@ -718,14 +691,14 @@ static void conflict_rename_rename(struct merge_options *o,
        const char *ren2_dst = ren2->pair->two->path;
        const char *dst_name1 = ren1_dst;
        const char *dst_name2 = ren2_dst;
-       if (string_list_has_string(&current_directory_set, ren1_dst)) {
-               dst_name1 = del[delp++] = unique_path(ren1_dst, branch1);
+       if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
+               dst_name1 = del[delp++] = unique_path(o, ren1_dst, branch1);
                output(o, 1, "%s is a directory in %s adding as %s instead",
                       ren1_dst, branch2, dst_name1);
                remove_file(o, 0, ren1_dst, 0);
        }
-       if (string_list_has_string(&current_directory_set, ren2_dst)) {
-               dst_name2 = del[delp++] = unique_path(ren2_dst, branch2);
+       if (string_list_has_string(&o->current_directory_set, ren2_dst)) {
+               dst_name2 = del[delp++] = unique_path(o, ren2_dst, branch2);
                output(o, 1, "%s is a directory in %s adding as %s instead",
                       ren2_dst, branch1, dst_name2);
                remove_file(o, 0, ren2_dst, 0);
@@ -751,7 +724,7 @@ static void conflict_rename_dir(struct merge_options *o,
                                struct rename *ren1,
                                const char *branch1)
 {
-       char *new_path = unique_path(ren1->pair->two->path, branch1);
+       char *new_path = unique_path(o, ren1->pair->two->path, branch1);
        output(o, 1, "Renaming %s to %s instead", ren1->pair->one->path, new_path);
        remove_file(o, 0, ren1->pair->two->path, 0);
        update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path);
@@ -764,8 +737,8 @@ static void conflict_rename_rename_2(struct merge_options *o,
                                     struct rename *ren2,
                                     const char *branch2)
 {
-       char *new_path1 = unique_path(ren1->pair->two->path, branch1);
-       char *new_path2 = unique_path(ren2->pair->two->path, branch2);
+       char *new_path1 = unique_path(o, ren1->pair->two->path, branch1);
+       char *new_path2 = unique_path(o, ren2->pair->two->path, branch2);
        output(o, 1, "Renaming %s to %s and %s to %s instead",
               ren1->pair->one->path, new_path1,
               ren2->pair->one->path, new_path2);
@@ -915,7 +888,7 @@ static int process_renames(struct merge_options *o,
 
                        try_merge = 0;
 
-                       if (string_list_has_string(&current_directory_set, ren1_dst)) {
+                       if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
                                clean_merge = 0;
                                output(o, 1, "CONFLICT (rename/directory): Rename %s->%s in %s "
                                       " directory %s added in %s",
@@ -937,7 +910,7 @@ static int process_renames(struct merge_options *o,
                                       "%s added in %s",
                                       ren1_src, ren1_dst, branch1,
                                       ren1_dst, branch2);
-                               new_path = unique_path(ren1_dst, branch2);
+                               new_path = unique_path(o, ren1_dst, branch2);
                                output(o, 1, "Adding as %s instead", new_path);
                                update_file(o, 0, dst_other.sha1, dst_other.mode, new_path);
                        } else if ((item = string_list_lookup(ren1_dst, renames2Dst))) {
@@ -1075,8 +1048,8 @@ static int process_entry(struct merge_options *o,
                        sha = b_sha;
                        conf = "directory/file";
                }
-               if (string_list_has_string(&current_directory_set, path)) {
-                       const char *new_path = unique_path(path, add_branch);
+               if (string_list_has_string(&o->current_directory_set, path)) {
+                       const char *new_path = unique_path(o, path, add_branch);
                        clean_merge = 0;
                        output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. "
                               "Adding %s as %s",
@@ -1167,10 +1140,10 @@ int merge_trees(struct merge_options *o,
        if (unmerged_cache()) {
                struct string_list *entries, *re_head, *re_merge;
                int i;
-               string_list_clear(&current_file_set, 1);
-               string_list_clear(&current_directory_set, 1);
-               get_files_dirs(head);
-               get_files_dirs(merge);
+               string_list_clear(&o->current_file_set, 1);
+               string_list_clear(&o->current_directory_set, 1);
+               get_files_dirs(o, head);
+               get_files_dirs(o, merge);
 
                entries = get_unmerged();
                re_head  = get_renames(o, head, common, head, merge, entries);
@@ -1289,7 +1262,7 @@ int merge_recursive(struct merge_options *o,
                commit_list_insert(h1, &(*result)->parents);
                commit_list_insert(h2, &(*result)->parents->next);
        }
-       flush_output();
+       flush_output(o);
        return clean;
 }
 
@@ -1359,7 +1332,7 @@ static int merge_recursive_config(const char *var, const char *value, void *cb)
                o->merge_rename_limit = git_config_int(var, value);
                return 0;
        }
-       return git_default_config(var, value, cb);
+       return git_xmerge_config(var, value, cb);
 }
 
 void init_merge_options(struct merge_options *o)
@@ -1375,4 +1348,9 @@ void init_merge_options(struct merge_options *o)
                        strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
        if (o->verbosity >= 5)
                o->buffer_output = 0;
+       strbuf_init(&o->obuf, 0);
+       memset(&o->current_file_set, 0, sizeof(struct string_list));
+       o->current_file_set.strdup_strings = 1;
+       memset(&o->current_directory_set, 0, sizeof(struct string_list));
+       o->current_directory_set.strdup_strings = 1;
 }