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