001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.svn;
020
021/**
022 * Command utilities for svn commands.
023 *
024 * @author <a href="mailto:jerome@coffeebreaks.org">Jerome Lacoste</a>
025 *
026 */
027public final class SvnCommandUtils {
028
029    private SvnCommandUtils() {}
030
031    /**
032     * Add or overrides the username into a url with a svn+ssh scheme.
033     * <p>
034     * Svn 1.3.1 doesn't use the username information specified by --username when the url
035     * uses the svn+ssh scheme. This allows to fix it. See MRELEASE-35.
036     * <p>
037     * Convert file url which derived from windows file path to unix path.
038     *
039     * @param url      the url, not <code>null</code>
040     * @param username the username, may be <code>null</code>
041     * @return the fixed url
042     * @throws NullPointerException if url is <code>null</code>
043     */
044    public static String fixUrl(String url, String username) {
045        if (!(username == null || username.isEmpty()) && url.startsWith("svn+ssh://")) {
046            // is there a username to override ? If so we cut after
047            int idx = url.indexOf('@');
048            int cutIdx = idx < 0 ? "svn+ssh://".length() : idx + 1;
049            url = "svn+ssh://" + username + "@" + url.substring(cutIdx);
050        } else if (url.startsWith("file://")) {
051            // some svn commands does not understand windows path separator in file URL derived from windows file path
052            url = url.replace('\\', '/');
053        }
054
055        return url;
056    }
057}