You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Merge.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2011, Christian Halstrick <christian.halstrick@sap.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.pgm;
  44. import java.io.IOException;
  45. import java.text.MessageFormat;
  46. import java.util.Map;
  47. import org.eclipse.jgit.api.Git;
  48. import org.eclipse.jgit.api.MergeCommand;
  49. import org.eclipse.jgit.api.MergeResult;
  50. import org.eclipse.jgit.api.MergeCommand.FastForwardMode;
  51. import org.eclipse.jgit.lib.AnyObjectId;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.eclipse.jgit.lib.ObjectId;
  54. import org.eclipse.jgit.lib.Ref;
  55. import org.eclipse.jgit.merge.MergeStrategy;
  56. import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
  57. import org.eclipse.jgit.revwalk.RevCommit;
  58. import org.eclipse.jgit.revwalk.RevWalk;
  59. import org.kohsuke.args4j.Argument;
  60. import org.kohsuke.args4j.Option;
  61. @Command(common = true, usage = "usage_MergesTwoDevelopmentHistories")
  62. class Merge extends TextBuiltin {
  63. @Option(name = "--strategy", aliases = { "-s" }, usage = "usage_mergeStrategy")
  64. private String strategyName;
  65. @Option(name = "--squash", usage = "usage_squash")
  66. private boolean squash;
  67. private MergeStrategy mergeStrategy = MergeStrategy.RESOLVE;
  68. @Argument(required = true)
  69. private String ref;
  70. @Option(name = "--ff")
  71. private FastForwardMode ff = FastForwardMode.FF;
  72. @Option(name = "--no-ff")
  73. void noff(@SuppressWarnings("unused") final boolean ignored) {
  74. ff = FastForwardMode.NO_FF;
  75. }
  76. @Option(name = "--ff-only")
  77. void ffonly(@SuppressWarnings("unused") final boolean ignored) {
  78. ff = FastForwardMode.FF_ONLY;
  79. }
  80. @Override
  81. protected void run() throws Exception {
  82. if (squash && ff == FastForwardMode.NO_FF)
  83. throw die(CLIText.get().cannotCombineSquashWithNoff);
  84. // determine the merge strategy
  85. if (strategyName != null) {
  86. mergeStrategy = MergeStrategy.get(strategyName);
  87. if (mergeStrategy == null)
  88. throw die(MessageFormat.format(
  89. CLIText.get().unknownMergeStrategy, strategyName));
  90. }
  91. // determine the other revision we want to merge with HEAD
  92. final Ref srcRef = db.getRef(ref);
  93. final ObjectId src = db.resolve(ref + "^{commit}"); //$NON-NLS-1$
  94. if (src == null)
  95. throw die(MessageFormat.format(
  96. CLIText.get().refDoesNotExistOrNoCommit, ref));
  97. Ref oldHead = db.getRef(Constants.HEAD);
  98. Git git = new Git(db);
  99. MergeCommand mergeCmd = git.merge().setStrategy(mergeStrategy)
  100. .setSquash(squash).setFastForward(ff);
  101. if (srcRef != null)
  102. mergeCmd.include(srcRef);
  103. else
  104. mergeCmd.include(src);
  105. MergeResult result = mergeCmd.call();
  106. switch (result.getMergeStatus()) {
  107. case ALREADY_UP_TO_DATE:
  108. if (squash)
  109. outw.print(CLIText.get().nothingToSquash);
  110. outw.println(CLIText.get().alreadyUpToDate);
  111. break;
  112. case FAST_FORWARD:
  113. ObjectId oldHeadId = oldHead.getObjectId();
  114. outw.println(MessageFormat.format(CLIText.get().updating, oldHeadId
  115. .abbreviate(7).name(), result.getNewHead().abbreviate(7)
  116. .name()));
  117. outw.println(result.getMergeStatus().toString());
  118. break;
  119. case CONFLICTING:
  120. for (String collidingPath : result.getConflicts().keySet())
  121. outw.println(MessageFormat.format(CLIText.get().mergeConflict,
  122. collidingPath));
  123. outw.println(CLIText.get().mergeFailed);
  124. break;
  125. case FAILED:
  126. for (Map.Entry<String, MergeFailureReason> entry : result
  127. .getFailingPaths().entrySet())
  128. switch (entry.getValue()) {
  129. case DIRTY_WORKTREE:
  130. case DIRTY_INDEX:
  131. outw.println(CLIText.get().dontOverwriteLocalChanges);
  132. outw.println(" " + entry.getKey()); //$NON-NLS-1$
  133. break;
  134. case COULD_NOT_DELETE:
  135. outw.println(CLIText.get().cannotDeleteFile);
  136. outw.println(" " + entry.getKey()); //$NON-NLS-1$
  137. break;
  138. }
  139. break;
  140. case MERGED:
  141. String name;
  142. if (!isMergedInto(oldHead, src))
  143. name = mergeStrategy.getName();
  144. else
  145. name = "recursive"; //$NON-NLS-1$
  146. outw.println(MessageFormat.format(CLIText.get().mergeMadeBy, name));
  147. break;
  148. case MERGED_SQUASHED:
  149. case FAST_FORWARD_SQUASHED:
  150. outw.println(CLIText.get().mergedSquashed);
  151. break;
  152. case ABORTED:
  153. throw die(CLIText.get().ffNotPossibleAborting);
  154. case NOT_SUPPORTED:
  155. outw.println(MessageFormat.format(
  156. CLIText.get().unsupportedOperation, result.toString()));
  157. }
  158. }
  159. private boolean isMergedInto(Ref oldHead, AnyObjectId src)
  160. throws IOException {
  161. RevWalk revWalk = new RevWalk(db);
  162. ObjectId oldHeadObjectId = oldHead.getPeeledObjectId();
  163. if (oldHeadObjectId == null)
  164. oldHeadObjectId = oldHead.getObjectId();
  165. RevCommit oldHeadCommit = revWalk.lookupCommit(oldHeadObjectId);
  166. RevCommit srcCommit = revWalk.lookupCommit(src);
  167. return revWalk.isMergedInto(oldHeadCommit, srcCommit);
  168. }
  169. }