t9001: style modernisation phase #4
[gitweb.git] / merge-recursive.c
index d38a3b2eb5bf4b0940a2aa4d5c32f02b9f35cc15..fdb7d0f10ba471bcdff26dc87851d7df34fdc971 100644 (file)
@@ -3,8 +3,9 @@
  * Fredrik Kuivinen.
  * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
  */
-#include "advice.h"
 #include "cache.h"
+#include "advice.h"
+#include "lockfile.h"
 #include "cache-tree.h"
 #include "commit.h"
 #include "blob.h"
@@ -163,15 +164,13 @@ static void output(struct merge_options *o, int v, const char *fmt, ...)
        if (!show(o, v))
                return;
 
-       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);
+       strbuf_addchars(&o->obuf, ' ', o->call_depth * 2);
 
        va_start(ap, fmt);
        strbuf_vaddf(&o->obuf, fmt, ap);
        va_end(ap);
 
-       strbuf_add(&o->obuf, "\n", 1);
+       strbuf_addch(&o->obuf, '\n');
        if (!o->buffer_output)
                flush_output(o);
 }
@@ -267,9 +266,7 @@ struct tree *write_tree_from_memory(struct merge_options *o)
                active_cache_tree = cache_tree();
 
        if (!cache_tree_fully_valid(active_cache_tree) &&
-           cache_tree_update(active_cache_tree,
-                             (const struct cache_entry * const *)active_cache,
-                             active_nr, 0) < 0)
+           cache_tree_update(&the_index, 0) < 0)
                die(_("error building trees"));
 
        result = lookup_tree(active_cache_tree->sha1);
@@ -603,25 +600,36 @@ static int remove_file(struct merge_options *o, int clean,
        return 0;
 }
 
+/* add a string to a strbuf, but converting "/" to "_" */
+static void add_flattened_path(struct strbuf *out, const char *s)
+{
+       size_t i = out->len;
+       strbuf_addstr(out, s);
+       for (; i < out->len; i++)
+               if (out->buf[i] == '/')
+                       out->buf[i] = '_';
+}
+
 static char *unique_path(struct merge_options *o, const char *path, const char *branch)
 {
-       char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1);
+       struct strbuf newpath = STRBUF_INIT;
        int suffix = 0;
        struct stat st;
-       char *p = newpath + strlen(path);
-       strcpy(newpath, path);
-       *(p++) = '~';
-       strcpy(p, branch);
-       for (; *p; ++p)
-               if ('/' == *p)
-                       *p = '_';
-       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(&o->current_file_set, newpath);
-       return newpath;
+       size_t base_len;
+
+       strbuf_addf(&newpath, "%s~", path);
+       add_flattened_path(&newpath, branch);
+
+       base_len = newpath.len;
+       while (string_list_has_string(&o->current_file_set, newpath.buf) ||
+              string_list_has_string(&o->current_directory_set, newpath.buf) ||
+              lstat(newpath.buf, &st) == 0) {
+               strbuf_setlen(&newpath, base_len);
+               strbuf_addf(&newpath, "_%d", suffix++);
+       }
+
+       string_list_insert(&o->current_file_set, newpath.buf);
+       return strbuf_detach(&newpath, NULL);
 }
 
 static int dir_in_way(const char *path, int check_working_copy)
@@ -971,14 +979,10 @@ merge_file_special_markers(struct merge_options *o,
        char *side2 = NULL;
        struct merge_file_info mfi;
 
-       if (filename1) {
-               side1 = xmalloc(strlen(branch1) + strlen(filename1) + 2);
-               sprintf(side1, "%s:%s", branch1, filename1);
-       }
-       if (filename2) {
-               side2 = xmalloc(strlen(branch2) + strlen(filename2) + 2);
-               sprintf(side2, "%s:%s", branch2, filename2);
-       }
+       if (filename1)
+               side1 = xstrfmt("%s:%s", branch1, filename1);
+       if (filename2)
+               side2 = xstrfmt("%s:%s", branch2, filename2);
 
        mfi = merge_file_1(o, one, a, b,
                           side1 ? side1 : branch1, side2 ? side2 : branch2);
@@ -1552,7 +1556,7 @@ static int blob_unchanged(const unsigned char *o_sha,
         * unchanged since their sha1s have already been compared.
         */
        if (renormalize_buffer(path, o.buf, o.len, &o) |
-           renormalize_buffer(path, a.buf, o.len, &a))
+           renormalize_buffer(path, a.buf, a.len, &a))
                ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len));
 
 error_return:
@@ -1683,10 +1687,6 @@ static int merge_content(struct merge_options *o,
 static int process_entry(struct merge_options *o,
                         const char *path, struct stage_data *entry)
 {
-       /*
-       printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
-       print_index_entry("\tpath: ", entry);
-       */
        int clean_merge = 1;
        int normalize = o->renormalize;
        unsigned o_mode = entry->stages[1].mode;
@@ -1994,7 +1994,7 @@ int merge_recursive_generic(struct merge_options *o,
                            const unsigned char **base_list,
                            struct commit **result)
 {
-       int clean, index_fd;
+       int clean;
        struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
        struct commit *head_commit = get_ref(head, o->branch1);
        struct commit *next_commit = get_ref(merge, o->branch2);
@@ -2011,33 +2011,22 @@ int merge_recursive_generic(struct merge_options *o,
                }
        }
 
-       index_fd = hold_locked_index(lock, 1);
+       hold_locked_index(lock, 1);
        clean = merge_recursive(o, head_commit, next_commit, ca,
                        result);
        if (active_cache_changed &&
-                       (write_cache(index_fd, active_cache, active_nr) ||
-                        commit_locked_index(lock)))
+           write_locked_index(&the_index, lock, COMMIT_LOCK))
                return error(_("Unable to write index."));
 
        return clean ? 0 : 1;
 }
 
-static int merge_recursive_config(const char *var, const char *value, void *cb)
+static void merge_recursive_config(struct merge_options *o)
 {
-       struct merge_options *o = cb;
-       if (!strcmp(var, "merge.verbosity")) {
-               o->verbosity = git_config_int(var, value);
-               return 0;
-       }
-       if (!strcmp(var, "diff.renamelimit")) {
-               o->diff_rename_limit = git_config_int(var, value);
-               return 0;
-       }
-       if (!strcmp(var, "merge.renamelimit")) {
-               o->merge_rename_limit = git_config_int(var, value);
-               return 0;
-       }
-       return git_xmerge_config(var, value, cb);
+       git_config_get_int("merge.verbosity", &o->verbosity);
+       git_config_get_int("diff.renamelimit", &o->diff_rename_limit);
+       git_config_get_int("merge.renamelimit", &o->merge_rename_limit);
+       git_config(git_xmerge_config, NULL);
 }
 
 void init_merge_options(struct merge_options *o)
@@ -2048,23 +2037,22 @@ void init_merge_options(struct merge_options *o)
        o->diff_rename_limit = -1;
        o->merge_rename_limit = -1;
        o->renormalize = 0;
-       git_config(merge_recursive_config, o);
+       merge_recursive_config(o);
        if (getenv("GIT_MERGE_VERBOSITY"))
                o->verbosity =
                        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;
-       memset(&o->df_conflict_file_set, 0, sizeof(struct string_list));
-       o->df_conflict_file_set.strdup_strings = 1;
+       string_list_init(&o->current_file_set, 1);
+       string_list_init(&o->current_directory_set, 1);
+       string_list_init(&o->df_conflict_file_set, 1);
 }
 
 int parse_merge_opt(struct merge_options *o, const char *s)
 {
+       const char *arg;
+
        if (!s || !*s)
                return -1;
        if (!strcmp(s, "ours"))
@@ -2073,14 +2061,14 @@ int parse_merge_opt(struct merge_options *o, const char *s)
                o->recursive_variant = MERGE_RECURSIVE_THEIRS;
        else if (!strcmp(s, "subtree"))
                o->subtree_shift = "";
-       else if (starts_with(s, "subtree="))
-               o->subtree_shift = s + strlen("subtree=");
+       else if (skip_prefix(s, "subtree=", &arg))
+               o->subtree_shift = arg;
        else if (!strcmp(s, "patience"))
                o->xdl_opts = DIFF_WITH_ALG(o, PATIENCE_DIFF);
        else if (!strcmp(s, "histogram"))
                o->xdl_opts = DIFF_WITH_ALG(o, HISTOGRAM_DIFF);
-       else if (starts_with(s, "diff-algorithm=")) {
-               long value = parse_algorithm_value(s + strlen("diff-algorithm="));
+       else if (skip_prefix(s, "diff-algorithm=", &arg)) {
+               long value = parse_algorithm_value(arg);
                if (value < 0)
                        return -1;
                /* clear out previous settings */
@@ -2098,9 +2086,8 @@ int parse_merge_opt(struct merge_options *o, const char *s)
                o->renormalize = 1;
        else if (!strcmp(s, "no-renormalize"))
                o->renormalize = 0;
-       else if (starts_with(s, "rename-threshold=")) {
-               const char *score = s + strlen("rename-threshold=");
-               if ((o->rename_score = parse_rename_score(&score)) == -1 || *score != 0)
+       else if (skip_prefix(s, "rename-threshold=", &arg)) {
+               if ((o->rename_score = parse_rename_score(&arg)) == -1 || *arg != 0)
                        return -1;
        }
        else