Merge branch 'jc/renormalize-merge-kill-safer-crlf'
authorJunio C Hamano <gitster@pobox.com>
Mon, 19 Dec 2016 22:45:30 +0000 (14:45 -0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 19 Dec 2016 22:45:30 +0000 (14:45 -0800)
Fix a corner case in merge-recursive regression that crept in
during 2.10 development cycle.

* jc/renormalize-merge-kill-safer-crlf:
convert: git cherry-pick -Xrenormalize did not work
merge-recursive: handle NULL in add_cacheinfo() correctly
cherry-pick: demonstrate a segmentation fault

1  2 
convert.c
merge-recursive.c
t/t3501-revert-cherry-pick.sh
diff --combined convert.c
index be91358462a72c9d5a6c02f2b0e44585a76d4783,2f90f363c664be87d7fc395abf2d03ab067dbcd6..4e17e45ed265b3f4861d1b7a1e86edaa4df3b1ac
+++ b/convert.c
@@@ -3,7 -3,6 +3,7 @@@
  #include "run-command.h"
  #include "quote.h"
  #include "sigchain.h"
 +#include "pkt-line.h"
  
  /*
   * convert.c - convert a file when checking it out and checking it in.
@@@ -198,21 -197,17 +198,21 @@@ static void check_safe_crlf(const char 
                 * CRLFs would not be restored by checkout
                 */
                if (checksafe == SAFE_CRLF_WARN)
 -                      warning("CRLF will be replaced by LF in %s.\nThe file will have its original line endings in your working directory.", path);
 +                      warning(_("CRLF will be replaced by LF in %s.\n"
 +                                "The file will have its original line"
 +                                " endings in your working directory."), path);
                else /* i.e. SAFE_CRLF_FAIL */
 -                      die("CRLF would be replaced by LF in %s.", path);
 +                      die(_("CRLF would be replaced by LF in %s."), path);
        } else if (old_stats->lonelf && !new_stats->lonelf ) {
                /*
                 * CRLFs would be added by checkout
                 */
                if (checksafe == SAFE_CRLF_WARN)
 -                      warning("LF will be replaced by CRLF in %s.\nThe file will have its original line endings in your working directory.", path);
 +                      warning(_("LF will be replaced by CRLF in %s.\n"
 +                                "The file will have its original line"
 +                                " endings in your working directory."), path);
                else /* i.e. SAFE_CRLF_FAIL */
 -                      die("LF would be replaced by CRLF in %s", path);
 +                      die(_("LF would be replaced by CRLF in %s"), path);
        }
  }
  
@@@ -279,15 -274,16 +279,16 @@@ static int crlf_to_git(const char *path
                if (convert_is_binary(len, &stats))
                        return 0;
                /*
-                * If the file in the index has any CR in it, do not convert.
-                * This is the new safer autocrlf handling.
+                * If the file in the index has any CR in it, do not
+                * convert.  This is the new safer autocrlf handling,
+                * unless we want to renormalize in a merge or
+                * cherry-pick.
                 */
-               if (checksafe == SAFE_CRLF_RENORMALIZE)
-                       checksafe = SAFE_CRLF_FALSE;
-               else if (has_cr_in_index(path))
+               if ((checksafe != SAFE_CRLF_RENORMALIZE) && has_cr_in_index(path))
                        convert_crlf_into_lf = 0;
        }
-       if (checksafe && len) {
+       if ((checksafe == SAFE_CRLF_WARN ||
+           (checksafe == SAFE_CRLF_FAIL)) && len) {
                struct text_stat new_stats;
                memcpy(&new_stats, &stats, sizeof(new_stats));
                /* simulate "git add" */
@@@ -417,7 -413,7 +418,7 @@@ static int filter_buffer_or_fd(int in, 
        child_process.out = out;
  
        if (start_command(&child_process))
 -              return error("cannot fork to run external filter %s", params->cmd);
 +              return error("cannot fork to run external filter '%s'", params->cmd);
  
        sigchain_push(SIGPIPE, SIG_IGN);
  
        if (close(child_process.in))
                write_err = 1;
        if (write_err)
 -              error("cannot feed the input to external filter %s", params->cmd);
 +              error("cannot feed the input to external filter '%s'", params->cmd);
  
        sigchain_pop(SIGPIPE);
  
        status = finish_command(&child_process);
        if (status)
 -              error("external filter %s failed %d", params->cmd, status);
 +              error("external filter '%s' failed %d", params->cmd, status);
  
        strbuf_release(&cmd);
        return (write_err || status);
  }
  
 -static int apply_filter(const char *path, const char *src, size_t len, int fd,
 +static int apply_single_file_filter(const char *path, const char *src, size_t len, int fd,
                          struct strbuf *dst, const char *cmd)
  {
        /*
         *
         * (child --> cmd) --> us
         */
 -      int ret = 1;
 +      int err = 0;
        struct strbuf nbuf = STRBUF_INIT;
        struct async async;
        struct filter_params params;
  
 -      if (!cmd || !*cmd)
 -              return 0;
 -
 -      if (!dst)
 -              return 1;
 -
        memset(&async, 0, sizeof(async));
        async.proc = filter_buffer_or_fd;
        async.data = &params;
                return 0;       /* error was already reported */
  
        if (strbuf_read(&nbuf, async.out, len) < 0) {
 -              error("read from external filter %s failed", cmd);
 -              ret = 0;
 +              err = error("read from external filter '%s' failed", cmd);
        }
        if (close(async.out)) {
 -              error("read from external filter %s failed", cmd);
 -              ret = 0;
 +              err = error("read from external filter '%s' failed", cmd);
        }
        if (finish_async(&async)) {
 -              error("external filter %s failed", cmd);
 -              ret = 0;
 +              err = error("external filter '%s' failed", cmd);
        }
  
 -      if (ret) {
 +      if (!err) {
                strbuf_swap(dst, &nbuf);
        }
        strbuf_release(&nbuf);
 -      return ret;
 +      return !err;
 +}
 +
 +#define CAP_CLEAN    (1u<<0)
 +#define CAP_SMUDGE   (1u<<1)
 +
 +struct cmd2process {
 +      struct hashmap_entry ent; /* must be the first member! */
 +      unsigned int supported_capabilities;
 +      const char *cmd;
 +      struct child_process process;
 +};
 +
 +static int cmd_process_map_initialized;
 +static struct hashmap cmd_process_map;
 +
 +static int cmd2process_cmp(const struct cmd2process *e1,
 +                         const struct cmd2process *e2,
 +                         const void *unused)
 +{
 +      return strcmp(e1->cmd, e2->cmd);
 +}
 +
 +static struct cmd2process *find_multi_file_filter_entry(struct hashmap *hashmap, const char *cmd)
 +{
 +      struct cmd2process key;
 +      hashmap_entry_init(&key, strhash(cmd));
 +      key.cmd = cmd;
 +      return hashmap_get(hashmap, &key, NULL);
 +}
 +
 +static int packet_write_list(int fd, const char *line, ...)
 +{
 +      va_list args;
 +      int err;
 +      va_start(args, line);
 +      for (;;) {
 +              if (!line)
 +                      break;
 +              if (strlen(line) > LARGE_PACKET_DATA_MAX)
 +                      return -1;
 +              err = packet_write_fmt_gently(fd, "%s\n", line);
 +              if (err)
 +                      return err;
 +              line = va_arg(args, const char*);
 +      }
 +      va_end(args);
 +      return packet_flush_gently(fd);
 +}
 +
 +static void read_multi_file_filter_status(int fd, struct strbuf *status)
 +{
 +      struct strbuf **pair;
 +      char *line;
 +      for (;;) {
 +              line = packet_read_line(fd, NULL);
 +              if (!line)
 +                      break;
 +              pair = strbuf_split_str(line, '=', 2);
 +              if (pair[0] && pair[0]->len && pair[1]) {
 +                      /* the last "status=<foo>" line wins */
 +                      if (!strcmp(pair[0]->buf, "status=")) {
 +                              strbuf_reset(status);
 +                              strbuf_addbuf(status, pair[1]);
 +                      }
 +              }
 +              strbuf_list_free(pair);
 +      }
 +}
 +
 +static void kill_multi_file_filter(struct hashmap *hashmap, struct cmd2process *entry)
 +{
 +      if (!entry)
 +              return;
 +
 +      entry->process.clean_on_exit = 0;
 +      kill(entry->process.pid, SIGTERM);
 +      finish_command(&entry->process);
 +
 +      hashmap_remove(hashmap, entry, NULL);
 +      free(entry);
 +}
 +
 +static void stop_multi_file_filter(struct child_process *process)
 +{
 +      sigchain_push(SIGPIPE, SIG_IGN);
 +      /* Closing the pipe signals the filter to initiate a shutdown. */
 +      close(process->in);
 +      close(process->out);
 +      sigchain_pop(SIGPIPE);
 +      /* Finish command will wait until the shutdown is complete. */
 +      finish_command(process);
 +}
 +
 +static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, const char *cmd)
 +{
 +      int err;
 +      struct cmd2process *entry;
 +      struct child_process *process;
 +      const char *argv[] = { cmd, NULL };
 +      struct string_list cap_list = STRING_LIST_INIT_NODUP;
 +      char *cap_buf;
 +      const char *cap_name;
 +
 +      entry = xmalloc(sizeof(*entry));
 +      entry->cmd = cmd;
 +      entry->supported_capabilities = 0;
 +      process = &entry->process;
 +
 +      child_process_init(process);
 +      process->argv = argv;
 +      process->use_shell = 1;
 +      process->in = -1;
 +      process->out = -1;
 +      process->clean_on_exit = 1;
 +      process->clean_on_exit_handler = stop_multi_file_filter;
 +
 +      if (start_command(process)) {
 +              error("cannot fork to run external filter '%s'", cmd);
 +              return NULL;
 +      }
 +
 +      hashmap_entry_init(entry, strhash(cmd));
 +
 +      sigchain_push(SIGPIPE, SIG_IGN);
 +
 +      err = packet_write_list(process->in, "git-filter-client", "version=2", NULL);
 +      if (err)
 +              goto done;
 +
 +      err = strcmp(packet_read_line(process->out, NULL), "git-filter-server");
 +      if (err) {
 +              error("external filter '%s' does not support filter protocol version 2", cmd);
 +              goto done;
 +      }
 +      err = strcmp(packet_read_line(process->out, NULL), "version=2");
 +      if (err)
 +              goto done;
 +      err = packet_read_line(process->out, NULL) != NULL;
 +      if (err)
 +              goto done;
 +
 +      err = packet_write_list(process->in, "capability=clean", "capability=smudge", NULL);
 +
 +      for (;;) {
 +              cap_buf = packet_read_line(process->out, NULL);
 +              if (!cap_buf)
 +                      break;
 +              string_list_split_in_place(&cap_list, cap_buf, '=', 1);
 +
 +              if (cap_list.nr != 2 || strcmp(cap_list.items[0].string, "capability"))
 +                      continue;
 +
 +              cap_name = cap_list.items[1].string;
 +              if (!strcmp(cap_name, "clean")) {
 +                      entry->supported_capabilities |= CAP_CLEAN;
 +              } else if (!strcmp(cap_name, "smudge")) {
 +                      entry->supported_capabilities |= CAP_SMUDGE;
 +              } else {
 +                      warning(
 +                              "external filter '%s' requested unsupported filter capability '%s'",
 +                              cmd, cap_name
 +                      );
 +              }
 +
 +              string_list_clear(&cap_list, 0);
 +      }
 +
 +done:
 +      sigchain_pop(SIGPIPE);
 +
 +      if (err || errno == EPIPE) {
 +              error("initialization for external filter '%s' failed", cmd);
 +              kill_multi_file_filter(hashmap, entry);
 +              return NULL;
 +      }
 +
 +      hashmap_add(hashmap, entry);
 +      return entry;
 +}
 +
 +static int apply_multi_file_filter(const char *path, const char *src, size_t len,
 +                                 int fd, struct strbuf *dst, const char *cmd,
 +                                 const unsigned int wanted_capability)
 +{
 +      int err;
 +      struct cmd2process *entry;
 +      struct child_process *process;
 +      struct strbuf nbuf = STRBUF_INIT;
 +      struct strbuf filter_status = STRBUF_INIT;
 +      const char *filter_type;
 +
 +      if (!cmd_process_map_initialized) {
 +              cmd_process_map_initialized = 1;
 +              hashmap_init(&cmd_process_map, (hashmap_cmp_fn) cmd2process_cmp, 0);
 +              entry = NULL;
 +      } else {
 +              entry = find_multi_file_filter_entry(&cmd_process_map, cmd);
 +      }
 +
 +      fflush(NULL);
 +
 +      if (!entry) {
 +              entry = start_multi_file_filter(&cmd_process_map, cmd);
 +              if (!entry)
 +                      return 0;
 +      }
 +      process = &entry->process;
 +
 +      if (!(wanted_capability & entry->supported_capabilities))
 +              return 0;
 +
 +      if (CAP_CLEAN & wanted_capability)
 +              filter_type = "clean";
 +      else if (CAP_SMUDGE & wanted_capability)
 +              filter_type = "smudge";
 +      else
 +              die("unexpected filter type");
 +
 +      sigchain_push(SIGPIPE, SIG_IGN);
 +
 +      assert(strlen(filter_type) < LARGE_PACKET_DATA_MAX - strlen("command=\n"));
 +      err = packet_write_fmt_gently(process->in, "command=%s\n", filter_type);
 +      if (err)
 +              goto done;
 +
 +      err = strlen(path) > LARGE_PACKET_DATA_MAX - strlen("pathname=\n");
 +      if (err) {
 +              error("path name too long for external filter");
 +              goto done;
 +      }
 +
 +      err = packet_write_fmt_gently(process->in, "pathname=%s\n", path);
 +      if (err)
 +              goto done;
 +
 +      err = packet_flush_gently(process->in);
 +      if (err)
 +              goto done;
 +
 +      if (fd >= 0)
 +              err = write_packetized_from_fd(fd, process->in);
 +      else
 +              err = write_packetized_from_buf(src, len, process->in);
 +      if (err)
 +              goto done;
 +
 +      read_multi_file_filter_status(process->out, &filter_status);
 +      err = strcmp(filter_status.buf, "success");
 +      if (err)
 +              goto done;
 +
 +      err = read_packetized_to_strbuf(process->out, &nbuf) < 0;
 +      if (err)
 +              goto done;
 +
 +      read_multi_file_filter_status(process->out, &filter_status);
 +      err = strcmp(filter_status.buf, "success");
 +
 +done:
 +      sigchain_pop(SIGPIPE);
 +
 +      if (err || errno == EPIPE) {
 +              if (!strcmp(filter_status.buf, "error")) {
 +                      /* The filter signaled a problem with the file. */
 +              } else if (!strcmp(filter_status.buf, "abort")) {
 +                      /*
 +                       * The filter signaled a permanent problem. Don't try to filter
 +                       * files with the same command for the lifetime of the current
 +                       * Git process.
 +                       */
 +                       entry->supported_capabilities &= ~wanted_capability;
 +              } else {
 +                      /*
 +                       * Something went wrong with the protocol filter.
 +                       * Force shutdown and restart if another blob requires filtering.
 +                       */
 +                      error("external filter '%s' failed", cmd);
 +                      kill_multi_file_filter(&cmd_process_map, entry);
 +              }
 +      } else {
 +              strbuf_swap(dst, &nbuf);
 +      }
 +      strbuf_release(&nbuf);
 +      return !err;
  }
  
  static struct convert_driver {
        struct convert_driver *next;
        const char *smudge;
        const char *clean;
 +      const char *process;
        int required;
  } *user_convert, **user_convert_tail;
  
 +static int apply_filter(const char *path, const char *src, size_t len,
 +                      int fd, struct strbuf *dst, struct convert_driver *drv,
 +                      const unsigned int wanted_capability)
 +{
 +      const char *cmd = NULL;
 +
 +      if (!drv)
 +              return 0;
 +
 +      if (!dst)
 +              return 1;
 +
 +      if ((CAP_CLEAN & wanted_capability) && !drv->process && drv->clean)
 +              cmd = drv->clean;
 +      else if ((CAP_SMUDGE & wanted_capability) && !drv->process && drv->smudge)
 +              cmd = drv->smudge;
 +
 +      if (cmd && *cmd)
 +              return apply_single_file_filter(path, src, len, fd, dst, cmd);
 +      else if (drv->process && *drv->process)
 +              return apply_multi_file_filter(path, src, len, fd, dst, drv->process, wanted_capability);
 +
 +      return 0;
 +}
 +
  static int read_convert_config(const char *var, const char *value, void *cb)
  {
        const char *key, *name;
        if (!strcmp("clean", key))
                return git_config_string(&drv->clean, var, value);
  
 +      if (!strcmp("process", key))
 +              return git_config_string(&drv->process, var, value);
 +
        if (!strcmp("required", key)) {
                drv->required = git_config_bool(var, value);
                return 0;
@@@ -1151,7 -843,7 +1152,7 @@@ int would_convert_to_git_filter_fd(cons
        if (!ca.drv->required)
                return 0;
  
 -      return apply_filter(path, NULL, 0, -1, NULL, ca.drv->clean);
 +      return apply_filter(path, NULL, 0, -1, NULL, ca.drv, CAP_CLEAN);
  }
  
  const char *get_convert_attr_ascii(const char *path)
@@@ -1184,12 -876,18 +1185,12 @@@ int convert_to_git(const char *path, co
                     struct strbuf *dst, enum safe_crlf checksafe)
  {
        int ret = 0;
 -      const char *filter = NULL;
 -      int required = 0;
        struct conv_attrs ca;
  
        convert_attrs(&ca, path);
 -      if (ca.drv) {
 -              filter = ca.drv->clean;
 -              required = ca.drv->required;
 -      }
  
 -      ret |= apply_filter(path, src, len, -1, dst, filter);
 -      if (!ret && required)
 +      ret |= apply_filter(path, src, len, -1, dst, ca.drv, CAP_CLEAN);
 +      if (!ret && ca.drv && ca.drv->required)
                die("%s: clean filter '%s' failed", path, ca.drv->name);
  
        if (ret && dst) {
@@@ -1211,9 -909,9 +1212,9 @@@ void convert_to_git_filter_fd(const cha
        convert_attrs(&ca, path);
  
        assert(ca.drv);
 -      assert(ca.drv->clean);
 +      assert(ca.drv->clean || ca.drv->process);
  
 -      if (!apply_filter(path, NULL, 0, fd, dst, ca.drv->clean))
 +      if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN))
                die("%s: clean filter '%s' failed", path, ca.drv->name);
  
        crlf_to_git(path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
@@@ -1225,9 -923,15 +1226,9 @@@ static int convert_to_working_tree_inte
                                            int normalizing)
  {
        int ret = 0, ret_filter = 0;
 -      const char *filter = NULL;
 -      int required = 0;
        struct conv_attrs ca;
  
        convert_attrs(&ca, path);
 -      if (ca.drv) {
 -              filter = ca.drv->smudge;
 -              required = ca.drv->required;
 -      }
  
        ret |= ident_to_worktree(path, src, len, dst, ca.ident);
        if (ret) {
        }
        /*
         * CRLF conversion can be skipped if normalizing, unless there
 -       * is a smudge filter.  The filter might expect CRLFs.
 +       * is a smudge or process filter (even if the process filter doesn't
 +       * support smudge).  The filters might expect CRLFs.
         */
 -      if (filter || !normalizing) {
 +      if ((ca.drv && (ca.drv->smudge || ca.drv->process)) || !normalizing) {
                ret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action);
                if (ret) {
                        src = dst->buf;
                }
        }
  
 -      ret_filter = apply_filter(path, src, len, -1, dst, filter);
 -      if (!ret_filter && required)
 +      ret_filter = apply_filter(path, src, len, -1, dst, ca.drv, CAP_SMUDGE);
 +      if (!ret_filter && ca.drv && ca.drv->required)
                die("%s: smudge filter %s failed", path, ca.drv->name);
  
        return ret | ret_filter;
@@@ -1700,7 -1403,7 +1701,7 @@@ struct stream_filter *get_stream_filter
        struct stream_filter *filter = NULL;
  
        convert_attrs(&ca, path);
 -      if (ca.drv && (ca.drv->smudge || ca.drv->clean))
 +      if (ca.drv && (ca.drv->process || ca.drv->smudge || ca.drv->clean))
                return NULL;
  
        if (ca.crlf_action == CRLF_AUTO || ca.crlf_action == CRLF_AUTO_CRLF)
diff --combined merge-recursive.c
index c07ba0df9d908782d6001c9c9c4aab2a0036b9a8,56385d4c01b732b77fdf463408109f8d3ca67be8..23d6992f4037b37a9c3e133001af16595e8da394
  #include "dir.h"
  #include "submodule.h"
  
 +static void flush_output(struct merge_options *o)
 +{
 +      if (o->buffer_output < 2 && o->obuf.len) {
 +              fputs(o->obuf.buf, stdout);
 +              strbuf_reset(&o->obuf);
 +      }
 +}
 +
 +static int err(struct merge_options *o, const char *err, ...)
 +{
 +      va_list params;
 +
 +      if (o->buffer_output < 2)
 +              flush_output(o);
 +      else {
 +              strbuf_complete(&o->obuf, '\n');
 +              strbuf_addstr(&o->obuf, "error: ");
 +      }
 +      va_start(params, err);
 +      strbuf_vaddf(&o->obuf, err, params);
 +      va_end(params);
 +      if (o->buffer_output > 1)
 +              strbuf_addch(&o->obuf, '\n');
 +      else {
 +              error("%s", o->obuf.buf);
 +              strbuf_reset(&o->obuf);
 +      }
 +
 +      return -1;
 +}
 +
  static struct tree *shift_tree_object(struct tree *one, struct tree *two,
                                      const char *subtree_shift)
  {
        struct object_id shifted;
  
        if (!*subtree_shift) {
 -              shift_tree(one->object.oid.hash, two->object.oid.hash, shifted.hash, 0);
 +              shift_tree(&one->object.oid, &two->object.oid, &shifted, 0);
        } else {
 -              shift_tree_by(one->object.oid.hash, two->object.oid.hash, shifted.hash,
 +              shift_tree_by(&one->object.oid, &two->object.oid, &shifted,
                              subtree_shift);
        }
        if (!oidcmp(&two->object.oid, &shifted))
  static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
  {
        struct commit *commit = alloc_commit_node();
 -      struct merge_remote_desc *desc = xmalloc(sizeof(*desc));
  
 -      desc->name = comment;
 -      desc->obj = (struct object *)commit;
 +      set_merge_remote_desc(commit, comment, (struct object *)commit);
        commit->tree = tree;
 -      commit->util = desc;
        commit->object.parsed = 1;
        return commit;
  }
   * Since we use get_tree_entry(), which does not put the read object into
   * the object pool, we cannot rely on a == b.
   */
 -static int sha_eq(const unsigned char *a, const unsigned char *b)
 +static int oid_eq(const struct object_id *a, const struct object_id *b)
  {
        if (!a && !b)
                return 2;
 -      return a && b && hashcmp(a, b) == 0;
 +      return a && b && oidcmp(a, b) == 0;
  }
  
  enum rename_type {
@@@ -118,7 -90,7 +118,7 @@@ struct rename_conflict_info 
  struct stage_data {
        struct {
                unsigned mode;
 -              unsigned char sha[20];
 +              struct object_id oid;
        } stages[4];
        struct rename_conflict_info *rename_conflict_info;
        unsigned processed:1;
@@@ -162,11 -134,11 +162,11 @@@ static inline void setup_rename_conflic
                int ostage2 = ostage1 ^ 1;
  
                ci->ren1_other.path = pair1->one->path;
 -              hashcpy(ci->ren1_other.sha1, src_entry1->stages[ostage1].sha);
 +              oidcpy(&ci->ren1_other.oid, &src_entry1->stages[ostage1].oid);
                ci->ren1_other.mode = src_entry1->stages[ostage1].mode;
  
                ci->ren2_other.path = pair2->one->path;
 -              hashcpy(ci->ren2_other.sha1, src_entry2->stages[ostage2].sha);
 +              oidcpy(&ci->ren2_other.oid, &src_entry2->stages[ostage2].oid);
                ci->ren2_other.mode = src_entry2->stages[ostage2].mode;
        }
  }
@@@ -176,6 -148,14 +176,6 @@@ static int show(struct merge_options *o
        return (!o->call_depth && o->verbosity >= v) || o->verbosity >= 5;
  }
  
 -static void flush_output(struct merge_options *o)
 -{
 -      if (o->obuf.len) {
 -              fputs(o->obuf.buf, stdout);
 -              strbuf_reset(&o->obuf);
 -      }
 -}
 -
  __attribute__((format (printf, 3, 4)))
  static void output(struct merge_options *o, int v, const char *fmt, ...)
  {
  
  static void output_commit_title(struct merge_options *o, struct commit *commit)
  {
 -      int i;
 -      flush_output(o);
 -      for (i = o->call_depth; i--;)
 -              fputs("  ", stdout);
 +      strbuf_addchars(&o->obuf, ' ', o->call_depth * 2);
        if (commit->util)
 -              printf("virtual %s\n", merge_remote_util(commit)->name);
 +              strbuf_addf(&o->obuf, "virtual %s\n",
 +                      merge_remote_util(commit)->name);
        else {
 -              printf("%s ", find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV));
 +              strbuf_add_unique_abbrev(&o->obuf, commit->object.oid.hash,
 +                                       DEFAULT_ABBREV);
 +              strbuf_addch(&o->obuf, ' ');
                if (parse_commit(commit) != 0)
 -                      printf(_("(bad commit)\n"));
 +                      strbuf_addstr(&o->obuf, _("(bad commit)\n"));
                else {
                        const char *title;
                        const char *msg = get_commit_buffer(commit, NULL);
                        int len = find_commit_subject(msg, &title);
                        if (len)
 -                              printf("%.*s\n", len, title);
 +                              strbuf_addf(&o->obuf, "%.*s\n", len, title);
                        unuse_commit_buffer(commit, msg);
                }
        }
 +      flush_output(o);
  }
  
 -static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
 +static int add_cacheinfo(struct merge_options *o,
 +              unsigned int mode, const struct object_id *oid,
                const char *path, int stage, int refresh, int options)
  {
        struct cache_entry *ce;
        int ret;
  
 -      ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, 0);
 +      ce = make_cache_entry(mode, oid ? oid->hash : null_sha1, path, stage, 0);
        if (!ce)
 -              return error(_("addinfo_cache failed for path '%s'"), path);
 +              return err(o, _("addinfo_cache failed for path '%s'"), path);
  
        ret = add_cache_entry(ce, options);
        if (refresh) {
                struct cache_entry *nce;
  
                nce = refresh_cache_entry(ce, CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING);
 -                      return error(_("addinfo_cache failed for path '%s'"), path);
+               if (!nce)
++                      return err(o, _("addinfo_cache failed for path '%s'"), path);
                if (nce != ce)
                        ret = add_cache_entry(nce, options);
        }
@@@ -290,17 -270,15 +292,17 @@@ struct tree *write_tree_from_memory(str
                                fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce),
                                        (int)ce_namelen(ce), ce->name);
                }
 -              die("Bug in merge-recursive.c");
 +              die("BUG: unmerged index entries in merge-recursive.c");
        }
  
        if (!active_cache_tree)
                active_cache_tree = cache_tree();
  
        if (!cache_tree_fully_valid(active_cache_tree) &&
 -          cache_tree_update(&the_index, 0) < 0)
 -              die(_("error building trees"));
 +          cache_tree_update(&the_index, 0) < 0) {
 +              err(o, _("error building trees"));
 +              return NULL;
 +      }
  
        result = lookup_tree(active_cache_tree->sha1);
  
@@@ -347,11 -325,11 +349,11 @@@ static struct stage_data *insert_stage_
        struct string_list_item *item;
        struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
        get_tree_entry(o->object.oid.hash, path,
 -                      e->stages[1].sha, &e->stages[1].mode);
 +                      e->stages[1].oid.hash, &e->stages[1].mode);
        get_tree_entry(a->object.oid.hash, path,
 -                      e->stages[2].sha, &e->stages[2].mode);
 +                      e->stages[2].oid.hash, &e->stages[2].mode);
        get_tree_entry(b->object.oid.hash, path,
 -                      e->stages[3].sha, &e->stages[3].mode);
 +                      e->stages[3].oid.hash, &e->stages[3].mode);
        item = string_list_insert(entries, path);
        item->util = e;
        return e;
@@@ -382,16 -360,18 +384,16 @@@ static struct string_list *get_unmerged
                }
                e = item->util;
                e->stages[ce_stage(ce)].mode = ce->ce_mode;
 -              hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1);
 +              oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid);
        }
  
        return unmerged;
  }
  
 -static int string_list_df_name_compare(const void *a, const void *b)
 +static int string_list_df_name_compare(const char *one, const char *two)
  {
 -      const struct string_list_item *one = a;
 -      const struct string_list_item *two = b;
 -      int onelen = strlen(one->string);
 -      int twolen = strlen(two->string);
 +      int onelen = strlen(one);
 +      int twolen = strlen(two);
        /*
         * Here we only care that entries for D/F conflicts are
         * adjacent, in particular with the file of the D/F conflict
         * since in other cases any changes in their order due to
         * sorting cause no problems for us.
         */
 -      int cmp = df_name_compare(one->string, onelen, S_IFDIR,
 -                                two->string, twolen, S_IFDIR);
 +      int cmp = df_name_compare(one, onelen, S_IFDIR,
 +                                two, twolen, S_IFDIR);
        /*
         * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
         * that 'foo' comes before 'foo/bar'.
@@@ -431,7 -411,7 +433,7 @@@ static void record_df_conflict_files(st
         * and the file need to be present, then the D/F file will be
         * reinstated with a new unique name at the time it is processed.
         */
 -      struct string_list df_sorted_entries;
 +      struct string_list df_sorted_entries = STRING_LIST_INIT_NODUP;
        const char *last_file = NULL;
        int last_len = 0;
        int i;
                return;
  
        /* Ensure D/F conflicts are adjacent in the entries list. */
 -      memset(&df_sorted_entries, 0, sizeof(struct string_list));
        for (i = 0; i < entries->nr; i++) {
                struct string_list_item *next = &entries->items[i];
                string_list_append(&df_sorted_entries, next->string)->util =
                                   next->util;
        }
 -      qsort(df_sorted_entries.items, entries->nr, sizeof(*entries->items),
 -            string_list_df_name_compare);
 +      df_sorted_entries.cmp = string_list_df_name_compare;
 +      string_list_sort(&df_sorted_entries);
  
        string_list_clear(&o->df_conflict_file_set, 1);
        for (i = 0; i < df_sorted_entries.nr; i++) {
@@@ -563,8 -544,7 +565,8 @@@ static struct string_list *get_renames(
        return renames;
  }
  
 -static int update_stages(const char *path, const struct diff_filespec *o,
 +static int update_stages(struct merge_options *opt, const char *path,
 +                       const struct diff_filespec *o,
                         const struct diff_filespec *a,
                         const struct diff_filespec *b)
  {
                if (remove_file_from_cache(path))
                        return -1;
        if (o)
 -              if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options))
 +              if (add_cacheinfo(opt, o->mode, &o->oid, path, 1, 0, options))
                        return -1;
        if (a)
 -              if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options))
 +              if (add_cacheinfo(opt, a->mode, &a->oid, path, 2, 0, options))
                        return -1;
        if (b)
 -              if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options))
 +              if (add_cacheinfo(opt, b->mode, &b->oid, path, 3, 0, options))
                        return -1;
        return 0;
  }
@@@ -603,9 -583,9 +605,9 @@@ static void update_entry(struct stage_d
        entry->stages[1].mode = o->mode;
        entry->stages[2].mode = a->mode;
        entry->stages[3].mode = b->mode;
 -      hashcpy(entry->stages[1].sha, o->sha1);
 -      hashcpy(entry->stages[2].sha, a->sha1);
 -      hashcpy(entry->stages[3].sha, b->sha1);
 +      oidcpy(&entry->stages[1].oid, &o->oid);
 +      oidcpy(&entry->stages[2].oid, &a->oid);
 +      oidcpy(&entry->stages[3].oid, &b->oid);
  }
  
  static int remove_file(struct merge_options *o, int clean,
@@@ -653,7 -633,7 +655,7 @@@ static char *unique_path(struct merge_o
        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) ||
 -             file_exists(newpath.buf)) {
 +             (!o->call_depth && file_exists(newpath.buf))) {
                strbuf_setlen(&newpath, base_len);
                strbuf_addf(&newpath, "_%d", suffix++);
        }
        return strbuf_detach(&newpath, NULL);
  }
  
 -static int dir_in_way(const char *path, int check_working_copy)
 +/**
 + * Check whether a directory in the index is in the way of an incoming
 + * file.  Return 1 if so.  If check_working_copy is non-zero, also
 + * check the working directory.  If empty_ok is non-zero, also return
 + * 0 in the case where the working-tree dir exists but is empty.
 + */
 +static int dir_in_way(const char *path, int check_working_copy, int empty_ok)
  {
        int pos;
        struct strbuf dirpath = STRBUF_INIT;
        }
  
        strbuf_release(&dirpath);
 -      return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode);
 +      return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode) &&
 +              !(empty_ok && is_empty_dir(path));
  }
  
  static int was_tracked(const char *path)
  {
        int pos = cache_name_pos(path, strlen(path));
  
 -      if (pos < 0)
 -              pos = -1 - pos;
 -      while (pos < active_nr &&
 -             !strcmp(path, active_cache[pos]->name)) {
 -              /*
 -               * If stage #0, it is definitely tracked.
 -               * If it has stage #2 then it was tracked
 -               * before this merge started.  All other
 -               * cases the path was not tracked.
 -               */
 -              switch (ce_stage(active_cache[pos])) {
 -              case 0:
 -              case 2:
 +      if (0 <= pos)
 +              /* we have been tracking this path */
 +              return 1;
 +
 +      /*
 +       * Look for an unmerged entry for the path,
 +       * specifically stage #2, which would indicate
 +       * that "our" side before the merge started
 +       * had the path tracked (and resulted in a conflict).
 +       */
 +      for (pos = -1 - pos;
 +           pos < active_nr && !strcmp(path, active_cache[pos]->name);
 +           pos++)
 +              if (ce_stage(active_cache[pos]) == 2)
                        return 1;
 -              }
 -              pos++;
 -      }
        return 0;
  }
  
@@@ -745,10 -720,12 +747,10 @@@ static int make_room_for_path(struct me
        /* Make sure leading directories are created */
        status = safe_create_leading_directories_const(path);
        if (status) {
 -              if (status == SCLD_EXISTS) {
 +              if (status == SCLD_EXISTS)
                        /* something else exists */
 -                      error(msg, path, _(": perhaps a D/F conflict?"));
 -                      return -1;
 -              }
 -              die(msg, path, "");
 +                      return err(o, msg, path, _(": perhaps a D/F conflict?"));
 +              return err(o, msg, path, "");
        }
  
        /*
         * tracking it.
         */
        if (would_lose_untracked(path))
 -              return error(_("refusing to lose untracked file at '%s'"),
 +              return err(o, _("refusing to lose untracked file at '%s'"),
                             path);
  
        /* Successful unlink is good.. */
        if (errno == ENOENT)
                return 0;
        /* .. but not some other error (who really cares what?) */
 -      return error(msg, path, _(": perhaps a D/F conflict?"));
 +      return err(o, msg, path, _(": perhaps a D/F conflict?"));
  }
  
 -static void update_file_flags(struct merge_options *o,
 -                            const unsigned char *sha,
 -                            unsigned mode,
 -                            const char *path,
 -                            int update_cache,
 -                            int update_wd)
 +static int update_file_flags(struct merge_options *o,
 +                           const struct object_id *oid,
 +                           unsigned mode,
 +                           const char *path,
 +                           int update_cache,
 +                           int update_wd)
  {
 +      int ret = 0;
 +
        if (o->call_depth)
                update_wd = 0;
  
                        goto update_index;
                }
  
 -              buf = read_sha1_file(sha, &type, &size);
 +              buf = read_sha1_file(oid->hash, &type, &size);
                if (!buf)
 -                      die(_("cannot read object %s '%s'"), sha1_to_hex(sha), path);
 -              if (type != OBJ_BLOB)
 -                      die(_("blob expected for %s '%s'"), sha1_to_hex(sha), path);
 +                      return err(o, _("cannot read object %s '%s'"), oid_to_hex(oid), path);
 +              if (type != OBJ_BLOB) {
 +                      ret = err(o, _("blob expected for %s '%s'"), oid_to_hex(oid), path);
 +                      goto free_buf;
 +              }
                if (S_ISREG(mode)) {
                        struct strbuf strbuf = STRBUF_INIT;
                        if (convert_to_working_tree(path, buf, size, &strbuf)) {
  
                if (make_room_for_path(o, path) < 0) {
                        update_wd = 0;
 -                      free(buf);
 -                      goto update_index;
 +                      goto free_buf;
                }
                if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
                        int fd;
                        else
                                mode = 0666;
                        fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
 -                      if (fd < 0)
 -                              die_errno(_("failed to open '%s'"), path);
 +                      if (fd < 0) {
 +                              ret = err(o, _("failed to open '%s': %s"),
 +                                        path, strerror(errno));
 +                              goto free_buf;
 +                      }
                        write_in_full(fd, buf, size);
                        close(fd);
                } else if (S_ISLNK(mode)) {
                        safe_create_leading_directories_const(path);
                        unlink(path);
                        if (symlink(lnk, path))
 -                              die_errno(_("failed to symlink '%s'"), path);
 +                              ret = err(o, _("failed to symlink '%s': %s"),
 +                                      path, strerror(errno));
                        free(lnk);
                } else
 -                      die(_("do not know what to do with %06o %s '%s'"),
 -                          mode, sha1_to_hex(sha), path);
 +                      ret = err(o,
 +                                _("do not know what to do with %06o %s '%s'"),
 +                                mode, oid_to_hex(oid), path);
 + free_buf:
                free(buf);
        }
   update_index:
 -      if (update_cache)
 -              add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
 +      if (!ret && update_cache)
 +              add_cacheinfo(o, mode, oid, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
 +      return ret;
  }
  
 -static void update_file(struct merge_options *o,
 -                      int clean,
 -                      const unsigned char *sha,
 -                      unsigned mode,
 -                      const char *path)
 +static int update_file(struct merge_options *o,
 +                     int clean,
 +                     const struct object_id *oid,
 +                     unsigned mode,
 +                     const char *path)
  {
 -      update_file_flags(o, sha, mode, path, o->call_depth || clean, !o->call_depth);
 +      return update_file_flags(o, oid, mode, path, o->call_depth || clean, !o->call_depth);
  }
  
  /* Low level file merging, update and removal */
  
  struct merge_file_info {
 -      unsigned char sha[20];
 +      struct object_id oid;
        unsigned mode;
        unsigned clean:1,
                 merge:1;
@@@ -915,9 -882,9 +917,9 @@@ static int merge_3way(struct merge_opti
                name2 = mkpathdup("%s", branch2);
        }
  
 -      read_mmblob(&orig, one->sha1);
 -      read_mmblob(&src1, a->sha1);
 -      read_mmblob(&src2, b->sha1);
 +      read_mmblob(&orig, &one->oid);
 +      read_mmblob(&src1, &a->oid);
 +      read_mmblob(&src2, &b->oid);
  
        merge_status = ll_merge(result_buf, a->path, &orig, base_name,
                                &src1, name1, &src2, name2, &ll_opts);
        return merge_status;
  }
  
 -static struct merge_file_info merge_file_1(struct merge_options *o,
 +static int merge_file_1(struct merge_options *o,
                                           const struct diff_filespec *one,
                                           const struct diff_filespec *a,
                                           const struct diff_filespec *b,
                                           const char *branch1,
 -                                         const char *branch2)
 +                                         const char *branch2,
 +                                         struct merge_file_info *result)
  {
 -      struct merge_file_info result;
 -      result.merge = 0;
 -      result.clean = 1;
 +      result->merge = 0;
 +      result->clean = 1;
  
        if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
 -              result.clean = 0;
 +              result->clean = 0;
                if (S_ISREG(a->mode)) {
 -                      result.mode = a->mode;
 -                      hashcpy(result.sha, a->sha1);
 +                      result->mode = a->mode;
 +                      oidcpy(&result->oid, &a->oid);
                } else {
 -                      result.mode = b->mode;
 -                      hashcpy(result.sha, b->sha1);
 +                      result->mode = b->mode;
 +                      oidcpy(&result->oid, &b->oid);
                }
        } else {
 -              if (!sha_eq(a->sha1, one->sha1) && !sha_eq(b->sha1, one->sha1))
 -                      result.merge = 1;
 +              if (!oid_eq(&a->oid, &one->oid) && !oid_eq(&b->oid, &one->oid))
 +                      result->merge = 1;
  
                /*
                 * Merge modes
                 */
                if (a->mode == b->mode || a->mode == one->mode)
 -                      result.mode = b->mode;
 +                      result->mode = b->mode;
                else {
 -                      result.mode = a->mode;
 +                      result->mode = a->mode;
                        if (b->mode != one->mode) {
 -                              result.clean = 0;
 -                              result.merge = 1;
 +                              result->clean = 0;
 +                              result->merge = 1;
                        }
                }
  
 -              if (sha_eq(a->sha1, b->sha1) || sha_eq(a->sha1, one->sha1))
 -                      hashcpy(result.sha, b->sha1);
 -              else if (sha_eq(b->sha1, one->sha1))
 -                      hashcpy(result.sha, a->sha1);
 +              if (oid_eq(&a->oid, &b->oid) || oid_eq(&a->oid, &one->oid))
 +                      oidcpy(&result->oid, &b->oid);
 +              else if (oid_eq(&b->oid, &one->oid))
 +                      oidcpy(&result->oid, &a->oid);
                else if (S_ISREG(a->mode)) {
                        mmbuffer_t result_buf;
 -                      int merge_status;
 +                      int ret = 0, merge_status;
  
                        merge_status = merge_3way(o, &result_buf, one, a, b,
                                                  branch1, branch2);
  
                        if ((merge_status < 0) || !result_buf.ptr)
 -                              die(_("Failed to execute internal merge"));
 +                              ret = err(o, _("Failed to execute internal merge"));
  
 -                      if (write_sha1_file(result_buf.ptr, result_buf.size,
 -                                          blob_type, result.sha))
 -                              die(_("Unable to add %s to database"),
 -                                  a->path);
 +                      if (!ret && write_sha1_file(result_buf.ptr, result_buf.size,
 +                                                  blob_type, result->oid.hash))
 +                              ret = err(o, _("Unable to add %s to database"),
 +                                        a->path);
  
                        free(result_buf.ptr);
 -                      result.clean = (merge_status == 0);
 +                      if (ret)
 +                              return ret;
 +                      result->clean = (merge_status == 0);
                } else if (S_ISGITLINK(a->mode)) {
 -                      result.clean = merge_submodule(result.sha,
 -                                                     one->path, one->sha1,
 -                                                     a->sha1, b->sha1,
 +                      result->clean = merge_submodule(result->oid.hash,
 +                                                     one->path,
 +                                                     one->oid.hash,
 +                                                     a->oid.hash,
 +                                                     b->oid.hash,
                                                       !o->call_depth);
                } else if (S_ISLNK(a->mode)) {
 -                      hashcpy(result.sha, a->sha1);
 +                      oidcpy(&result->oid, &a->oid);
  
 -                      if (!sha_eq(a->sha1, b->sha1))
 -                              result.clean = 0;
 -              } else {
 -                      die(_("unsupported object type in the tree"));
 -              }
 +                      if (!oid_eq(&a->oid, &b->oid))
 +                              result->clean = 0;
 +              } else
 +                      die("BUG: unsupported object type in the tree");
        }
  
 -      return result;
 +      return 0;
  }
  
 -static struct merge_file_info
 -merge_file_special_markers(struct merge_options *o,
 +static int merge_file_special_markers(struct merge_options *o,
                           const struct diff_filespec *one,
                           const struct diff_filespec *a,
                           const struct diff_filespec *b,
                           const char *branch1,
                           const char *filename1,
                           const char *branch2,
 -                         const char *filename2)
 +                         const char *filename2,
 +                         struct merge_file_info *mfi)
  {
        char *side1 = NULL;
        char *side2 = NULL;
 -      struct merge_file_info mfi;
 +      int ret;
  
        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);
 +      ret = merge_file_1(o, one, a, b,
 +                         side1 ? side1 : branch1,
 +                         side2 ? side2 : branch2, mfi);
        free(side1);
        free(side2);
 -      return mfi;
 +      return ret;
  }
  
 -static struct merge_file_info merge_file_one(struct merge_options *o,
 +static int merge_file_one(struct merge_options *o,
                                         const char *path,
 -                                       const unsigned char *o_sha, int o_mode,
 -                                       const unsigned char *a_sha, int a_mode,
 -                                       const unsigned char *b_sha, int b_mode,
 +                                       const struct object_id *o_oid, int o_mode,
 +                                       const struct object_id *a_oid, int a_mode,
 +                                       const struct object_id *b_oid, int b_mode,
                                         const char *branch1,
 -                                       const char *branch2)
 +                                       const char *branch2,
 +                                       struct merge_file_info *mfi)
  {
        struct diff_filespec one, a, b;
  
        one.path = a.path = b.path = (char *)path;
 -      hashcpy(one.sha1, o_sha);
 +      oidcpy(&one.oid, o_oid);
        one.mode = o_mode;
 -      hashcpy(a.sha1, a_sha);
 +      oidcpy(&a.oid, a_oid);
        a.mode = a_mode;
 -      hashcpy(b.sha1, b_sha);
 +      oidcpy(&b.oid, b_oid);
        b.mode = b_mode;
 -      return merge_file_1(o, &one, &a, &b, branch1, branch2);
 +      return merge_file_1(o, &one, &a, &b, branch1, branch2, mfi);
  }
  
 -static void handle_change_delete(struct merge_options *o,
 +static int handle_change_delete(struct merge_options *o,
                                 const char *path,
 -                               const unsigned char *o_sha, int o_mode,
 -                               const unsigned char *a_sha, int a_mode,
 -                               const unsigned char *b_sha, int b_mode,
 +                               const struct object_id *o_oid, int o_mode,
 +                               const struct object_id *a_oid, int a_mode,
 +                               const struct object_id *b_oid, int b_mode,
                                 const char *change, const char *change_past)
  {
        char *renamed = NULL;
 -      if (dir_in_way(path, !o->call_depth)) {
 -              renamed = unique_path(o, path, a_sha ? o->branch1 : o->branch2);
 +      int ret = 0;
 +      if (dir_in_way(path, !o->call_depth, 0)) {
 +              renamed = unique_path(o, path, a_oid ? o->branch1 : o->branch2);
        }
  
        if (o->call_depth) {
                 * correct; since there is no true "middle point" between
                 * them, simply reuse the base version for virtual merge base.
                 */
 -              remove_file_from_cache(path);
 -              update_file(o, 0, o_sha, o_mode, renamed ? renamed : path);
 -      } else if (!a_sha) {
 +              ret = remove_file_from_cache(path);
 +              if (!ret)
 +                      ret = update_file(o, 0, o_oid, o_mode,
 +                                        renamed ? renamed : path);
 +      } else if (!a_oid) {
                if (!renamed) {
                        output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
                               "and %s in %s. Version %s of %s left in tree."),
                               change, path, o->branch1, change_past,
                               o->branch2, o->branch2, path);
 -                      update_file(o, 0, b_sha, b_mode, path);
 +                      ret = update_file(o, 0, b_oid, b_mode, path);
                } else {
                        output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
                               "and %s in %s. Version %s of %s left in tree at %s."),
                               change, path, o->branch1, change_past,
                               o->branch2, o->branch2, path, renamed);
 -                      update_file(o, 0, b_sha, b_mode, renamed);
 +                      ret = update_file(o, 0, b_oid, b_mode, renamed);
                }
        } else {
                if (!renamed) {
                               "and %s in %s. Version %s of %s left in tree at %s."),
                               change, path, o->branch2, change_past,
                               o->branch1, o->branch1, path, renamed);
 -                      update_file(o, 0, a_sha, a_mode, renamed);
 +                      ret = update_file(o, 0, a_oid, a_mode, renamed);
                }
                /*
                 * No need to call update_file() on path when !renamed, since
                 */
        }
        free(renamed);
 +
 +      return ret;
  }
  
 -static void conflict_rename_delete(struct merge_options *o,
 +static int conflict_rename_delete(struct merge_options *o,
                                   struct diff_filepair *pair,
                                   const char *rename_branch,
                                   const char *other_branch)
  {
        const struct diff_filespec *orig = pair->one;
        const struct diff_filespec *dest = pair->two;
 -      const unsigned char *a_sha = NULL;
 -      const unsigned char *b_sha = NULL;
 +      const struct object_id *a_oid = NULL;
 +      const struct object_id *b_oid = NULL;
        int a_mode = 0;
        int b_mode = 0;
  
        if (rename_branch == o->branch1) {
 -              a_sha = dest->sha1;
 +              a_oid = &dest->oid;
                a_mode = dest->mode;
        } else {
 -              b_sha = dest->sha1;
 +              b_oid = &dest->oid;
                b_mode = dest->mode;
        }
  
 -      handle_change_delete(o,
 -                           o->call_depth ? orig->path : dest->path,
 -                           orig->sha1, orig->mode,
 -                           a_sha, a_mode,
 -                           b_sha, b_mode,
 -                           _("rename"), _("renamed"));
 -
 -      if (o->call_depth) {
 -              remove_file_from_cache(dest->path);
 -      } else {
 -              update_stages(dest->path, NULL,
 -                            rename_branch == o->branch1 ? dest : NULL,
 -                            rename_branch == o->branch1 ? NULL : dest);
 -      }
 +      if (handle_change_delete(o,
 +                               o->call_depth ? orig->path : dest->path,
 +                               &orig->oid, orig->mode,
 +                               a_oid, a_mode,
 +                               b_oid, b_mode,
 +                               _("rename"), _("renamed")))
 +              return -1;
  
 +      if (o->call_depth)
 +              return remove_file_from_cache(dest->path);
 +      else
 +              return update_stages(o, dest->path, NULL,
 +                                   rename_branch == o->branch1 ? dest : NULL,
 +                                   rename_branch == o->branch1 ? NULL : dest);
  }
  
  static struct diff_filespec *filespec_from_entry(struct diff_filespec *target,
                                                 struct stage_data *entry,
                                                 int stage)
  {
 -      unsigned char *sha = entry->stages[stage].sha;
 +      struct object_id *oid = &entry->stages[stage].oid;
        unsigned mode = entry->stages[stage].mode;
 -      if (mode == 0 || is_null_sha1(sha))
 +      if (mode == 0 || is_null_oid(oid))
                return NULL;
 -      hashcpy(target->sha1, sha);
 +      oidcpy(&target->oid, oid);
        target->mode = mode;
        return target;
  }
  
 -static void handle_file(struct merge_options *o,
 +static int handle_file(struct merge_options *o,
                        struct diff_filespec *rename,
                        int stage,
                        struct rename_conflict_info *ci)
        const char *cur_branch, *other_branch;
        struct diff_filespec other;
        struct diff_filespec *add;
 +      int ret;
  
        if (stage == 2) {
                dst_entry = ci->dst_entry1;
        add = filespec_from_entry(&other, dst_entry, stage ^ 1);
        if (add) {
                char *add_name = unique_path(o, rename->path, other_branch);
 -              update_file(o, 0, add->sha1, add->mode, add_name);
 +              if (update_file(o, 0, &add->oid, add->mode, add_name))
 +                      return -1;
  
                remove_file(o, 0, rename->path, 0);
                dst_name = unique_path(o, rename->path, cur_branch);
        } else {
 -              if (dir_in_way(rename->path, !o->call_depth)) {
 +              if (dir_in_way(rename->path, !o->call_depth, 0)) {
                        dst_name = unique_path(o, rename->path, cur_branch);
                        output(o, 1, _("%s is a directory in %s adding as %s instead"),
                               rename->path, other_branch, dst_name);
                }
        }
 -      update_file(o, 0, rename->sha1, rename->mode, dst_name);
 -      if (stage == 2)
 -              update_stages(rename->path, NULL, rename, add);
 +      if ((ret = update_file(o, 0, &rename->oid, rename->mode, dst_name)))
 +              ; /* fall through, do allow dst_name to be released */
 +      else if (stage == 2)
 +              ret = update_stages(o, rename->path, NULL, rename, add);
        else
 -              update_stages(rename->path, NULL, add, rename);
 +              ret = update_stages(o, rename->path, NULL, add, rename);
  
        if (dst_name != rename->path)
                free(dst_name);
 +
 +      return ret;
  }
  
 -static void conflict_rename_rename_1to2(struct merge_options *o,
 +static int conflict_rename_rename_1to2(struct merge_options *o,
                                        struct rename_conflict_info *ci)
  {
        /* One file was renamed in both branches, but to different names. */
                struct merge_file_info mfi;
                struct diff_filespec other;
                struct diff_filespec *add;
 -              mfi = merge_file_one(o, one->path,
 -                               one->sha1, one->mode,
 -                               a->sha1, a->mode,
 -                               b->sha1, b->mode,
 -                               ci->branch1, ci->branch2);
 +              if (merge_file_one(o, one->path,
 +                               &one->oid, one->mode,
 +                               &a->oid, a->mode,
 +                               &b->oid, b->mode,
 +                               ci->branch1, ci->branch2, &mfi))
 +                      return -1;
 +
                /*
                 * FIXME: For rename/add-source conflicts (if we could detect
                 * such), this is wrong.  We should instead find a unique
                 * pathname and then either rename the add-source file to that
                 * unique path, or use that unique path instead of src here.
                 */
 -              update_file(o, 0, mfi.sha, mfi.mode, one->path);
 +              if (update_file(o, 0, &mfi.oid, mfi.mode, one->path))
 +                      return -1;
  
                /*
                 * Above, we put the merged content at the merge-base's
                 * resolving the conflict at that path in its favor.
                 */
                add = filespec_from_entry(&other, ci->dst_entry1, 2 ^ 1);
 -              if (add)
 -                      update_file(o, 0, add->sha1, add->mode, a->path);
 +              if (add) {
 +                      if (update_file(o, 0, &add->oid, add->mode, a->path))
 +                              return -1;
 +              }
                else
                        remove_file_from_cache(a->path);
                add = filespec_from_entry(&other, ci->dst_entry2, 3 ^ 1);
 -              if (add)
 -                      update_file(o, 0, add->sha1, add->mode, b->path);
 +              if (add) {
 +                      if (update_file(o, 0, &add->oid, add->mode, b->path))
 +                              return -1;
 +              }
                else
                        remove_file_from_cache(b->path);
 -      } else {
 -              handle_file(o, a, 2, ci);
 -              handle_file(o, b, 3, ci);
 -      }
 +      } else if (handle_file(o, a, 2, ci) || handle_file(o, b, 3, ci))
 +              return -1;
 +
 +      return 0;
  }
  
 -static void conflict_rename_rename_2to1(struct merge_options *o,
 +static int conflict_rename_rename_2to1(struct merge_options *o,
                                        struct rename_conflict_info *ci)
  {
        /* Two files, a & b, were renamed to the same thing, c. */
        char *path = c1->path; /* == c2->path */
        struct merge_file_info mfi_c1;
        struct merge_file_info mfi_c2;
 +      int ret;
  
        output(o, 1, _("CONFLICT (rename/rename): "
               "Rename %s->%s in %s. "
               a->path, c1->path, ci->branch1,
               b->path, c2->path, ci->branch2);
  
 -      remove_file(o, 1, a->path, would_lose_untracked(a->path));
 -      remove_file(o, 1, b->path, would_lose_untracked(b->path));
 +      remove_file(o, 1, a->path, o->call_depth || would_lose_untracked(a->path));
 +      remove_file(o, 1, b->path, o->call_depth || would_lose_untracked(b->path));
  
 -      mfi_c1 = merge_file_special_markers(o, a, c1, &ci->ren1_other,
 -                                          o->branch1, c1->path,
 -                                          o->branch2, ci->ren1_other.path);
 -      mfi_c2 = merge_file_special_markers(o, b, &ci->ren2_other, c2,
 -                                          o->branch1, ci->ren2_other.path,
 -                                          o->branch2, c2->path);
 +      if (merge_file_special_markers(o, a, c1, &ci->ren1_other,
 +                                     o->branch1, c1->path,
 +                                     o->branch2, ci->ren1_other.path, &mfi_c1) ||
 +          merge_file_special_markers(o, b, &ci->ren2_other, c2,
 +                                     o->branch1, ci->ren2_other.path,
 +                                     o->branch2, c2->path, &mfi_c2))
 +              return -1;
  
        if (o->call_depth) {
                /*
                 * again later for the non-recursive merge.
                 */
                remove_file(o, 0, path, 0);
 -              update_file(o, 0, mfi_c1.sha, mfi_c1.mode, a->path);
 -              update_file(o, 0, mfi_c2.sha, mfi_c2.mode, b->path);
 +              ret = update_file(o, 0, &mfi_c1.oid, mfi_c1.mode, a->path);
 +              if (!ret)
 +                      ret = update_file(o, 0, &mfi_c2.oid, mfi_c2.mode,
 +                                        b->path);
        } else {
                char *new_path1 = unique_path(o, path, ci->branch1);
                char *new_path2 = unique_path(o, path, ci->branch2);
                output(o, 1, _("Renaming %s to %s and %s to %s instead"),
                       a->path, new_path1, b->path, new_path2);
                remove_file(o, 0, path, 0);
 -              update_file(o, 0, mfi_c1.sha, mfi_c1.mode, new_path1);
 -              update_file(o, 0, mfi_c2.sha, mfi_c2.mode, new_path2);
 +              ret = update_file(o, 0, &mfi_c1.oid, mfi_c1.mode, new_path1);
 +              if (!ret)
 +                      ret = update_file(o, 0, &mfi_c2.oid, mfi_c2.mode,
 +                                        new_path2);
                free(new_path2);
                free(new_path1);
        }
 +
 +      return ret;
  }
  
  static int process_renames(struct merge_options *o,
                        const char *ren2_dst = ren2->pair->two->path;
                        enum rename_type rename_type;
                        if (strcmp(ren1_src, ren2_src) != 0)
 -                              die("ren1_src != ren2_src");
 +                              die("BUG: ren1_src != ren2_src");
                        ren2->dst_entry->processed = 1;
                        ren2->processed = 1;
                        if (strcmp(ren1_dst, ren2_dst) != 0) {
                        ren2 = lookup->util;
                        ren2_dst = ren2->pair->two->path;
                        if (strcmp(ren1_dst, ren2_dst) != 0)
 -                              die("ren1_dst != ren2_dst");
 +                              die("BUG: ren1_dst != ren2_dst");
  
                        clean_merge = 0;
                        ren2->processed = 1;
                        remove_file(o, 1, ren1_src,
                                    renamed_stage == 2 || !was_tracked(ren1_src));
  
 -                      hashcpy(src_other.sha1, ren1->src_entry->stages[other_stage].sha);
 +                      oidcpy(&src_other.oid,
 +                             &ren1->src_entry->stages[other_stage].oid);
                        src_other.mode = ren1->src_entry->stages[other_stage].mode;
 -                      hashcpy(dst_other.sha1, ren1->dst_entry->stages[other_stage].sha);
 +                      oidcpy(&dst_other.oid,
 +                             &ren1->dst_entry->stages[other_stage].oid);
                        dst_other.mode = ren1->dst_entry->stages[other_stage].mode;
                        try_merge = 0;
  
 -                      if (sha_eq(src_other.sha1, null_sha1)) {
 +                      if (oid_eq(&src_other.oid, &null_oid)) {
                                setup_rename_conflict_info(RENAME_DELETE,
                                                           ren1->pair,
                                                           NULL,
                                                           NULL,
                                                           NULL);
                        } else if ((dst_other.mode == ren1->pair->two->mode) &&
 -                                 sha_eq(dst_other.sha1, ren1->pair->two->sha1)) {
 +                                 oid_eq(&dst_other.oid, &ren1->pair->two->oid)) {
                                /*
                                 * Added file on the other side identical to
                                 * the file being renamed: clean merge.
                                 * update_file_flags() instead of
                                 * update_file().
                                 */
 -                              update_file_flags(o,
 -                                                ren1->pair->two->sha1,
 -                                                ren1->pair->two->mode,
 -                                                ren1_dst,
 -                                                1, /* update_cache */
 -                                                0  /* update_wd    */);
 -                      } else if (!sha_eq(dst_other.sha1, null_sha1)) {
 +                              if (update_file_flags(o,
 +                                                    &ren1->pair->two->oid,
 +                                                    ren1->pair->two->mode,
 +                                                    ren1_dst,
 +                                                    1, /* update_cache */
 +                                                    0  /* update_wd    */))
 +                                      clean_merge = -1;
 +                      } else if (!oid_eq(&dst_other.oid, &null_oid)) {
                                clean_merge = 0;
                                try_merge = 1;
                                output(o, 1, _("CONFLICT (rename/add): Rename %s->%s in %s. "
                                       ren1_dst, branch2);
                                if (o->call_depth) {
                                        struct merge_file_info mfi;
 -                                      mfi = merge_file_one(o, ren1_dst, null_sha1, 0,
 -                                                       ren1->pair->two->sha1, ren1->pair->two->mode,
 -                                                       dst_other.sha1, dst_other.mode,
 -                                                       branch1, branch2);
 +                                      if (merge_file_one(o, ren1_dst, &null_oid, 0,
 +                                                         &ren1->pair->two->oid,
 +                                                         ren1->pair->two->mode,
 +                                                         &dst_other.oid,
 +                                                         dst_other.mode,
 +                                                         branch1, branch2, &mfi)) {
 +                                              clean_merge = -1;
 +                                              goto cleanup_and_return;
 +                                      }
                                        output(o, 1, _("Adding merged %s"), ren1_dst);
 -                                      update_file(o, 0, mfi.sha, mfi.mode, ren1_dst);
 +                                      if (update_file(o, 0, &mfi.oid,
 +                                                      mfi.mode, ren1_dst))
 +                                              clean_merge = -1;
                                        try_merge = 0;
                                } else {
                                        char *new_path = unique_path(o, ren1_dst, branch2);
                                        output(o, 1, _("Adding as %s instead"), new_path);
 -                                      update_file(o, 0, dst_other.sha1, dst_other.mode, new_path);
 +                                      if (update_file(o, 0, &dst_other.oid,
 +                                                      dst_other.mode, new_path))
 +                                              clean_merge = -1;
                                        free(new_path);
                                }
                        } else
                                try_merge = 1;
  
 +                      if (clean_merge < 0)
 +                              goto cleanup_and_return;
                        if (try_merge) {
                                struct diff_filespec *one, *a, *b;
                                src_other.path = (char *)ren1_src;
                        }
                }
        }
 +cleanup_and_return:
        string_list_clear(&a_by_dst, 0);
        string_list_clear(&b_by_dst, 0);
  
        return clean_merge;
  }
  
 -static unsigned char *stage_sha(const unsigned char *sha, unsigned mode)
 +static struct object_id *stage_oid(const struct object_id *oid, unsigned mode)
  {
 -      return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha;
 +      return (is_null_oid(oid) || mode == 0) ? NULL: (struct object_id *)oid;
  }
  
 -static int read_sha1_strbuf(const unsigned char *sha1, struct strbuf *dst)
 +static int read_oid_strbuf(struct merge_options *o,
 +      const struct object_id *oid, struct strbuf *dst)
  {
        void *buf;
        enum object_type type;
        unsigned long size;
 -      buf = read_sha1_file(sha1, &type, &size);
 +      buf = read_sha1_file(oid->hash, &type, &size);
        if (!buf)
 -              return error(_("cannot read object %s"), sha1_to_hex(sha1));
 +              return err(o, _("cannot read object %s"), oid_to_hex(oid));
        if (type != OBJ_BLOB) {
                free(buf);
 -              return error(_("object %s is not a blob"), sha1_to_hex(sha1));
 +              return err(o, _("object %s is not a blob"), oid_to_hex(oid));
        }
        strbuf_attach(dst, buf, size, size + 1);
        return 0;
  }
  
 -static int blob_unchanged(const unsigned char *o_sha,
 +static int blob_unchanged(struct merge_options *opt,
 +                        const struct object_id *o_oid,
                          unsigned o_mode,
 -                        const unsigned char *a_sha,
 +                        const struct object_id *a_oid,
                          unsigned a_mode,
                          int renormalize, const char *path)
  {
  
        if (a_mode != o_mode)
                return 0;
 -      if (sha_eq(o_sha, a_sha))
 +      if (oid_eq(o_oid, a_oid))
                return 1;
        if (!renormalize)
                return 0;
  
 -      assert(o_sha && a_sha);
 -      if (read_sha1_strbuf(o_sha, &o) || read_sha1_strbuf(a_sha, &a))
 +      assert(o_oid && a_oid);
 +      if (read_oid_strbuf(opt, o_oid, &o) || read_oid_strbuf(opt, a_oid, &a))
                goto error_return;
        /*
         * Note: binary | is used so that both renormalizations are
@@@ -1657,25 -1578,25 +1659,25 @@@ error_return
        return ret;
  }
  
 -static void handle_modify_delete(struct merge_options *o,
 +static int handle_modify_delete(struct merge_options *o,
                                 const char *path,
 -                               unsigned char *o_sha, int o_mode,
 -                               unsigned char *a_sha, int a_mode,
 -                               unsigned char *b_sha, int b_mode)
 +                               struct object_id *o_oid, int o_mode,
 +                               struct object_id *a_oid, int a_mode,
 +                               struct object_id *b_oid, int b_mode)
  {
 -      handle_change_delete(o,
 -                           path,
 -                           o_sha, o_mode,
 -                           a_sha, a_mode,
 -                           b_sha, b_mode,
 -                           _("modify"), _("modified"));
 +      return handle_change_delete(o,
 +                                  path,
 +                                  o_oid, o_mode,
 +                                  a_oid, a_mode,
 +                                  b_oid, b_mode,
 +                                  _("modify"), _("modified"));
  }
  
  static int merge_content(struct merge_options *o,
                         const char *path,
 -                       unsigned char *o_sha, int o_mode,
 -                       unsigned char *a_sha, int a_mode,
 -                       unsigned char *b_sha, int b_mode,
 +                       struct object_id *o_oid, int o_mode,
 +                       struct object_id *a_oid, int a_mode,
 +                       struct object_id *b_oid, int b_mode,
                         struct rename_conflict_info *rename_conflict_info)
  {
        const char *reason = _("content");
        struct diff_filespec one, a, b;
        unsigned df_conflict_remains = 0;
  
 -      if (!o_sha) {
 +      if (!o_oid) {
                reason = _("add/add");
 -              o_sha = (unsigned char *)null_sha1;
 +              o_oid = (struct object_id *)&null_oid;
        }
        one.path = a.path = b.path = (char *)path;
 -      hashcpy(one.sha1, o_sha);
 +      oidcpy(&one.oid, o_oid);
        one.mode = o_mode;
 -      hashcpy(a.sha1, a_sha);
 +      oidcpy(&a.oid, a_oid);
        a.mode = a_mode;
 -      hashcpy(b.sha1, b_sha);
 +      oidcpy(&b.oid, b_oid);
        b.mode = b_mode;
  
        if (rename_conflict_info) {
                         o->branch2 == rename_conflict_info->branch1) ?
                        pair1->two->path : pair1->one->path;
  
 -              if (dir_in_way(path, !o->call_depth))
 +              if (dir_in_way(path, !o->call_depth,
 +                             S_ISGITLINK(pair1->two->mode)))
                        df_conflict_remains = 1;
        }
 -      mfi = merge_file_special_markers(o, &one, &a, &b,
 -                                       o->branch1, path1,
 -                                       o->branch2, path2);
 +      if (merge_file_special_markers(o, &one, &a, &b,
 +                                     o->branch1, path1,
 +                                     o->branch2, path2, &mfi))
 +              return -1;
  
        if (mfi.clean && !df_conflict_remains &&
 -          sha_eq(mfi.sha, a_sha) && mfi.mode == a_mode) {
 +          oid_eq(&mfi.oid, a_oid) && mfi.mode == a_mode) {
                int path_renamed_outside_HEAD;
                output(o, 3, _("Skipped %s (merged same as existing)"), path);
                /*
                 */
                path_renamed_outside_HEAD = !path2 || !strcmp(path, path2);
                if (!path_renamed_outside_HEAD) {
 -                      add_cacheinfo(mfi.mode, mfi.sha, path,
 +                      add_cacheinfo(o, mfi.mode, &mfi.oid, path,
                                      0, (!o->call_depth), 0);
                        return mfi.clean;
                }
                output(o, 1, _("CONFLICT (%s): Merge conflict in %s"),
                                reason, path);
                if (rename_conflict_info && !df_conflict_remains)
 -                      update_stages(path, &one, &a, &b);
 +                      if (update_stages(o, path, &one, &a, &b))
 +                              return -1;
        }
  
        if (df_conflict_remains) {
                if (o->call_depth) {
                        remove_file_from_cache(path);
                } else {
 -                      if (!mfi.clean)
 -                              update_stages(path, &one, &a, &b);
 -                      else {
 +                      if (!mfi.clean) {
 +                              if (update_stages(o, path, &one, &a, &b))
 +                                      return -1;
 +                      } else {
                                int file_from_stage2 = was_tracked(path);
                                struct diff_filespec merged;
 -                              hashcpy(merged.sha1, mfi.sha);
 +                              oidcpy(&merged.oid, &mfi.oid);
                                merged.mode = mfi.mode;
  
 -                              update_stages(path, NULL,
 -                                            file_from_stage2 ? &merged : NULL,
 -                                            file_from_stage2 ? NULL : &merged);
 +                              if (update_stages(o, path, NULL,
 +                                                file_from_stage2 ? &merged : NULL,
 +                                                file_from_stage2 ? NULL : &merged))
 +                                      return -1;
                        }
  
                }
                new_path = unique_path(o, path, rename_conflict_info->branch1);
                output(o, 1, _("Adding as %s instead"), new_path);
 -              update_file(o, 0, mfi.sha, mfi.mode, new_path);
 +              if (update_file(o, 0, &mfi.oid, mfi.mode, new_path)) {
 +                      free(new_path);
 +                      return -1;
 +              }
                free(new_path);
                mfi.clean = 0;
 -      } else {
 -              update_file(o, mfi.clean, mfi.sha, mfi.mode, path);
 -      }
 +      } else if (update_file(o, mfi.clean, &mfi.oid, mfi.mode, path))
 +              return -1;
        return mfi.clean;
 -
  }
  
  /* Per entry merge function */
@@@ -1790,9 -1705,9 +1792,9 @@@ static int process_entry(struct merge_o
        unsigned o_mode = entry->stages[1].mode;
        unsigned a_mode = entry->stages[2].mode;
        unsigned b_mode = entry->stages[3].mode;
 -      unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode);
 -      unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode);
 -      unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode);
 +      struct object_id *o_oid = stage_oid(&entry->stages[1].oid, o_mode);
 +      struct object_id *a_oid = stage_oid(&entry->stages[2].oid, a_mode);
 +      struct object_id *b_oid = stage_oid(&entry->stages[3].oid, b_mode);
  
        entry->processed = 1;
        if (entry->rename_conflict_info) {
                case RENAME_NORMAL:
                case RENAME_ONE_FILE_TO_ONE:
                        clean_merge = merge_content(o, path,
 -                                                  o_sha, o_mode, a_sha, a_mode, b_sha, b_mode,
 +                                                  o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,
                                                    conflict_info);
                        break;
                case RENAME_DELETE:
                        clean_merge = 0;
 -                      conflict_rename_delete(o, conflict_info->pair1,
 -                                             conflict_info->branch1,
 -                                             conflict_info->branch2);
 +                      if (conflict_rename_delete(o,
 +                                                 conflict_info->pair1,
 +                                                 conflict_info->branch1,
 +                                                 conflict_info->branch2))
 +                              clean_merge = -1;
                        break;
                case RENAME_ONE_FILE_TO_TWO:
                        clean_merge = 0;
 -                      conflict_rename_rename_1to2(o, conflict_info);
 +                      if (conflict_rename_rename_1to2(o, conflict_info))
 +                              clean_merge = -1;
                        break;
                case RENAME_TWO_FILES_TO_ONE:
                        clean_merge = 0;
 -                      conflict_rename_rename_2to1(o, conflict_info);
 +                      if (conflict_rename_rename_2to1(o, conflict_info))
 +                              clean_merge = -1;
                        break;
                default:
                        entry->processed = 0;
                        break;
                }
 -      } else if (o_sha && (!a_sha || !b_sha)) {
 +      } else if (o_oid && (!a_oid || !b_oid)) {
                /* Case A: Deleted in one */
 -              if ((!a_sha && !b_sha) ||
 -                  (!b_sha && blob_unchanged(o_sha, o_mode, a_sha, a_mode, normalize, path)) ||
 -                  (!a_sha && blob_unchanged(o_sha, o_mode, b_sha, b_mode, normalize, path))) {
 +              if ((!a_oid && !b_oid) ||
 +                  (!b_oid && blob_unchanged(o, o_oid, o_mode, a_oid, a_mode, normalize, path)) ||
 +                  (!a_oid && blob_unchanged(o, o_oid, o_mode, b_oid, b_mode, normalize, path))) {
                        /* Deleted in both or deleted in one and
                         * unchanged in the other */
 -                      if (a_sha)
 +                      if (a_oid)
                                output(o, 2, _("Removing %s"), path);
                        /* do not touch working file if it did not exist */
 -                      remove_file(o, 1, path, !a_sha);
 +                      remove_file(o, 1, path, !a_oid);
                } else {
                        /* Modify/delete; deleted side may have put a directory in the way */
                        clean_merge = 0;
 -                      handle_modify_delete(o, path, o_sha, o_mode,
 -                                           a_sha, a_mode, b_sha, b_mode);
 +                      if (handle_modify_delete(o, path, o_oid, o_mode,
 +                                               a_oid, a_mode, b_oid, b_mode))
 +                              clean_merge = -1;
                }
 -      } else if ((!o_sha && a_sha && !b_sha) ||
 -                 (!o_sha && !a_sha && b_sha)) {
 +      } else if ((!o_oid && a_oid && !b_oid) ||
 +                 (!o_oid && !a_oid && b_oid)) {
                /* Case B: Added in one. */
                /* [nothing|directory] -> ([nothing|directory], file) */
  
                const char *add_branch;
                const char *other_branch;
                unsigned mode;
 -              const unsigned char *sha;
 +              const struct object_id *oid;
                const char *conf;
  
 -              if (a_sha) {
 +              if (a_oid) {
                        add_branch = o->branch1;
                        other_branch = o->branch2;
                        mode = a_mode;
 -                      sha = a_sha;
 +                      oid = a_oid;
                        conf = _("file/directory");
                } else {
                        add_branch = o->branch2;
                        other_branch = o->branch1;
                        mode = b_mode;
 -                      sha = b_sha;
 +                      oid = b_oid;
                        conf = _("directory/file");
                }
 -              if (dir_in_way(path, !o->call_depth)) {
 +              if (dir_in_way(path, !o->call_depth,
 +                             S_ISGITLINK(a_mode))) {
                        char *new_path = unique_path(o, path, add_branch);
                        clean_merge = 0;
                        output(o, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
                               "Adding %s as %s"),
                               conf, path, other_branch, path, new_path);
 -                      if (o->call_depth)
 -                              remove_file_from_cache(path);
 -                      update_file(o, 0, sha, mode, new_path);
 -                      if (o->call_depth)
 +                      if (update_file(o, 0, oid, mode, new_path))
 +                              clean_merge = -1;
 +                      else if (o->call_depth)
                                remove_file_from_cache(path);
                        free(new_path);
                } else {
                        output(o, 2, _("Adding %s"), path);
                        /* do not overwrite file if already present */
 -                      update_file_flags(o, sha, mode, path, 1, !a_sha);
 +                      if (update_file_flags(o, oid, mode, path, 1, !a_oid))
 +                              clean_merge = -1;
                }
 -      } else if (a_sha && b_sha) {
 +      } else if (a_oid && b_oid) {
                /* Case C: Added in both (check for same permissions) and */
                /* case D: Modified in both, but differently. */
                clean_merge = merge_content(o, path,
 -                                          o_sha, o_mode, a_sha, a_mode, b_sha, b_mode,
 +                                          o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,
                                            NULL);
 -      } else if (!o_sha && !a_sha && !b_sha) {
 +      } else if (!o_oid && !a_oid && !b_oid) {
                /*
                 * this entry was deleted altogether. a_mode == 0 means
                 * we had that path and want to actively remove it.
                 */
                remove_file(o, 1, path, !a_mode);
        } else
 -              die(_("Fatal merge failure, shouldn't happen."));
 +              die("BUG: fatal merge failure, shouldn't happen.");
  
        return clean_merge;
  }
@@@ -1917,7 -1826,7 +1919,7 @@@ int merge_trees(struct merge_options *o
                common = shift_tree_object(head, common, o->subtree_shift);
        }
  
 -      if (sha_eq(common->object.oid.hash, merge->object.oid.hash)) {
 +      if (oid_eq(&common->object.oid, &merge->object.oid)) {
                output(o, 0, _("Already up-to-date!"));
                *result = head;
                return 1;
  
        if (code != 0) {
                if (show(o, 4) || o->call_depth)
 -                      die(_("merging of trees %s and %s failed"),
 +                      err(o, _("merging of trees %s and %s failed"),
                            oid_to_hex(&head->object.oid),
                            oid_to_hex(&merge->object.oid));
 -              else
 -                      exit(128);
 +              return -1;
        }
  
        if (unmerged_cache()) {
                re_head  = get_renames(o, head, common, head, merge, entries);
                re_merge = get_renames(o, merge, common, head, merge, entries);
                clean = process_renames(o, re_head, re_merge);
 +              if (clean < 0)
 +                      return clean;
                for (i = entries->nr-1; 0 <= i; i--) {
                        const char *path = entries->items[i].string;
                        struct stage_data *e = entries->items[i].util;
 -                      if (!e->processed
 -                              && !process_entry(o, path, e))
 -                              clean = 0;
 +                      if (!e->processed) {
 +                              int ret = process_entry(o, path, e);
 +                              if (!ret)
 +                                      clean = 0;
 +                              else if (ret < 0)
 +                                      return ret;
 +                      }
                }
                for (i = 0; i < entries->nr; i++) {
                        struct stage_data *e = entries->items[i].util;
                        if (!e->processed)
 -                              die(_("Unprocessed path??? %s"),
 +                              die("BUG: unprocessed path??? %s",
                                    entries->items[i].string);
                }
  
        else
                clean = 1;
  
 -      if (o->call_depth)
 -              *result = write_tree_from_memory(o);
 +      if (o->call_depth && !(*result = write_tree_from_memory(o)))
 +              return -1;
  
        return clean;
  }
@@@ -2044,25 -1948,23 +2046,25 @@@ int merge_recursive(struct merge_option
                /*
                 * When the merge fails, the result contains files
                 * with conflict markers. The cleanness flag is
 -               * ignored, it was never actually used, as result of
 -               * merge_trees has always overwritten it: the committed
 -               * "conflicts" were already resolved.
 +               * ignored (unless indicating an error), it was never
 +               * actually used, as result of merge_trees has always
 +               * overwritten it: the committed "conflicts" were
 +               * already resolved.
                 */
                discard_cache();
                saved_b1 = o->branch1;
                saved_b2 = o->branch2;
                o->branch1 = "Temporary merge branch 1";
                o->branch2 = "Temporary merge branch 2";
 -              merge_recursive(o, merged_common_ancestors, iter->item,
 -                              NULL, &merged_common_ancestors);
 +              if (merge_recursive(o, merged_common_ancestors, iter->item,
 +                                  NULL, &merged_common_ancestors) < 0)
 +                      return -1;
                o->branch1 = saved_b1;
                o->branch2 = saved_b2;
                o->call_depth--;
  
                if (!merged_common_ancestors)
 -                      die(_("merge returned no commit"));
 +                      return err(o, _("merge returned no commit"));
        }
  
        discard_cache();
        o->ancestor = "merged common ancestors";
        clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,
                            &mrtree);
 +      if (clean < 0) {
 +              flush_output(o);
 +              return clean;
 +      }
  
        if (o->call_depth) {
                *result = make_virtual_commit(mrtree, "merged tree");
                commit_list_insert(h2, &(*result)->parents->next);
        }
        flush_output(o);
 +      if (!o->call_depth && o->buffer_output < 2)
 +              strbuf_release(&o->obuf);
        if (show(o, 2))
                diff_warn_rename_limit("merge.renamelimit",
                                       o->needed_rename_limit, 0);
        return clean;
  }
  
 -static struct commit *get_ref(const unsigned char *sha1, const char *name)
 +static struct commit *get_ref(const struct object_id *oid, const char *name)
  {
        struct object *object;
  
 -      object = deref_tag(parse_object(sha1), name, strlen(name));
 +      object = deref_tag(parse_object(oid->hash), name, strlen(name));
        if (!object)
                return NULL;
        if (object->type == OBJ_TREE)
  }
  
  int merge_recursive_generic(struct merge_options *o,
 -                          const unsigned char *head,
 -                          const unsigned char *merge,
 +                          const struct object_id *head,
 +                          const struct object_id *merge,
                            int num_base_list,
 -                          const unsigned char **base_list,
 +                          const struct object_id **base_list,
                            struct commit **result)
  {
        int clean;
                int i;
                for (i = 0; i < num_base_list; ++i) {
                        struct commit *base;
 -                      if (!(base = get_ref(base_list[i], sha1_to_hex(base_list[i]))))
 -                              return error(_("Could not parse object '%s'"),
 -                                      sha1_to_hex(base_list[i]));
 +                      if (!(base = get_ref(base_list[i], oid_to_hex(base_list[i]))))
 +                              return err(o, _("Could not parse object '%s'"),
 +                                      oid_to_hex(base_list[i]));
                        commit_list_insert(base, &ca);
                }
        }
        hold_locked_index(lock, 1);
        clean = merge_recursive(o, head_commit, next_commit, ca,
                        result);
 +      if (clean < 0)
 +              return clean;
 +
        if (active_cache_changed &&
            write_locked_index(&the_index, lock, COMMIT_LOCK))
 -              return error(_("Unable to write index."));
 +              return err(o, _("Unable to write index."));
  
        return clean ? 0 : 1;
  }
index 394f0005a195c1fbc713226c97ecee5cf4600007,22970d2223853a23da8d5ef4b5719c8c56028ff9..4f2a263b63e14348032959059c15b160fecba39c
@@@ -96,7 -96,7 +96,7 @@@ test_expect_success 'revert forbidden o
        echo content >extra_file &&
        git add extra_file &&
        test_must_fail git revert HEAD 2>errors &&
 -      test_i18ngrep "Your local changes would be overwritten by " errors
 +      test_i18ngrep "your local changes would be overwritten by " errors
  
  '
  
@@@ -141,4 -141,16 +141,16 @@@ test_expect_success 'cherry-pick "-" wo
        test_cmp expect actual
  '
  
+ test_expect_success 'cherry-pick works with dirty renamed file' '
+       test_commit to-rename &&
+       git checkout -b unrelated &&
+       test_commit unrelated &&
+       git checkout @{-1} &&
+       git mv to-rename.t renamed &&
+       test_tick &&
+       git commit -m renamed &&
+       echo modified >renamed &&
+       git cherry-pick refs/heads/unrelated
+ '
  test_done