connect.c: stricter port validation, silence compiler warning
[gitweb.git] / builtin-merge.c
index 5c65a5869900ad1a1014fb3aad88c874a5410bf7..cf869751b41c256363bf5f0c465e46684e2920b8 100644 (file)
@@ -50,6 +50,7 @@ static unsigned char head[20], stash[20];
 static struct strategy **use_strategies;
 static size_t use_strategies_nr, use_strategies_alloc;
 static const char *branch;
+static int verbosity;
 
 static struct strategy all_strategy[] = {
        { "recursive",  DEFAULT_TWOHEAD | NO_TRIVIAL },
@@ -123,8 +124,7 @@ static struct strategy *get_strategy(const char *name)
                exit(1);
        }
 
-       ret = xmalloc(sizeof(struct strategy));
-       memset(ret, 0, sizeof(struct strategy));
+       ret = xcalloc(1, sizeof(struct strategy));
        ret->name = xstrdup(name);
        return ret;
 }
@@ -172,6 +172,7 @@ static struct option builtin_merge_options[] = {
        OPT_CALLBACK('m', "message", &merge_msg, "message",
                "message to be used for the merge commit (if any)",
                option_parse_message),
+       OPT__VERBOSITY(&verbosity),
        OPT_END()
 };
 
@@ -180,6 +181,7 @@ static void drop_save(void)
 {
        unlink(git_path("MERGE_HEAD"));
        unlink(git_path("MERGE_MSG"));
+       unlink(git_path("MERGE_MODE"));
 }
 
 static void save_state(void)
@@ -227,7 +229,7 @@ static void reset_hard(unsigned const char *sha1, int verbose)
 
 static void restore_state(void)
 {
-       struct strbuf sb;
+       struct strbuf sb = STRBUF_INIT;
        const char *args[] = { "stash", "apply", NULL, NULL };
 
        if (is_null_sha1(stash))
@@ -235,7 +237,6 @@ static void restore_state(void)
 
        reset_hard(head, 1);
 
-       strbuf_init(&sb, 0);
        args[2] = sha1_to_hex(stash);
 
        /*
@@ -251,7 +252,8 @@ static void restore_state(void)
 /* This is called when no merge was necessary. */
 static void finish_up_to_date(const char *msg)
 {
-       printf("%s%s\n", squash ? " (nothing to squash)" : "", msg);
+       if (verbosity >= 0)
+               printf("%s%s\n", squash ? " (nothing to squash)" : "", msg);
        drop_save();
 }
 
@@ -259,7 +261,7 @@ static void squash_message(void)
 {
        struct rev_info rev;
        struct commit *commit;
-       struct strbuf out;
+       struct strbuf out = STRBUF_INIT;
        struct commit_list *j;
        int fd;
 
@@ -283,7 +285,6 @@ static void squash_message(void)
        if (prepare_revision_walk(&rev))
                die("revision walk setup failed");
 
-       strbuf_init(&out, 0);
        strbuf_addstr(&out, "Squashed commit of the following:\n");
        while ((commit = get_revision(&rev)) != NULL) {
                strbuf_addch(&out, '\n');
@@ -292,8 +293,10 @@ static void squash_message(void)
                pretty_print_commit(rev.commit_format, commit, &out, rev.abbrev,
                        NULL, NULL, rev.date_mode, 0);
        }
-       write(fd, out.buf, out.len);
-       close(fd);
+       if (write(fd, out.buf, out.len) < 0)
+               die("Writing SQUASH_MSG: %s", strerror(errno));
+       if (close(fd))
+               die("Finishing SQUASH_MSG: %s", strerror(errno));
        strbuf_release(&out);
 }
 
@@ -328,20 +331,20 @@ static int run_hook(const char *name)
 
 static void finish(const unsigned char *new_head, const char *msg)
 {
-       struct strbuf reflog_message;
+       struct strbuf reflog_message = STRBUF_INIT;
 
-       strbuf_init(&reflog_message, 0);
        if (!msg)
                strbuf_addstr(&reflog_message, getenv("GIT_REFLOG_ACTION"));
        else {
-               printf("%s\n", msg);
+               if (verbosity >= 0)
+                       printf("%s\n", msg);
                strbuf_addf(&reflog_message, "%s: %s",
                        getenv("GIT_REFLOG_ACTION"), msg);
        }
        if (squash) {
                squash_message();
        } else {
-               if (!merge_msg.len)
+               if (verbosity >= 0 && !merge_msg.len)
                        printf("No merge message -- not updating HEAD\n");
                else {
                        const char *argv_gc_auto[] = { "gc", "--auto", NULL };
@@ -381,7 +384,7 @@ static void merge_name(const char *remote, struct strbuf *msg)
 {
        struct object *remote_head;
        unsigned char branch_head[20], buf_sha[20];
-       struct strbuf buf;
+       struct strbuf buf = STRBUF_INIT;
        const char *ptr;
        int len, early;
 
@@ -390,7 +393,6 @@ static void merge_name(const char *remote, struct strbuf *msg)
        if (!remote_head)
                die("'%s' does not point to a commit", remote);
 
-       strbuf_init(&buf, 0);
        strbuf_addstr(&buf, "refs/heads/");
        strbuf_addstr(&buf, remote);
        resolve_ref(buf.buf, branch_head, 0, 0);
@@ -445,10 +447,9 @@ static void merge_name(const char *remote, struct strbuf *msg)
        if (!strcmp(remote, "FETCH_HEAD") &&
                        !access(git_path("FETCH_HEAD"), R_OK)) {
                FILE *fp;
-               struct strbuf line;
+               struct strbuf line = STRBUF_INIT;
                char *ptr;
 
-               strbuf_init(&line, 0);
                fp = fopen(git_path("FETCH_HEAD"), "r");
                if (!fp)
                        die("could not open %s for reading: %s",
@@ -546,7 +547,17 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
        const char **args;
        int i = 0, ret;
        struct commit_list *j;
-       struct strbuf buf;
+       struct strbuf buf = STRBUF_INIT;
+       int index_fd;
+       struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
+
+       index_fd = 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)))
+               return error("Unable to write index.");
+       rollback_lock_file(lock);
 
        if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
                int clean;
@@ -583,7 +594,6 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
        } else {
                args = xmalloc((4 + commit_list_count(common) +
                                        commit_list_count(remoteheads)) * sizeof(char *));
-               strbuf_init(&buf, 0);
                strbuf_addf(&buf, "merge-%s", strategy);
                args[i++] = buf.buf;
                for (j = common; j; j = j->next)
@@ -723,12 +733,12 @@ static void add_strategies(const char *string, unsigned attr)
 static int merge_trivial(void)
 {
        unsigned char result_tree[20], result_commit[20];
-       struct commit_list *parent = xmalloc(sizeof(struct commit_list *));
+       struct commit_list *parent = xmalloc(sizeof(*parent));
 
        write_tree_trivial(result_tree);
        printf("Wonderful.\n");
        parent->item = lookup_commit(head);
-       parent->next = xmalloc(sizeof(struct commit_list *));
+       parent->next = xmalloc(sizeof(*parent->next));
        parent->next->item = remoteheads->item;
        parent->next->next = NULL;
        commit_tree(merge_msg.buf, result_tree, parent, result_commit, NULL);
@@ -838,7 +848,7 @@ static int evaluate_result(void)
 int cmd_merge(int argc, const char **argv, const char *prefix)
 {
        unsigned char result_tree[20];
-       struct strbuf buf;
+       struct strbuf buf = STRBUF_INIT;
        const char *head_arg;
        int flag, head_invalid = 0, i;
        int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
@@ -868,6 +878,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 
        argc = parse_options(argc, argv, builtin_merge_options,
                        builtin_merge_usage, 0);
+       if (verbosity < 0)
+               show_diffstat = 0;
 
        if (squash) {
                if (!allow_fast_forward)
@@ -887,7 +899,6 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
         * Traditional format never would have "-m" so it is an
         * additional safety measure to check for it.
         */
-       strbuf_init(&buf, 0);
 
        if (!have_message && is_old_style_invocation(argc, argv)) {
                strbuf_addstr(&merge_msg, argv[0]);
@@ -917,7 +928,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
                reset_hard(remote_head->sha1, 0);
                return 0;
        } else {
-               struct strbuf msg;
+               struct strbuf msg = STRBUF_INIT;
 
                /* We are invoked directly as the first-class UI. */
                head_arg = "HEAD";
@@ -930,7 +941,6 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
                 * codepath so we discard the error in this
                 * loop.
                 */
-               strbuf_init(&msg, 0);
                for (i = 0; i < argc; i++)
                        merge_name(argv[i], &msg);
                fmt_merge_msg(option_log, &msg, &merge_msg);
@@ -1005,17 +1015,17 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
                        !common->next &&
                        !hashcmp(common->item->object.sha1, head)) {
                /* Again the most common case of merging one remote. */
-               struct strbuf msg;
+               struct strbuf msg = STRBUF_INIT;
                struct object *o;
                char hex[41];
 
                strcpy(hex, find_unique_abbrev(head, DEFAULT_ABBREV));
 
-               printf("Updating %s..%s\n",
-                       hex,
-                       find_unique_abbrev(remoteheads->item->object.sha1,
-                       DEFAULT_ABBREV));
-               strbuf_init(&msg, 0);
+               if (verbosity >= 0)
+                       printf("Updating %s..%s\n",
+                               hex,
+                               find_unique_abbrev(remoteheads->item->object.sha1,
+                               DEFAULT_ABBREV));
                strbuf_addstr(&msg, "Fast forward");
                if (have_message)
                        strbuf_addstr(&msg,
@@ -1210,6 +1220,15 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
                        merge_msg.len)
                        die("Could not write to %s", git_path("MERGE_MSG"));
                close(fd);
+               fd = open(git_path("MERGE_MODE"), O_WRONLY | O_CREAT | O_TRUNC, 0666);
+               if (fd < 0)
+                       die("Could open %s for writing", git_path("MERGE_MODE"));
+               strbuf_reset(&buf);
+               if (!allow_fast_forward)
+                       strbuf_addf(&buf, "no-ff");
+               if (write_in_full(fd, buf.buf, buf.len) != buf.len)
+                       die("Could not write to %s", git_path("MERGE_MODE"));
+               close(fd);
        }
 
        if (merge_was_ok) {