1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.merge;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import org.eclipse.jgit.lib.Constants;
16 import org.eclipse.jgit.lib.ObjectId;
17 import org.eclipse.jgit.lib.Ref;
18 import org.eclipse.jgit.lib.Repository;
19 import org.eclipse.jgit.util.ChangeIdUtil;
20 import org.eclipse.jgit.util.StringUtils;
21
22
23
24
25
26
27 public class MergeMessageFormatter {
28
29
30
31
32
33
34
35
36
37 public String format(List<Ref> refsToMerge, Ref target) {
38 StringBuilder sb = new StringBuilder();
39 sb.append("Merge ");
40
41 List<String> branches = new ArrayList<>();
42 List<String> remoteBranches = new ArrayList<>();
43 List<String> tags = new ArrayList<>();
44 List<String> commits = new ArrayList<>();
45 List<String> others = new ArrayList<>();
46 for (Ref ref : refsToMerge) {
47 if (ref.getName().startsWith(Constants.R_HEADS)) {
48 branches.add("'" + Repository.shortenRefName(ref.getName())
49 + "'");
50 } else if (ref.getName().startsWith(Constants.R_REMOTES)) {
51 remoteBranches.add("'"
52 + Repository.shortenRefName(ref.getName()) + "'");
53 } else if (ref.getName().startsWith(Constants.R_TAGS)) {
54 tags.add("'" + Repository.shortenRefName(ref.getName()) + "'");
55 } else {
56 ObjectId objectId = ref.getObjectId();
57 if (objectId != null && ref.getName().equals(objectId.getName())) {
58 commits.add("'" + ref.getName() + "'");
59 } else {
60 others.add(ref.getName());
61 }
62 }
63 }
64
65 List<String> listings = new ArrayList<>();
66
67 if (!branches.isEmpty())
68 listings.add(joinNames(branches, "branch", "branches"));
69
70 if (!remoteBranches.isEmpty())
71 listings.add(joinNames(remoteBranches, "remote-tracking branch",
72 "remote-tracking branches"));
73
74 if (!tags.isEmpty())
75 listings.add(joinNames(tags, "tag", "tags"));
76
77 if (!commits.isEmpty())
78 listings.add(joinNames(commits, "commit", "commits"));
79
80 if (!others.isEmpty())
81 listings.add(StringUtils.join(others, ", ", " and "));
82
83 sb.append(StringUtils.join(listings, ", "));
84
85 String targetName = target.getLeaf().getName();
86 if (!targetName.equals(Constants.R_HEADS + Constants.MASTER)) {
87 String targetShortName = Repository.shortenRefName(targetName);
88 sb.append(" into " + targetShortName);
89 }
90
91 return sb.toString();
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106 @Deprecated
107 public String formatWithConflicts(String message,
108 List<String> conflictingPaths) {
109 return formatWithConflicts(message, conflictingPaths, '#');
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124 public String formatWithConflicts(String message,
125 Iterable<String> conflictingPaths, char commentChar) {
126 StringBuilder sb = new StringBuilder();
127 String[] lines = message.split("\n");
128 int firstFooterLine = ChangeIdUtil.indexOfFirstFooterLine(lines);
129 for (int i = 0; i < firstFooterLine; i++) {
130 sb.append(lines[i]).append('\n');
131 }
132 if (firstFooterLine == lines.length && message.length() != 0) {
133 sb.append('\n');
134 }
135 addConflictsMessage(conflictingPaths, sb, commentChar);
136 if (firstFooterLine < lines.length) {
137 sb.append('\n');
138 }
139 for (int i = firstFooterLine; i < lines.length; i++) {
140 sb.append(lines[i]).append('\n');
141 }
142 return sb.toString();
143 }
144
145 private static void addConflictsMessage(Iterable<String> conflictingPaths,
146 StringBuilder sb, char commentChar) {
147 sb.append(commentChar).append(" Conflicts:\n");
148 for (String conflictingPath : conflictingPaths) {
149 sb.append(commentChar).append('\t').append(conflictingPath)
150 .append('\n');
151 }
152 }
153
154 private static String joinNames(List<String> names, String singular,
155 String plural) {
156 if (names.size() == 1) {
157 return singular + " " + names.get(0);
158 }
159 return plural + " " + StringUtils.join(names, ", ", " and ");
160 }
161 }