git-merge-octopus.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/bin/sh
  2. #
  3. # Copyright (c) 2005 Junio C Hamano
  4. #
  5. # Resolve two or more trees.
  6. #
  7. . git-sh-setup
  8. LF='
  9. '
  10. # The first parameters up to -- are merge bases; the rest are heads.
  11. bases= head= remotes= sep_seen=
  12. for arg
  13. do
  14. case ",$sep_seen,$head,$arg," in
  15. *,--,)
  16. sep_seen=yes
  17. ;;
  18. ,yes,,*)
  19. head=$arg
  20. ;;
  21. ,yes,*)
  22. remotes="$remotes$arg "
  23. ;;
  24. *)
  25. bases="$bases$arg "
  26. ;;
  27. esac
  28. done
  29. # Reject if this is not an octopus -- resolve should be used instead.
  30. case "$remotes" in
  31. ?*' '?*)
  32. ;;
  33. *)
  34. exit 2 ;;
  35. esac
  36. # MRC is the current "merge reference commit"
  37. # MRT is the current "merge result tree"
  38. if ! git diff-index --quiet --cached HEAD --
  39. then
  40. gettextln "Error: Your local changes to the following files would be overwritten by merge"
  41. git diff-index --cached --name-only HEAD -- | sed -e 's/^/ /'
  42. exit 2
  43. fi
  44. MRC=$(git rev-parse --verify -q $head)
  45. MRT=$(git write-tree)
  46. NON_FF_MERGE=0
  47. OCTOPUS_FAILURE=0
  48. for SHA1 in $remotes
  49. do
  50. case "$OCTOPUS_FAILURE" in
  51. 1)
  52. # We allow only last one to have a hand-resolvable
  53. # conflicts. Last round failed and we still had
  54. # a head to merge.
  55. gettextln "Automated merge did not work."
  56. gettextln "Should not be doing an octopus."
  57. exit 2
  58. esac
  59. eval pretty_name=\${GITHEAD_$SHA1:-$SHA1}
  60. if test "$SHA1" = "$pretty_name"
  61. then
  62. SHA1_UP="$(echo "$SHA1" | tr a-z A-Z)"
  63. eval pretty_name=\${GITHEAD_$SHA1_UP:-$pretty_name}
  64. fi
  65. common=$(git merge-base --all $SHA1 $MRC) ||
  66. die "$(eval_gettext "Unable to find common commit with \$pretty_name")"
  67. case "$LF$common$LF" in
  68. *"$LF$SHA1$LF"*)
  69. eval_gettextln "Already up to date with \$pretty_name"
  70. continue
  71. ;;
  72. esac
  73. if test "$common,$NON_FF_MERGE" = "$MRC,0"
  74. then
  75. # The first head being merged was a fast-forward.
  76. # Advance MRC to the head being merged, and use that
  77. # tree as the intermediate result of the merge.
  78. # We still need to count this as part of the parent set.
  79. eval_gettextln "Fast-forwarding to: \$pretty_name"
  80. git read-tree -u -m $head $SHA1 || exit
  81. MRC=$SHA1 MRT=$(git write-tree)
  82. continue
  83. fi
  84. NON_FF_MERGE=1
  85. eval_gettextln "Trying simple merge with \$pretty_name"
  86. git read-tree -u -m --aggressive $common $MRT $SHA1 || exit 2
  87. next=$(git write-tree 2>/dev/null)
  88. if test $? -ne 0
  89. then
  90. gettextln "Simple merge did not work, trying automatic merge."
  91. git merge-index -o git-merge-one-file -a ||
  92. OCTOPUS_FAILURE=1
  93. next=$(git write-tree 2>/dev/null)
  94. fi
  95. MRC="$MRC $SHA1"
  96. MRT=$next
  97. done
  98. exit "$OCTOPUS_FAILURE"