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.Ref;
50 import org.eclipse.jgit.lib.Repository;
51 import org.eclipse.jgit.util.ChangeIdUtil;
52 import org.eclipse.jgit.util.StringUtils;
53
54
55
56
57
58
59 public class MergeMessageFormatter {
60
61
62
63
64
65
66
67
68
69 public String format(List<Ref> refsToMerge, Ref target) {
70 StringBuilder sb = new StringBuilder();
71 sb.append("Merge ");
72
73 List<String> branches = new ArrayList<String>();
74 List<String> remoteBranches = new ArrayList<String>();
75 List<String> tags = new ArrayList<String>();
76 List<String> commits = new ArrayList<String>();
77 List<String> others = new ArrayList<String>();
78 for (Ref ref : refsToMerge) {
79 if (ref.getName().startsWith(Constants.R_HEADS))
80 branches.add("'" + Repository.shortenRefName(ref.getName())
81 + "'");
82
83 else if (ref.getName().startsWith(Constants.R_REMOTES))
84 remoteBranches.add("'"
85 + Repository.shortenRefName(ref.getName()) + "'");
86
87 else if (ref.getName().startsWith(Constants.R_TAGS))
88 tags.add("'" + Repository.shortenRefName(ref.getName()) + "'");
89
90 else if (ref.getName().equals(ref.getObjectId().getName()))
91 commits.add("'" + ref.getName() + "'");
92
93 else
94 others.add(ref.getName());
95 }
96
97 List<String> listings = new ArrayList<String>();
98
99 if (!branches.isEmpty())
100 listings.add(joinNames(branches, "branch", "branches"));
101
102 if (!remoteBranches.isEmpty())
103 listings.add(joinNames(remoteBranches, "remote-tracking branch",
104 "remote-tracking branches"));
105
106 if (!tags.isEmpty())
107 listings.add(joinNames(tags, "tag", "tags"));
108
109 if (!commits.isEmpty())
110 listings.add(joinNames(commits, "commit", "commits"));
111
112 if (!others.isEmpty())
113 listings.add(StringUtils.join(others, ", ", " and "));
114
115 sb.append(StringUtils.join(listings, ", "));
116
117 String targetName = target.getLeaf().getName();
118 if (!targetName.equals(Constants.R_HEADS + Constants.MASTER)) {
119 String targetShortName = Repository.shortenRefName(targetName);
120 sb.append(" into " + targetShortName);
121 }
122
123 return sb.toString();
124 }
125
126
127
128
129
130
131
132
133
134
135 public String formatWithConflicts(String message,
136 List<String> conflictingPaths) {
137 StringBuilder sb = new StringBuilder();
138 String[] lines = message.split("\n");
139 int firstFooterLine = ChangeIdUtil.indexOfFirstFooterLine(lines);
140 for (int i = 0; i < firstFooterLine; i++)
141 sb.append(lines[i]).append('\n');
142 if (firstFooterLine == lines.length && message.length() != 0)
143 sb.append('\n');
144 addConflictsMessage(conflictingPaths, sb);
145 if (firstFooterLine < lines.length)
146 sb.append('\n');
147 for (int i = firstFooterLine; i < lines.length; i++)
148 sb.append(lines[i]).append('\n');
149 return sb.toString();
150 }
151
152 private static void addConflictsMessage(List<String> conflictingPaths,
153 StringBuilder sb) {
154 sb.append("Conflicts:\n");
155 for (String conflictingPath : conflictingPaths)
156 sb.append('\t').append(conflictingPath).append('\n');
157 }
158
159 private static String joinNames(List<String> names, String singular,
160 String plural) {
161 if (names.size() == 1)
162 return singular + " " + names.get(0);
163 else
164 return plural + " " + StringUtils.join(names, ", ", " and ");
165 }
166 }