2 * propget-cmd.c -- Print properties and values of files/dirs
4 * ====================================================================
5 * Copyright (c) 2000-2007 CollabNet. All rights reserved.
7 * This software is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at http://subversion.tigris.org/license-1.html.
10 * If newer versions of this license are posted there, you may use a
11 * newer version instead, at your option.
13 * This software consists of voluntary contributions made by many
14 * individuals. For exact contribution history, see the revision
15 * history and logs, available at http://subversion.tigris.org/.
16 * ====================================================================
19 /* ==================================================================== */
25 #include "svn_cmdline.h"
26 #include "svn_pools.h"
27 #include "svn_client.h"
28 #include "svn_string.h"
29 #include "svn_error_codes.h"
30 #include "svn_error.h"
32 #include "svn_subst.h"
34 #include "svn_props.h"
38 #include "svn_private_config.h"
44 stream_write(svn_stream_t
*out
,
48 apr_size_t write_len
= len
;
50 /* We're gonna bail on an incomplete write here only because we know
51 that this stream is really stdout, which should never be blocking
53 SVN_ERR(svn_stream_write(out
, data
, &write_len
));
55 return svn_error_create(SVN_ERR_STREAM_UNEXPECTED_EOF
, NULL
,
56 "Error writing to stream");
62 print_properties_xml(const char *pname
,
67 apr_pool_t
*iterpool
= svn_pool_create(pool
);
69 for (hi
= apr_hash_first(pool
, props
); hi
; hi
= apr_hash_next(hi
))
74 svn_string_t
*propval
;
75 svn_stringbuf_t
*sb
= NULL
;
77 svn_pool_clear(iterpool
);
78 apr_hash_this(hi
, &key
, NULL
, &val
);
82 svn_xml_make_open_tag(&sb
, iterpool
, svn_xml_normal
, "target",
83 "path", filename
, NULL
);
84 svn_cl__print_xml_prop(&sb
, pname
, propval
, iterpool
);
85 svn_xml_make_close_tag(&sb
, iterpool
, "target");
87 SVN_ERR(svn_cl__error_checked_fputs(sb
->data
, stdout
));
90 svn_pool_destroy(iterpool
);
97 print_properties(svn_stream_t
*out
,
99 const char *pname_utf8
,
101 svn_boolean_t print_filenames
,
102 svn_cl__opt_state_t
*opt_state
,
105 apr_hash_index_t
*hi
;
106 apr_pool_t
*iterpool
= svn_pool_create(pool
);
108 for (hi
= apr_hash_first(pool
, props
); hi
; hi
= apr_hash_next(hi
))
112 const char *filename
;
113 svn_string_t
*propval
;
115 svn_pool_clear(iterpool
);
116 apr_hash_this(hi
, &key
, NULL
, &val
);
120 /* If this is a special Subversion property, it is stored as
121 UTF8, so convert to the native format. */
122 if (svn_prop_needs_translation(pname_utf8
))
124 SVN_ERR(svn_subst_detranslate_string(&propval
, propval
,
130 const char *filename_stdout
;
134 SVN_ERR(svn_cmdline_path_local_style_from_utf8
135 (&filename_stdout
, filename
, iterpool
));
139 SVN_ERR(svn_cmdline_cstring_from_utf8
140 (&filename_stdout
, filename
, iterpool
));
143 SVN_ERR(stream_write(out
, filename_stdout
,
144 strlen(filename_stdout
)));
145 SVN_ERR(stream_write(out
, " - ", 3));
148 SVN_ERR(stream_write(out
, propval
->data
, propval
->len
));
149 if (! opt_state
->strict
)
150 SVN_ERR(stream_write(out
, APR_EOL_STR
,
151 strlen(APR_EOL_STR
)));
154 svn_pool_destroy(iterpool
);
160 /* This implements the `svn_opt_subcommand_t' interface. */
162 svn_cl__propget(apr_getopt_t
*os
,
166 svn_cl__opt_state_t
*opt_state
= ((svn_cl__cmd_baton_t
*) baton
)->opt_state
;
167 svn_client_ctx_t
*ctx
= ((svn_cl__cmd_baton_t
*) baton
)->ctx
;
168 const char *pname
, *pname_utf8
;
169 apr_array_header_t
*args
, *targets
;
170 apr_array_header_t
*changelist_targets
= NULL
, *combined_targets
= NULL
;
174 /* PNAME is first argument (and PNAME_UTF8 will be a UTF-8 version
176 SVN_ERR(svn_opt_parse_num_args(&args
, os
, 1, pool
));
177 pname
= APR_ARRAY_IDX(args
, 0, const char *);
178 SVN_ERR(svn_utf_cstring_to_utf8(&pname_utf8
, pname
, pool
));
179 if (! svn_prop_name_is_valid(pname_utf8
))
180 return svn_error_createf(SVN_ERR_CLIENT_PROPERTY_NAME
, NULL
,
181 _("'%s' is not a valid Subversion property name"),
184 /* Before allowing svn_opt_args_to_target_array2() to canonicalize
185 all the remaining targets, we need to build a list of targets made of both
186 ones the user typed, as well as any specified by --changelist. */
187 if (opt_state
->changelist
)
189 SVN_ERR(svn_client_get_changelist(&changelist_targets
,
190 opt_state
->changelist
,
194 if (apr_is_empty_array(changelist_targets
))
195 return svn_error_createf(SVN_ERR_UNKNOWN_CHANGELIST
, NULL
,
196 _("Unknown changelist '%s'"),
197 opt_state
->changelist
);
200 if (opt_state
->targets
&& changelist_targets
)
201 combined_targets
= apr_array_append(pool
, opt_state
->targets
,
203 else if (opt_state
->targets
)
204 combined_targets
= opt_state
->targets
;
205 else if (changelist_targets
)
206 combined_targets
= changelist_targets
;
208 SVN_ERR(svn_opt_args_to_target_array2(&targets
, os
,
209 combined_targets
, pool
));
211 /* Add "." if user passed 0 file arguments */
212 svn_opt_push_implicit_dot_target(targets
, pool
);
214 /* Open a stream to stdout. */
215 SVN_ERR(svn_stream_for_stdout(&out
, pool
));
217 if (opt_state
->revprop
) /* operate on a revprop */
221 svn_string_t
*propval
;
223 SVN_ERR(svn_cl__revprop_prepare(&opt_state
->start_revision
, targets
,
226 /* Let libsvn_client do the real work. */
227 SVN_ERR(svn_client_revprop_get(pname_utf8
, &propval
,
228 URL
, &(opt_state
->start_revision
),
235 svn_stringbuf_t
*sb
= NULL
;
236 char *revstr
= apr_psprintf(pool
, "%ld", rev
);
238 SVN_ERR(svn_cl__xml_print_header("properties", pool
));
240 svn_xml_make_open_tag(&sb
, pool
, svn_xml_normal
,
242 "rev", revstr
, NULL
);
244 svn_cl__print_xml_prop(&sb
, pname_utf8
, propval
, pool
);
246 svn_xml_make_close_tag(&sb
, pool
, "revprops");
248 SVN_ERR(svn_cl__error_checked_fputs(sb
->data
, stdout
));
249 SVN_ERR(svn_cl__xml_print_footer("properties", pool
));
253 svn_string_t
*printable_val
= propval
;
255 /* If this is a special Subversion property, it is stored as
256 UTF8 and LF, so convert to the native locale and eol-style. */
258 if (svn_prop_needs_translation(pname_utf8
))
259 SVN_ERR(svn_subst_detranslate_string(&printable_val
, propval
,
262 SVN_ERR(stream_write(out
, printable_val
->data
,
263 printable_val
->len
));
264 if (! opt_state
->strict
)
265 SVN_ERR(stream_write(out
, APR_EOL_STR
, strlen(APR_EOL_STR
)));
269 else /* operate on a normal, versioned property (not a revprop) */
271 apr_pool_t
*subpool
= svn_pool_create(pool
);
274 SVN_ERR(svn_cl__xml_print_header("properties", subpool
));
276 if (opt_state
->depth
== svn_depth_unknown
)
277 opt_state
->depth
= svn_depth_empty
;
279 for (i
= 0; i
< targets
->nelts
; i
++)
281 const char *target
= APR_ARRAY_IDX(targets
, i
, const char *);
283 svn_boolean_t print_filenames
= FALSE
;
284 const char *truepath
;
285 svn_opt_revision_t peg_revision
;
287 svn_pool_clear(subpool
);
288 SVN_ERR(svn_cl__check_cancel(ctx
->cancel_baton
));
290 /* Check for a peg revision. */
291 SVN_ERR(svn_opt_parse_path(&peg_revision
, &truepath
, target
,
294 SVN_ERR(svn_client_propget4(&props
, pname_utf8
, truepath
,
296 &(opt_state
->start_revision
),
297 NULL
, opt_state
->depth
,
300 /* Any time there is more than one thing to print, or where
301 the path associated with a printed thing is not obvious,
302 we'll print filenames. That is, unless we've been told
303 not to do so with the --strict option. */
304 print_filenames
= ((SVN_DEPTH_IS_RECURSIVE(opt_state
->depth
)
305 || targets
->nelts
> 1
306 || apr_hash_count(props
) > 1)
307 && (! opt_state
->strict
));
310 print_properties_xml(pname_utf8
, props
, subpool
);
312 print_properties(out
, svn_path_is_url(target
), pname_utf8
, props
,
313 print_filenames
, opt_state
, subpool
);
317 SVN_ERR(svn_cl__xml_print_footer("properties", subpool
));
319 svn_pool_destroy(subpool
);