Skip a test when run against old servers.
[svn.git] / subversion / svn / propget-cmd.c
blob4467a148cf1386dd6efddb61c3d4811fb3eba059
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 svn_stream_t *out;
171 int i;
173 /* PNAME is first argument (and PNAME_UTF8 will be a UTF-8 version
174 thereof) */
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"),
181 pname_utf8);
183 SVN_ERR(svn_cl__args_to_target_array_print_reserved(&targets, os,
184 opt_state->targets,
185 pool));
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 */
195 svn_revnum_t rev;
196 const char *URL;
197 svn_string_t *propval;
199 SVN_ERR(svn_cl__revprop_prepare(&opt_state->start_revision, targets,
200 &URL, pool));
202 /* Let libsvn_client do the real work. */
203 SVN_ERR(svn_client_revprop_get(pname_utf8, &propval,
204 URL, &(opt_state->start_revision),
205 &rev, ctx, pool));
207 if (propval != NULL)
209 if (opt_state->xml)
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,
217 "revprops",
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));
227 else
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,
236 TRUE, pool));
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);
249 if (opt_state->xml)
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 *);
258 apr_hash_t *props;
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,
268 subpool));
270 SVN_ERR(svn_client_propget3(&props, pname_utf8, truepath,
271 &peg_revision,
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));
285 if (opt_state->xml)
286 print_properties_xml(pname_utf8, props, subpool);
287 else
288 print_properties(out, svn_path_is_url(target), pname_utf8, props,
289 print_filenames, opt_state, subpool);
292 if (opt_state->xml)
293 SVN_ERR(svn_cl__xml_print_footer("properties", subpool));
295 svn_pool_destroy(subpool);
298 return SVN_NO_ERROR;