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.

SquashMessageFormatter.java 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (C) 2012, IBM Corporation and others. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.merge;
  11. import java.util.List;
  12. import org.eclipse.jgit.lib.PersonIdent;
  13. import org.eclipse.jgit.lib.Ref;
  14. import org.eclipse.jgit.revwalk.RevCommit;
  15. import org.eclipse.jgit.util.GitDateFormatter;
  16. import org.eclipse.jgit.util.GitDateFormatter.Format;
  17. /**
  18. * Formatter for constructing the commit message for a squashed commit.
  19. * <p>
  20. * The format should be the same as C Git does it, for compatibility.
  21. */
  22. public class SquashMessageFormatter {
  23. private GitDateFormatter dateFormatter;
  24. /**
  25. * Create a new squash message formatter.
  26. */
  27. public SquashMessageFormatter() {
  28. dateFormatter = new GitDateFormatter(Format.DEFAULT);
  29. }
  30. /**
  31. * Construct the squashed commit message.
  32. *
  33. * @param squashedCommits
  34. * the squashed commits
  35. * @param target
  36. * the target branch
  37. * @return squashed commit message
  38. */
  39. public String format(List<RevCommit> squashedCommits, Ref target) {
  40. StringBuilder sb = new StringBuilder();
  41. sb.append("Squashed commit of the following:\n"); //$NON-NLS-1$
  42. for (RevCommit c : squashedCommits) {
  43. sb.append("\ncommit "); //$NON-NLS-1$
  44. sb.append(c.getName());
  45. sb.append("\n"); //$NON-NLS-1$
  46. sb.append(toString(c.getAuthorIdent()));
  47. sb.append("\n\t"); //$NON-NLS-1$
  48. sb.append(c.getShortMessage());
  49. sb.append("\n"); //$NON-NLS-1$
  50. }
  51. return sb.toString();
  52. }
  53. private String toString(PersonIdent author) {
  54. final StringBuilder a = new StringBuilder();
  55. a.append("Author: "); //$NON-NLS-1$
  56. a.append(author.getName());
  57. a.append(" <"); //$NON-NLS-1$
  58. a.append(author.getEmailAddress());
  59. a.append(">\n"); //$NON-NLS-1$
  60. a.append("Date: "); //$NON-NLS-1$
  61. a.append(dateFormatter.formatDate(author));
  62. a.append("\n"); //$NON-NLS-1$
  63. return a.toString();
  64. }
  65. }