001 package 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 021 import org.apache.commons.lang.StringUtils; 022 import org.apache.maven.scm.CommandParameters; 023 import org.apache.maven.scm.ScmException; 024 import org.apache.maven.scm.ScmFileSet; 025 import org.apache.maven.scm.command.remoteinfo.AbstractRemoteInfoCommand; 026 import org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult; 027 import org.apache.maven.scm.log.ScmLogger; 028 import org.apache.maven.scm.provider.ScmProviderRepository; 029 import org.apache.maven.scm.provider.svn.command.SvnCommand; 030 import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository; 031 import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils; 032 import org.apache.maven.scm.util.AbstractConsumer; 033 import org.codehaus.plexus.util.cli.CommandLineException; 034 import org.codehaus.plexus.util.cli.CommandLineUtils; 035 import org.codehaus.plexus.util.cli.Commandline; 036 037 import java.util.HashMap; 038 import java.util.Map; 039 040 /** 041 * @author Olivier Lamy 042 * @since 1.6 043 */ 044 public 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 if ( output.indexOf( "W160013" ) >= 0 ) 154 { 155 return false; 156 } 157 throw new ScmException( cl.toString() + ".The svn command failed:" + stderr.getOutput() ); 158 } 159 160 return true; 161 } 162 163 private static class LsConsumer 164 extends AbstractConsumer 165 { 166 Map<String, String> infos = new HashMap<String, String>(); 167 168 String url; 169 170 LsConsumer( ScmLogger logger, String url ) 171 { 172 super( logger ); 173 this.url = url; 174 } 175 176 public void consumeLine( String s ) 177 { 178 infos.put( StringUtils.removeEnd( s, "/" ), url + "/" + s ); 179 } 180 181 Map<String, String> getInfos() 182 { 183 return infos; 184 } 185 } 186 }