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_cat2(svn_stream_t *out,
180 const char *path_or_url,
181 const svn_opt_revision_t *peg_revision,
182 const svn_opt_revision_t *revision,
183 svn_client_ctx_t *ctx,
186 svn_ra_session_t *ra_session;
187 svn_client__pathrev_t *loc;
188 svn_string_t *eol_style;
189 svn_string_t *keywords;
191 const char *repos_root_url;
192 svn_stream_t *output = out;
195 /* ### Inconsistent default revision logic in this command. */
196 if (peg_revision->kind == svn_opt_revision_unspecified)
198 peg_revision = svn_cl__rev_default_to_head_or_working(peg_revision,
200 revision = svn_cl__rev_default_to_head_or_base(revision, path_or_url);
204 peg_revision = svn_cl__rev_default_to_head_or_working(peg_revision,
206 revision = svn_cl__rev_default_to_peg(revision, peg_revision);
209 if (! svn_path_is_url(path_or_url)
210 && SVN_CLIENT__REVKIND_IS_LOCAL_TO_WC(peg_revision->kind)
211 && SVN_CLIENT__REVKIND_IS_LOCAL_TO_WC(revision->kind))
213 const char *local_abspath;
214 svn_stream_t *normal_stream;
216 SVN_ERR(svn_dirent_get_absolute(&local_abspath, path_or_url, pool));
217 SVN_ERR(svn_client__get_normalized_stream(&normal_stream, ctx->wc_ctx,
218 local_abspath, revision, TRUE, FALSE,
219 ctx->cancel_func, ctx->cancel_baton,
222 /* We don't promise to close output, so disown it to ensure we don't. */
223 output = svn_stream_disown(output, pool);
225 return svn_error_trace(svn_stream_copy3(normal_stream, output,
227 ctx->cancel_baton, pool));
230 /* Get an RA plugin for this filesystem object. */
231 SVN_ERR(svn_client__ra_session_from_path2(&ra_session, &loc,
234 revision, ctx, pool));
236 /* Find the repos root URL */
237 SVN_ERR(svn_ra_get_repos_root2(ra_session, &repos_root_url, pool));
239 /* Grab some properties we need to know in order to figure out if anything
240 special needs to be done with this file. */
241 err = svn_ra_get_file(ra_session, "", loc->rev, NULL, NULL, &props, pool);
244 if (err->apr_err == SVN_ERR_FS_NOT_FILE)
246 return svn_error_createf(SVN_ERR_CLIENT_IS_DIRECTORY, err,
247 _("URL '%s' refers to a directory"),
252 return svn_error_trace(err);
256 eol_style = svn_hash_gets(props, SVN_PROP_EOL_STYLE);
257 keywords = svn_hash_gets(props, SVN_PROP_KEYWORDS);
259 if (eol_style || keywords)
261 /* It's a file with no special eol style or keywords. */
262 svn_subst_eol_style_t eol;
267 svn_subst_eol_style_from_value(&eol, &eol_str, eol_style->data);
270 eol = svn_subst_eol_style_none;
277 svn_string_t *cmt_rev, *cmt_date, *cmt_author;
280 cmt_rev = svn_hash_gets(props, SVN_PROP_ENTRY_COMMITTED_REV);
281 cmt_date = svn_hash_gets(props, SVN_PROP_ENTRY_COMMITTED_DATE);
282 cmt_author = svn_hash_gets(props, SVN_PROP_ENTRY_LAST_AUTHOR);
284 SVN_ERR(svn_time_from_cstring(&when, cmt_date->data, pool));
286 SVN_ERR(svn_subst_build_keywords3(&kw, keywords->data,
287 cmt_rev->data, loc->url,
288 repos_root_url, when,
290 cmt_author->data : NULL,
296 /* Interject a translating stream */
297 output = svn_subst_stream_translated(svn_stream_disown(out, pool),
298 eol_str, FALSE, kw, TRUE, pool);
301 SVN_ERR(svn_ra_get_file(ra_session, "", loc->rev, output, NULL, NULL, pool));
304 /* Close the interjected stream */
305 SVN_ERR(svn_stream_close(output));