2 * status.c: the command-line's portion of the "svn status" command
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 /* ==================================================================== */
30 #include "svn_cmdline.h"
32 #include "svn_dirent_uri.h"
36 #include "svn_private_config.h"
37 #include "cl-conflicts.h"
38 #include "private/svn_wc_private.h"
40 /* Return the single character representation of STATUS */
42 generate_status_code(enum svn_wc_status_kind status)
46 case svn_wc_status_none: return ' ';
47 case svn_wc_status_normal: return ' ';
48 case svn_wc_status_added: return 'A';
49 case svn_wc_status_missing: return '!';
50 case svn_wc_status_incomplete: return '!';
51 case svn_wc_status_deleted: return 'D';
52 case svn_wc_status_replaced: return 'R';
53 case svn_wc_status_modified: return 'M';
54 case svn_wc_status_conflicted: return 'C';
55 case svn_wc_status_obstructed: return '~';
56 case svn_wc_status_ignored: return 'I';
57 case svn_wc_status_external: return 'X';
58 case svn_wc_status_unversioned: return '?';
63 /* Return the combined STATUS as shown in 'svn status' based
64 on the node status and text status */
65 static enum svn_wc_status_kind
66 combined_status(const svn_client_status_t *status)
68 enum svn_wc_status_kind new_status = status->node_status;
70 switch (status->node_status)
72 case svn_wc_status_conflicted:
73 if (!status->versioned && status->conflicted)
75 /* Report unversioned tree conflict victims as missing: '!' */
76 new_status = svn_wc_status_missing;
80 case svn_wc_status_modified:
81 /* This value might be the property status */
82 new_status = status->text_status;
91 /* Return the combined repository STATUS as shown in 'svn status' based
92 on the repository node status and repository text status */
93 static enum svn_wc_status_kind
94 combined_repos_status(const svn_client_status_t *status)
96 if (status->repos_node_status == svn_wc_status_modified)
97 return status->repos_text_status;
99 return status->repos_node_status;
102 /* Return the single character representation of the switched column
105 generate_switch_column_code(const svn_client_status_t *status)
107 if (status->switched)
109 else if (status->file_external)
115 /* Return the detailed string representation of STATUS */
117 generate_status_desc(enum svn_wc_status_kind status)
121 case svn_wc_status_none: return "none";
122 case svn_wc_status_normal: return "normal";
123 case svn_wc_status_added: return "added";
124 case svn_wc_status_missing: return "missing";
125 case svn_wc_status_incomplete: return "incomplete";
126 case svn_wc_status_deleted: return "deleted";
127 case svn_wc_status_replaced: return "replaced";
128 case svn_wc_status_modified: return "modified";
129 case svn_wc_status_conflicted: return "conflicted";
130 case svn_wc_status_obstructed: return "obstructed";
131 case svn_wc_status_ignored: return "ignored";
132 case svn_wc_status_external: return "external";
133 case svn_wc_status_unversioned: return "unversioned";
135 SVN_ERR_MALFUNCTION_NO_RETURN();
139 /* Make a relative path containing '..' elements as needed.
140 TARGET_ABSPATH shall be the absolute version of TARGET_PATH.
141 TARGET_ABSPATH, TARGET_PATH and PATH shall be canonical.
143 If above conditions are met, a relative path that leads to PATH
144 from TARGET_PATH is returned, but there is no error checking involved.
146 The returned path is allocated from RESULT_POOL, all other
147 allocations are made in SCRATCH_POOL. */
149 make_relpath(const char *target_abspath,
150 const char *target_path,
152 apr_pool_t *result_pool,
153 apr_pool_t *scratch_pool)
156 const char *parent_dir_els = "";
157 const char *abspath, *relative;
158 svn_error_t *err = svn_dirent_get_absolute(&abspath, path, scratch_pool);
162 /* We probably got passed some invalid path. */
163 svn_error_clear(err);
164 return apr_pstrdup(result_pool, path);
167 relative = svn_dirent_skip_ancestor(target_abspath, abspath);
170 return svn_dirent_join(target_path, relative, result_pool);
174 * relative_to_path = /a/b/c
176 * result = ../../x/y/z
178 * Another example (Windows specific):
179 * relative_to_path = F:/wc
184 /* Skip the common ancestor of both paths, here '/a'. */
185 la = svn_dirent_get_longest_ancestor(target_abspath, abspath,
189 /* Nothing in common: E.g. C:/ vs F:/ on Windows */
190 return apr_pstrdup(result_pool, path);
192 relative = svn_dirent_skip_ancestor(la, target_abspath);
193 path = svn_dirent_skip_ancestor(la, path);
195 /* In above example, we'd now have:
196 * relative_to_path = b/c
199 /* Count the elements of relative_to_path and prepend as many '..' elements
203 svn_dirent_split(&relative, NULL, relative,
205 parent_dir_els = svn_dirent_join(parent_dir_els, "..", scratch_pool);
208 return svn_dirent_join(parent_dir_els, path, result_pool);
212 /* Print STATUS and PATH in a format determined by DETAILED and
213 SHOW_LAST_COMMITTED. */
215 print_status(const char *target_abspath,
216 const char *target_path,
218 svn_boolean_t detailed,
219 svn_boolean_t show_last_committed,
220 svn_boolean_t repos_locks,
221 const svn_client_status_t *status,
222 unsigned int *text_conflicts,
223 unsigned int *prop_conflicts,
224 unsigned int *tree_conflicts,
225 svn_client_ctx_t *ctx,
228 enum svn_wc_status_kind node_status = status->node_status;
229 enum svn_wc_status_kind prop_status = status->prop_status;
230 char tree_status_code = ' ';
231 const char *tree_desc_line = "";
232 const char *moved_from_line = "";
233 const char *moved_to_line = "";
235 path = make_relpath(target_abspath, target_path, path, pool, pool);
237 /* For historic reasons svn ignores the property status for added nodes, even
238 if these nodes were copied and have local property changes.
240 Note that it doesn't do this on replacements, or children of copies.
242 ### Our test suite would catch more errors if we reported property
243 changes on copies. */
244 if (node_status == svn_wc_status_added)
245 prop_status = svn_wc_status_none;
247 /* To indicate this node is the victim of a tree conflict, we show
248 'C' in the tree-conflict column, overriding any other status.
249 We also print a separate line describing the nature of the tree
251 if (status->conflicted)
254 const char *local_abspath = status->local_abspath;
255 svn_boolean_t text_conflicted;
256 svn_boolean_t prop_conflicted;
257 svn_boolean_t tree_conflicted;
259 if (status->versioned)
263 err = svn_wc_conflicted_p3(&text_conflicted,
265 &tree_conflicted, ctx->wc_ctx,
266 local_abspath, pool);
268 if (err && err->apr_err == SVN_ERR_WC_UPGRADE_REQUIRED)
270 svn_error_clear(err);
271 text_conflicted = FALSE;
272 prop_conflicted = FALSE;
273 tree_conflicted = FALSE;
280 text_conflicted = FALSE;
281 prop_conflicted = FALSE;
282 tree_conflicted = TRUE;
287 const svn_wc_conflict_description2_t *tree_conflict;
288 SVN_ERR(svn_wc__get_tree_conflict(&tree_conflict, ctx->wc_ctx,
289 local_abspath, pool, pool));
290 SVN_ERR_ASSERT(tree_conflict != NULL);
292 tree_status_code = 'C';
293 SVN_ERR(svn_cl__get_human_readable_tree_conflict_description(
294 &desc, tree_conflict, pool));
295 tree_desc_line = apr_psprintf(pool, "\n > %s", desc);
298 else if (text_conflicted)
300 else if (prop_conflicted)
304 /* Note that moved-from and moved-to information is only available in STATUS
305 * for (op-)roots of a move. Those are exactly the nodes we want to show
306 * move info for in 'svn status'. See also comments in svn_wc_status3_t. */
307 if (status->moved_from_abspath && status->moved_to_abspath &&
308 strcmp(status->moved_from_abspath, status->moved_to_abspath) == 0)
312 relpath = make_relpath(target_abspath, target_path,
313 status->moved_from_abspath,
315 relpath = svn_dirent_local_style(relpath, pool);
316 moved_from_line = apr_pstrcat(pool, "\n > ",
318 _("swapped places with %s"),
322 else if (status->moved_from_abspath || status->moved_to_abspath)
326 if (status->moved_from_abspath)
328 relpath = make_relpath(target_abspath, target_path,
329 status->moved_from_abspath,
331 relpath = svn_dirent_local_style(relpath, pool);
332 moved_from_line = apr_pstrcat(pool, "\n > ",
333 apr_psprintf(pool, _("moved from %s"),
338 if (status->moved_to_abspath)
340 relpath = make_relpath(target_abspath, target_path,
341 status->moved_to_abspath,
343 relpath = svn_dirent_local_style(relpath, pool);
344 moved_to_line = apr_pstrcat(pool, "\n > ",
345 apr_psprintf(pool, _("moved to %s"),
351 path = svn_dirent_local_style(path, pool);
355 char ood_status, lock_status;
356 const char *working_rev;
358 if (! status->versioned)
360 else if (status->copied
361 || ! SVN_IS_VALID_REVNUM(status->revision))
364 working_rev = apr_psprintf(pool, "%ld", status->revision);
366 if (status->repos_node_status != svn_wc_status_none)
373 if (status->repos_lock)
377 if (strcmp(status->repos_lock->token, status->lock->token)
386 else if (status->lock)
392 lock_status = (status->lock) ? 'K' : ' ';
394 if (show_last_committed)
396 const char *commit_rev;
397 const char *commit_author;
399 if (SVN_IS_VALID_REVNUM(status->changed_rev))
400 commit_rev = apr_psprintf(pool, "%ld", status->changed_rev);
401 else if (status->versioned)
406 if (status->changed_author)
407 commit_author = status->changed_author;
408 else if (status->versioned)
409 commit_author = " ? ";
414 (svn_cmdline_printf(pool,
415 "%c%c%c%c%c%c%c %c %8s %8s %-12s %s%s%s%s\n",
416 generate_status_code(combined_status(status)),
417 generate_status_code(prop_status),
418 status->wc_is_locked ? 'L' : ' ',
419 status->copied ? '+' : ' ',
420 generate_switch_column_code(status),
434 svn_cmdline_printf(pool, "%c%c%c%c%c%c%c %c %8s %s%s%s%s\n",
435 generate_status_code(combined_status(status)),
436 generate_status_code(prop_status),
437 status->wc_is_locked ? 'L' : ' ',
438 status->copied ? '+' : ' ',
439 generate_switch_column_code(status),
451 svn_cmdline_printf(pool, "%c%c%c%c%c%c%c %s%s%s%s\n",
452 generate_status_code(combined_status(status)),
453 generate_status_code(prop_status),
454 status->wc_is_locked ? 'L' : ' ',
455 status->copied ? '+' : ' ',
456 generate_switch_column_code(status),
465 return svn_cmdline_fflush(stdout);
470 svn_cl__print_status_xml(const char *target_abspath,
471 const char *target_path,
473 const svn_client_status_t *status,
474 svn_client_ctx_t *ctx,
477 svn_stringbuf_t *sb = svn_stringbuf_create_empty(pool);
478 apr_hash_t *att_hash;
479 const char *local_abspath = status->local_abspath;
480 svn_boolean_t tree_conflicted = FALSE;
482 if (status->node_status == svn_wc_status_none
483 && status->repos_node_status == svn_wc_status_none)
486 if (status->conflicted)
487 SVN_ERR(svn_wc_conflicted_p3(NULL, NULL, &tree_conflicted,
488 ctx->wc_ctx, local_abspath, pool));
490 path = make_relpath(target_abspath, target_path, path, pool, pool);
492 svn_xml_make_open_tag(&sb, pool, svn_xml_normal, "entry",
493 "path", svn_dirent_local_style(path, pool), NULL);
495 att_hash = apr_hash_make(pool);
496 svn_hash_sets(att_hash, "item",
497 generate_status_desc(combined_status(status)));
499 svn_hash_sets(att_hash, "props",
500 generate_status_desc(
501 (status->node_status != svn_wc_status_deleted)
502 ? status->prop_status
503 : svn_wc_status_none));
504 if (status->wc_is_locked)
505 svn_hash_sets(att_hash, "wc-locked", "true");
507 svn_hash_sets(att_hash, "copied", "true");
508 if (status->switched)
509 svn_hash_sets(att_hash, "switched", "true");
510 if (status->file_external)
511 svn_hash_sets(att_hash, "file-external", "true");
512 if (status->versioned && ! status->copied)
513 svn_hash_sets(att_hash, "revision",
514 apr_psprintf(pool, "%ld", status->revision));
516 svn_hash_sets(att_hash, "tree-conflicted", "true");
517 if (status->moved_from_abspath || status->moved_to_abspath)
521 if (status->moved_from_abspath)
523 relpath = make_relpath(target_abspath, target_path,
524 status->moved_from_abspath,
526 relpath = svn_dirent_local_style(relpath, pool);
527 svn_hash_sets(att_hash, "moved-from", relpath);
529 if (status->moved_to_abspath)
531 relpath = make_relpath(target_abspath, target_path,
532 status->moved_to_abspath,
534 relpath = svn_dirent_local_style(relpath, pool);
535 svn_hash_sets(att_hash, "moved-to", relpath);
538 svn_xml_make_open_tag_hash(&sb, pool, svn_xml_normal, "wc-status",
541 if (SVN_IS_VALID_REVNUM(status->changed_rev))
543 svn_cl__print_xml_commit(&sb, status->changed_rev,
544 status->changed_author,
545 svn_time_to_cstring(status->changed_date,
551 svn_cl__print_xml_lock(&sb, status->lock, pool);
553 svn_xml_make_close_tag(&sb, pool, "wc-status");
555 if (status->repos_node_status != svn_wc_status_none
556 || status->repos_lock)
558 svn_xml_make_open_tag(&sb, pool, svn_xml_normal, "repos-status",
560 generate_status_desc(combined_repos_status(status)),
562 generate_status_desc(status->repos_prop_status),
564 if (status->repos_lock)
565 svn_cl__print_xml_lock(&sb, status->repos_lock, pool);
567 svn_xml_make_close_tag(&sb, pool, "repos-status");
570 svn_xml_make_close_tag(&sb, pool, "entry");
572 return svn_cl__error_checked_fputs(sb->data, stdout);
575 /* Called by status-cmd.c */
577 svn_cl__print_status(const char *target_abspath,
578 const char *target_path,
580 const svn_client_status_t *status,
581 svn_boolean_t suppress_externals_placeholders,
582 svn_boolean_t detailed,
583 svn_boolean_t show_last_committed,
584 svn_boolean_t skip_unrecognized,
585 svn_boolean_t repos_locks,
586 unsigned int *text_conflicts,
587 unsigned int *prop_conflicts,
588 unsigned int *tree_conflicts,
589 svn_client_ctx_t *ctx,
593 || (skip_unrecognized
594 && !(status->versioned
595 || status->conflicted
596 || status->node_status == svn_wc_status_external))
597 || (status->node_status == svn_wc_status_none
598 && status->repos_node_status == svn_wc_status_none))
601 /* If we're trying not to print boring "X /path/to/external"
603 if (suppress_externals_placeholders)
605 /* ... skip regular externals unmodified in the repository. */
606 if ((status->node_status == svn_wc_status_external)
607 && (status->repos_node_status == svn_wc_status_none)
608 && (! status->conflicted))
611 /* ... skip file externals that aren't modified locally or
612 remotely, changelisted, or locked (in either sense of the
614 if ((status->file_external)
615 && (status->repos_node_status == svn_wc_status_none)
616 && ((status->node_status == svn_wc_status_normal)
617 || (status->node_status == svn_wc_status_none))
618 && ((status->prop_status == svn_wc_status_normal)
619 || (status->prop_status == svn_wc_status_none))
620 && (! status->changelist)
622 && (! status->wc_is_locked)
623 && (! status->conflicted))
627 return print_status(target_abspath, target_path, path,
628 detailed, show_last_committed, repos_locks, status,
629 text_conflicts, prop_conflicts, tree_conflicts,