1/*
2 * changelist-cmd.c -- Associate (or deassociate) a wc path with a changelist.
3 *
4 * ====================================================================
5 *    Licensed to the Apache Software Foundation (ASF) under one
6 *    or more contributor license agreements.  See the NOTICE file
7 *    distributed with this work for additional information
8 *    regarding copyright ownership.  The ASF licenses this file
9 *    to you under the Apache License, Version 2.0 (the
10 *    "License"); you may not use this file except in compliance
11 *    with the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 *    Unless required by applicable law or agreed to in writing,
16 *    software distributed under the License is distributed on an
17 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 *    KIND, either express or implied.  See the License for the
19 *    specific language governing permissions and limitations
20 *    under the License.
21 * ====================================================================
22 */
23
24#include "svn_client.h"
25#include "svn_error_codes.h"
26#include "svn_error.h"
27#include "svn_path.h"
28#include "svn_utf.h"
29
30#include "cl.h"
31
32#include "svn_private_config.h"
33
34
35
36
37/* This implements the `svn_opt_subcommand_t' interface. */
38svn_error_t *
39svn_cl__changelist(apr_getopt_t *os,
40                   void *baton,
41                   apr_pool_t *pool)
42{
43  const char *changelist_name = NULL;
44  svn_cl__opt_state_t *opt_state = ((svn_cl__cmd_baton_t *) baton)->opt_state;
45  svn_client_ctx_t *ctx = ((svn_cl__cmd_baton_t *) baton)->ctx;
46  apr_array_header_t *targets;
47  svn_depth_t depth = opt_state->depth;
48  apr_array_header_t *errors = apr_array_make(pool, 0, sizeof(apr_status_t));
49
50  /* If we're not removing changelists, then our first argument should
51     be the name of a changelist. */
52
53  if (! opt_state->remove)
54    {
55      apr_array_header_t *args;
56      SVN_ERR(svn_opt_parse_num_args(&args, os, 1, pool));
57      changelist_name = APR_ARRAY_IDX(args, 0, const char *);
58      SVN_ERR(svn_utf_cstring_to_utf8(&changelist_name,
59                                      changelist_name, pool));
60    }
61
62  /* Parse the remaining arguments as paths. */
63  SVN_ERR(svn_cl__args_to_target_array_print_reserved(&targets, os,
64                                                      opt_state->targets,
65                                                      ctx, FALSE, pool));
66
67  /* Changelist has no implicit dot-target `.', so don't you put that
68     code here! */
69  if (! targets->nelts)
70    return svn_error_create(SVN_ERR_CL_INSUFFICIENT_ARGS, 0, NULL);
71
72  SVN_ERR(svn_cl__check_targets_are_local_paths(targets));
73
74  if (opt_state->quiet)
75    /* FIXME: This is required because svn_client_create_context()
76       always initializes ctx->notify_func2 to a wrapper function
77       which calls ctx->notify_func() if it isn't NULL.  In other
78       words, typically, ctx->notify_func2 is never NULL.  This isn't
79       usually a problem, but the changelist logic generates
80       svn_error_t's as part of its notification.
81
82       So, svn_wc_set_changelist() checks its notify_func (our
83       ctx->notify_func2) for NULL-ness, and seeing non-NULL-ness,
84       generates a notificaton object and svn_error_t to describe some
85       problem.  It passes that off to its notify_func (our
86       ctx->notify_func2) which drops the notification on the floor
87       (because it wraps a NULL ctx->notify_func).  But svn_error_t's
88       dropped on the floor cause SEGFAULTs at pool cleanup time --
89       they need instead to be cleared.
90
91       SOOOooo... we set our ctx->notify_func2 to NULL so the WC code
92       doesn't even generate the errors.  */
93    ctx->notify_func2 = NULL;
94
95  if (depth == svn_depth_unknown)
96    depth = svn_depth_empty;
97
98  SVN_ERR(svn_cl__eat_peg_revisions(&targets, targets, pool));
99
100  if (changelist_name)
101    {
102      SVN_ERR(svn_cl__try(
103               svn_client_add_to_changelist(targets, changelist_name,
104                                            depth, opt_state->changelists,
105                                            ctx, pool),
106               errors, opt_state->quiet,
107               SVN_ERR_UNVERSIONED_RESOURCE,
108               SVN_ERR_WC_PATH_NOT_FOUND,
109               SVN_NO_ERROR));
110    }
111  else
112    {
113      SVN_ERR(svn_cl__try(
114               svn_client_remove_from_changelists(targets, depth,
115                                                  opt_state->changelists,
116                                                  ctx, pool),
117               errors, opt_state->quiet,
118               SVN_ERR_UNVERSIONED_RESOURCE,
119               SVN_ERR_WC_PATH_NOT_FOUND,
120               SVN_NO_ERROR));
121    }
122
123  if (errors->nelts > 0)
124    {
125      int i;
126      svn_error_t *err;
127
128      err = svn_error_create(SVN_ERR_ILLEGAL_TARGET, NULL, NULL);
129      for (i = 0; i < errors->nelts; i++)
130        {
131          apr_status_t status = APR_ARRAY_IDX(errors, i, apr_status_t);
132
133          if (status == SVN_ERR_WC_PATH_NOT_FOUND)
134            err = svn_error_quick_wrap(err,
135                                       _("Could not set changelists on "
136                                         "all targets because some targets "
137                                         "don't exist"));
138          else if (status == SVN_ERR_UNVERSIONED_RESOURCE)
139            err = svn_error_quick_wrap(err,
140                                       _("Could not set changelists on "
141                                         "all targets because some targets "
142                                         "are not versioned"));
143        }
144
145      return svn_error_trace(err);
146    }
147
148  return SVN_NO_ERROR;
149}
150