Skip to content

Commit 5aa840f

Browse files
authored
Merge pull request #12 from seblatre/seblatre/scmtag
Seblatre/scmtag
2 parents e994562 + 758bb30 commit 5aa840f

14 files changed

Lines changed: 307 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ target
33
.classpath
44
.project
55
dependency-reduced-pom.xml
6+
/.idea

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
<dependency>
9696
<groupId>org.codehaus.mojo</groupId>
9797
<artifactId>versions-maven-plugin</artifactId>
98-
<version>2.1</version>
98+
<version>2.7</version>
9999
<type>maven-plugin</type>
100100
<exclusions>
101101
<exclusion>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package de.oppermann.pomutils.rules;
2+
3+
import de.oppermann.pomutils.select.SelectionStrategy;
4+
import de.oppermann.pomutils.util.POM;
5+
import de.oppermann.pomutils.util.VersionFieldType;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
/*
10+
* Licensed to the Apache Software Foundation (ASF) under one
11+
* or more contributor license agreements. See the NOTICE file
12+
* distributed with this work for additional information
13+
* regarding copyright ownership. The ASF licenses this file
14+
* to you under the Apache License, Version 2.0 (the
15+
* "License"); you may not use this file except in compliance
16+
* with the License. You may obtain a copy of the License at
17+
*
18+
* http://www.apache.org/licenses/LICENSE-2.0
19+
*
20+
* Unless required by applicable law or agreed to in writing,
21+
* software distributed under the License is distributed on an
22+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23+
* KIND, either express or implied. See the License for the
24+
* specific language governing permissions and limitations
25+
* under the License.
26+
*/
27+
28+
/**
29+
*
30+
* @author Sébastien Latre
31+
*
32+
*/
33+
34+
public class ScmTagRule extends AbstractRule {
35+
36+
private final Logger logger = LoggerFactory.getLogger(ScmTagRule.class);
37+
38+
public ScmTagRule() {
39+
// for creating this instance via snakeyaml
40+
}
41+
42+
public ScmTagRule(SelectionStrategy strategy) {
43+
super(strategy);
44+
logger.debug("Using ScmTagRule with strategy [{}]", strategy.toString());
45+
}
46+
47+
@Override
48+
public void evaluate(POM basePom, POM ourPom, POM theirPom) {
49+
String baseScmTag = basePom.getScmTag();
50+
String ourScmTag = ourPom.getScmTag();
51+
String theirScmTag = theirPom.getScmTag();
52+
if (baseScmTag != null && ourScmTag != null && theirScmTag != null && !ourScmTag.equals(theirScmTag)) {
53+
String newScmTag;
54+
if (baseScmTag.equals(ourScmTag)) {
55+
/*
56+
* Our ScmTag hasn't changed, so no conflict. Just use theirScmTag.
57+
*/
58+
newScmTag = theirScmTag;
59+
} else if (baseScmTag.equals(theirScmTag)) {
60+
/*
61+
* Their scmTag hasn't changed, so no conflict. Just use ourScmTag.
62+
*/
63+
newScmTag = ourScmTag;
64+
} else {
65+
/*
66+
* Both our scmTag and their scmTag have changed from the base, so conflict.
67+
*/
68+
switch (getStrategy()) {
69+
case OUR:
70+
newScmTag = ourScmTag;
71+
break;
72+
case THEIR:
73+
newScmTag = theirScmTag;
74+
break;
75+
default:
76+
throw new IllegalArgumentException("Strategy [" + getStrategy().toString() + "] not implemented.");
77+
}
78+
}
79+
80+
if (newScmTag != null) {
81+
/*
82+
* newScmTag can be null if the user wants to skip resolution.
83+
*/
84+
85+
POM pomToAdjust = newScmTag.equals(ourScmTag)
86+
? theirPom
87+
: ourPom;
88+
89+
pomToAdjust.setScmTag(newScmTag);
90+
}
91+
}
92+
}
93+
}

src/main/java/de/oppermann/pomutils/util/POM.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public class POM {
6565
private String projectVersion;
6666
private String parentVersion;
6767
private String projectIdentifier;
68+
private String scmTag;
6869
private Model rawModel;
6970

7071
public POM(String pomFileAsString) throws IOException, XMLStreamException {
@@ -82,6 +83,7 @@ public POM(String pomFileAsString) throws IOException, XMLStreamException {
8283
projectIdentifier = null;
8384
projectVersion = "";
8485
parentVersion = "";
86+
scmTag = "";
8587
return;
8688
}
8789

@@ -104,6 +106,9 @@ private void initialize() throws IOException, XMLStreamException {
104106
parentVersion = rawModel.getParent() != null
105107
? rawModel.getParent().getVersion()
106108
: null;
109+
scmTag = rawModel.getScm() != null
110+
? rawModel.getScm().getTag()
111+
: null;
107112
}
108113

109114
private String calculateProjectIdentifier() {
@@ -161,6 +166,14 @@ public String getProjectIdentifier() {
161166
return projectIdentifier;
162167
}
163168

169+
/**
170+
*
171+
* @return the SCM tag or null if the SCM tag is unset
172+
*/
173+
public String getScmTag() {
174+
return scmTag;
175+
}
176+
164177
/**
165178
* Sets the parent version to the given one, if it exists
166179
* @param newVersion
@@ -188,6 +201,20 @@ public void setProjectVersion(String newVersion) {
188201

189202
}
190203

204+
/**
205+
* Sets the SCM tag to the given one, if it exists
206+
* @param newScmTag
207+
*/
208+
public void setScmTag(String newScmTag) {
209+
if (this.scmTag == null || this.scmTag.equals(newScmTag)) {
210+
return;
211+
}
212+
logger.debug("Adjusting scm tag from [{}] to [{}] of [{}] for [{}]", this.scmTag, newScmTag, getPath(), this.projectIdentifier);
213+
this.scmTag = newScmTag;
214+
this.changed = true;
215+
216+
}
217+
191218
/**
192219
* Saves the pom, if it was changed.
193220
*/
@@ -204,6 +231,10 @@ public void savePom() throws IOException, XMLStreamException {
204231
changed |= PomHelper.setProjectParentVersion(pom, this.parentVersion);
205232
}
206233

234+
if (this.scmTag != null) {
235+
changed |= PomHelper.setProjectValue(pom, "/project/scm/tag", this.scmTag);
236+
}
237+
207238
if (!changed) {
208239
return;
209240
}

src/main/resources/logback.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
4+
<level>TRACE</level>
5+
</filter>
6+
<encoder>
7+
<pattern>%green(%d{HH:mm:ss.SSS}) [%30.30thread] [%mdc{request.id:-noRequestId}] %highlight(%-5level) %cyan(%40.40logger:%-4.4line) - %msg%n</pattern>
8+
</encoder>
9+
</appender>
10+
<appender name="FILE"
11+
class="ch.qos.logback.core.rolling.RollingFileAppender">
12+
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
13+
<level>TRACE</level>
14+
</filter>
15+
<file>/busdata/batch/tid_mod_cus/log/tid_mod_cus.log</file>
16+
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
17+
<fileNamePattern>/busdata/batch/tid_mod_cus/log/tid_mod_cus.log.%d{yyyy-MM-dd}.%i</fileNamePattern>
18+
<maxFileSize>32MB</maxFileSize>
19+
<maxHistory>90</maxHistory>
20+
<totalSizeCap>1GB</totalSizeCap>
21+
</rollingPolicy>
22+
<encoder>
23+
<pattern>%d{ISO8601} %-5level [%10.10thread] [%mdc{request.id:-noRequestId}] %-50.50logger:%-4.4line - %msg%n</pattern>
24+
</encoder>
25+
</appender>
26+
<root level="TRACE">
27+
<appender-ref ref="FILE"/>
28+
<appender-ref ref="STDOUT"/>
29+
</root>
30+
<logger name="com.zaxxer" level="INFO"/>
31+
<logger name="io" level="INFO"/>
32+
<logger name="org" level="WARN"/>
33+
</configuration>

src/test/java/de/oppermann/pomutils/RulesetTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,52 @@ public void testPropertyRuleWithTheirStrategy() throws Exception {
173173
.getProperty("regex.version"));
174174
}
175175

176+
public void testScmTagWithOurStrategy() throws Exception {
177+
String myTestSubFolder = "rulesets/rulesetScmTag/ourStrategy";
178+
179+
TestUtils.prepareTestFolder(myTestSubFolder);
180+
181+
File rulesetFile = new File(TestUtils.resourceBaseTestFolder + "/" + myTestSubFolder + "/ruleset.yaml");
182+
String basePomFile = TestUtils.resourceBaseTestFolder + "/" + myTestSubFolder + "/base.pom.xml";
183+
String ourPomFile = TestUtils.resourceBaseTestFolder + "/" + myTestSubFolder + "/our.pom.xml";
184+
String theirPomFile = TestUtils.resourceBaseTestFolder + "/" + myTestSubFolder + "/their.pom.xml";
185+
186+
String scmTagBeforeMerge = new POM(ourPomFile).getScmTag();
187+
Ruleset ruleset = new Ruleset(rulesetFile);
188+
189+
int mergeReturnValue = doMerge(ruleset, basePomFile, ourPomFile, theirPomFile);
190+
191+
assertTrue("merge succeeded", mergeReturnValue == 0);
192+
193+
POM theirPom = new POM(theirPomFile);
194+
POM ourPom = new POM(ourPomFile);
195+
196+
assertEquals("same version now", ourPom.getScmTag(), theirPom.getScmTag());
197+
assertEquals("our version should win", scmTagBeforeMerge, ourPom.getScmTag());
198+
}
199+
200+
public void testScmTagWithTheirsStrategy() throws Exception {
201+
String myTestSubFolder = "rulesets/rulesetScmTag/theirStrategy";
202+
203+
TestUtils.prepareTestFolder(myTestSubFolder);
204+
205+
File rulesetFile = new File(TestUtils.resourceBaseTestFolder + "/" + myTestSubFolder + "/ruleset.yaml");
206+
String basePomFile = TestUtils.resourceBaseTestFolder + "/" + myTestSubFolder + "/base.pom.xml";
207+
String ourPomFile = TestUtils.resourceBaseTestFolder + "/" + myTestSubFolder + "/our.pom.xml";
208+
String theirPomFile = TestUtils.resourceBaseTestFolder + "/" + myTestSubFolder + "/their.pom.xml";
209+
210+
String scmTagBeforeMerge = new POM(theirPomFile).getScmTag();
211+
Ruleset ruleset = new Ruleset(rulesetFile);
212+
213+
int mergeReturnValue = doMerge(ruleset, basePomFile, ourPomFile, theirPomFile);
214+
215+
assertTrue("merge succeeded", mergeReturnValue == 0);
216+
217+
POM theirPom = new POM(theirPomFile);
218+
POM ourPom = new POM(ourPomFile);
219+
220+
assertEquals("same version now", ourPom.getScmTag(), theirPom.getScmTag());
221+
assertEquals("their version should win", scmTagBeforeMerge, ourPom.getScmTag());
222+
}
223+
176224
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>de.oppermann.pomutils</groupId>
4+
<artifactId>pomutils</artifactId>
5+
<dependencies>
6+
<dependency>
7+
<groupId>org.codehaus.mojo</groupId>
8+
<artifactId>versions-maven-plugin</artifactId>
9+
<version>2.1</version>
10+
<type>maven-plugin</type>
11+
</dependency>
12+
</dependencies>
13+
<scm>
14+
<tag>base</tag>
15+
</scm>
16+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>de.oppermann.pomutils</groupId>
4+
<artifactId>pomutils</artifactId>
5+
<dependencies>
6+
<dependency>
7+
<groupId>org.codehaus.mojo</groupId>
8+
<artifactId>versions-maven-plugin</artifactId>
9+
<version>2.1</version>
10+
<type>maven-plugin</type>
11+
</dependency>
12+
</dependencies>
13+
<scm>
14+
<tag>our</tag>
15+
</scm>
16+
</project>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--- !!de.oppermann.pomutils.rules.ScmTagRule
2+
strategy: OUR
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>de.oppermann.pomutils</groupId>
4+
<artifactId>pomutils</artifactId>
5+
<dependencies>
6+
<dependency>
7+
<groupId>org.codehaus.mojo</groupId>
8+
<artifactId>versions-maven-plugin</artifactId>
9+
<version>2.5</version>
10+
<type>maven-plugin</type>
11+
</dependency>
12+
</dependencies>
13+
<scm>
14+
<tag>their</tag>
15+
</scm>
16+
</project>

0 commit comments

Comments
 (0)