001package org.apache.maven.scm.provider.svn.svnexe.command.remoteinfo; 002/* 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 */ 020 021import org.apache.commons.lang.StringUtils; 022import org.apache.maven.scm.CommandParameters; 023import org.apache.maven.scm.ScmException; 024import org.apache.maven.scm.ScmFileSet; 025import org.apache.maven.scm.command.remoteinfo.AbstractRemoteInfoCommand; 026import org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult; 027import org.apache.maven.scm.log.ScmLogger; 028import org.apache.maven.scm.provider.ScmProviderRepository; 029import org.apache.maven.scm.provider.svn.command.SvnCommand; 030import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository; 031import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils; 032import org.apache.maven.scm.util.AbstractConsumer; 033import org.codehaus.plexus.util.cli.CommandLineException; 034import org.codehaus.plexus.util.cli.CommandLineUtils; 035import org.codehaus.plexus.util.cli.Commandline; 036 037import java.util.HashMap; 038import java.util.Map; 039 040/** 041 * @author Olivier Lamy 042 * @since 1.6 043 */ 044public class SvnRemoteInfoCommand 045 extends AbstractRemoteInfoCommand 046 implements SvnCommand 047{ 048 @Override 049 public RemoteInfoScmResult executeRemoteInfoCommand( ScmProviderRepository repository, ScmFileSet fileSet, 050 CommandParameters parameters ) 051 throws ScmException 052 { 053 054 String url = ( (SvnScmProviderRepository) repository ).getUrl(); 055 // use a default svn layout, url is here http://svn.apache.org/repos/asf/maven/maven-3/trunk 056 // so as we presume we have good users using standard svn layout, we calculate tags and branches url 057 String baseUrl = StringUtils.endsWith( url, "/" ) 058 ? StringUtils.substringAfter( StringUtils.removeEnd( url, "/" ), "/" ) 059 : StringUtils.substringBeforeLast( url, "/" ); 060 061 Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( fileSet == null ? null : fileSet.getBasedir(), 062 (SvnScmProviderRepository) repository ); 063 064 cl.createArg().setValue( "ls" ); 065 066 cl.createArg().setValue( baseUrl + "/tags" ); 067 068 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); 069 070 LsConsumer consumer = new LsConsumer( getLogger(), baseUrl ); 071 072 int exitCode = 0; 073 074 Map<String, String> tagsInfos = null; 075 076 try 077 { 078 exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() ); 079 tagsInfos = consumer.infos; 080 081 } 082 catch ( CommandLineException ex ) 083 { 084 throw new ScmException( "Error while executing svn command.", ex ); 085 } 086 087 if ( exitCode != 0 ) 088 { 089 return new RemoteInfoScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false ); 090 } 091 092 cl = SvnCommandLineUtils.getBaseSvnCommandLine( fileSet == null ? null : fileSet.getBasedir(), 093 (SvnScmProviderRepository) repository ); 094 095 cl.createArg().setValue( "ls" ); 096 097 cl.createArg().setValue( baseUrl + "/tags" ); 098 099 stderr = new CommandLineUtils.StringStreamConsumer(); 100 101 consumer = new LsConsumer( getLogger(), baseUrl ); 102 103 Map<String, String> branchesInfos = null; 104 105 try 106 { 107 exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() ); 108 branchesInfos = consumer.infos; 109 110 } 111 catch ( CommandLineException ex ) 112 { 113 throw new ScmException( "Error while executing svn command.", ex ); 114 } 115 116 if ( exitCode != 0 ) 117 { 118 return new RemoteInfoScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false ); 119 } 120 121 return new RemoteInfoScmResult( cl.toString(), branchesInfos, tagsInfos ); 122 } 123 124 public boolean remoteUrlExist( ScmProviderRepository repository, CommandParameters parameters ) 125 throws ScmException 126 { 127 String url = ( (SvnScmProviderRepository) repository ).getUrl(); 128 129 Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( null, (SvnScmProviderRepository) repository ); 130 131 cl.createArg().setValue( "ls" ); 132 133 cl.createArg().setValue( url ); 134 135 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); 136 137 LsConsumer consumer = new LsConsumer( getLogger(), url ); 138 139 int exitCode = 0; 140 141 try 142 { 143 exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() ); 144 } 145 catch ( CommandLineException ex ) 146 { 147 throw new ScmException( "Error while executing svn command.", ex ); 148 } 149 150 if ( exitCode != 0 ) 151 { 152 String output = stderr.getOutput(); 153 //olamy: a bit ugly but.... 154 // trying to parse error from svn cli which indicate no remote path 155 if ( output.indexOf( "W160013" ) >= 0 || output.indexOf( "svn: URL" ) >= 0 ) 156 { 157 return false; 158 } 159 throw new ScmException( cl.toString() + ".The svn command failed:" + stderr.getOutput() ); 160 } 161 162 return true; 163 } 164 165 private static class LsConsumer 166 extends AbstractConsumer 167 { 168 Map<String, String> infos = new HashMap<String, String>(); 169 170 String url; 171 172 LsConsumer( ScmLogger logger, String url ) 173 { 174 super( logger ); 175 this.url = url; 176 } 177 178 public void consumeLine( String s ) 179 { 180 infos.put( StringUtils.removeEnd( s, "/" ), url + "/" + s ); 181 } 182 183 Map<String, String> getInfos() 184 { 185 return infos; 186 } 187 } 188}