In the command-line client, forbid
[svn.git] / subversion / svn / propget-cmd.c
blob416e69413481ccb4551a02d55d07fd35fe0bab58
1 /*
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 /* ==================================================================== */
23 /*** Includes. ***/
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"
31 #include "svn_utf.h"
32 #include "svn_subst.h"
33 #include "svn_path.h"
34 #include "svn_props.h"
35 #include "svn_xml.h"
36 #include "cl.h"
38 #include "svn_private_config.h"
41 /*** Code. ***/
43 static svn_error_t *
44 stream_write(svn_stream_t *out,
45 const char *data,
46 apr_size_t len)
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
52 on us. */
53 SVN_ERR(svn_stream_write(out, data, &write_len));
54 if (write_len != len)
55 return svn_error_create(SVN_ERR_STREAM_UNEXPECTED_EOF, NULL,
56 "Error writing to stream");
57 return SVN_NO_ERROR;
61 static svn_error_t *
62 print_properties_xml(const char *pname,
63 apr_hash_t *props,
64 apr_pool_t *pool)
66 apr_hash_index_t *hi;
67 apr_pool_t *iterpool = svn_pool_create(pool);
69 for (hi = apr_hash_first(pool, props); hi; hi = apr_hash_next(hi))
71 const void *key;
72 void *val;
73 const char *filename;
74 svn_string_t *propval;
75 svn_stringbuf_t *sb = NULL;
77 svn_pool_clear(iterpool);
78 apr_hash_this(hi, &key, NULL, &val);
79 filename = key;
80 propval = 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);
92 return SVN_NO_ERROR;
96 static svn_error_t *
97 print_properties(svn_stream_t *out,
98 svn_boolean_t is_url,
99 const char *pname_utf8,
100 apr_hash_t *props,
101 svn_boolean_t print_filenames,
102 svn_cl__opt_state_t *opt_state,
103 apr_pool_t *pool)
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))
110 const void *key;
111 void *val;
112 const char *filename;
113 svn_string_t *propval;
115 svn_pool_clear(iterpool);
116 apr_hash_this(hi, &key, NULL, &val);
117 filename = key;
118 propval = 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,
125 TRUE, iterpool));
128 if (print_filenames)
130 const char *filename_stdout;
132 if (! is_url)
134 SVN_ERR(svn_cmdline_path_local_style_from_utf8
135 (&filename_stdout, filename, iterpool));
137 else
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);
156 return SVN_NO_ERROR;
160 /* This implements the `svn_opt_subcommand_t' interface. */
161 svn_error_t *
162 svn_cl__propget(apr_getopt_t *os,
163 void *baton,
164 apr_pool_t *pool)
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;
171 svn_stream_t *out;
172 int i;
174 /* PNAME is first argument (and PNAME_UTF8 will be a UTF-8 version
175 thereof) */
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"),
182 pname_utf8);
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,
192 ctx,
193 pool));
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,
202 changelist_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 */
219 svn_revnum_t rev;
220 const char *URL;
221 svn_string_t *propval;
223 SVN_ERR(svn_cl__revprop_prepare(&opt_state->start_revision, targets,
224 &URL, pool));
226 /* Let libsvn_client do the real work. */
227 SVN_ERR(svn_client_revprop_get(pname_utf8, &propval,
228 URL, &(opt_state->start_revision),
229 &rev, ctx, pool));
231 if (propval != NULL)
233 if (opt_state->xml)
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,
241 "revprops",
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));
251 else
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,
260 TRUE, pool));
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);
273 if (opt_state->xml)
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 *);
282 apr_hash_t *props;
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,
292 subpool));
294 SVN_ERR(svn_client_propget4(&props, pname_utf8, truepath,
295 &peg_revision,
296 &(opt_state->start_revision),
297 NULL, opt_state->depth,
298 ctx, subpool));
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));
309 if (opt_state->xml)
310 print_properties_xml(pname_utf8, props, subpool);
311 else
312 print_properties(out, svn_path_is_url(target), pname_utf8, props,
313 print_filenames, opt_state, subpool);
316 if (opt_state->xml)
317 SVN_ERR(svn_cl__xml_print_footer("properties", subpool));
319 svn_pool_destroy(subpool);
322 return SVN_NO_ERROR;