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
  • Loading branch information
nielsbasjes committed May 23, 2022
1 parent 915235e commit 4c64430
Show file tree
Hide file tree
Showing 19 changed files with 472 additions and 30 deletions.
53 changes: 53 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 Collections.emptyList();
}
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,7 @@ public String toString()
{
StringBuilder result = new StringBuilder( author == null ? " null " : author );
result.append( "\n" ).append( date == null ? "null " : date.toString() ).append( "\n" );
result.append( "tags:" ).append( getTags() ).append( "\n" );
// parent(s)
if ( parentRevision != null )
{
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 @@ -36,4 +36,11 @@ public void initRepo()
{
HgRepoUtils.initRepo();
}

@Override
public boolean aTagIsAnExtraCommit()
{
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,6 +28,7 @@

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -178,4 +179,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( TimeZone.getTimeZone( "GMT" ) );

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 4c64430

Please sign in to comment.