001package org.apache.maven.scm.provider.bazaar;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.scm.CommandParameters;
023import org.apache.maven.scm.ScmException;
024import org.apache.maven.scm.ScmFileSet;
025import org.apache.maven.scm.ScmResult;
026import org.apache.maven.scm.command.add.AddScmResult;
027import org.apache.maven.scm.command.blame.BlameScmResult;
028import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
029import org.apache.maven.scm.command.checkin.CheckInScmResult;
030import org.apache.maven.scm.command.checkout.CheckOutScmResult;
031import org.apache.maven.scm.command.diff.DiffScmResult;
032import org.apache.maven.scm.command.remove.RemoveScmResult;
033import org.apache.maven.scm.command.status.StatusScmResult;
034import org.apache.maven.scm.command.tag.TagScmResult;
035import org.apache.maven.scm.command.update.UpdateScmResult;
036import org.apache.maven.scm.provider.AbstractScmProvider;
037import org.apache.maven.scm.provider.ScmProviderRepository;
038import org.apache.maven.scm.provider.bazaar.command.BazaarConstants;
039import org.apache.maven.scm.provider.bazaar.command.add.BazaarAddCommand;
040import org.apache.maven.scm.provider.bazaar.command.blame.BazaarBlameCommand;
041import org.apache.maven.scm.provider.bazaar.command.changelog.BazaarChangeLogCommand;
042import org.apache.maven.scm.provider.bazaar.command.checkin.BazaarCheckInCommand;
043import org.apache.maven.scm.provider.bazaar.command.checkout.BazaarCheckOutCommand;
044import org.apache.maven.scm.provider.bazaar.command.diff.BazaarDiffCommand;
045import org.apache.maven.scm.provider.bazaar.command.remove.BazaarRemoveCommand;
046import org.apache.maven.scm.provider.bazaar.command.status.BazaarStatusCommand;
047import org.apache.maven.scm.provider.bazaar.command.tag.BazaarTagCommand;
048import org.apache.maven.scm.provider.bazaar.command.update.BazaarUpdateCommand;
049import org.apache.maven.scm.provider.bazaar.repository.BazaarScmProviderRepository;
050import org.apache.maven.scm.repository.ScmRepositoryException;
051import org.apache.maven.scm.repository.UnknownRepositoryStructure;
052
053import java.io.File;
054import java.util.ArrayList;
055import java.util.List;
056
057/**
058 * Bazaar NG http://bazaar-vcs.org/ is a decentralized revision control system. <br>
059 *
060 * @author <a href="mailto:torbjorn@smorgrav.org">Torbjorn Eikli Smorgrav</a>
061 *
062 * @plexus.component role="org.apache.maven.scm.provider.ScmProvider" role-hint="bazaar"
063 */
064public class BazaarScmProvider
065    extends AbstractScmProvider
066{
067    /** {@inheritDoc} */
068    public String getScmSpecificFilename()
069    {
070        return ".bzr";
071    }
072
073    /** {@inheritDoc} */
074    public ScmProviderRepository makeProviderScmRepository( String scmSpecificUrl, char delimiter )
075        throws ScmRepositoryException
076    {
077        return new BazaarScmProviderRepository( scmSpecificUrl );
078    }
079
080    /** {@inheritDoc} */
081    public ScmProviderRepository makeProviderScmRepository( File path )
082        throws ScmRepositoryException, UnknownRepositoryStructure
083    {
084        if ( path == null )
085        {
086            throw new NullPointerException( "Path argument is null" );
087        }
088
089        if ( !path.isDirectory() )
090        {
091            throw new ScmRepositoryException( path.getAbsolutePath() + " isn't a valid directory." );
092        }
093
094        File bzrDir = new File( path, ".bzr" );
095
096        if ( !bzrDir.exists() )
097        {
098            throw new ScmRepositoryException( path.getAbsolutePath() + " isn't a bazaar directory." );
099        }
100
101        return makeProviderScmRepository( "file:///" + path.getAbsolutePath(), ':' );
102    }
103
104    /** {@inheritDoc} */
105    public List<String> validateScmUrl( String scmSpecificUrl, char delimiter )
106    {
107
108        List<String> errorMessages = new ArrayList<String>();
109
110        String[] checkCmd = new String[]{BazaarConstants.CHECK, scmSpecificUrl};
111        ScmResult result;
112        try
113        {
114            File tmpDir = new File( System.getProperty( "java.io.tmpdir" ) );
115            result = BazaarUtils.execute( tmpDir, checkCmd );
116            if ( !result.isSuccess() )
117            {
118                errorMessages.add( result.getCommandOutput() );
119                errorMessages.add( result.getProviderMessage() );
120            }
121        }
122        catch ( ScmException e )
123        {
124            errorMessages.add( e.getMessage() );
125        }
126
127        return errorMessages;
128    }
129
130    /** {@inheritDoc} */
131    public String getScmType()
132    {
133        return "bazaar";
134    }
135
136    /** {@inheritDoc} */
137    public AddScmResult add( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
138        throws ScmException
139    {
140        BazaarAddCommand command = new BazaarAddCommand();
141
142        command.setLogger( getLogger() );
143
144        return (AddScmResult) command.execute( repository, fileSet, parameters );
145    }
146
147    /** {@inheritDoc} */
148    public ChangeLogScmResult changelog( ScmProviderRepository repository, ScmFileSet fileSet,
149                                         CommandParameters parameters )
150        throws ScmException
151    {
152        BazaarChangeLogCommand command = new BazaarChangeLogCommand();
153
154        command.setLogger( getLogger() );
155
156        return (ChangeLogScmResult) command.execute( repository, fileSet, parameters );
157    }
158
159    /** {@inheritDoc} */
160    public CheckInScmResult checkin( ScmProviderRepository repository, ScmFileSet fileSet,
161                                     CommandParameters parameters )
162        throws ScmException
163    {
164        BazaarCheckInCommand command = new BazaarCheckInCommand();
165
166        command.setLogger( getLogger() );
167
168        return (CheckInScmResult) command.execute( repository, fileSet, parameters );
169    }
170
171    /** {@inheritDoc} */
172    public CheckOutScmResult checkout( ScmProviderRepository repository, ScmFileSet fileSet,
173                                       CommandParameters parameters )
174        throws ScmException
175    {
176        BazaarCheckOutCommand command = new BazaarCheckOutCommand();
177
178        command.setLogger( getLogger() );
179
180        return (CheckOutScmResult) command.execute( repository, fileSet, parameters );
181    }
182
183    /** {@inheritDoc} */
184    public DiffScmResult diff( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
185        throws ScmException
186    {
187        BazaarDiffCommand command = new BazaarDiffCommand();
188
189        command.setLogger( getLogger() );
190
191        return (DiffScmResult) command.execute( repository, fileSet, parameters );
192    }
193
194    /** {@inheritDoc} */
195    public RemoveScmResult remove( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
196        throws ScmException
197    {
198        BazaarRemoveCommand command = new BazaarRemoveCommand();
199
200        command.setLogger( getLogger() );
201
202        return (RemoveScmResult) command.execute( repository, fileSet, parameters );
203    }
204
205    /** {@inheritDoc} */
206    public StatusScmResult status( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
207        throws ScmException
208    {
209        BazaarStatusCommand command = new BazaarStatusCommand();
210
211        command.setLogger( getLogger() );
212
213        return (StatusScmResult) command.execute( repository, fileSet, parameters );
214    }
215
216    /** {@inheritDoc} */
217    public TagScmResult tag( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
218        throws ScmException
219    {
220        BazaarTagCommand command = new BazaarTagCommand();
221
222        command.setLogger( getLogger() );
223
224        return (TagScmResult) command.execute( repository, fileSet, parameters );
225    }
226
227    /** {@inheritDoc} */
228    public UpdateScmResult update( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
229        throws ScmException
230    {
231        BazaarUpdateCommand command = new BazaarUpdateCommand();
232
233        command.setLogger( getLogger() );
234
235        return (UpdateScmResult) command.execute( repository, fileSet, parameters );
236    }
237
238    /** {@inheritDoc} */
239    protected BlameScmResult blame( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
240        throws ScmException
241    {
242        BazaarBlameCommand command = new BazaarBlameCommand();
243
244        command.setLogger( getLogger() );
245
246        return (BlameScmResult) command.execute( repository, fileSet, parameters );
247    }
248}