<commit_before>/*
 * cmdline.c:  command-line processing
 *
 * ====================================================================
 *    Licensed to the Apache Software Foundation (ASF) under one
 *    or more contributor license agreements.  See the NOTICE file
 *    distributed with this work for additional information
 *    regarding copyright ownership.  The ASF licenses this file
 *    to you under the Apache License, Version 2.0 (the
 *    "License"); you may not use this file except in compliance
 *    with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing,
 *    software distributed under the License is distributed on an
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *    KIND, either express or implied.  See the License for the
 *    specific language governing permissions and limitations
 *    under the License.
 * ====================================================================
 */

/* ==================================================================== */



/*** Includes. ***/
#include "svn_client.h"
#include "svn_error.h"
#include "svn_dirent_uri.h"
#include "svn_path.h"
#include "svn_opt.h"
#include "svn_utf.h"

#include "client.h"

#include "private/svn_opt_private.h"

#include "svn_private_config.h"



/*** Code. ***/

#define DEFAULT_ARRAY_SIZE 5

/* Return true iff ARG is a repository-relative URL: specifically that
 * it starts with the characters "^/".
 * ARG is in UTF-8 encoding.
 * Do not check whether ARG is properly URI-encoded, canonical, or valid
 * in any other way. */
static svn_boolean_t
arg_is_repos_relative_url(const char *arg)
{
  return (0 == strncmp("^/", arg, 2));
}

/* Set *ABSOLUTE_URL to the absolute URL represented by RELATIVE_URL
 * relative to REPOS_ROOT_URL.
 * *ABSOLUTE_URL will end with a peg revision specifier if RELATIVE_URL did.
 * RELATIVE_URL is in repository-relative syntax:
 * "^/[REL-URL][@PEG]",
 * REPOS_ROOT_URL is the absolute URL of the repository root.
 * All strings are in UTF-8 encoding.
 * Allocate *ABSOLUTE_URL in POOL.
 *
 * REPOS_ROOT_URL and RELATIVE_URL do not have to be properly URI-encoded,
 * canonical, or valid in any other way.  The caller is expected to perform
 * canonicalization on *ABSOLUTE_URL after the call to the function.
 */
static svn_error_t *
resolve_repos_relative_url(const char **absolute_url,
                           const char *relative_url,
                           const char *repos_root_url,
                           apr_pool_t *pool)
{
  if (! arg_is_repos_relative_url(relative_url))
    return svn_error_createf(SVN_ERR_BAD_URL, NULL,
                             _("Improper relative URL '%s'"),
                             relative_url);

  /* No assumptions are made about the canonicalization of the input
   * arguments, it is presumed that the output will be canonicalized after
   * this function, which will remove any duplicate path separator.
   */
  *absolute_url = apr_pstrcat(pool, repos_root_url, relative_url + 1, NULL);

  return SVN_NO_ERROR;
}


/* Attempt to find the repository root url for TARGET, possibly using CTX for
 * authentication.  If one is found and *ROOT_URL is not NULL, then just check
 * that the root url for TARGET matches the value given in *ROOT_URL and
 * return an error if it does not.  If one is found and *ROOT_URL is NULL then
 * set *ROOT_URL to the root url for TARGET, allocated from POOL.
 * If a root url is not found for TARGET because it does not exist in the
 * repository, then return with no error.
 *
 * TARGET is a UTF-8 encoded string that is fully canonicalized and escaped.
 */
static svn_error_t *
check_root_url_of_target(const char **root_url,
                         const char *target,
                         svn_client_ctx_t *ctx,
                         apr_pool_t *pool)
{
  svn_error_t *err;
  const char *tmp_root_url;
  const char *truepath;
  svn_opt_revision_t opt_rev;

  SVN_ERR(svn_opt_parse_path(&opt_rev, &truepath, target, pool));
  if (!svn_path_is_url(truepath))
    SVN_ERR(svn_dirent_get_absolute(&truepath, truepath, pool));

  err =  svn_client__get_repos_root(&tmp_root_url, truepath, &opt_rev,
                                    ctx, pool, pool);

  if (err)
    {
      /* It is OK if the given target does not exist, it just means
       * we will not be able to determine the root url from this particular
       * argument.
       */
      if ((err->apr_err == SVN_ERR_ENTRY_NOT_FOUND)
          || (err->apr_err == SVN_ERR_WC_NOT_WORKING_COPY))
        {
          svn_error_clear(err);
          return SVN_NO_ERROR;
        }
      else
        return svn_error_return(err);
     }

   if (*root_url != NULL)
     {
       if (strcmp(*root_url, tmp_root_url) != 0)
         return svn_error_createf(SVN_ERR_ILLEGAL_TARGET, NULL,
                                  _("All non-relative targets must have "
                                    "the same root URL"));
     }
   else
     *root_url = tmp_root_url;

   return SVN_NO_ERROR;
}

/* Note: This is substantially copied from svn_opt__args_to_target_array() in
 * order to move to libsvn_client while maintaining backward compatibility. */
svn_error_t *<commit_msg>* subversion/libsvn_client/cmdline.c:   (check_root_url_of_target): Catch SVN_ERR_WC_PATH_NOT_FOUND in addition   to other similar error codes, allowing argument processing to continue   as expected.<commit_after>/*
 * cmdline.c:  command-line processing
 *
 * ====================================================================
 *    Licensed to the Apache Software Foundation (ASF) under one
 *    or more contributor license agreements.  See the NOTICE file
 *    distributed with this work for additional information
 *    regarding copyright ownership.  The ASF licenses this file
 *    to you under the Apache License, Version 2.0 (the
 *    "License"); you may not use this file except in compliance
 *    with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing,
 *    software distributed under the License is distributed on an
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *    KIND, either express or implied.  See the License for the
 *    specific language governing permissions and limitations
 *    under the License.
 * ====================================================================
 */

/* ==================================================================== */



/*** Includes. ***/
#include "svn_client.h"
#include "svn_error.h"
#include "svn_dirent_uri.h"
#include "svn_path.h"
#include "svn_opt.h"
#include "svn_utf.h"

#include "client.h"

#include "private/svn_opt_private.h"

#include "svn_private_config.h"



/*** Code. ***/

#define DEFAULT_ARRAY_SIZE 5

/* Return true iff ARG is a repository-relative URL: specifically that
 * it starts with the characters "^/".
 * ARG is in UTF-8 encoding.
 * Do not check whether ARG is properly URI-encoded, canonical, or valid
 * in any other way. */
static svn_boolean_t
arg_is_repos_relative_url(const char *arg)
{
  return (0 == strncmp("^/", arg, 2));
}

/* Set *ABSOLUTE_URL to the absolute URL represented by RELATIVE_URL
 * relative to REPOS_ROOT_URL.
 * *ABSOLUTE_URL will end with a peg revision specifier if RELATIVE_URL did.
 * RELATIVE_URL is in repository-relative syntax:
 * "^/[REL-URL][@PEG]",
 * REPOS_ROOT_URL is the absolute URL of the repository root.
 * All strings are in UTF-8 encoding.
 * Allocate *ABSOLUTE_URL in POOL.
 *
 * REPOS_ROOT_URL and RELATIVE_URL do not have to be properly URI-encoded,
 * canonical, or valid in any other way.  The caller is expected to perform
 * canonicalization on *ABSOLUTE_URL after the call to the function.
 */
static svn_error_t *
resolve_repos_relative_url(const char **absolute_url,
                           const char *relative_url,
                           const char *repos_root_url,
                           apr_pool_t *pool)
{
  if (! arg_is_repos_relative_url(relative_url))
    return svn_error_createf(SVN_ERR_BAD_URL, NULL,
                             _("Improper relative URL '%s'"),
                             relative_url);

  /* No assumptions are made about the canonicalization of the input
   * arguments, it is presumed that the output will be canonicalized after
   * this function, which will remove any duplicate path separator.
   */
  *absolute_url = apr_pstrcat(pool, repos_root_url, relative_url + 1, NULL);

  return SVN_NO_ERROR;
}


/* Attempt to find the repository root url for TARGET, possibly using CTX for
 * authentication.  If one is found and *ROOT_URL is not NULL, then just check
 * that the root url for TARGET matches the value given in *ROOT_URL and
 * return an error if it does not.  If one is found and *ROOT_URL is NULL then
 * set *ROOT_URL to the root url for TARGET, allocated from POOL.
 * If a root url is not found for TARGET because it does not exist in the
 * repository, then return with no error.
 *
 * TARGET is a UTF-8 encoded string that is fully canonicalized and escaped.
 */
static svn_error_t *
check_root_url_of_target(const char **root_url,
                         const char *target,
                         svn_client_ctx_t *ctx,
                         apr_pool_t *pool)
{
  svn_error_t *err;
  const char *tmp_root_url;
  const char *truepath;
  svn_opt_revision_t opt_rev;

  SVN_ERR(svn_opt_parse_path(&opt_rev, &truepath, target, pool));
  if (!svn_path_is_url(truepath))
    SVN_ERR(svn_dirent_get_absolute(&truepath, truepath, pool));

  err =  svn_client__get_repos_root(&tmp_root_url, truepath, &opt_rev,
                                    ctx, pool, pool);

  if (err)
    {
      /* It is OK if the given target does not exist, it just means
       * we will not be able to determine the root url from this particular
       * argument.
       */
      if ((err->apr_err == SVN_ERR_ENTRY_NOT_FOUND)
          || (err->apr_err == SVN_ERR_WC_PATH_NOT_FOUND)
          || (err->apr_err == SVN_ERR_WC_NOT_WORKING_COPY))
        {
          svn_error_clear(err);
          return SVN_NO_ERROR;
        }
      else
        return svn_error_return(err);
     }

   if (*root_url != NULL)
     {
       if (strcmp(*root_url, tmp_root_url) != 0)
         return svn_error_createf(SVN_ERR_ILLEGAL_TARGET, NULL,
                                  _("All non-relative targets must have "
                                    "the same root URL"));
     }
   else
     *root_url = tmp_root_url;

   return SVN_NO_ERROR;
}

/* Note: This is substantially copied from svn_opt__args_to_target_array() in
 * order to move to libsvn_client while maintaining backward compatibility. */
svn_error_t *
{'fraction_non_alphanumeric': 0.11448059729007282, 'fraction_numerical': 0.0022121854548806342, 'mean_word_length': 2.954081632653061, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4650351', 'n_tokens_mistral': 3292, 'n_tokens_neox': 3175, 'n_words': 1110}