2 * status-cmd.c -- Display status information in current directory
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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
21 * ====================================================================
24 /* ==================================================================== */
31 #include "svn_string.h"
33 #include "svn_client.h"
34 #include "svn_error_codes.h"
35 #include "svn_error.h"
36 #include "svn_pools.h"
38 #include "svn_dirent_uri.h"
40 #include "svn_cmdline.h"
43 #include "svn_private_config.h"
44 #include "private/svn_wc_private.h"
52 /* These fields all correspond to the ones in the
53 svn_cl__print_status() interface. */
54 const char *cwd_abspath;
55 svn_boolean_t suppress_externals_placeholders;
56 svn_boolean_t detailed;
57 svn_boolean_t show_last_committed;
58 svn_boolean_t skip_unrecognized;
59 svn_boolean_t repos_locks;
61 apr_hash_t *cached_changelists;
62 apr_pool_t *cl_pool; /* where cached changelists are allocated */
64 svn_boolean_t had_print_error; /* To avoid printing lots of errors if we get
65 errors while printing to stdout */
66 svn_boolean_t xml_mode;
69 unsigned int text_conflicts;
70 unsigned int prop_conflicts;
71 unsigned int tree_conflicts;
73 svn_client_ctx_t *ctx;
80 svn_client_status_t *status;
83 /* Print conflict stats accumulated in status baton SB.
84 * Do temporary allocations in POOL. */
86 print_conflict_stats(struct status_baton *sb, apr_pool_t *pool)
88 if (sb->text_conflicts > 0 || sb->prop_conflicts > 0 ||
89 sb->tree_conflicts > 0)
90 SVN_ERR(svn_cmdline_printf(pool, "%s", _("Summary of conflicts:\n")));
92 if (sb->text_conflicts > 0)
93 SVN_ERR(svn_cmdline_printf
94 (pool, _(" Text conflicts: %u\n"), sb->text_conflicts));
96 if (sb->prop_conflicts > 0)
97 SVN_ERR(svn_cmdline_printf
98 (pool, _(" Property conflicts: %u\n"), sb->prop_conflicts));
100 if (sb->tree_conflicts > 0)
101 SVN_ERR(svn_cmdline_printf
102 (pool, _(" Tree conflicts: %u\n"), sb->tree_conflicts));
107 /* Prints XML target element with path attribute TARGET, using POOL for
108 temporary allocations. */
110 print_start_target_xml(const char *target, apr_pool_t *pool)
112 svn_stringbuf_t *sb = svn_stringbuf_create_empty(pool);
114 svn_xml_make_open_tag(&sb, pool, svn_xml_normal, "target",
115 "path", target, NULL);
117 return svn_cl__error_checked_fputs(sb->data, stdout);
121 /* Finish a target element by optionally printing an against element if
122 * REPOS_REV is a valid revision number, and then printing an target end tag.
123 * Use POOL for temporary allocations. */
125 print_finish_target_xml(svn_revnum_t repos_rev,
128 svn_stringbuf_t *sb = svn_stringbuf_create_empty(pool);
130 if (SVN_IS_VALID_REVNUM(repos_rev))
132 const char *repos_rev_str;
133 repos_rev_str = apr_psprintf(pool, "%ld", repos_rev);
134 svn_xml_make_open_tag(&sb, pool, svn_xml_self_closing, "against",
135 "revision", repos_rev_str, NULL);
138 svn_xml_make_close_tag(&sb, pool, "target");
140 return svn_cl__error_checked_fputs(sb->data, stdout);
144 /* Function which *actually* causes a status structure to be output to
145 the user. Called by both print_status() and svn_cl__status(). */
147 print_status_normal_or_xml(void *baton,
149 const svn_client_status_t *status,
152 struct status_baton *sb = baton;
155 return svn_cl__print_status_xml(sb->cwd_abspath, path, status,
158 return svn_cl__print_status(sb->cwd_abspath, path, status,
159 sb->suppress_externals_placeholders,
161 sb->show_last_committed,
162 sb->skip_unrecognized,
172 /* A status callback function for printing STATUS for PATH. */
174 print_status(void *baton,
176 const svn_client_status_t *status,
179 struct status_baton *sb = baton;
180 const char *local_abspath = status->local_abspath;
182 /* ### The revision information with associates are based on what
183 * ### _read_info() returns. The svn_wc_status_func4_t callback is
184 * ### suppposed to handle the gathering of additional information from the
185 * ### WORKING nodes on its own. Until we've agreed on how the CLI should
186 * ### handle the revision information, we use this appproach to stay compat
187 * ### with our testsuite. */
188 if (status->versioned
189 && !SVN_IS_VALID_REVNUM(status->revision)
191 && (status->node_status == svn_wc_status_deleted
192 || status->node_status == svn_wc_status_replaced))
194 svn_client_status_t *twks = svn_client_status_dup(status, sb->cl_pool);
196 /* Copied is FALSE, so either we have a local addition, or we have
197 a delete that directly shadows a BASE node */
199 switch(status->node_status)
201 case svn_wc_status_replaced:
202 /* Just retrieve the revision below the replacement.
203 The other fields are filled by a copy.
204 (With ! copied, we know we have a BASE node)
206 ### Is this really what we want to provide? */
207 SVN_ERR(svn_wc__node_get_pre_ng_status_data(&twks->revision,
213 case svn_wc_status_deleted:
214 /* Retrieve some data from the original version below the delete */
215 SVN_ERR(svn_wc__node_get_pre_ng_status_data(&twks->revision,
218 &twks->changed_author,
225 /* This space intentionally left blank. */
232 /* If the path is part of a changelist, then we don't print
233 the item, but instead dup & cache the status structure for later. */
234 if (status->changelist)
236 /* The hash maps a changelist name to an array of status_cache
238 apr_array_header_t *path_array;
239 const char *cl_key = apr_pstrdup(sb->cl_pool, status->changelist);
240 struct status_cache *scache = apr_pcalloc(sb->cl_pool, sizeof(*scache));
241 scache->path = apr_pstrdup(sb->cl_pool, path);
242 scache->status = svn_client_status_dup(status, sb->cl_pool);
245 svn_hash_gets(sb->cached_changelists, cl_key);
246 if (path_array == NULL)
248 path_array = apr_array_make(sb->cl_pool, 1,
249 sizeof(struct status_cache *));
250 svn_hash_sets(sb->cached_changelists, cl_key, path_array);
253 APR_ARRAY_PUSH(path_array, struct status_cache *) = scache;
257 return print_status_normal_or_xml(baton, path, status, pool);
260 /* This implements the `svn_opt_subcommand_t' interface. */
262 svn_cl__status(apr_getopt_t *os,
264 apr_pool_t *scratch_pool)
266 svn_cl__opt_state_t *opt_state = ((svn_cl__cmd_baton_t *) baton)->opt_state;
267 svn_client_ctx_t *ctx = ((svn_cl__cmd_baton_t *) baton)->ctx;
268 apr_array_header_t *targets;
269 apr_pool_t *iterpool;
270 apr_hash_t *master_cl_hash = apr_hash_make(scratch_pool);
272 svn_opt_revision_t rev;
273 struct status_baton sb;
275 SVN_ERR(svn_cl__args_to_target_array_print_reserved(&targets, os,
280 /* Add "." if user passed 0 arguments */
281 svn_opt_push_implicit_dot_target(targets, scratch_pool);
283 SVN_ERR(svn_cl__check_targets_are_local_paths(targets));
285 /* We want our -u statuses to be against HEAD. */
286 rev.kind = svn_opt_revision_head;
288 sb.had_print_error = FALSE;
292 /* If output is not incremental, output the XML header and wrap
293 everything in a top-level element. This makes the output in
294 its entirety a well-formed XML document. */
295 if (! opt_state->incremental)
296 SVN_ERR(svn_cl__xml_print_header("status", scratch_pool));
300 if (opt_state->incremental)
301 return svn_error_create(SVN_ERR_CL_ARG_PARSING_ERROR, NULL,
302 _("'incremental' option only valid in XML "
306 SVN_ERR(svn_dirent_get_absolute(&(sb.cwd_abspath), "", scratch_pool));
307 sb.suppress_externals_placeholders = (opt_state->quiet
308 && (! opt_state->verbose));
309 sb.detailed = (opt_state->verbose || opt_state->update);
310 sb.show_last_committed = opt_state->verbose;
311 sb.skip_unrecognized = opt_state->quiet;
312 sb.repos_locks = opt_state->update;
313 sb.xml_mode = opt_state->xml;
314 sb.cached_changelists = master_cl_hash;
315 sb.cl_pool = scratch_pool;
316 sb.text_conflicts = 0;
317 sb.prop_conflicts = 0;
318 sb.tree_conflicts = 0;
321 SVN_ERR(svn_cl__eat_peg_revisions(&targets, targets, scratch_pool));
323 iterpool = svn_pool_create(scratch_pool);
324 for (i = 0; i < targets->nelts; i++)
326 const char *target = APR_ARRAY_IDX(targets, i, const char *);
327 svn_revnum_t repos_rev = SVN_INVALID_REVNUM;
329 svn_pool_clear(iterpool);
331 SVN_ERR(svn_cl__check_cancel(ctx->cancel_baton));
334 SVN_ERR(print_start_target_xml(svn_dirent_local_style(target, iterpool),
337 /* Retrieve a hash of status structures with the information
338 requested by the user. */
339 SVN_ERR(svn_cl__try(svn_client_status5(&repos_rev, ctx, target, &rev,
343 opt_state->no_ignore,
344 opt_state->ignore_externals,
345 FALSE /* depth_as_sticky */,
346 opt_state->changelists,
349 NULL, opt_state->quiet,
351 SVN_ERR_WC_NOT_WORKING_COPY,
352 SVN_ERR_WC_PATH_NOT_FOUND));
355 SVN_ERR(print_finish_target_xml(repos_rev, iterpool));
358 /* If any paths were cached because they were associatied with
359 changelists, we can now display them as grouped changelists. */
360 if (apr_hash_count(master_cl_hash) > 0)
362 apr_hash_index_t *hi;
363 svn_stringbuf_t *buf;
366 buf = svn_stringbuf_create_empty(scratch_pool);
368 for (hi = apr_hash_first(scratch_pool, master_cl_hash); hi;
369 hi = apr_hash_next(hi))
371 const char *changelist_name = svn__apr_hash_index_key(hi);
372 apr_array_header_t *path_array = svn__apr_hash_index_val(hi);
375 /* ### TODO: For non-XML output, we shouldn't print the
376 ### leading \n on the first changelist if there were no
377 ### non-changelist entries. */
380 svn_stringbuf_setempty(buf);
381 svn_xml_make_open_tag(&buf, scratch_pool, svn_xml_normal,
382 "changelist", "name", changelist_name,
384 SVN_ERR(svn_cl__error_checked_fputs(buf->data, stdout));
387 SVN_ERR(svn_cmdline_printf(scratch_pool,
388 _("\n--- Changelist '%s':\n"),
391 for (j = 0; j < path_array->nelts; j++)
393 struct status_cache *scache =
394 APR_ARRAY_IDX(path_array, j, struct status_cache *);
395 SVN_ERR(print_status_normal_or_xml(&sb, scache->path,
396 scache->status, scratch_pool));
401 svn_stringbuf_setempty(buf);
402 svn_xml_make_close_tag(&buf, scratch_pool, "changelist");
403 SVN_ERR(svn_cl__error_checked_fputs(buf->data, stdout));
407 svn_pool_destroy(iterpool);
409 if (opt_state->xml && (! opt_state->incremental))
410 SVN_ERR(svn_cl__xml_print_footer("status", scratch_pool));
412 if (! opt_state->quiet && ! opt_state->xml)
413 SVN_ERR(print_conflict_stats(&sb, scratch_pool));