Merge branch 'rs/code-cleaning'
authorJunio C Hamano <gitster@pobox.com>
Wed, 16 Jul 2014 18:33:09 +0000 (11:33 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 16 Jul 2014 18:33:09 +0000 (11:33 -0700)
* rs/code-cleaning:
fsck: simplify fsck_commit_buffer() by using commit_list_count()
commit: use commit_list_append() instead of duplicating its code
merge: simplify merge_trivial() by using commit_list_append()
use strbuf_addch for adding single characters
use strbuf_addbuf for adding strbufs

1  2 
builtin/merge.c
commit.c
merge-recursive.c
pretty.c
rerere.c
diff --combined builtin/merge.c
index 22491a1c2b906f4e4ae607cda8925b1254b202b3,f50270efb1fd00c41369ce909e0e40f1d10a285f..ce82eb297db3d03394f4e73a9b07574661db7090
@@@ -657,12 -657,14 +657,12 @@@ static int try_merge_strategy(const cha
                              struct commit_list *remoteheads,
                              struct commit *head, const char *head_arg)
  {
 -      int index_fd;
        struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
  
 -      index_fd = hold_locked_index(lock, 1);
 +      hold_locked_index(lock, 1);
        refresh_cache(REFRESH_QUIET);
        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."));
        rollback_lock_file(lock);
  
                int clean, x;
                struct commit *result;
                struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
 -              int index_fd;
                struct commit_list *reversed = NULL;
                struct merge_options o;
                struct commit_list *j;
                for (j = common; j; j = j->next)
                        commit_list_insert(j->item, &reversed);
  
 -              index_fd = hold_locked_index(lock, 1);
 +              hold_locked_index(lock, 1);
                clean = merge_recursive(&o, head,
                                remoteheads->item, reversed, &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))
                        die (_("unable to write %s"), get_index_file());
                rollback_lock_file(lock);
                return clean ? 0 : 1;
@@@ -839,16 -843,14 +839,14 @@@ static void prepare_to_commit(struct co
  static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
  {
        unsigned char result_tree[20], result_commit[20];
-       struct commit_list *parent = xmalloc(sizeof(*parent));
+       struct commit_list *parents, **pptr = &parents;
  
        write_tree_trivial(result_tree);
        printf(_("Wonderful.\n"));
-       parent->item = head;
-       parent->next = xmalloc(sizeof(*parent->next));
-       parent->next->item = remoteheads->item;
-       parent->next->next = NULL;
+       pptr = commit_list_append(head, pptr);
+       pptr = commit_list_append(remoteheads->item, pptr);
        prepare_to_commit(remoteheads);
-       if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parent,
+       if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parents,
                        result_commit, NULL, sign_commit))
                die(_("failed to write commit object"));
        finish(head, remoteheads, result_commit, "In-index merge");
@@@ -1278,7 -1280,10 +1276,7 @@@ int cmd_merge(int argc, const char **ar
                                printf(_("Commit %s has a good GPG signature by %s\n"),
                                       hex, signature_check.signer);
  
 -                      free(signature_check.gpg_output);
 -                      free(signature_check.gpg_status);
 -                      free(signature_check.signer);
 -                      free(signature_check.key);
 +                      signature_check_clear(&signature_check);
                }
        }
  
diff --combined commit.c
index acb74b55d4ee72ddf626754535a9febeb05ba945,61d2e13f4870a47be3f48b7cebeee8ba4ceaee1b..f43970dca1e11baf3b7690df81e13866f536f8db
+++ b/commit.c
@@@ -447,12 -447,7 +447,7 @@@ struct commit_list *copy_commit_list(st
        struct commit_list *head = NULL;
        struct commit_list **pp = &head;
        while (list) {
-               struct commit_list *new;
-               new = xmalloc(sizeof(struct commit_list));
-               new->item = list->item;
-               new->next = NULL;
-               *pp = new;
-               pp = &new->next;
+               pp = commit_list_append(list->item, pp);
                list = list->next;
        }
        return head;
@@@ -1270,7 -1265,6 +1265,7 @@@ void check_commit_signature(const struc
                                      &gpg_output, &gpg_status);
        if (status && !gpg_output.len)
                goto out;
 +      sigc->payload = strbuf_detach(&payload, NULL);
        sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
        sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
        parse_gpg_output(sigc);
diff --combined merge-recursive.c
index 8e44d7e5f3d72ee71580dc98d5e7d13c4d56779f,fad7b2c2b8f186fb0129dd1167677c5c2f42b326..5814d056ff9fdfad6de5a748806cf307b1e5d388
@@@ -171,7 -171,7 +171,7 @@@ static void output(struct merge_option
        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,7 -267,9 +267,7 @@@ struct tree *write_tree_from_memory(str
                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);
@@@ -1999,7 -2001,7 +1999,7 @@@ int merge_recursive_generic(struct merg
                            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);
                }
        }
  
 -      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;
diff --combined pretty.c
index 14357e233f3174963add310a29bb35c3de229121,6e5493472302dcf452f21c6b6010a3af71a060f6..eb676d6d54b0cf7a0d82fd960ad6dcfbabb84003
+++ b/pretty.c
@@@ -1376,7 -1376,7 +1376,7 @@@ static size_t format_and_pad_commit(str
                case trunc_none:
                        break;
                }
-               strbuf_addstr(sb, local_sb.buf);
+               strbuf_addbuf(sb, &local_sb);
        } else {
                int sb_len = sb->len, offset = 0;
                if (c->flush_type == flush_left)
@@@ -1520,6 -1520,8 +1520,6 @@@ void format_commit_message(const struc
  
        free(context.commit_encoding);
        unuse_commit_buffer(commit, context.message);
 -      free(context.signature_check.gpg_output);
 -      free(context.signature_check.signer);
  }
  
  static void pp_header(struct pretty_print_context *pp,
diff --combined rerere.c
index ffc6a5b168304726b5fe326211f26dafa460c7fa,04d923d8cf59d591f133d31d3641cc6cf070605c..d84b495895bc6d42980c388ed03c560174604f92
+++ b/rerere.c
@@@ -207,11 -207,11 +207,11 @@@ static int handle_path(unsigned char *s
                        strbuf_reset(&one);
                        strbuf_reset(&two);
                } else if (hunk == RR_SIDE_1)
-                       strbuf_addstr(&one, buf.buf);
+                       strbuf_addbuf(&one, &buf);
                else if (hunk == RR_ORIGINAL)
                        ; /* discard */
                else if (hunk == RR_SIDE_2)
-                       strbuf_addstr(&two, buf.buf);
+                       strbuf_addbuf(&two, &buf);
                else
                        rerere_io_putstr(buf.buf, io);
                continue;
@@@ -492,7 -492,8 +492,7 @@@ static int update_paths(struct string_l
        }
  
        if (!status && active_cache_changed) {
 -              if (write_cache(fd, active_cache, active_nr) ||
 -                  commit_locked_index(&index_lock))
 +              if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
                        die("Unable to write new index file");
        } else if (fd >= 0)
                rollback_lock_file(&index_lock);