Skip to content

Commit

Permalink
[SCM-977] Support for retrieving tags from the changelog
Browse files Browse the repository at this point in the history
This closes apache#135
  • Loading branch information
nielsbasjes authored and michael-o committed May 26, 2022
1 parent 087c1b3 commit 3e93ebe
Show file tree
Hide file tree
Showing 20 changed files with 490 additions and 33 deletions.
67 changes: 67 additions & 0 deletions maven-scm-api/src/main/java/org/apache/maven/scm/ChangeSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public class ChangeSet
*/
private List<ChangeFile> files;

/**
* List of tags
*/
private List<String> tags;

/**
* The SCM revision id for this changeset.
* @since 1.3
Expand Down Expand Up @@ -414,6 +419,53 @@ public String getTimeFormatted()
return TIME_FORMAT.format( getDate() );
}

/**
* Getter for property tags.
*
* @return Value of property author.
*/
public List<String> getTags()
{
if ( tags == null )
{
return new ArrayList<>();
}
return tags;
}

/**
* Setter for property tags.
*
* @param tags New value of property tags. This replaces the existing list (if any).
*/
public void setTags( List<String> tags )
{
this.tags = tags;
}

/**
* Setter for property tags.
*
* @param tag New tag to add to the list of tags.
*/
public void addTag( String tag )
{
if ( tag == null )
{
return;
}
tag = tag.trim();
if ( tag.isEmpty() )
{
return;
}
if ( tags == null )
{
tags = new ArrayList<>();
}
tags.add( tag );
}

/**
* @return TODO
* @since 1.3
Expand Down Expand Up @@ -466,6 +518,11 @@ public String toString()
{
StringBuilder result = new StringBuilder( author == null ? " null " : author );
result.append( "\n" ).append( date == null ? "null " : date.toString() ).append( "\n" );
List<String> tags = getTags();
if ( !tags.isEmpty() )
{
result.append( "tags:" ).append( tags ).append( "\n" );
}
// parent(s)
if ( parentRevision != null )
{
Expand Down Expand Up @@ -552,6 +609,16 @@ public String toXML()
buffer.append( "\t\t<msg><![CDATA[" )
.append( removeCDataEnd( comment ) )
.append( "]]></msg>\n" );
List<String> tags = getTags();
if ( !tags.isEmpty() )
{
buffer.append( "\t\t<tags>\n" );
for ( String tag: tags )
{
buffer.append( "\t\t\t<tag>" ).append( escapeValue( tag ) ).append( "</tag>\n" );
}
buffer.append( "\t\t</tags>\n" );
}
buffer.append( "\t</changelog-entry>\n" );

return buffer.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* under the License.
*/

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

Expand Down Expand Up @@ -59,6 +60,7 @@ private static ChangeSet createInstance()
instance.setAuthor( "dion" );
instance.setComment( "comment" );
instance.setDate( "2002/04/01 00:00:00" );
instance.setTags( Arrays.asList( "v3.14", "v2<bla>.7]]1828", "<![CDATA[NastyTag" ) );
return instance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ public class HgChangeLogConsumer

private String currentRevision;

@SuppressWarnings( "unused" )
private String currentTag; // don't know what to do with this

@SuppressWarnings( "unused" )
private String currentBranch; // don't know what to do with this

Expand Down Expand Up @@ -127,7 +124,7 @@ else if ( line.startsWith( TIME_STAMP_TOKEN ) )
else if ( line.startsWith( TAG_TAG ) )
{
tmpLine = line.substring( TAG_TAG.length() ).trim();
currentTag = tmpLine;
currentChange.addTag( tmpLine );
}
else if ( line.startsWith( FILES_TOKEN ) )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ public void initRepo()
{
HgRepoUtils.initRepo();
}

@Override
public boolean isTagAnExtraCommit()
{
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ static Commandline createCommandLine( GitScmProviderRepository repository, File

Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "whatchanged" );
cl.createArg().setValue( "--format=medium" );
cl.createArg().setValue( "--decorate=short" );
cl.createArg().setValue( "--raw" );
cl.createArg().setValue( "--no-merges" );

if ( startDate != null || endDate != null )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class GitChangeLogConsumer
/**
* The pattern used to match git header lines
*/
private static final Pattern HEADER_PATTERN = Pattern.compile( "^commit (.*)" );
private static final Pattern HEADER_PATTERN = Pattern.compile( "^commit ([A-Fa-f0-9]+)(?: \\((.*)\\))?$" );

/**
* The pattern used to match git author lines
Expand Down Expand Up @@ -246,6 +246,21 @@ private void processGetHeader( String line )

currentChange.setRevision( currentRevision );

// Extract the tags (if present)
String tagList = matcher.group( 2 );
if ( tagList != null )
{
String[] rawTags = tagList.split( "," );
for ( String rawTag : rawTags )
{
String[] tagParts = rawTag.trim().split( ":" );
if ( tagParts.length == 2 && "tag".equals( tagParts[0] ) )
{
currentChange.addTag( tagParts[1].trim() );
}
}
}

status = STATUS_GET_AUTHOR;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ public void testCommandLineNoDates()
throws Exception
{
testCommandLine( "scm:git:http://foo.com/git", null, (Date) null, (Date) null, 40,
"git whatchanged --format=medium --date=iso --max-count=40 -- ." );
"git whatchanged --format=medium --decorate=short --raw --no-merges --date=iso --max-count=40 -- ." );
}

@Test
public void testCommandLineNoDatesLimitedCount()
throws Exception
{
testCommandLine( "scm:git:http://foo.com/git", null, (Date) null, (Date) null,
"git whatchanged --format=medium --date=iso -- ." );
"git whatchanged --format=medium --decorate=short --raw --no-merges --date=iso -- ." );
}

@Test
Expand All @@ -75,7 +75,7 @@ public void testCommandLineWithDates()
Date endDate = getDate( 2007, Calendar.OCTOBER, 10, GMT_TIME_ZONE );

testCommandLine( "scm:git:http://foo.com/git", null, startDate, endDate,
"git whatchanged --format=medium \"--since=2003-09-10 00:00:00 +0000\" \"--until=2007-10-10 00:00:00 +0000\" --date=iso -- ." );
"git whatchanged --format=medium --decorate=short --raw --no-merges \"--since=2003-09-10 00:00:00 +0000\" \"--until=2007-10-10 00:00:00 +0000\" --date=iso -- ." );
}

@Test
Expand All @@ -85,7 +85,7 @@ public void testCommandLineStartDateOnly()
Date startDate = getDate( 2003, Calendar.SEPTEMBER, 10, 1, 1, 1, GMT_TIME_ZONE );

testCommandLine( "scm:git:http://foo.com/git", null, startDate, null,
"git whatchanged --format=medium \"--since=2003-09-10 01:01:01 +0000\" --date=iso -- ." );
"git whatchanged --format=medium --decorate=short --raw --no-merges \"--since=2003-09-10 01:01:01 +0000\" --date=iso -- ." );
}

@Test
Expand All @@ -96,7 +96,7 @@ public void testCommandLineDateFormat()
Date endDate = getDate( 2005, Calendar.NOVEMBER, 13, 23, 23, 23, GMT_TIME_ZONE );

testCommandLine( "scm:git:http://foo.com/git", null, startDate, endDate,
"git whatchanged --format=medium \"--since=2003-09-10 01:01:01 +0000\" \"--until=2005-11-13 23:23:23 +0000\" --date=iso -- ." );
"git whatchanged --format=medium --decorate=short --raw --no-merges \"--since=2003-09-10 01:01:01 +0000\" \"--until=2005-11-13 23:23:23 +0000\" --date=iso -- ." );
}

@Test
Expand All @@ -107,7 +107,7 @@ public void testCommandLineDateVersionRanges()
Date endDate = getDate( 2005, Calendar.NOVEMBER, 13, 23, 23, 23, GMT_TIME_ZONE );

testCommandLine( "scm:git:http://foo.com/git", null, startDate, endDate, new ScmRevision( "1" ), new ScmRevision( "10" ),
"git whatchanged --format=medium \"--since=2003-09-10 01:01:01 +0000\" \"--until=2005-11-13 23:23:23 +0000\" --date=iso 1..10 -- ." );
"git whatchanged --format=medium --decorate=short --raw --no-merges \"--since=2003-09-10 01:01:01 +0000\" \"--until=2005-11-13 23:23:23 +0000\" --date=iso 1..10 -- ." );
}

@Test
Expand All @@ -118,15 +118,15 @@ public void testCommandLineEndDateOnly()

// Only specifying end date should print no dates at all
testCommandLine( "scm:git:http://foo.com/git", null, null, endDate,
"git whatchanged --format=medium \"--until=2003-11-10 00:00:00 +0000\" --date=iso -- ." );
"git whatchanged --format=medium --decorate=short --raw --no-merges \"--until=2003-11-10 00:00:00 +0000\" --date=iso -- ." );
}

@Test
public void testCommandLineWithBranchNoDates()
throws Exception
{
testCommandLine( "scm:git:http://foo.com/git", new ScmBranch( "my-test-branch" ), (Date) null, (Date) null,
"git whatchanged --format=medium --date=iso my-test-branch -- ." );
"git whatchanged --format=medium --decorate=short --raw --no-merges --date=iso my-test-branch -- ." );
}


Expand All @@ -135,31 +135,31 @@ public void testCommandLineWithStartVersion()
throws Exception
{
testCommandLine( "scm:git:http://foo.com/git", null, new ScmRevision( "1" ), null,
"git whatchanged --format=medium --date=iso 1.. -- ." );
"git whatchanged --format=medium --decorate=short --raw --no-merges --date=iso 1.. -- ." );
}

@Test
public void testCommandLineWithStartVersionAndEndVersion()
throws Exception
{
testCommandLine( "scm:git:http://foo.com/git", null, new ScmRevision( "1" ), new ScmRevision( "10" ),
"git whatchanged --format=medium --date=iso 1..10 -- ." );
"git whatchanged --format=medium --decorate=short --raw --no-merges --date=iso 1..10 -- ." );
}

@Test
public void testCommandLineWithStartVersionAndEndVersionEquals()
throws Exception
{
testCommandLine( "scm:git:http://foo.com/git", null, new ScmRevision( "1" ), new ScmRevision( "1" ),
"git whatchanged --format=medium --date=iso 1..1 -- ." );
"git whatchanged --format=medium --decorate=short --raw --no-merges --date=iso 1..1 -- ." );
}

@Test
public void testCommandLineWithStartVersionAndEndVersionAndBranch()
throws Exception
{
testCommandLine( "scm:git:http://foo.com/git", new ScmBranch( "my-test-branch" ), new ScmRevision( "1" ), new ScmRevision( "10" ),
"git whatchanged --format=medium --date=iso 1..10 my-test-branch -- ." );
"git whatchanged --format=medium --decorate=short --raw --no-merges --date=iso 1..10 my-test-branch -- ." );
}

// ----------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -93,7 +93,7 @@ public void testConsumer1()

assertNotNull( entry.getDate() );
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss Z" );
sdf.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
sdf.setTimeZone( GMT_TIME_ZONE );

assertEquals( "2007-11-24 00:10:42 +0000", sdf.format( entry.getDate() ) );

Expand Down Expand Up @@ -158,7 +158,7 @@ public void testConsumer2()

assertNotNull( entry.getDate() );
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss Z" );
sdf.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
sdf.setTimeZone( GMT_TIME_ZONE );

assertEquals( "2007-11-27 13:05:36 +0000", sdf.format( entry.getDate() ) );

Expand All @@ -178,4 +178,72 @@ public void testConsumer2()
assertTrue( cf.getRevision() != null && cf.getRevision().length() > 0 );
}

public void testGitLogConsumer3()
throws Exception
{
GitChangeLogConsumer consumer = new GitChangeLogConsumer( null );

File f = getTestFile( "/src/test/resources/git/changelog/gitlog3.gitlog" );

ConsumerUtils.consumeFile( f, consumer );

List<ChangeSet> modifications = consumer.getModifications();

assertEquals( 10, modifications.size() );

SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss Z" );
sdf.setTimeZone( GMT_TIME_ZONE );

ChangeSet first = modifications.get( 0 );
assertEquals( "Michael Osipov <[email protected]>", first.getAuthor() );
assertEquals( "2022-01-08 20:02:12 +0000", sdf.format( first.getDate() ) );
assertEquals( "[maven-release-plugin] prepare for next development iteration", first.getComment() );
assertEquals( "4fc34cfa14f2e72506187b03a492ce55ed459d4c", first.getRevision() );
assertEquals( Collections.emptyList(), first.getTags() );
assertNotNull( first.getFiles() );
assertFalse( first.getFiles().isEmpty() );

ChangeSet second = modifications.get( 1 );
assertEquals( "Michael Osipov <[email protected]>", second.getAuthor() );
assertEquals( "2022-01-08 20:02:01 +0000", sdf.format( second.getDate() ) );
assertEquals( "[maven-release-plugin] prepare release maven-scm-2.0.0-M1", second.getComment() );
assertEquals( "3a6d9817fe809c43eca588d7c0f4428254eae17c", second.getRevision() );
assertEquals( Collections.singletonList("maven-scm-2.0.0-M1"), second.getTags() );
assertNotNull( second.getFiles() );
assertFalse( second.getFiles().isEmpty() );
}

public void testTagAndBranchConsumer() {
String[] lines = {
"commit a6d03ee7bcec7bfd6b0fc890a277f004a1c54077 (HEAD -> main, tag: TestTag, origin/main, origin/HEAD)",
"Author: Niels Basjes <[email protected]>",
"Date: 2022-02-06 16:19:01 +0100",
"",
" This",
" is",
" a",
" multiline",
" comment",
"",
":100644 100644 2019174 808473f M\tdocumentation/pom.xml",
""
};
GitChangeLogConsumer consumer = new GitChangeLogConsumer( null );

for ( String line : lines ) {
consumer.consumeLine( line );
}

List<ChangeSet> modifications = consumer.getModifications();

assertEquals( 1, modifications.size() );
ChangeSet changeSet = modifications.get( 0 );
assertEquals( Collections.singletonList( "TestTag" ), changeSet.getTags() );
assertEquals( "This\nis\na\nmultiline\ncomment", changeSet.getComment() );
List<ChangeFile> files = changeSet.getFiles();
assertEquals( 1, files.size() );
ChangeFile changeFile = files.get (0 );
assertEquals( "documentation/pom.xml", changeFile.getName() );
}

}
Loading

0 comments on commit 3e93ebe

Please sign in to comment.