Skip to content

Commit 2a70033

Browse files
kwinmichael-o
authored andcommitted
[SCM-925] Implement RemoveCommand in maven-scm-provider-jgit with TCK test for all providers
Co-authored-by: Georg Tsakumagos <[email protected]> This closes #144 and closes #156
1 parent 21e43b2 commit 2a70033

File tree

20 files changed

+576
-61
lines changed

20 files changed

+576
-61
lines changed

maven-scm-api/src/main/java/org/apache/maven/scm/manager/ScmManager.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ ChangeLogScmResult changeLog( ScmRepository repository, ScmFileSet fileSet, ScmV
274274
* @param repository the source control system
275275
* @param fileSet the files to check in (sometimes called commit)
276276
* @param message a string that is a comment on the changes that where done
277-
* @return result object encapsulating all file paths (relative to {@code fileSet.getBasedir()})
277+
* @return a {@link CheckInScmResult} that contains the file paths (relative to {@code fileSet.getBasedir()}) that
278+
* have been checked in.
278279
* @throws ScmException if any
279280
*/
280281
CheckInScmResult checkIn( ScmRepository repository, ScmFileSet fileSet, String message )
@@ -291,7 +292,8 @@ CheckInScmResult checkIn( ScmRepository repository, ScmFileSet fileSet, String m
291292
* @param fileSet the files to check in (sometimes called commit)
292293
* @param revision branch/tag/revision
293294
* @param message a string that is a comment on the changes that where done
294-
* @return result object encapsulating all file paths (relative to {@code fileSet.getBasedir()})
295+
* @return a {@link CheckInScmResult} that contains the file paths (relative to {@code fileSet.getBasedir()}) that
296+
* have been checked in.
295297
* @throws ScmException if any
296298
*/
297299
CheckInScmResult checkIn( ScmRepository repository, ScmFileSet fileSet, ScmVersion revision, String message )
@@ -453,7 +455,8 @@ MkdirScmResult mkdir( ScmRepository repository, ScmFileSet fileSet, String messa
453455
* @param repository the source control system
454456
* @param fileSet the files to be removed
455457
* @param message TODO
456-
* @return TODO
458+
* @return a {@link RemoveScmResult} that contains the file paths (relative to {@code fileSet.getBasedir()}) that
459+
* have been removed
457460
* @throws ScmException if any
458461
*/
459462
RemoveScmResult remove( ScmRepository repository, ScmFileSet fileSet, String message )

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/remove/GitRemoveCommand.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
import org.apache.maven.scm.provider.ScmProviderRepository;
2828
import org.apache.maven.scm.provider.git.command.GitCommand;
2929
import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
30+
import org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusCommand;
3031
import org.codehaus.plexus.util.cli.CommandLineUtils;
3132
import org.codehaus.plexus.util.cli.Commandline;
3233

3334
import java.io.File;
35+
import java.net.URI;
3436
import java.util.List;
3537

3638
/**
@@ -56,7 +58,9 @@ protected ScmResult executeRemoveCommand( ScmProviderRepository repo, ScmFileSet
5658

5759
Commandline cl = createCommandLine( fileSet.getBasedir(), fileSet.getFileList() );
5860

59-
GitRemoveConsumer consumer = new GitRemoveConsumer();
61+
// git-rm uses repositoryRoot instead of workingDirectory, adjust it with relativeRepositoryPath
62+
URI relativeRepositoryPath = GitStatusCommand.getRelativeCWD( logger, fileSet );
63+
GitRemoveConsumer consumer = new GitRemoveConsumer( relativeRepositoryPath );
6064

6165
CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
6266

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/remove/GitRemoveConsumer.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121

2222
import org.apache.maven.scm.ScmFile;
2323
import org.apache.maven.scm.ScmFileStatus;
24+
import org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusConsumer;
2425
import org.apache.maven.scm.util.AbstractConsumer;
2526

27+
import java.net.URI;
2628
import java.util.ArrayList;
2729
import java.util.List;
2830
import java.util.regex.Matcher;
@@ -43,10 +45,22 @@ public class GitRemoveConsumer
4345

4446
private final List<ScmFile> removedFiles = new ArrayList<>();
4547

48+
private final URI relativeRepositoryPath;
49+
4650
// ----------------------------------------------------------------------
4751
// StreamConsumer Implementation
4852
// ----------------------------------------------------------------------
4953

54+
public GitRemoveConsumer()
55+
{
56+
this( null );
57+
}
58+
59+
public GitRemoveConsumer( URI relativeRepositoryPath )
60+
{
61+
this.relativeRepositoryPath = relativeRepositoryPath;
62+
}
63+
5064
/**
5165
* {@inheritDoc}
5266
*/
@@ -60,7 +74,7 @@ public void consumeLine( String line )
6074
Matcher matcher = REMOVED_PATTERN.matcher( line );
6175
if ( matcher.matches() )
6276
{
63-
String file = matcher.group( 1 );
77+
String file = GitStatusConsumer.resolvePath( matcher.group( 1 ), relativeRepositoryPath );
6478
removedFiles.add( new ScmFile( file, ScmFileStatus.DELETED ) );
6579
}
6680
else

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/status/GitStatusConsumer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ private boolean isFile( String file )
279279
return targetFile.isFile();
280280
}
281281

282-
protected static String resolvePath( String fileEntry, URI path )
282+
public static String resolvePath( String fileEntry, URI path )
283283
{
284284
/* Quotes may be included (from the git status line) when an fileEntry includes spaces */
285285
String cleanedEntry = stripQuotes( fileEntry );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.apache.maven.scm.provider.git.gitexe.command.remove;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import java.io.File;
23+
24+
import org.apache.maven.scm.command.checkout.CheckOutScmResult;
25+
import org.apache.maven.scm.provider.git.command.remove.GitRemoveCommandTckTest;
26+
import org.apache.maven.scm.provider.git.GitScmTestUtils;
27+
import org.apache.maven.scm.repository.ScmRepository;
28+
29+
import static org.apache.maven.scm.provider.git.GitScmTestUtils.GIT_COMMAND_LINE;
30+
31+
public class GitExeRemoveCommandTckTest
32+
extends GitRemoveCommandTckTest
33+
{
34+
@Override
35+
public String getScmProviderCommand()
36+
{
37+
return GIT_COMMAND_LINE;
38+
}
39+
40+
@Override
41+
protected CheckOutScmResult checkOut( File workingDirectory, ScmRepository repository ) throws Exception
42+
{
43+
try
44+
{
45+
return super.checkOut( workingDirectory, repository );
46+
}
47+
finally
48+
{
49+
GitScmTestUtils.setDefaultUser( workingDirectory );
50+
}
51+
}
52+
}

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/test/java/org/apache/maven/scm/provider/git/gitexe/command/remove/GitRemoveConsumerTest.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.maven.scm.ScmFile;
2323
import org.apache.maven.scm.ScmFileStatus;
2424
import org.apache.maven.scm.ScmTestCase;
25+
import org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusConsumer;
2526
import org.apache.maven.scm.util.ConsumerUtils;
2627
import org.junit.Test;
2728

@@ -42,13 +43,28 @@ public class GitRemoveConsumerTest
4243
public void testConsumerRemovedFile()
4344
{
4445
GitRemoveConsumer consumer = new GitRemoveConsumer();
45-
46+
4647
consumer.consumeLine( "rm 'project.xml'" );
47-
48+
4849
List<ScmFile> changedFiles = consumer.getRemovedFiles();
49-
50+
5051
assertNotNull( changedFiles );
5152
assertEquals( 1, changedFiles.size() );
53+
assertEquals( "project.xml", changedFiles.get( 0 ).getPath() );
54+
}
55+
56+
@Test
57+
public void testConsumerRemovedFileInDifferentDir()
58+
{
59+
GitRemoveConsumer consumer = new GitRemoveConsumer( GitStatusConsumer.uriFromPath( "main" ) );
60+
61+
consumer.consumeLine( "rm 'main/project.xml'" );
62+
63+
List<ScmFile> changedFiles = consumer.getRemovedFiles();
64+
65+
assertNotNull( changedFiles );
66+
assertEquals( 1, changedFiles.size() );
67+
assertEquals( "project.xml", changedFiles.get( 0 ).getPath() );
5268
}
5369

5470
@Test
@@ -62,7 +78,7 @@ public void testLog1Consumer()
6278
ConsumerUtils.consumeFile( f, consumer );
6379

6480
List<ScmFile> changedFiles = consumer.getRemovedFiles();
65-
81+
6682
assertEquals( 2, changedFiles.size() );
6783

6884
testScmFile( (ScmFile) changedFiles.get( 0 ), "src/main/java/Application.java", ScmFileStatus.DELETED );
@@ -80,14 +96,14 @@ public void testEmptyLogConsumer()
8096
ConsumerUtils.consumeFile( f, consumer );
8197

8298
List<ScmFile> changedFiles = consumer.getRemovedFiles();
83-
99+
84100
assertEquals( 0, changedFiles.size() );
85-
}
86-
101+
}
102+
87103
private void testScmFile( ScmFile fileToTest, String expectedFilePath, ScmFileStatus expectedStatus )
88104
{
89105
assertEquals( expectedFilePath, fileToTest.getPath() );
90106
assertEquals( expectedStatus, fileToTest.getStatus() );
91107
}
92-
108+
93109
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.apache.maven.scm.provider.git.command.remove;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import org.apache.maven.scm.provider.git.GitScmTestUtils;
23+
import org.apache.maven.scm.tck.command.remove.RemoveCommandTckTest;
24+
25+
/**
26+
* This test tests the remove command.
27+
*
28+
* @author Georg Tsakumagos
29+
*/
30+
public abstract class GitRemoveCommandTckTest
31+
extends RemoveCommandTckTest
32+
{
33+
/** {@inheritDoc} */
34+
public String getScmUrl()
35+
throws Exception
36+
{
37+
return GitScmTestUtils.getScmUrl( getRepositoryRoot(), "git" );
38+
}
39+
40+
/** {@inheritDoc} */
41+
public void initRepo()
42+
throws Exception
43+
{
44+
GitScmTestUtils.initRepo( "src/test/resources/repository/", getRepositoryRoot(), getWorkingDirectory() );
45+
}
46+
}

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/JGitScmProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.apache.maven.scm.provider.git.jgit.command.info.JGitInfoCommand;
4242
import org.apache.maven.scm.provider.git.jgit.command.list.JGitListCommand;
4343
import org.apache.maven.scm.provider.git.jgit.command.remoteinfo.JGitRemoteInfoCommand;
44+
import org.apache.maven.scm.provider.git.jgit.command.remove.JGitRemoveCommand;
4445
import org.apache.maven.scm.provider.git.jgit.command.status.JGitStatusCommand;
4546
import org.apache.maven.scm.provider.git.jgit.command.tag.JGitTagCommand;
4647
import org.apache.maven.scm.provider.git.jgit.command.untag.JGitUntagCommand;
@@ -142,7 +143,7 @@ protected GitCommand getExportCommand()
142143
@Override
143144
protected GitCommand getRemoveCommand()
144145
{
145-
throw new UnsupportedOperationException( "getRemoveCommand" );
146+
return new JGitRemoveCommand();
146147
}
147148

148149
/**

0 commit comments

Comments
 (0)