summaryrefslogtreecommitdiff
path: root/subversion/svn/list-cmd.c
blob: 5ea140fc93d1b30932df7c0f83fdedfc002d26c8 (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
/*
 * list-cmd.c -- list a URL
 *
 * ====================================================================
 *    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_cmdline.h"
#include "svn_client.h"
#include "svn_error.h"
#include "svn_pools.h"
#include "svn_time.h"
#include "svn_xml.h"
#include "svn_dirent_uri.h"
#include "svn_path.h"
#include "svn_utf.h"
#include "svn_opt.h"

#include "cl.h"

#include "svn_private_config.h"



/* Baton used when printing directory entries. */
struct print_baton {
  svn_boolean_t verbose;
  svn_client_ctx_t *ctx;

  /* To keep track of last seen external information. */
  const char *last_external_parent_url;
  const char *last_external_target;
  svn_boolean_t in_external;
};

/* Field flags required for this function */
static const apr_uint32_t print_dirent_fields = SVN_DIRENT_KIND;
static const apr_uint32_t print_dirent_fields_verbose = (
    SVN_DIRENT_KIND  | SVN_DIRENT_SIZE | SVN_DIRENT_TIME |
    SVN_DIRENT_CREATED_REV | SVN_DIRENT_LAST_AUTHOR);

/* This implements the svn_client_list_func2_t API, printing a single
   directory entry in text format. */
static svn_error_t *
print_dirent(void *baton,
             const char *path,
             const svn_dirent_t *dirent,
             const svn_lock_t *lock,
             const char *abs_path,
             const char *external_parent_url,
             const char *external_target,
             apr_pool_t *scratch_pool)
{
  struct print_baton *pb = baton;
  const char *entryname;
  static const char *time_format_long = NULL;
  static const char *time_format_short = NULL;

  SVN_ERR_ASSERT((external_parent_url == NULL && external_target == NULL) ||
                 (external_parent_url && external_target));

  if (time_format_long == NULL)
    time_format_long = _("%b %d %H:%M");
  if (time_format_short == NULL)
    time_format_short = _("%b %d  %Y");

  if (pb->ctx->cancel_func)
    SVN_ERR(pb->ctx->cancel_func(pb->ctx->cancel_baton));

  if (strcmp(path, "") == 0)
    {
      if (dirent->kind == svn_node_file)
        entryname = svn_dirent_basename(abs_path, scratch_pool);
      else if (pb->verbose)
        entryname = ".";
      else
        /* Don't bother to list if no useful information will be shown. */
        return SVN_NO_ERROR;
    }
  else
    entryname = path;

  if (external_parent_url && external_target)
    {
      if ((pb->last_external_parent_url == NULL
           && pb->last_external_target == NULL)
          || (strcmp(pb->last_external_parent_url, external_parent_url) != 0
              || strcmp(pb->last_external_target, external_target) != 0))
        {
          SVN_ERR(svn_cmdline_printf(scratch_pool,
                                     _("Listing external '%s'"
                                       " defined on '%s':\n"),
                                     external_target,
                                     external_parent_url));

          pb->last_external_parent_url = external_parent_url;
          pb->last_external_target = external_target;
        }
    }

  if (pb->verbose)
    {
      apr_time_t now = apr_time_now();
      apr_time_exp_t exp_time;
      apr_status_t apr_err;
      apr_size_t size;
      char timestr[20];
      const char *sizestr, *utf8_timestr;

      /* svn_time_to_human_cstring gives us something *way* too long
         to use for this, so we have to roll our own.  We include
         the year if the entry's time is not within half a year. */
      apr_time_exp_lt(&exp_time, dirent->time);
      if (apr_time_sec(now - dirent->time) < (365 * 86400 / 2)
          && apr_time_sec(dirent->time - now) < (365 * 86400 / 2))
        {
          apr_err = apr_strftime(timestr, &size, sizeof(timestr),
                                 time_format_long, &exp_time);
        }
      else
        {
          apr_err = apr_strftime(timestr, &size, sizeof(timestr),
                                 time_format_short, &exp_time);
        }

      /* if that failed, just zero out the string and print nothing */
      if (apr_err)
        timestr[0] = '\0';

      /* we need it in UTF-8. */
      SVN_ERR(svn_utf_cstring_to_utf8(&utf8_timestr, timestr, scratch_pool));

      sizestr = apr_psprintf(scratch_pool, "%" SVN_FILESIZE_T_FMT,
                             dirent->size);

      return svn_cmdline_printf
              (scratch_pool, "%7ld %-8.8s %c %10s %12s %s%s\n",
               dirent->created_rev,
               dirent->last_author ? dirent->last_author : " ? ",
               lock ? 'O' : ' ',
               (dirent->kind == svn_node_file) ? sizestr : "",
               utf8_timestr,
               entryname,
               (dirent->kind == svn_node_dir) ? "/" : "");
    }
  else
    {
      return svn_cmdline_printf(scratch_pool, "%s%s\n", entryname,
                                (dirent->kind == svn_node_dir)
                                ? "/" : "");
    }
}

/* Field flags required for this function */
static const apr_uint32_t print_dirent_xml_fields = (
    SVN_DIRENT_KIND  | SVN_DIRENT_SIZE | SVN_DIRENT_TIME |
    SVN_DIRENT_CREATED_REV | SVN_DIRENT_LAST_AUTHOR);
/* This implements the svn_client_list_func2_t API, printing a single dirent
   in XML format. */
static svn_error_t *
print_dirent_xml(void *baton,
                 const char *path,
                 const svn_dirent_t *dirent,
                 const svn_lock_t *lock,
                 const char *abs_path,
                 const char *external_parent_url,
                 const char *external_target,
                 apr_pool_t *scratch_pool)
{
  struct print_baton *pb = baton;
  const char *entryname;
  svn_stringbuf_t *sb = svn_stringbuf_create_empty(scratch_pool);

  SVN_ERR_ASSERT((external_parent_url == NULL && external_target == NULL) ||
                 (external_parent_url && external_target));

  if (strcmp(path, "") == 0)
    {
      if (dirent->kind == svn_node_file)
        entryname = svn_dirent_basename(abs_path, scratch_pool);
      else
        /* Don't bother to list if no useful information will be shown. */
        return SVN_NO_ERROR;
    }
  else
    entryname = path;

  if (pb->ctx->cancel_func)
    SVN_ERR(pb->ctx->cancel_func(pb->ctx->cancel_baton));

  if (external_parent_url && external_target)
    {
      if ((pb->last_external_parent_url == NULL
           && pb->last_external_target == NULL)
          || (strcmp(pb->last_external_parent_url, external_parent_url) != 0
              || strcmp(pb->last_external_target, external_target) != 0))
        {
          if (pb->in_external)
            {
              /* The external item being listed is different from the previous
                 one, so close the tag. */
              svn_xml_make_close_tag(&sb, scratch_pool, "external");
              pb->in_external = FALSE;
            }

          svn_xml_make_open_tag(&sb, scratch_pool, svn_xml_normal, "external",
                                "parent_url", external_parent_url,
                                "target", external_target,
                                SVN_VA_NULL);

          pb->last_external_parent_url = external_parent_url;
          pb->last_external_target = external_target;
          pb->in_external = TRUE;
        }
    }

  svn_xml_make_open_tag(&sb, scratch_pool, svn_xml_normal, "entry",
                        "kind", svn_cl__node_kind_str_xml(dirent->kind),
                        SVN_VA_NULL);

  svn_cl__xml_tagged_cdata(&sb, scratch_pool, "name", entryname);

  if (dirent->kind == svn_node_file)
    {
      svn_cl__xml_tagged_cdata
        (&sb, scratch_pool, "size",
         apr_psprintf(scratch_pool, "%" SVN_FILESIZE_T_FMT, dirent->size));
    }

  svn_xml_make_open_tag(&sb, scratch_pool, svn_xml_normal, "commit",
                        "revision",
                        apr_psprintf(scratch_pool, "%ld", dirent->created_rev),
                        SVN_VA_NULL);
  svn_cl__xml_tagged_cdata(&sb, scratch_pool, "author", dirent->last_author);
  if (dirent->time)
    svn_cl__xml_tagged_cdata(&sb, scratch_pool, "date",
                             svn_time_to_cstring(dirent->time, scratch_pool));
  svn_xml_make_close_tag(&sb, scratch_pool, "commit");

  if (lock)
    {
      svn_xml_make_open_tag(&sb, scratch_pool, svn_xml_normal, "lock",
                            SVN_VA_NULL);
      svn_cl__xml_tagged_cdata(&sb, scratch_pool, "token", lock->token);
      svn_cl__xml_tagged_cdata(&sb, scratch_pool, "owner", lock->owner);
      svn_cl__xml_tagged_cdata(&sb, scratch_pool, "comment", lock->comment);
      svn_cl__xml_tagged_cdata(&sb, scratch_pool, "created",
                               svn_time_to_cstring(lock->creation_date,
                                                   scratch_pool));
      if (lock->expiration_date != 0)
        svn_cl__xml_tagged_cdata(&sb, scratch_pool, "expires",
                                 svn_time_to_cstring
                                 (lock->expiration_date, scratch_pool));
      svn_xml_make_close_tag(&sb, scratch_pool, "lock");
    }

  svn_xml_make_close_tag(&sb, scratch_pool, "entry");

  return svn_cl__error_checked_fputs(sb->data, stdout);
}


/* This implements the `svn_opt_subcommand_t' interface. */
svn_error_t *
svn_cl__list(apr_getopt_t *os,
             void *baton,
             apr_pool_t *pool)
{
  svn_cl__opt_state_t *opt_state = ((svn_cl__cmd_baton_t *) baton)->opt_state;
  svn_client_ctx_t *ctx = ((svn_cl__cmd_baton_t *) baton)->ctx;
  apr_array_header_t *targets;
  int i;
  apr_pool_t *subpool = svn_pool_create(pool);
  apr_uint32_t dirent_fields;
  struct print_baton pb;
  svn_boolean_t seen_nonexistent_target = FALSE;
  svn_error_t *err;
  svn_error_t *externals_err = SVN_NO_ERROR;
  struct svn_cl__check_externals_failed_notify_baton nwb;

  SVN_ERR(svn_cl__args_to_target_array_print_reserved(&targets, os,
                                                      opt_state->targets,
                                                      ctx, FALSE, pool));

  /* Add "." if user passed 0 arguments */
  svn_opt_push_implicit_dot_target(targets, pool);

  if (opt_state->xml)
    {
      /* The XML output contains all the information, so "--verbose"
         does not apply. */
      if (opt_state->verbose)
        return svn_error_create(SVN_ERR_CL_ARG_PARSING_ERROR, NULL,
                                _("'verbose' option invalid in XML mode"));

      /* If output is not incremental, output the XML header and wrap
         everything in a top-level element. This makes the output in
         its entirety a well-formed XML document. */
      if (! opt_state->incremental)
        SVN_ERR(svn_cl__xml_print_header("lists", pool));
    }
  else
    {
      if (opt_state->incremental)
        return svn_error_create(SVN_ERR_CL_ARG_PARSING_ERROR, NULL,
                                _("'incremental' option only valid in XML "
                                  "mode"));
    }

  if (opt_state->xml)
    dirent_fields = print_dirent_xml_fields;
  else if (opt_state->verbose)
    dirent_fields = print_dirent_fields_verbose;
  else
    dirent_fields = print_dirent_fields;

  pb.ctx = ctx;
  pb.verbose = opt_state->verbose;

  if (opt_state->depth == svn_depth_unknown)
    opt_state->depth = svn_depth_immediates;

  if (opt_state->include_externals)
    {
      nwb.wrapped_func = ctx->notify_func2;
      nwb.wrapped_baton = ctx->notify_baton2;
      nwb.had_externals_error = FALSE;
      ctx->notify_func2 = svn_cl__check_externals_failed_notify_wrapper;
      ctx->notify_baton2 = &nwb;
    }

  /* For each target, try to list it. */
  for (i = 0; i < targets->nelts; i++)
    {
      const char *target = APR_ARRAY_IDX(targets, i, const char *);
      const char *truepath;
      svn_opt_revision_t peg_revision;

      /* Initialize the following variables for
         every list target. */
      pb.last_external_parent_url = NULL;
      pb.last_external_target = NULL;
      pb.in_external = FALSE;

      svn_pool_clear(subpool);

      SVN_ERR(svn_cl__check_cancel(ctx->cancel_baton));

      /* Get peg revisions. */
      SVN_ERR(svn_opt_parse_path(&peg_revision, &truepath, target,
                                 subpool));

      if (opt_state->xml)
        {
          svn_stringbuf_t *sb = svn_stringbuf_create_empty(pool);
          svn_xml_make_open_tag(&sb, pool, svn_xml_normal, "list",
                                "path", truepath[0] == '\0' ? "." : truepath,
                                SVN_VA_NULL);
          SVN_ERR(svn_cl__error_checked_fputs(sb->data, stdout));
        }

      err = svn_client_list3(truepath, &peg_revision,
                             &(opt_state->start_revision),
                             opt_state->depth,
                             dirent_fields,
                             (opt_state->xml || opt_state->verbose),
                             opt_state->include_externals,
                             opt_state->xml ? print_dirent_xml : print_dirent,
                             &pb, ctx, subpool);

      if (err)
        {
          /* If one of the targets is a non-existent URL or wc-entry,
             don't bail out.  Just warn and move on to the next target. */
          if (err->apr_err == SVN_ERR_WC_PATH_NOT_FOUND ||
              err->apr_err == SVN_ERR_FS_NOT_FOUND)
              svn_handle_warning2(stderr, err, "svn: ");
          else
              return svn_error_trace(err);

          svn_error_clear(err);
          err = NULL;
          seen_nonexistent_target = TRUE;
        }

      if (opt_state->xml)
        {
          svn_stringbuf_t *sb = svn_stringbuf_create_empty(pool);

          if (pb.in_external)
            {
              /* close the final external item's tag */
              svn_xml_make_close_tag(&sb, pool, "external");
              pb.in_external = FALSE;
            }

          svn_xml_make_close_tag(&sb, pool, "list");
          SVN_ERR(svn_cl__error_checked_fputs(sb->data, stdout));
        }
    }

  svn_pool_destroy(subpool);

  if (opt_state->include_externals && nwb.had_externals_error)
    {
      externals_err = svn_error_create(SVN_ERR_CL_ERROR_PROCESSING_EXTERNALS,
                                       NULL,
                                       _("Failure occurred processing one or "
                                         "more externals definitions"));
    }

  if (opt_state->xml && ! opt_state->incremental)
    SVN_ERR(svn_cl__xml_print_footer("lists", pool));

  if (seen_nonexistent_target)
    err = svn_error_create(SVN_ERR_ILLEGAL_TARGET, NULL,
          _("Could not list all targets because some targets don't exist"));
  else
    err = NULL;

  return svn_error_compose_create(externals_err, err);
}