grep: break down an "if" stmt in preparation for next changes
[gitweb.git] / merge-recursive.c
index a1ee9b7e24eda27ee16f1fe273840fc4c502f2f3..8eabde20fbe030a1242cf934d6e2e4cdb6ac14e7 100644 (file)
 static struct tree *shift_tree_object(struct tree *one, struct tree *two,
                                      const char *subtree_shift)
 {
-       unsigned char shifted[20];
+       struct object_id shifted;
 
        if (!*subtree_shift) {
-               shift_tree(one->object.sha1, two->object.sha1, shifted, 0);
+               shift_tree(one->object.oid.hash, two->object.oid.hash, shifted.hash, 0);
        } else {
-               shift_tree_by(one->object.sha1, two->object.sha1, shifted,
+               shift_tree_by(one->object.oid.hash, two->object.oid.hash, shifted.hash,
                              subtree_shift);
        }
-       if (!hashcmp(two->object.sha1, shifted))
+       if (!oidcmp(&two->object.oid, &shifted))
                return two;
-       return lookup_tree(shifted);
+       return lookup_tree(shifted.hash);
 }
 
 static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
@@ -184,7 +184,7 @@ static void output_commit_title(struct merge_options *o, struct commit *commit)
        if (commit->util)
                printf("virtual %s\n", merge_remote_util(commit)->name);
        else {
-               printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
+               printf("%s ", find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV));
                if (parse_commit(commit) != 0)
                        printf(_("(bad commit)\n"));
                else {
@@ -275,23 +275,20 @@ struct tree *write_tree_from_memory(struct merge_options *o)
 }
 
 static int save_files_dirs(const unsigned char *sha1,
-               const char *base, int baselen, const char *path,
+               struct strbuf *base, const char *path,
                unsigned int mode, int stage, void *context)
 {
-       int len = strlen(path);
-       char *newpath = xmalloc(baselen + len + 1);
+       int baselen = base->len;
        struct merge_options *o = context;
 
-       memcpy(newpath, base, baselen);
-       memcpy(newpath + baselen, path, len);
-       newpath[baselen + len] = '\0';
+       strbuf_addstr(base, path);
 
        if (S_ISDIR(mode))
-               string_list_insert(&o->current_directory_set, newpath);
+               string_list_insert(&o->current_directory_set, base->buf);
        else
-               string_list_insert(&o->current_file_set, newpath);
-       free(newpath);
+               string_list_insert(&o->current_file_set, base->buf);
 
+       strbuf_setlen(base, baselen);
        return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
 }
 
@@ -316,11 +313,11 @@ static struct stage_data *insert_stage_data(const char *path,
 {
        struct string_list_item *item;
        struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
-       get_tree_entry(o->object.sha1, path,
+       get_tree_entry(o->object.oid.hash, path,
                        e->stages[1].sha, &e->stages[1].mode);
-       get_tree_entry(a->object.sha1, path,
+       get_tree_entry(a->object.oid.hash, path,
                        e->stages[2].sha, &e->stages[2].mode);
-       get_tree_entry(b->object.sha1, path,
+       get_tree_entry(b->object.oid.hash, path,
                        e->stages[3].sha, &e->stages[3].mode);
        item = string_list_insert(entries, path);
        item->util = e;
@@ -496,7 +493,7 @@ static struct string_list *get_renames(struct merge_options *o,
        opts.show_rename_progress = o->show_rename_progress;
        opts.output_format = DIFF_FORMAT_NO_OUTPUT;
        diff_setup_done(&opts);
-       diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts);
+       diff_tree_sha1(o_tree->object.oid.hash, tree->object.oid.hash, "", &opts);
        diffcore_std(&opts);
        if (opts.needed_rename_limit > o->needed_rename_limit)
                o->needed_rename_limit = opts.needed_rename_limit;
@@ -614,7 +611,6 @@ static char *unique_path(struct merge_options *o, const char *path, const char *
 {
        struct strbuf newpath = STRBUF_INIT;
        int suffix = 0;
-       struct stat st;
        size_t base_len;
 
        strbuf_addf(&newpath, "%s~", path);
@@ -623,7 +619,7 @@ static char *unique_path(struct merge_options *o, const char *path, const char *
        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) {
+              file_exists(newpath.buf)) {
                strbuf_setlen(&newpath, base_len);
                strbuf_addf(&newpath, "_%d", suffix++);
        }
@@ -634,25 +630,24 @@ static char *unique_path(struct merge_options *o, const char *path, const char *
 
 static int dir_in_way(const char *path, int check_working_copy)
 {
-       int pos, pathlen = strlen(path);
-       char *dirpath = xmalloc(pathlen + 2);
+       int pos;
+       struct strbuf dirpath = STRBUF_INIT;
        struct stat st;
 
-       strcpy(dirpath, path);
-       dirpath[pathlen] = '/';
-       dirpath[pathlen+1] = '\0';
+       strbuf_addstr(&dirpath, path);
+       strbuf_addch(&dirpath, '/');
 
-       pos = cache_name_pos(dirpath, pathlen+1);
+       pos = cache_name_pos(dirpath.buf, dirpath.len);
 
        if (pos < 0)
                pos = -1 - pos;
        if (pos < active_nr &&
-           !strncmp(dirpath, active_cache[pos]->name, pathlen+1)) {
-               free(dirpath);
+           !strncmp(dirpath.buf, active_cache[pos]->name, dirpath.len)) {
+               strbuf_release(&dirpath);
                return 1;
        }
 
-       free(dirpath);
+       strbuf_release(&dirpath);
        return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode);
 }
 
@@ -1817,7 +1812,7 @@ int merge_trees(struct merge_options *o,
                common = shift_tree_object(head, common, o->subtree_shift);
        }
 
-       if (sha_eq(common->object.sha1, merge->object.sha1)) {
+       if (sha_eq(common->object.oid.hash, merge->object.oid.hash)) {
                output(o, 0, _("Already up-to-date!"));
                *result = head;
                return 1;
@@ -1828,8 +1823,8 @@ int merge_trees(struct merge_options *o,
        if (code != 0) {
                if (show(o, 4) || o->call_depth)
                        die(_("merging of trees %s and %s failed"),
-                           sha1_to_hex(head->object.sha1),
-                           sha1_to_hex(merge->object.sha1));
+                           oid_to_hex(&head->object.oid),
+                           oid_to_hex(&merge->object.oid));
                else
                        exit(128);
        }
@@ -1865,6 +1860,9 @@ int merge_trees(struct merge_options *o,
                string_list_clear(re_head, 0);
                string_list_clear(entries, 1);
 
+               free(re_merge);
+               free(re_head);
+               free(entries);
        }
        else
                clean = 1;
@@ -1908,7 +1906,7 @@ int merge_recursive(struct merge_options *o,
        }
 
        if (!ca) {
-               ca = get_merge_bases(h1, h2, 1);
+               ca = get_merge_bases(h1, h2);
                ca = reverse_commit_list(ca);
        }