1/*
2 * compat.c :  Wrappers and callbacks for compatibility.
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_types.h"
29#include "svn_error.h"
30#include "svn_compat.h"
31#include "svn_props.h"
32
33
34/* Baton for use with svn_compat_wrap_commit_callback */
35struct commit_wrapper_baton {
36  void *baton;
37  svn_commit_callback_t callback;
38};
39
40/* This implements svn_commit_callback2_t. */
41static svn_error_t *
42commit_wrapper_callback(const svn_commit_info_t *commit_info,
43                        void *baton, apr_pool_t *pool)
44{
45  struct commit_wrapper_baton *cwb = baton;
46
47  if (cwb->callback)
48    return cwb->callback(commit_info->revision,
49                         commit_info->date,
50                         commit_info->author,
51                         cwb->baton);
52
53  return SVN_NO_ERROR;
54}
55
56void
57svn_compat_wrap_commit_callback(svn_commit_callback2_t *callback2,
58                                void **callback2_baton,
59                                svn_commit_callback_t callback,
60                                void *callback_baton,
61                                apr_pool_t *pool)
62{
63  struct commit_wrapper_baton *cwb = apr_palloc(pool, sizeof(*cwb));
64
65  /* Set the user provided old format callback in the baton */
66  cwb->baton = callback_baton;
67  cwb->callback = callback;
68
69  *callback2_baton = cwb;
70  *callback2 = commit_wrapper_callback;
71}
72
73
74void
75svn_compat_log_revprops_clear(apr_hash_t *revprops)
76{
77  if (revprops)
78    {
79      svn_hash_sets(revprops, SVN_PROP_REVISION_AUTHOR, NULL);
80      svn_hash_sets(revprops, SVN_PROP_REVISION_DATE, NULL);
81      svn_hash_sets(revprops, SVN_PROP_REVISION_LOG, NULL);
82    }
83}
84
85apr_array_header_t *
86svn_compat_log_revprops_in(apr_pool_t *pool)
87{
88  apr_array_header_t *revprops = apr_array_make(pool, 3, sizeof(char *));
89
90  APR_ARRAY_PUSH(revprops, const char *) = SVN_PROP_REVISION_AUTHOR;
91  APR_ARRAY_PUSH(revprops, const char *) = SVN_PROP_REVISION_DATE;
92  APR_ARRAY_PUSH(revprops, const char *) = SVN_PROP_REVISION_LOG;
93
94  return revprops;
95}
96
97void
98svn_compat_log_revprops_out_string(const svn_string_t **author,
99                                   const svn_string_t **date,
100                                   const svn_string_t **message,
101                                   apr_hash_t *revprops)
102{
103  *author = *date = *message = NULL;
104  if (revprops)
105    {
106      *author = svn_hash_gets(revprops, SVN_PROP_REVISION_AUTHOR);
107      *date = svn_hash_gets(revprops, SVN_PROP_REVISION_DATE);
108      *message = svn_hash_gets(revprops, SVN_PROP_REVISION_LOG);
109    }
110}
111
112void
113svn_compat_log_revprops_out(const char **author, const char **date,
114                            const char **message, apr_hash_t *revprops)
115{
116  const svn_string_t *author_s, *date_s,  *message_s;
117  svn_compat_log_revprops_out_string(&author_s, &date_s,  &message_s,
118                                     revprops);
119
120  *author = author_s ? author_s->data : NULL;
121  *date = date_s ? date_s->data : NULL;
122  *message = message_s ? message_s->data : NULL;
123}
124
125/* Baton for use with svn_compat_wrap_log_receiver */
126struct log_wrapper_baton {
127  void *baton;
128  svn_log_message_receiver_t receiver;
129};
130
131/* This implements svn_log_entry_receiver_t. */
132static svn_error_t *
133log_wrapper_callback(void *baton,
134                     svn_log_entry_t *log_entry,
135                     apr_pool_t *pool)
136{
137  struct log_wrapper_baton *lwb = baton;
138
139  if (lwb->receiver && SVN_IS_VALID_REVNUM(log_entry->revision))
140    {
141      const char *author, *date, *message;
142      svn_compat_log_revprops_out(&author, &date, &message,
143                                  log_entry->revprops);
144      return lwb->receiver(lwb->baton,
145                           log_entry->changed_paths2,
146                           log_entry->revision,
147                           author, date, message,
148                           pool);
149    }
150
151  return SVN_NO_ERROR;
152}
153
154void
155svn_compat_wrap_log_receiver(svn_log_entry_receiver_t *receiver2,
156                             void **receiver2_baton,
157                             svn_log_message_receiver_t receiver,
158                             void *receiver_baton,
159                             apr_pool_t *pool)
160{
161  struct log_wrapper_baton *lwb = apr_palloc(pool, sizeof(*lwb));
162
163  /* Set the user provided old format callback in the baton. */
164  lwb->baton = receiver_baton;
165  lwb->receiver = receiver;
166
167  *receiver2_baton = lwb;
168  *receiver2 = log_wrapper_callback;
169}
170