1 package org.apache.maven.scm.provider.svn.svnexe.command.checkin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.scm.ScmException;
23 import org.apache.maven.scm.ScmFileSet;
24 import org.apache.maven.scm.ScmVersion;
25 import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
26 import org.apache.maven.scm.command.checkin.CheckInScmResult;
27 import org.apache.maven.scm.provider.ScmProviderRepository;
28 import org.apache.maven.scm.provider.svn.command.SvnCommand;
29 import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
30 import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
31 import org.codehaus.plexus.util.FileUtils;
32 import org.codehaus.plexus.util.Os;
33 import org.codehaus.plexus.util.StringUtils;
34 import org.codehaus.plexus.util.cli.CommandLineException;
35 import org.codehaus.plexus.util.cli.CommandLineUtils;
36 import org.codehaus.plexus.util.cli.Commandline;
37
38 import java.io.File;
39 import java.io.IOException;
40
41
42
43
44
45
46 public class SvnCheckInCommand
47 extends AbstractCheckInCommand
48 implements SvnCommand
49 {
50
51 protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
52 ScmVersion version )
53 throws ScmException
54 {
55 if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
56 {
57 throw new ScmException( "This provider command can't handle tags." );
58 }
59
60 File messageFile = FileUtils.createTempFile( "maven-scm-", ".commit", null );
61
62 try
63 {
64 FileUtils.fileWrite( messageFile.getAbsolutePath(), "UTF-8", message );
65 }
66 catch ( IOException ex )
67 {
68 return new CheckInScmResult( null, "Error while making a temporary file for the commit message: "
69 + ex.getMessage(), null, false );
70 }
71
72 Commandline cl = createCommandLine( (SvnScmProviderRepository) repo, fileSet, messageFile );
73
74 SvnCheckInConsumer consumer = new SvnCheckInConsumer( getLogger(), fileSet.getBasedir() );
75
76 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
77
78 if ( getLogger().isInfoEnabled() )
79 {
80 getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
81
82 if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
83 {
84 getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
85 }
86 }
87
88 int exitCode;
89
90 try
91 {
92 exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
93 }
94 catch ( CommandLineException ex )
95 {
96 throw new ScmException( "Error while executing command.", ex );
97 }
98 finally
99 {
100 try
101 {
102 FileUtils.forceDelete( messageFile );
103 }
104 catch ( IOException ex )
105 {
106
107 }
108 }
109
110 if ( exitCode != 0 )
111 {
112 return new CheckInScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
113 }
114
115 return new CheckInScmResult( cl.toString(), consumer.getCheckedInFiles(),
116 Integer.toString( consumer.getRevision() ) );
117 }
118
119
120
121
122
123 public static Commandline createCommandLine( SvnScmProviderRepository repository, ScmFileSet fileSet,
124 File messageFile )
125 throws ScmException
126 {
127 Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( fileSet.getBasedir(), repository );
128
129 cl.createArg().setValue( "commit" );
130
131 cl.createArg().setValue( "--file" );
132
133 cl.createArg().setValue( messageFile.getAbsolutePath() );
134
135 cl.createArg().setValue( "--encoding" );
136
137 cl.createArg().setValue( "UTF-8" );
138
139 try
140 {
141 SvnCommandLineUtils.addTarget( cl, fileSet.getFileList() );
142 }
143 catch ( IOException e )
144 {
145 throw new ScmException( "Can't create the targets file", e );
146 }
147
148 return cl;
149 }
150 }