]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/subversion/subversion/libsvn_client/util.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / subversion / subversion / libsvn_client / util.c
1 /*
2  * util.c :  utility functions for the libsvn_client library
3  *
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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
20  *    under the License.
21  * ====================================================================
22  */
23
24 #include <apr_pools.h>
25 #include <apr_strings.h>
26
27 #include "svn_hash.h"
28 #include "svn_pools.h"
29 #include "svn_error.h"
30 #include "svn_types.h"
31 #include "svn_opt.h"
32 #include "svn_props.h"
33 #include "svn_path.h"
34 #include "svn_wc.h"
35 #include "svn_client.h"
36
37 #include "private/svn_client_private.h"
38 #include "private/svn_wc_private.h"
39 #include "private/svn_fspath.h"
40
41 #include "client.h"
42
43 #include "svn_private_config.h"
44
45 svn_client__pathrev_t *
46 svn_client__pathrev_create(const char *repos_root_url,
47                            const char *repos_uuid,
48                            svn_revnum_t rev,
49                            const char *url,
50                            apr_pool_t *result_pool)
51 {
52   svn_client__pathrev_t *loc = apr_palloc(result_pool, sizeof(*loc));
53
54   SVN_ERR_ASSERT_NO_RETURN(svn_path_is_url(repos_root_url));
55   SVN_ERR_ASSERT_NO_RETURN(svn_path_is_url(url));
56
57   loc->repos_root_url = apr_pstrdup(result_pool, repos_root_url);
58   loc->repos_uuid = apr_pstrdup(result_pool, repos_uuid);
59   loc->rev = rev;
60   loc->url = apr_pstrdup(result_pool, url);
61   return loc;
62 }
63
64 svn_client__pathrev_t *
65 svn_client__pathrev_create_with_relpath(const char *repos_root_url,
66                                         const char *repos_uuid,
67                                         svn_revnum_t rev,
68                                         const char *relpath,
69                                         apr_pool_t *result_pool)
70 {
71   SVN_ERR_ASSERT_NO_RETURN(svn_relpath_is_canonical(relpath));
72
73   return svn_client__pathrev_create(
74            repos_root_url, repos_uuid, rev,
75            svn_path_url_add_component2(repos_root_url, relpath, result_pool),
76            result_pool);
77 }
78
79 svn_error_t *
80 svn_client__pathrev_create_with_session(svn_client__pathrev_t **pathrev_p,
81                                         svn_ra_session_t *ra_session,
82                                         svn_revnum_t rev,
83                                         const char *url,
84                                         apr_pool_t *result_pool)
85 {
86   svn_client__pathrev_t *pathrev = apr_palloc(result_pool, sizeof(*pathrev));
87
88   SVN_ERR_ASSERT(svn_path_is_url(url));
89
90   SVN_ERR(svn_ra_get_repos_root2(ra_session, &pathrev->repos_root_url,
91                                  result_pool));
92   SVN_ERR(svn_ra_get_uuid2(ra_session, &pathrev->repos_uuid, result_pool));
93   pathrev->rev = rev;
94   pathrev->url = apr_pstrdup(result_pool, url);
95   *pathrev_p = pathrev;
96   return SVN_NO_ERROR;
97 }
98
99 svn_client__pathrev_t *
100 svn_client__pathrev_dup(const svn_client__pathrev_t *pathrev,
101                         apr_pool_t *result_pool)
102 {
103   return svn_client__pathrev_create(
104            pathrev->repos_root_url, pathrev->repos_uuid,
105            pathrev->rev, pathrev->url, result_pool);
106 }
107
108 svn_client__pathrev_t *
109 svn_client__pathrev_join_relpath(const svn_client__pathrev_t *pathrev,
110                                  const char *relpath,
111                                  apr_pool_t *result_pool)
112 {
113   return svn_client__pathrev_create(
114            pathrev->repos_root_url, pathrev->repos_uuid, pathrev->rev,
115            svn_path_url_add_component2(pathrev->url, relpath, result_pool),
116            result_pool);
117 }
118
119 const char *
120 svn_client__pathrev_relpath(const svn_client__pathrev_t *pathrev,
121                             apr_pool_t *result_pool)
122 {
123   return svn_uri_skip_ancestor(pathrev->repos_root_url, pathrev->url,
124                                result_pool);
125 }
126
127 const char *
128 svn_client__pathrev_fspath(const svn_client__pathrev_t *pathrev,
129                            apr_pool_t *result_pool)
130 {
131   return svn_fspath__canonicalize(svn_uri_skip_ancestor(
132                                     pathrev->repos_root_url, pathrev->url,
133                                     result_pool),
134                                   result_pool);
135 }
136
137
138 svn_client_commit_item3_t *
139 svn_client_commit_item3_create(apr_pool_t *pool)
140 {
141   return apr_pcalloc(pool, sizeof(svn_client_commit_item3_t));
142 }
143
144 svn_client_commit_item3_t *
145 svn_client_commit_item3_dup(const svn_client_commit_item3_t *item,
146                             apr_pool_t *pool)
147 {
148   svn_client_commit_item3_t *new_item = apr_palloc(pool, sizeof(*new_item));
149
150   *new_item = *item;
151
152   if (new_item->path)
153     new_item->path = apr_pstrdup(pool, new_item->path);
154
155   if (new_item->url)
156     new_item->url = apr_pstrdup(pool, new_item->url);
157
158   if (new_item->copyfrom_url)
159     new_item->copyfrom_url = apr_pstrdup(pool, new_item->copyfrom_url);
160
161   if (new_item->incoming_prop_changes)
162     new_item->incoming_prop_changes =
163       svn_prop_array_dup(new_item->incoming_prop_changes, pool);
164
165   if (new_item->outgoing_prop_changes)
166     new_item->outgoing_prop_changes =
167       svn_prop_array_dup(new_item->outgoing_prop_changes, pool);
168
169   if (new_item->session_relpath)
170     new_item->session_relpath = apr_pstrdup(pool, new_item->session_relpath);
171
172   if (new_item->moved_from_abspath)
173     new_item->moved_from_abspath = apr_pstrdup(pool,
174                                                new_item->moved_from_abspath);
175
176   return new_item;
177 }
178
179 svn_error_t *
180 svn_client__wc_node_get_base(svn_client__pathrev_t **base_p,
181                              const char *wc_abspath,
182                              svn_wc_context_t *wc_ctx,
183                              apr_pool_t *result_pool,
184                              apr_pool_t *scratch_pool)
185 {
186   const char *relpath;
187
188   *base_p = apr_palloc(result_pool, sizeof(**base_p));
189
190   SVN_ERR(svn_wc__node_get_base(NULL,
191                                 &(*base_p)->rev,
192                                 &relpath,
193                                 &(*base_p)->repos_root_url,
194                                 &(*base_p)->repos_uuid,
195                                 NULL,
196                                 wc_ctx, wc_abspath,
197                                 TRUE /* ignore_enoent */,
198                                 TRUE /* show_hidden */,
199                                 result_pool, scratch_pool));
200   if ((*base_p)->repos_root_url && relpath)
201     {
202       (*base_p)->url = svn_path_url_add_component2(
203                            (*base_p)->repos_root_url, relpath, result_pool);
204     }
205   else
206     {
207       *base_p = NULL;
208     }
209   return SVN_NO_ERROR;
210 }
211
212 svn_error_t *
213 svn_client__wc_node_get_origin(svn_client__pathrev_t **origin_p,
214                                const char *wc_abspath,
215                                svn_client_ctx_t *ctx,
216                                apr_pool_t *result_pool,
217                                apr_pool_t *scratch_pool)
218 {
219   const char *relpath;
220
221   *origin_p = apr_palloc(result_pool, sizeof(**origin_p));
222
223   SVN_ERR(svn_wc__node_get_origin(NULL /* is_copy */,
224                                   &(*origin_p)->rev,
225                                   &relpath,
226                                   &(*origin_p)->repos_root_url,
227                                   &(*origin_p)->repos_uuid,
228                                   NULL, ctx->wc_ctx, wc_abspath,
229                                   FALSE /* scan_deleted */,
230                                   result_pool, scratch_pool));
231   if ((*origin_p)->repos_root_url && relpath)
232     {
233       (*origin_p)->url = svn_path_url_add_component2(
234                            (*origin_p)->repos_root_url, relpath, result_pool);
235     }
236   else
237     {
238       *origin_p = NULL;
239     }
240   return SVN_NO_ERROR;
241 }
242
243 svn_error_t *
244 svn_client_get_repos_root(const char **repos_root,
245                           const char **repos_uuid,
246                           const char *abspath_or_url,
247                           svn_client_ctx_t *ctx,
248                           apr_pool_t *result_pool,
249                           apr_pool_t *scratch_pool)
250 {
251   svn_ra_session_t *ra_session;
252
253   /* If PATH_OR_URL is a local path we can fetch the repos root locally. */
254   if (!svn_path_is_url(abspath_or_url))
255     {
256       svn_error_t *err;
257       err = svn_wc__node_get_repos_info(NULL, NULL, repos_root, repos_uuid,
258                                         ctx->wc_ctx, abspath_or_url,
259                                         result_pool, scratch_pool);
260
261       if (err)
262         {
263           if (err->apr_err != SVN_ERR_WC_PATH_NOT_FOUND
264               && err->apr_err != SVN_ERR_WC_NOT_WORKING_COPY)
265             return svn_error_trace(err);
266
267           svn_error_clear(err);
268           if (repos_root)
269             *repos_root = NULL;
270           if (repos_uuid)
271             *repos_uuid = NULL;
272         }
273       return SVN_NO_ERROR;
274     }
275
276   /* If PATH_OR_URL was a URL, we use the RA layer to look it up. */
277   SVN_ERR(svn_client_open_ra_session2(&ra_session,  abspath_or_url, NULL,
278                                       ctx, scratch_pool, scratch_pool));
279
280   if (repos_root)
281     SVN_ERR(svn_ra_get_repos_root2(ra_session, repos_root, result_pool));
282   if (repos_uuid)
283     SVN_ERR(svn_ra_get_uuid2(ra_session, repos_uuid, result_pool));
284
285   return SVN_NO_ERROR;
286 }
287
288 const svn_opt_revision_t *
289 svn_cl__rev_default_to_head_or_base(const svn_opt_revision_t *revision,
290                                     const char *path_or_url)
291 {
292   static svn_opt_revision_t head_rev = { svn_opt_revision_head, { 0 } };
293   static svn_opt_revision_t base_rev = { svn_opt_revision_base, { 0 } };
294
295   if (revision->kind == svn_opt_revision_unspecified)
296     return svn_path_is_url(path_or_url) ? &head_rev : &base_rev;
297   return revision;
298 }
299
300 const svn_opt_revision_t *
301 svn_cl__rev_default_to_head_or_working(const svn_opt_revision_t *revision,
302                                        const char *path_or_url)
303 {
304   static svn_opt_revision_t head_rev = { svn_opt_revision_head, { 0 } };
305   static svn_opt_revision_t work_rev = { svn_opt_revision_working, { 0 } };
306
307   if (revision->kind == svn_opt_revision_unspecified)
308     return svn_path_is_url(path_or_url) ? &head_rev : &work_rev;
309   return revision;
310 }
311
312 const svn_opt_revision_t *
313 svn_cl__rev_default_to_peg(const svn_opt_revision_t *revision,
314                            const svn_opt_revision_t *peg_revision)
315 {
316   if (revision->kind == svn_opt_revision_unspecified)
317     return peg_revision;
318   return revision;
319 }
320
321 svn_error_t *
322 svn_client__assert_homogeneous_target_type(const apr_array_header_t *targets)
323 {
324   svn_boolean_t wc_present = FALSE, url_present = FALSE;
325   int i;
326
327   for (i = 0; i < targets->nelts; ++i)
328     {
329       const char *target = APR_ARRAY_IDX(targets, i, const char *);
330       if (! svn_path_is_url(target))
331         wc_present = TRUE;
332       else
333         url_present = TRUE;
334       if (url_present && wc_present)
335         return svn_error_createf(SVN_ERR_ILLEGAL_TARGET, NULL,
336                                  _("Cannot mix repository and working copy "
337                                    "targets"));
338     }
339
340   return SVN_NO_ERROR;
341 }
342
343 struct shim_callbacks_baton
344 {
345   svn_wc_context_t *wc_ctx;
346   apr_hash_t *relpath_map;
347 };
348
349 static svn_error_t *
350 fetch_props_func(apr_hash_t **props,
351                  void *baton,
352                  const char *path,
353                  svn_revnum_t base_revision,
354                  apr_pool_t *result_pool,
355                  apr_pool_t *scratch_pool)
356 {
357   struct shim_callbacks_baton *scb = baton;
358   const char *local_abspath;
359
360   local_abspath = svn_hash_gets(scb->relpath_map, path);
361   if (!local_abspath)
362     {
363       *props = apr_hash_make(result_pool);
364       return SVN_NO_ERROR;
365     }
366
367   /* Reads the pristine properties of WORKING, not those of BASE */
368   SVN_ERR(svn_wc_get_pristine_props(props, scb->wc_ctx, local_abspath,
369                                     result_pool, scratch_pool));
370
371   if (!*props)
372     *props = apr_hash_make(result_pool);
373
374   return SVN_NO_ERROR;
375 }
376
377 static svn_error_t *
378 fetch_kind_func(svn_node_kind_t *kind,
379                 void *baton,
380                 const char *path,
381                 svn_revnum_t base_revision,
382                 apr_pool_t *scratch_pool)
383 {
384   struct shim_callbacks_baton *scb = baton;
385   const char *local_abspath;
386
387   local_abspath = svn_hash_gets(scb->relpath_map, path);
388   if (!local_abspath)
389     {
390       *kind = svn_node_unknown;
391       return SVN_NO_ERROR;
392     }
393   /* Reads the WORKING kind. Not the BASE kind */
394   SVN_ERR(svn_wc_read_kind2(kind, scb->wc_ctx, local_abspath,
395                             TRUE, FALSE, scratch_pool));
396
397   return SVN_NO_ERROR;
398 }
399
400 static svn_error_t *
401 fetch_base_func(const char **filename,
402                 void *baton,
403                 const char *path,
404                 svn_revnum_t base_revision,
405                 apr_pool_t *result_pool,
406                 apr_pool_t *scratch_pool)
407 {
408   struct shim_callbacks_baton *scb = baton;
409   const char *local_abspath;
410   svn_stream_t *pristine_stream;
411   svn_stream_t *temp_stream;
412   svn_error_t *err;
413
414   local_abspath = svn_hash_gets(scb->relpath_map, path);
415   if (!local_abspath)
416     {
417       *filename = NULL;
418       return SVN_NO_ERROR;
419     }
420
421   /* Reads the pristine of WORKING, not of BASE */
422   err = svn_wc_get_pristine_contents2(&pristine_stream, scb->wc_ctx,
423                                       local_abspath, scratch_pool,
424                                       scratch_pool);
425   if (err && err->apr_err == SVN_ERR_WC_PATH_NOT_FOUND)
426     {
427       svn_error_clear(err);
428       *filename = NULL;
429       return SVN_NO_ERROR;
430     }
431   else if (err)
432     return svn_error_trace(err);
433
434   SVN_ERR(svn_stream_open_unique(&temp_stream, filename, NULL,
435                                  svn_io_file_del_on_pool_cleanup,
436                                  result_pool, scratch_pool));
437   SVN_ERR(svn_stream_copy3(pristine_stream, temp_stream, NULL, NULL,
438                            scratch_pool));
439
440   return SVN_NO_ERROR;
441 }
442
443 svn_delta_shim_callbacks_t *
444 svn_client__get_shim_callbacks(svn_wc_context_t *wc_ctx,
445                                apr_hash_t *relpath_map,
446                                apr_pool_t *result_pool)
447 {
448   svn_delta_shim_callbacks_t *callbacks =
449                             svn_delta_shim_callbacks_default(result_pool);
450   struct shim_callbacks_baton *scb = apr_pcalloc(result_pool, sizeof(*scb));
451
452   scb->wc_ctx = wc_ctx;
453   if (relpath_map)
454     scb->relpath_map = relpath_map;
455   else
456     scb->relpath_map = apr_hash_make(result_pool);
457
458   callbacks->fetch_props_func = fetch_props_func;
459   callbacks->fetch_kind_func = fetch_kind_func;
460   callbacks->fetch_base_func = fetch_base_func;
461   callbacks->fetch_baton = scb;
462
463   return callbacks;
464 }