merge-base.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "builtin.h"
  2. #include "cache.h"
  3. #include "config.h"
  4. #include "commit.h"
  5. #include "refs.h"
  6. #include "diff.h"
  7. #include "revision.h"
  8. #include "parse-options.h"
  9. #include "repository.h"
  10. #include "commit-reach.h"
  11. static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
  12. {
  13. struct commit_list *result, *r;
  14. result = get_merge_bases_many_dirty(rev[0], rev_nr - 1, rev + 1);
  15. if (!result)
  16. return 1;
  17. for (r = result; r; r = r->next) {
  18. printf("%s\n", oid_to_hex(&r->item->object.oid));
  19. if (!show_all)
  20. break;
  21. }
  22. free_commit_list(result);
  23. return 0;
  24. }
  25. static const char * const merge_base_usage[] = {
  26. N_("git merge-base [-a | --all] <commit> <commit>..."),
  27. N_("git merge-base [-a | --all] --octopus <commit>..."),
  28. N_("git merge-base --independent <commit>..."),
  29. N_("git merge-base --is-ancestor <commit> <commit>"),
  30. N_("git merge-base --fork-point <ref> [<commit>]"),
  31. NULL
  32. };
  33. static struct commit *get_commit_reference(const char *arg)
  34. {
  35. struct object_id revkey;
  36. struct commit *r;
  37. if (get_oid(arg, &revkey))
  38. die("Not a valid object name %s", arg);
  39. r = lookup_commit_reference(the_repository, &revkey);
  40. if (!r)
  41. die("Not a valid commit name %s", arg);
  42. return r;
  43. }
  44. static int handle_independent(int count, const char **args)
  45. {
  46. struct commit_list *revs = NULL, *rev;
  47. int i;
  48. for (i = count - 1; i >= 0; i--)
  49. commit_list_insert(get_commit_reference(args[i]), &revs);
  50. reduce_heads_replace(&revs);
  51. if (!revs)
  52. return 1;
  53. for (rev = revs; rev; rev = rev->next)
  54. printf("%s\n", oid_to_hex(&rev->item->object.oid));
  55. free_commit_list(revs);
  56. return 0;
  57. }
  58. static int handle_octopus(int count, const char **args, int show_all)
  59. {
  60. struct commit_list *revs = NULL;
  61. struct commit_list *result, *rev;
  62. int i;
  63. for (i = count - 1; i >= 0; i--)
  64. commit_list_insert(get_commit_reference(args[i]), &revs);
  65. result = get_octopus_merge_bases(revs);
  66. free_commit_list(revs);
  67. reduce_heads_replace(&result);
  68. if (!result)
  69. return 1;
  70. for (rev = result; rev; rev = rev->next) {
  71. printf("%s\n", oid_to_hex(&rev->item->object.oid));
  72. if (!show_all)
  73. break;
  74. }
  75. free_commit_list(result);
  76. return 0;
  77. }
  78. static int handle_is_ancestor(int argc, const char **argv)
  79. {
  80. struct commit *one, *two;
  81. if (argc != 2)
  82. die("--is-ancestor takes exactly two commits");
  83. one = get_commit_reference(argv[0]);
  84. two = get_commit_reference(argv[1]);
  85. if (in_merge_bases(one, two))
  86. return 0;
  87. else
  88. return 1;
  89. }
  90. static int handle_fork_point(int argc, const char **argv)
  91. {
  92. struct object_id oid;
  93. struct commit *derived, *fork_point;
  94. const char *commitname;
  95. commitname = (argc == 2) ? argv[1] : "HEAD";
  96. if (get_oid(commitname, &oid))
  97. die("Not a valid object name: '%s'", commitname);
  98. derived = lookup_commit_reference(the_repository, &oid);
  99. fork_point = get_fork_point(argv[0], derived);
  100. if (!fork_point)
  101. return 1;
  102. printf("%s\n", oid_to_hex(&fork_point->object.oid));
  103. return 0;
  104. }
  105. int cmd_merge_base(int argc, const char **argv, const char *prefix)
  106. {
  107. struct commit **rev;
  108. int rev_nr = 0;
  109. int show_all = 0;
  110. int cmdmode = 0;
  111. struct option options[] = {
  112. OPT_BOOL('a', "all", &show_all, N_("output all common ancestors")),
  113. OPT_CMDMODE(0, "octopus", &cmdmode,
  114. N_("find ancestors for a single n-way merge"), 'o'),
  115. OPT_CMDMODE(0, "independent", &cmdmode,
  116. N_("list revs not reachable from others"), 'r'),
  117. OPT_CMDMODE(0, "is-ancestor", &cmdmode,
  118. N_("is the first one ancestor of the other?"), 'a'),
  119. OPT_CMDMODE(0, "fork-point", &cmdmode,
  120. N_("find where <commit> forked from reflog of <ref>"), 'f'),
  121. OPT_END()
  122. };
  123. git_config(git_default_config, NULL);
  124. argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
  125. if (cmdmode == 'a') {
  126. if (argc < 2)
  127. usage_with_options(merge_base_usage, options);
  128. if (show_all)
  129. die("--is-ancestor cannot be used with --all");
  130. return handle_is_ancestor(argc, argv);
  131. }
  132. if (cmdmode == 'r' && show_all)
  133. die("--independent cannot be used with --all");
  134. if (cmdmode == 'o')
  135. return handle_octopus(argc, argv, show_all);
  136. if (cmdmode == 'r')
  137. return handle_independent(argc, argv);
  138. if (cmdmode == 'f') {
  139. if (argc < 1 || 2 < argc)
  140. usage_with_options(merge_base_usage, options);
  141. return handle_fork_point(argc, argv);
  142. }
  143. if (argc < 2)
  144. usage_with_options(merge_base_usage, options);
  145. ALLOC_ARRAY(rev, argc);
  146. while (argc-- > 0)
  147. rev[rev_nr++] = get_commit_reference(*argv++);
  148. return show_merge_base(rev, rev_nr, show_all);
  149. }