i18n: simplify numeric error reporting
[gitweb.git] / builtin / merge.c
index 0dfe09eb1922750cdbf0d7cf3a4fd21b94a05cb5..0ae099f746680e5c2fcc55dff56624da1619f2f1 100644 (file)
@@ -30,6 +30,7 @@
 #include "fmt-merge-msg.h"
 #include "gpg-interface.h"
 #include "sequencer.h"
+#include "string-list.h"
 
 #define DEFAULT_TWOHEAD (1<<0)
 #define DEFAULT_OCTOPUS (1<<1)
@@ -336,15 +337,9 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
        struct rev_info rev;
        struct strbuf out = STRBUF_INIT;
        struct commit_list *j;
-       const char *filename;
-       int fd;
        struct pretty_print_context ctx = {0};
 
        printf(_("Squash commit -- not updating HEAD\n"));
-       filename = git_path_squash_msg();
-       fd = open(filename, O_WRONLY | O_CREAT, 0666);
-       if (fd < 0)
-               die_errno(_("Could not write to '%s'"), filename);
 
        init_revisions(&rev, NULL);
        rev.ignore_merges = 1;
@@ -371,10 +366,7 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
                        oid_to_hex(&commit->object.oid));
                pretty_print_commit(&ctx, commit, &out);
        }
-       if (write_in_full(fd, out.buf, out.len) != out.len)
-               die_errno(_("Writing SQUASH_MSG"));
-       if (close(fd))
-               die_errno(_("Finishing SQUASH_MSG"));
+       write_file_buf(git_path_squash_msg(), out.buf, out.len);
        strbuf_release(&out);
 }
 
@@ -682,6 +674,8 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
                hold_locked_index(&lock, 1);
                clean = merge_recursive(&o, head,
                                remoteheads->item, reversed, &result);
+               if (clean < 0)
+                       exit(128);
                if (active_cache_changed &&
                    write_locked_index(&the_index, &lock, COMMIT_LOCK))
                        die (_("unable to write %s"), get_index_file());
@@ -712,42 +706,17 @@ static int count_unmerged_entries(void)
        return ret;
 }
 
-static void split_merge_strategies(const char *string, struct strategy **list,
-                                  int *nr, int *alloc)
-{
-       char *p, *q, *buf;
-
-       if (!string)
-               return;
-
-       buf = xstrdup(string);
-       q = buf;
-       for (;;) {
-               p = strchr(q, ' ');
-               if (!p) {
-                       ALLOC_GROW(*list, *nr + 1, *alloc);
-                       (*list)[(*nr)++].name = xstrdup(q);
-                       free(buf);
-                       return;
-               } else {
-                       *p = '\0';
-                       ALLOC_GROW(*list, *nr + 1, *alloc);
-                       (*list)[(*nr)++].name = xstrdup(q);
-                       q = ++p;
-               }
-       }
-}
-
 static void add_strategies(const char *string, unsigned attr)
 {
-       struct strategy *list = NULL;
-       int list_alloc = 0, list_nr = 0, i;
-
-       memset(&list, 0, sizeof(list));
-       split_merge_strategies(string, &list, &list_nr, &list_alloc);
-       if (list) {
-               for (i = 0; i < list_nr; i++)
-                       append_strategy(get_strategy(list[i].name));
+       int i;
+
+       if (string) {
+               struct string_list list = STRING_LIST_INIT_DUP;
+               struct string_list_item *item;
+               string_list_split(&list, string, ' ', -1);
+               for_each_string_list_item(item, &list)
+                       append_strategy(get_strategy(item->string));
+               string_list_clear(&list, 0);
                return;
        }
        for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
@@ -756,18 +725,6 @@ static void add_strategies(const char *string, unsigned attr)
 
 }
 
-static void write_merge_msg(struct strbuf *msg)
-{
-       const char *filename = git_path_merge_msg();
-       int fd = open(filename, O_WRONLY | O_CREAT, 0666);
-       if (fd < 0)
-               die_errno(_("Could not open '%s' for writing"),
-                         filename);
-       if (write_in_full(fd, msg->buf, msg->len) != msg->len)
-               die_errno(_("Could not write to '%s'"), filename);
-       close(fd);
-}
-
 static void read_merge_msg(struct strbuf *msg)
 {
        const char *filename = git_path_merge_msg();
@@ -801,7 +758,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
        strbuf_addch(&msg, '\n');
        if (0 < option_edit)
                strbuf_commented_addf(&msg, _(merge_editor_comment), comment_line_char);
-       write_merge_msg(&msg);
+       write_file_buf(git_path_merge_msg(), msg.buf, msg.len);
        if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg",
                            git_path_merge_msg(), "merge", NULL))
                abort_commit(remoteheads, NULL);
@@ -964,8 +921,6 @@ static int setup_with_upstream(const char ***argv)
 
 static void write_merge_state(struct commit_list *remoteheads)
 {
-       const char *filename;
-       int fd;
        struct commit_list *j;
        struct strbuf buf = STRBUF_INIT;
 
@@ -979,26 +934,14 @@ static void write_merge_state(struct commit_list *remoteheads)
                }
                strbuf_addf(&buf, "%s\n", oid_to_hex(oid));
        }
-       filename = git_path_merge_head();
-       fd = open(filename, O_WRONLY | O_CREAT, 0666);
-       if (fd < 0)
-               die_errno(_("Could not open '%s' for writing"), filename);
-       if (write_in_full(fd, buf.buf, buf.len) != buf.len)
-               die_errno(_("Could not write to '%s'"), filename);
-       close(fd);
+       write_file_buf(git_path_merge_head(), buf.buf, buf.len);
        strbuf_addch(&merge_msg, '\n');
-       write_merge_msg(&merge_msg);
+       write_file_buf(git_path_merge_msg(), merge_msg.buf, merge_msg.len);
 
-       filename = git_path_merge_mode();
-       fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
-       if (fd < 0)
-               die_errno(_("Could not open '%s' for writing"), filename);
        strbuf_reset(&buf);
        if (fast_forward == FF_NO)
                strbuf_addf(&buf, "no-ff");
-       if (write_in_full(fd, buf.buf, buf.len) != buf.len)
-               die_errno(_("Could not write to '%s'"), filename);
-       close(fd);
+       write_file_buf(git_path_merge_mode(), buf.buf, buf.len);
 }
 
 static int default_edit_option(void)