merge.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "cache.h"
  2. #include "diff.h"
  3. #include "diffcore.h"
  4. #include "lockfile.h"
  5. #include "commit.h"
  6. #include "run-command.h"
  7. #include "resolve-undo.h"
  8. #include "tree-walk.h"
  9. #include "unpack-trees.h"
  10. #include "dir.h"
  11. static const char *merge_argument(struct commit *commit)
  12. {
  13. return oid_to_hex(commit ? &commit->object.oid : the_hash_algo->empty_tree);
  14. }
  15. int try_merge_command(struct repository *r,
  16. const char *strategy, size_t xopts_nr,
  17. const char **xopts, struct commit_list *common,
  18. const char *head_arg, struct commit_list *remotes)
  19. {
  20. struct strvec args = STRVEC_INIT;
  21. int i, ret;
  22. struct commit_list *j;
  23. strvec_pushf(&args, "merge-%s", strategy);
  24. for (i = 0; i < xopts_nr; i++)
  25. strvec_pushf(&args, "--%s", xopts[i]);
  26. for (j = common; j; j = j->next)
  27. strvec_push(&args, merge_argument(j->item));
  28. strvec_push(&args, "--");
  29. strvec_push(&args, head_arg);
  30. for (j = remotes; j; j = j->next)
  31. strvec_push(&args, merge_argument(j->item));
  32. ret = run_command_v_opt(args.v, RUN_GIT_CMD);
  33. strvec_clear(&args);
  34. discard_index(r->index);
  35. if (repo_read_index(r) < 0)
  36. die(_("failed to read the cache"));
  37. resolve_undo_clear_index(r->index);
  38. return ret;
  39. }
  40. int checkout_fast_forward(struct repository *r,
  41. const struct object_id *head,
  42. const struct object_id *remote,
  43. int overwrite_ignore)
  44. {
  45. struct tree *trees[MAX_UNPACK_TREES];
  46. struct unpack_trees_options opts;
  47. struct tree_desc t[MAX_UNPACK_TREES];
  48. int i, nr_trees = 0;
  49. struct dir_struct dir;
  50. struct lock_file lock_file = LOCK_INIT;
  51. refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
  52. if (repo_hold_locked_index(r, &lock_file, LOCK_REPORT_ON_ERROR) < 0)
  53. return -1;
  54. memset(&trees, 0, sizeof(trees));
  55. memset(&t, 0, sizeof(t));
  56. trees[nr_trees] = parse_tree_indirect(head);
  57. if (!trees[nr_trees++]) {
  58. rollback_lock_file(&lock_file);
  59. return -1;
  60. }
  61. trees[nr_trees] = parse_tree_indirect(remote);
  62. if (!trees[nr_trees++]) {
  63. rollback_lock_file(&lock_file);
  64. return -1;
  65. }
  66. for (i = 0; i < nr_trees; i++) {
  67. parse_tree(trees[i]);
  68. init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
  69. }
  70. memset(&opts, 0, sizeof(opts));
  71. dir_init(&dir);
  72. if (overwrite_ignore) {
  73. dir.flags |= DIR_SHOW_IGNORED;
  74. setup_standard_excludes(&dir);
  75. opts.dir = &dir;
  76. }
  77. opts.head_idx = 1;
  78. opts.src_index = r->index;
  79. opts.dst_index = r->index;
  80. opts.update = 1;
  81. opts.verbose_update = 1;
  82. opts.merge = 1;
  83. opts.fn = twoway_merge;
  84. init_checkout_metadata(&opts.meta, NULL, remote, NULL);
  85. setup_unpack_trees_porcelain(&opts, "merge");
  86. if (unpack_trees(nr_trees, t, &opts)) {
  87. rollback_lock_file(&lock_file);
  88. clear_unpack_trees_porcelain(&opts);
  89. return -1;
  90. }
  91. dir_clear(&dir);
  92. clear_unpack_trees_porcelain(&opts);
  93. if (write_locked_index(r->index, &lock_file, COMMIT_LOCK))
  94. return error(_("unable to write new index file"));
  95. return 0;
  96. }