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 Feb 16, 2022
1 parent 5a716cb commit 81e1480
Show file tree
Hide file tree
Showing 17 changed files with 397 additions and 20 deletions.
46 changes: 45 additions & 1 deletion maven-scm-api/src/main/java/org/apache/maven/scm/ChangeSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ public class ChangeSet
* List of ChangeFile
*/
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,44 @@ 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 ( tags == null )
{
tags = new ArrayList<>();
}
tags.add( tag );
}

/**
* @return TODO
* @since 1.3
Expand Down Expand Up @@ -466,6 +509,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 @@ -207,7 +207,11 @@ static Commandline createCommandLine( GitScmProviderRepository repository, File
SimpleDateFormat dateFormat = new SimpleDateFormat( DATE_FORMAT );
dateFormat.setTimeZone( TimeZone.getTimeZone( "GMT" ) );

Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "whatchanged" );
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "log" );
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 @@ -96,7 +96,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 @@ -249,6 +249,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 @@ -51,14 +51,14 @@ public void testCommandLineNoDates()
throws Exception
{
testCommandLine( "scm:git:http://foo.com/git", null, (Date) null, (Date) null, 40,
"git whatchanged --date=iso --max-count=40 -- ." );
"git log --format=medium --decorate=short --raw --no-merges --date=iso --max-count=40 -- ." );
}

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

public void testCommandLineWithDates()
Expand All @@ -68,7 +68,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 \"--since=2003-09-10 00:00:00 +0000\" \"--until=2007-10-10 00:00:00 +0000\" --date=iso -- ." );
"git log --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 -- ." );
}

public void testCommandLineStartDateOnly()
Expand All @@ -77,7 +77,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 \"--since=2003-09-10 01:01:01 +0000\" --date=iso -- ." );
"git log --format=medium --decorate=short --raw --no-merges \"--since=2003-09-10 01:01:01 +0000\" --date=iso -- ." );
}

public void testCommandLineDateFormat()
Expand All @@ -87,7 +87,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 \"--since=2003-09-10 01:01:01 +0000\" \"--until=2005-11-13 23:23:23 +0000\" --date=iso -- ." );
"git log --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 -- ." );
}

public void testCommandLineDateVersionRanges()
Expand All @@ -97,7 +97,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 \"--since=2003-09-10 01:01:01 +0000\" \"--until=2005-11-13 23:23:23 +0000\" --date=iso 1..10 -- ." );
"git log --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 -- ." );
}

public void testCommandLineEndDateOnly()
Expand All @@ -107,43 +107,43 @@ 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 \"--until=2003-11-10 00:00:00 +0000\" --date=iso -- ." );
"git log --format=medium --decorate=short --raw --no-merges \"--until=2003-11-10 00:00:00 +0000\" --date=iso -- ." );
}

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


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

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

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

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 --date=iso 1..10 my-test-branch -- ." );
"git log --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 @@ -29,6 +29,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 @@ -172,4 +173,72 @@ public void testConsumer2()
assertTrue( cf.getRevision() != null && cf.getRevision().length() > 0 );
}

public void testGitLogConsumer3()
throws Exception
{
GitChangeLogConsumer consumer = new GitChangeLogConsumer( new DefaultLog(), 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( new DefaultLog(), 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 81e1480

Please sign in to comment.