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
;
173 /* PNAME is first argument (and PNAME_UTF8 will be a UTF-8 version
175 SVN_ERR(svn_opt_parse_num_args(&args
, os
, 1, pool
));
176 pname
= APR_ARRAY_IDX(args
, 0, const char *);
177 SVN_ERR(svn_utf_cstring_to_utf8(&pname_utf8
, pname
, pool
));
178 if (! svn_prop_name_is_valid(pname_utf8
))
179 return svn_error_createf(SVN_ERR_CLIENT_PROPERTY_NAME
, NULL
,
180 _("'%s' is not a valid Subversion property name"),
183 SVN_ERR(svn_cl__args_to_target_array_print_reserved(&targets
, os
,
187 /* Add "." if user passed 0 file arguments */
188 svn_opt_push_implicit_dot_target(targets
, pool
);
190 /* Open a stream to stdout. */
191 SVN_ERR(svn_stream_for_stdout(&out
, pool
));
193 if (opt_state
->revprop
) /* operate on a revprop */
197 svn_string_t
*propval
;
199 SVN_ERR(svn_cl__revprop_prepare(&opt_state
->start_revision
, targets
,
202 /* Let libsvn_client do the real work. */
203 SVN_ERR(svn_client_revprop_get(pname_utf8
, &propval
,
204 URL
, &(opt_state
->start_revision
),
211 svn_stringbuf_t
*sb
= NULL
;
212 char *revstr
= apr_psprintf(pool
, "%ld", rev
);
214 SVN_ERR(svn_cl__xml_print_header("properties", pool
));
216 svn_xml_make_open_tag(&sb
, pool
, svn_xml_normal
,
218 "rev", revstr
, NULL
);
220 svn_cl__print_xml_prop(&sb
, pname_utf8
, propval
, pool
);
222 svn_xml_make_close_tag(&sb
, pool
, "revprops");
224 SVN_ERR(svn_cl__error_checked_fputs(sb
->data
, stdout
));
225 SVN_ERR(svn_cl__xml_print_footer("properties", pool
));
229 svn_string_t
*printable_val
= propval
;
231 /* If this is a special Subversion property, it is stored as
232 UTF8 and LF, so convert to the native locale and eol-style. */
234 if (svn_prop_needs_translation(pname_utf8
))
235 SVN_ERR(svn_subst_detranslate_string(&printable_val
, propval
,
238 SVN_ERR(stream_write(out
, printable_val
->data
,
239 printable_val
->len
));
240 if (! opt_state
->strict
)
241 SVN_ERR(stream_write(out
, APR_EOL_STR
, strlen(APR_EOL_STR
)));
245 else /* operate on a normal, versioned property (not a revprop) */
247 apr_pool_t
*subpool
= svn_pool_create(pool
);
250 SVN_ERR(svn_cl__xml_print_header("properties", subpool
));
252 if (opt_state
->depth
== svn_depth_unknown
)
253 opt_state
->depth
= svn_depth_empty
;
255 for (i
= 0; i
< targets
->nelts
; i
++)
257 const char *target
= APR_ARRAY_IDX(targets
, i
, const char *);
259 svn_boolean_t print_filenames
= FALSE
;
260 const char *truepath
;
261 svn_opt_revision_t peg_revision
;
263 svn_pool_clear(subpool
);
264 SVN_ERR(svn_cl__check_cancel(ctx
->cancel_baton
));
266 /* Check for a peg revision. */
267 SVN_ERR(svn_opt_parse_path(&peg_revision
, &truepath
, target
,
270 SVN_ERR(svn_client_propget3(&props
, pname_utf8
, truepath
,
272 &(opt_state
->start_revision
),
273 NULL
, opt_state
->depth
,
274 opt_state
->changelists
, ctx
, subpool
));
276 /* Any time there is more than one thing to print, or where
277 the path associated with a printed thing is not obvious,
278 we'll print filenames. That is, unless we've been told
279 not to do so with the --strict option. */
280 print_filenames
= ((opt_state
->depth
> svn_depth_empty
281 || targets
->nelts
> 1
282 || apr_hash_count(props
) > 1)
283 && (! opt_state
->strict
));
286 print_properties_xml(pname_utf8
, props
, subpool
);
288 print_properties(out
, svn_path_is_url(target
), pname_utf8
, props
,
289 print_filenames
, opt_state
, subpool
);
293 SVN_ERR(svn_cl__xml_print_footer("properties", subpool
));
295 svn_pool_destroy(subpool
);