summaryrefslogtreecommitdiff
path: root/subversion/libsvn_client/repos_diff_summarize.c
blob: 23ed29c1ddc9b9690e81e799e467117db053d958 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
 * repos_diff_summarize.c -- The diff summarize editor for summarizing
 * the differences of two repository versions
 *
 * ====================================================================
 *    Licensed to the Apache Software Foundation (ASF) under one
 *    or more contributor license agreements.  See the NOTICE file
 *    distributed with this work for additional information
 *    regarding copyright ownership.  The ASF licenses this file
 *    to you under the Apache License, Version 2.0 (the
 *    "License"); you may not use this file except in compliance
 *    with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing,
 *    software distributed under the License is distributed on an
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *    KIND, either express or implied.  See the License for the
 *    specific language governing permissions and limitations
 *    under the License.
 * ====================================================================
 */


#include "svn_dirent_uri.h"
#include "svn_props.h"
#include "svn_pools.h"

#include "client.h"


/* Overall crawler editor baton.  */
struct edit_baton {
  /* The target of the diff, relative to the root of the edit */
  const char *target;

  /* The summarize callback passed down from the API */
  svn_client_diff_summarize_func_t summarize_func;

  /* The summarize callback baton */
  void *summarize_func_baton;

  /* An RA session used to check the kind of deleted paths */
  svn_ra_session_t *ra_session;

  /* The start revision for the comparison */
  svn_revnum_t revision;

  /* TRUE if the operation needs to walk deleted dirs on the "old" side.
     FALSE otherwise. */
  svn_boolean_t walk_deleted_repos_dirs;

  /* A callback used to see if the client wishes to cancel the running
     operation. */
  svn_cancel_func_t cancel_func;

  /* A baton to pass to the cancellation callback. */
  void *cancel_baton;

};


/* Item baton. */
struct item_baton {
  /* The overall crawler editor baton */
  struct edit_baton *edit_baton;

  /* The summarize filled by the editor calls, NULL if this item hasn't
     been modified (yet) */
  svn_client_diff_summarize_t *summarize;

  /* The path of the file or directory within the repository */
  const char *path;

  /* The kind of this item */
  svn_node_kind_t node_kind;

  /* The file/directory pool */
  apr_pool_t *item_pool;
};


/* Create an item baton, with the fields initialized to EDIT_BATON, PATH,
 * NODE_KIND and POOL, respectively.  Allocate the returned structure in POOL.
 */
static struct item_baton *
create_item_baton(struct edit_baton *edit_baton,
                  const char *path,
                  svn_node_kind_t node_kind,
                  apr_pool_t *pool)
{
  struct item_baton *b = apr_pcalloc(pool, sizeof(*b));

  b->edit_baton = edit_baton;
  /* Issue #2765: b->path is supposed to be relative to the target.
     If the target is a file, just use an empty path.  This way the
     receiver can just concatenate this path to the original path
     without doing any extra checks. */
  if (node_kind == svn_node_file && strcmp(path, edit_baton->target) == 0)
    b->path =  "";
  else
    b->path = apr_pstrdup(pool, path);
  b->node_kind = node_kind;
  b->item_pool = pool;

  return b;
}

/* Make sure that this item baton contains a summarize struct.
 * If it doesn't before this call, allocate a new struct in the item's pool,
 * initializing the diff kind to normal.
 * All other fields are also initialized from IB or to NULL/invalid values. */
static void
ensure_summarize(struct item_baton *ib)
{
  svn_client_diff_summarize_t *sum;
  if (ib->summarize)
    return;

  sum = apr_pcalloc(ib->item_pool, sizeof(*sum));

  sum->node_kind = ib->node_kind;
  sum->summarize_kind = svn_client_diff_summarize_kind_normal;
  sum->path = ib->path;

  ib->summarize = sum;
}


/* An editor function. The root of the comparison hierarchy */
static svn_error_t *
open_root(void *edit_baton,
          svn_revnum_t base_revision,
          apr_pool_t *pool,
          void **root_baton)
{
  struct item_baton *ib = create_item_baton(edit_baton, "",
                                            svn_node_dir, pool);

  *root_baton = ib;
  return SVN_NO_ERROR;
}

/* Recursively walk the tree rooted at DIR (at REVISION) in the
 * repository, reporting all files as deleted.  Part of a workaround
 * for issue 2333.
 *
 * DIR is a repository path relative to the URL in RA_SESSION.  REVISION
 * may be NULL, in which case it defaults to HEAD.  EDIT_BATON is the
 * overall crawler editor baton.  If CANCEL_FUNC is not NULL, then it
 * should refer to a cancellation function (along with CANCEL_BATON).
 */
/* ### TODO: Handle depth. */
static svn_error_t *
diff_deleted_dir(const char *dir,
                 svn_revnum_t revision,
                 svn_ra_session_t *ra_session,
                 void *edit_baton,
                 svn_cancel_func_t cancel_func,
                 void *cancel_baton,
                 apr_pool_t *pool)
{
  struct edit_baton *eb = edit_baton;
  apr_hash_t *dirents;
  apr_pool_t *iterpool = svn_pool_create(pool);
  apr_hash_index_t *hi;

  if (cancel_func)
    SVN_ERR(cancel_func(cancel_baton));

  SVN_ERR(svn_ra_get_dir2(ra_session,
                          &dirents,
                          NULL, NULL,
                          dir,
                          revision,
                          SVN_DIRENT_KIND,
                          pool));

  for (hi = apr_hash_first(pool, dirents); hi;
       hi = apr_hash_next(hi))
    {
      const char *path;
      const char *name = svn__apr_hash_index_key(hi);
      svn_dirent_t *dirent = svn__apr_hash_index_val(hi);
      svn_node_kind_t kind;
      svn_client_diff_summarize_t *sum;

      svn_pool_clear(iterpool);

      path = svn_relpath_join(dir, name, iterpool);

      SVN_ERR(svn_ra_check_path(eb->ra_session,
                                path,
                                eb->revision,
                                &kind,
                                iterpool));

      sum = apr_pcalloc(iterpool, sizeof(*sum));
      sum->summarize_kind = svn_client_diff_summarize_kind_deleted;
      sum->path = path;
      sum->node_kind = kind;

      SVN_ERR(eb->summarize_func(sum,
                                 eb->summarize_func_baton,
                                 iterpool));

      if (dirent->kind == svn_node_dir)
        SVN_ERR(diff_deleted_dir(path,
                                 revision,
                                 ra_session,
                                 eb,
                                 cancel_func,
                                 cancel_baton,
                                 iterpool));
    }

  svn_pool_destroy(iterpool);
  return SVN_NO_ERROR;
}

/* An editor function.  */
static svn_error_t *
delete_entry(const char *path,
             svn_revnum_t base_revision,
             void *parent_baton,
             apr_pool_t *pool)
{
  struct item_baton *ib = parent_baton;
  struct edit_baton *eb = ib->edit_baton;
  svn_client_diff_summarize_t *sum;
  svn_node_kind_t kind;

  /* We need to know if this is a directory or a file */
  SVN_ERR(svn_ra_check_path(eb->ra_session,
                            path,
                            eb->revision,
                            &kind,
                            pool));

  sum = apr_pcalloc(pool, sizeof(*sum));
  sum->summarize_kind = svn_client_diff_summarize_kind_deleted;
  sum->path = path;
  sum->node_kind = kind;

  SVN_ERR(eb->summarize_func(sum, eb->summarize_func_baton, pool));

  if (kind == svn_node_dir)
        SVN_ERR(diff_deleted_dir(path,
                                 eb->revision,
                                 eb->ra_session,
                                 eb,
                                 eb->cancel_func,
                                 eb->cancel_baton,
                                 pool));

  return SVN_NO_ERROR;
}

/* An editor function.  */
static svn_error_t *
add_directory(const char *path,
              void *parent_baton,
              const char *copyfrom_path,
              svn_revnum_t copyfrom_rev,
              apr_pool_t *pool,
              void **child_baton)
{
  struct item_baton *pb = parent_baton;
  struct item_baton *cb;

  cb = create_item_baton(pb->edit_baton, path, svn_node_dir, pool);
  ensure_summarize(cb);
  cb->summarize->summarize_kind = svn_client_diff_summarize_kind_added;

  *child_baton = cb;
  return SVN_NO_ERROR;
}

/* An editor function.  */
static svn_error_t *
open_directory(const char *path,
               void *parent_baton,
               svn_revnum_t base_revision,
               apr_pool_t *pool,
               void **child_baton)
{
  struct item_baton *pb = parent_baton;
  struct item_baton *cb;

  cb = create_item_baton(pb->edit_baton, path, svn_node_dir, pool);

  *child_baton = cb;
  return SVN_NO_ERROR;
}


/* An editor function.  */
static svn_error_t *
close_directory(void *dir_baton,
                apr_pool_t *pool)
{
  struct item_baton *ib = dir_baton;
  struct edit_baton *eb = ib->edit_baton;

  if (ib->summarize)
    SVN_ERR(eb->summarize_func(ib->summarize, eb->summarize_func_baton,
                               pool));

  return SVN_NO_ERROR;
}


/* An editor function.  */
static svn_error_t *
add_file(const char *path,
         void *parent_baton,
         const char *copyfrom_path,
         svn_revnum_t copyfrom_rev,
         apr_pool_t *pool,
         void **file_baton)
{
  struct item_baton *pb = parent_baton;
  struct item_baton *cb;

  cb = create_item_baton(pb->edit_baton, path, svn_node_file, pool);
  ensure_summarize(cb);
  cb->summarize->summarize_kind = svn_client_diff_summarize_kind_added;

  *file_baton = cb;
  return SVN_NO_ERROR;
}

/* An editor function.  */
static svn_error_t *
open_file(const char *path,
          void *parent_baton,
          svn_revnum_t base_revision,
          apr_pool_t *pool,
          void **file_baton)
{
  struct item_baton *pb = parent_baton;
  struct item_baton *cb;

  cb = create_item_baton(pb->edit_baton, path, svn_node_file, pool);

  *file_baton = cb;
  return SVN_NO_ERROR;
}

/* An editor function.  */
static svn_error_t *
apply_textdelta(void *file_baton,
                const char *base_checksum,
                apr_pool_t *pool,
                svn_txdelta_window_handler_t *handler,
                void **handler_baton)
{
  struct item_baton *ib = file_baton;

  ensure_summarize(ib);
  if (ib->summarize->summarize_kind == svn_client_diff_summarize_kind_normal)
    ib->summarize->summarize_kind = svn_client_diff_summarize_kind_modified;

  *handler = svn_delta_noop_window_handler;
  *handler_baton = NULL;

  return SVN_NO_ERROR;
}


/* An editor function.  */
static svn_error_t *
close_file(void *file_baton,
           const char *text_checksum,
           apr_pool_t *pool)
{
  struct item_baton *fb = file_baton;
  struct edit_baton *eb = fb->edit_baton;

  if (fb->summarize)
    SVN_ERR(eb->summarize_func(fb->summarize, eb->summarize_func_baton,
                               pool));

  return SVN_NO_ERROR;
}


/* An editor function, implementing both change_file_prop and
 * change_dir_prop.  */
static svn_error_t *
change_prop(void *entry_baton,
            const char *name,
            const svn_string_t *value,
            apr_pool_t *pool)
{
  struct item_baton *ib = entry_baton;

  if (svn_property_kind(NULL, name) == svn_prop_regular_kind)
    {
      ensure_summarize(ib);

      if (ib->summarize->summarize_kind != svn_client_diff_summarize_kind_added)
        ib->summarize->prop_changed = TRUE;
    }

  return SVN_NO_ERROR;
}

/* Create a repository diff summarize editor and baton.  */
svn_error_t *
svn_client__get_diff_summarize_editor(const char *target,
                                      svn_client_diff_summarize_func_t
                                      summarize_func,
                                      void *summarize_baton,
                                      svn_ra_session_t *ra_session,
                                      svn_revnum_t revision,
                                      svn_cancel_func_t cancel_func,
                                      void *cancel_baton,
                                      const svn_delta_editor_t **editor,
                                      void **edit_baton,
                                      apr_pool_t *pool)
{
  svn_delta_editor_t *tree_editor = svn_delta_default_editor(pool);
  struct edit_baton *eb = apr_palloc(pool, sizeof(*eb));

  eb->target = target;
  eb->summarize_func = summarize_func;
  eb->summarize_func_baton = summarize_baton;
  eb->ra_session = ra_session;
  eb->revision = revision;
  eb->walk_deleted_repos_dirs = TRUE;
  eb->cancel_func = cancel_func;
  eb->cancel_baton = cancel_baton;

  tree_editor->open_root = open_root;
  tree_editor->delete_entry = delete_entry;
  tree_editor->add_directory = add_directory;
  tree_editor->open_directory = open_directory;
  tree_editor->change_dir_prop = change_prop;
  tree_editor->close_directory = close_directory;

  tree_editor->add_file = add_file;
  tree_editor->open_file = open_file;
  tree_editor->apply_textdelta = apply_textdelta;
  tree_editor->change_file_prop = change_prop;
  tree_editor->close_file = close_file;

  return svn_delta_get_cancellation_editor(cancel_func, cancel_baton,
                                           tree_editor, eb, editor, edit_baton,
                                           pool);
}