2 * cat.c: implementation of the 'cat' 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 /* ==================================================================== */
31 #include "svn_client.h"
32 #include "svn_string.h"
33 #include "svn_error.h"
34 #include "svn_subst.h"
37 #include "svn_dirent_uri.h"
39 #include "svn_props.h"
42 #include "svn_private_config.h"
43 #include "private/svn_wc_private.h"
49 svn_client__get_normalized_stream(svn_stream_t **normal_stream,
50 svn_wc_context_t *wc_ctx,
51 const char *local_abspath,
52 const svn_opt_revision_t *revision,
53 svn_boolean_t expand_keywords,
54 svn_boolean_t normalize_eols,
55 svn_cancel_func_t cancel_func,
57 apr_pool_t *result_pool,
58 apr_pool_t *scratch_pool)
60 apr_hash_t *kw = NULL;
61 svn_subst_eol_style_t style;
63 svn_string_t *eol_style, *keywords, *special;
64 const char *eol = NULL;
65 svn_boolean_t local_mod = FALSE;
69 SVN_ERR_ASSERT(SVN_CLIENT__REVKIND_IS_LOCAL_TO_WC(revision->kind));
71 SVN_ERR(svn_wc_read_kind2(&kind, wc_ctx, local_abspath,
72 (revision->kind != svn_opt_revision_working),
73 FALSE, scratch_pool));
75 if (kind == svn_node_unknown || kind == svn_node_none)
76 return svn_error_createf(SVN_ERR_UNVERSIONED_RESOURCE, NULL,
77 _("'%s' is not under version control"),
78 svn_dirent_local_style(local_abspath,
80 if (kind != svn_node_file)
81 return svn_error_createf(SVN_ERR_CLIENT_IS_DIRECTORY, NULL,
82 _("'%s' refers to a directory"),
83 svn_dirent_local_style(local_abspath,
86 if (revision->kind != svn_opt_revision_working)
88 SVN_ERR(svn_wc_get_pristine_contents2(&input, wc_ctx, local_abspath,
89 result_pool, scratch_pool));
91 return svn_error_createf(SVN_ERR_ILLEGAL_TARGET, NULL,
92 _("'%s' has no pristine version until it is committed"),
93 svn_dirent_local_style(local_abspath, scratch_pool));
95 SVN_ERR(svn_wc_get_pristine_props(&props, wc_ctx, local_abspath,
96 scratch_pool, scratch_pool));
100 svn_wc_status3_t *status;
102 SVN_ERR(svn_stream_open_readonly(&input, local_abspath, scratch_pool,
105 SVN_ERR(svn_wc_prop_list2(&props, wc_ctx, local_abspath, scratch_pool,
107 SVN_ERR(svn_wc_status3(&status, wc_ctx, local_abspath, scratch_pool,
109 if (status->node_status != svn_wc_status_normal)
113 eol_style = svn_hash_gets(props, SVN_PROP_EOL_STYLE);
114 keywords = svn_hash_gets(props, SVN_PROP_KEYWORDS);
115 special = svn_hash_gets(props, SVN_PROP_SPECIAL);
118 svn_subst_eol_style_from_value(&style, &eol, eol_style->data);
122 svn_revnum_t changed_rev;
127 const char *repos_root_url;
128 const char *repos_relpath;
130 SVN_ERR(svn_wc__node_get_changed_info(&changed_rev, &tm, &author, wc_ctx,
131 local_abspath, scratch_pool,
133 SVN_ERR(svn_wc__node_get_repos_info(NULL, &repos_relpath, &repos_root_url,
135 wc_ctx, local_abspath, scratch_pool,
137 url = svn_path_url_add_component2(repos_root_url, repos_relpath,
142 /* For locally modified files, we'll append an 'M'
143 to the revision number, and set the author to
144 "(local)" since we can't always determine the
145 current user's username */
146 rev_str = apr_psprintf(scratch_pool, "%ldM", changed_rev);
147 author = _("(local)");
151 /* Use the modified time from the working copy for files */
152 SVN_ERR(svn_io_file_affected_time(&tm, local_abspath,
158 rev_str = apr_psprintf(scratch_pool, "%ld", changed_rev);
161 SVN_ERR(svn_subst_build_keywords3(&kw, keywords->data, rev_str, url,
162 repos_root_url, tm, author,
166 /* Wrap the output stream if translation is needed. */
167 if (eol != NULL || kw != NULL)
168 input = svn_subst_stream_translated(
170 (eol_style && normalize_eols) ? SVN_SUBST_NATIVE_EOL_STR : eol,
171 FALSE, kw, expand_keywords, result_pool);
173 *normal_stream = input;
179 svn_client_cat3(apr_hash_t **returned_props,
181 const char *path_or_url,
182 const svn_opt_revision_t *peg_revision,
183 const svn_opt_revision_t *revision,
184 svn_boolean_t expand_keywords,
185 svn_client_ctx_t *ctx,
186 apr_pool_t *result_pool,
187 apr_pool_t *scratch_pool)
189 svn_ra_session_t *ra_session;
190 svn_client__pathrev_t *loc;
191 svn_string_t *eol_style;
192 svn_string_t *keywords;
193 apr_hash_t *props = NULL;
194 const char *repos_root_url;
195 svn_stream_t *output = out;
198 /* ### Inconsistent default revision logic in this command. */
199 if (peg_revision->kind == svn_opt_revision_unspecified)
201 peg_revision = svn_cl__rev_default_to_head_or_working(peg_revision,
203 revision = svn_cl__rev_default_to_head_or_base(revision, path_or_url);
207 revision = svn_cl__rev_default_to_peg(revision, peg_revision);
210 if (! svn_path_is_url(path_or_url)
211 && SVN_CLIENT__REVKIND_IS_LOCAL_TO_WC(peg_revision->kind)
212 && SVN_CLIENT__REVKIND_IS_LOCAL_TO_WC(revision->kind))
214 const char *local_abspath;
215 svn_stream_t *normal_stream;
217 SVN_ERR(svn_dirent_get_absolute(&local_abspath, path_or_url,
219 SVN_ERR(svn_client__get_normalized_stream(&normal_stream, ctx->wc_ctx,
220 local_abspath, revision,
221 expand_keywords, FALSE,
222 ctx->cancel_func, ctx->cancel_baton,
223 scratch_pool, scratch_pool));
225 /* We don't promise to close output, so disown it to ensure we don't. */
226 output = svn_stream_disown(output, scratch_pool);
229 SVN_ERR(svn_wc_prop_list2(returned_props, ctx->wc_ctx, local_abspath,
230 result_pool, scratch_pool));
232 return svn_error_trace(svn_stream_copy3(normal_stream, output,
234 ctx->cancel_baton, scratch_pool));
237 /* Get an RA plugin for this filesystem object. */
238 SVN_ERR(svn_client__ra_session_from_path2(&ra_session, &loc,
241 revision, ctx, scratch_pool));
243 /* Find the repos root URL */
244 SVN_ERR(svn_ra_get_repos_root2(ra_session, &repos_root_url, scratch_pool));
246 /* Grab some properties we need to know in order to figure out if anything
247 special needs to be done with this file. */
248 err = svn_ra_get_file(ra_session, "", loc->rev, NULL, NULL, &props,
252 if (err->apr_err == SVN_ERR_FS_NOT_FILE)
254 return svn_error_createf(SVN_ERR_CLIENT_IS_DIRECTORY, err,
255 _("URL '%s' refers to a directory"),
260 return svn_error_trace(err);
264 eol_style = svn_hash_gets(props, SVN_PROP_EOL_STYLE);
265 keywords = svn_hash_gets(props, SVN_PROP_KEYWORDS);
267 if (eol_style || keywords)
269 /* It's a file with no special eol style or keywords. */
270 svn_subst_eol_style_t eol;
275 svn_subst_eol_style_from_value(&eol, &eol_str, eol_style->data);
278 eol = svn_subst_eol_style_none;
283 if (keywords && expand_keywords)
285 svn_string_t *cmt_rev, *cmt_date, *cmt_author;
288 cmt_rev = svn_hash_gets(props, SVN_PROP_ENTRY_COMMITTED_REV);
289 cmt_date = svn_hash_gets(props, SVN_PROP_ENTRY_COMMITTED_DATE);
290 cmt_author = svn_hash_gets(props, SVN_PROP_ENTRY_LAST_AUTHOR);
292 SVN_ERR(svn_time_from_cstring(&when, cmt_date->data, scratch_pool));
294 SVN_ERR(svn_subst_build_keywords3(&kw, keywords->data,
295 cmt_rev->data, loc->url,
296 repos_root_url, when,
298 cmt_author->data : NULL,
304 /* Interject a translating stream */
305 output = svn_subst_stream_translated(svn_stream_disown(out,
307 eol_str, FALSE, kw, TRUE,
313 /* filter entry and WC props */
314 apr_hash_index_t *hi;
318 for (hi = apr_hash_first(scratch_pool, props);
319 hi; hi = apr_hash_next(hi))
321 apr_hash_this(hi, &key, &klen, NULL);
322 if (!svn_wc_is_normal_prop(key))
323 apr_hash_set(props, key, klen, NULL);
326 *returned_props = props;
329 SVN_ERR(svn_ra_get_file(ra_session, "", loc->rev, output, NULL, NULL,
333 /* Close the interjected stream */
334 SVN_ERR(svn_stream_close(output));