File: cmd-apply-delta.c

package info (click to toggle)
tla 1.3.5%2Bdfsg1-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 22,292 kB
  • ctags: 11,952
  • sloc: ansic: 149,771; sh: 16,009; xml: 2,689; lisp: 1,927; makefile: 1,064; cpp: 363; tcl: 230; awk: 48; sed: 25
file content (314 lines) | stat: -rw-r--r-- 8,963 bytes parent folder | download | duplicates (4)
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
/* cmd-apply-delta.c:
 *
 ****************************************************************
 * Copyright (C) 2003 Tom Lord
 *
 * See the file "COPYING" for further information about
 * the copyright and warranty status of this work.
 */




#include "config-options.h"
#include "hackerlab/cmd/main.h"
#include "hackerlab/fs/file-names.h"
#include "hackerlab/fs/cwd.h"
#include "tla/libfsutils/tmp-files.h"
#include "tla/libfsutils/rmrf.h"
#include "tla/libfsutils/dir-as-cwd.h"
#include "tla/libarch/namespace.h"
#include "tla/libarch/project-tree.h"
#include "tla/libarch/my.h"
#include "tla/libarch/archive.h"
#include "tla/libarch/pristines.h"
#include "tla/libarch/local-cache.h"
#include "tla/libarch/make-changeset.h"
#include "tla/libarch/copy-project-tree.h"
#include "tla/libarch/apply-changeset.h"
#include "tla/libarch/merge-three-way.h"
#include "tla/libarch/cmd.h"
#include "tla/libarch/cmd-get.h"
#include "tla/libarch/cmd-delta.h"
#include "tla/libarch/cmd-apply-delta.h"
#include "tla/libarch/cmdutils.h"


/* __STDC__ prototypes for static functions */
static void apply_delta_callback (void * vfd, const char * fmt, va_list ap);



static t_uchar * usage = "[options] FROM(REVISION|DIR) TO(REVISION|DIR)";
static t_uchar * version_string = (cfg__std__package " from regexps.com\n"
                                   "\n"
                                   "Copyright 2003 Tom Lord\n"
                                   "\n"
                                   "This is free software; see the source for copying conditions.\n"
                                   "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n"
                                   "PARTICULAR PURPOSE.\n"
                                   "\n"
                                   "Report bugs to " cfg__tla_bug_mail ".\n"
                                   "\n"
                                   cfg__std__release_id_string
                                   "\n");

#define OPTS(OP) \
  OP (opt_help_msg, "h", "help", 0, \
      "Display a help message and exit.") \
  OP (opt_long_help, "H", 0, 0, \
      "Display a verbose help message and exit.") \
  OP (opt_version, "V", "version", 0, \
      "Display a release identifier string\n" \
      "and exit.") \
  OP (opt_archive, "A", "archive", 1, \
      "Override `my-default-archive'") \
  OP (opt_forward, "N", "forward", 0, \
      "pass the --forward option to `patch'") \
  OP (opt_cache, 0, "cache DIR", 1, \
      "specify a cache root for pristine copies") \
  OP (opt_dir, "d", "dir DIR", 1, \
      "Operate on project tree in DIR (default `.')") \
  OP (opt_three_way, "t", "three-way", 0, \
      "Perform a 3-way (diff3-style) merge.") \
  OP (opt_dest, 0, "dest DEST", 1, \
      "Instead of modifying the project tree in-place,\n" \
      "make a copy of it to DEST and apply the result to that") \
  OP (opt_unescaped, 0, "unescaped", 0, \
      "show filenames in unescaped form")


t_uchar arch_cmd_apply_delta_help[] =
  ("compute a changeset between any two trees or revisions and apply it to a project tree\n"

   "A delta between A and B (both of which may be either a full revision or\n"
   "a project tree) is computed, and then applied to the project tree.\n"
   "\n"
   "Exit Status Codes:\n"
   "\n"
   "    0  No conflict during patch\n"
   "    1  Conflicts occurred during patch\n"
   "    3  Internal Error\n");

enum options
{
  OPTS (OPT_ENUM)
};

static struct opt_desc opts[] =
{
  OPTS (OPT_DESC)
    {-1, 0, 0, 0, 0}
};

static void 
do_three_way(int chatter_fd, t_uchar * cache_dir, t_uchar * default_archive, t_uchar * upon_root, t_uchar * from_spec, t_uchar * to_spec, int escape_classes);



int
arch_cmd_apply_delta (t_uchar * program_name, int argc, char * argv[])
{
  int o;
  struct opt_parsed * option;
  int three_way = 0;
  t_uchar * default_archive = 0;
  t_uchar * cache_dir = 0;
  t_uchar * upon = 0;
  t_uchar * dest = 0;
  int forward = 0;
  int exit_status = 0;
  int escape_classes = arch_escape_classes;

  safe_buffer_fd (1, 0, O_WRONLY, 0);

  option = 0;

  while (1)
    {
      o = opt_standard (lim_use_must_malloc, &option, opts, &argc, argv, program_name, usage, version_string, arch_cmd_apply_delta_help, opt_help_msg, opt_long_help, opt_version);
      if (o == opt_none)
        break;
      switch (o)
        {
        default:
          safe_printfmt (2, "unhandled option `%s'\n", option->opt_string);
          panic ("internal error parsing arguments");

        usage_error:
          opt_usage (2, argv[0], program_name, usage, 1);
          exit (1);

          /* bogus_arg: */
          safe_printfmt (2, "ill-formed argument for `%s' (`%s')\n", option->opt_string, option->arg_string);
          goto usage_error;

        case opt_archive:
          {
            lim_free (0, default_archive);
            default_archive = str_save (0, option->arg_string);
            break;
          }

        case opt_cache:
          {
            lim_free (0, cache_dir);
            cache_dir = str_save (0, option->arg_string);
            break;
          }

        case opt_three_way:
          {
            three_way = 1;
            break;
          }

        case opt_dir:
          {
            lim_free (0, upon);
            upon = str_save (0, option->arg_string);
            break;
          }

        case opt_dest:
          {
            lim_free (0, dest);
            dest = str_save (0, option->arg_string);
            break;
          }

        case opt_forward:
          {
            forward = 1;
            break;
          }

	case opt_unescaped:
	  {
	    escape_classes = 0;
	    break;
	  }
        }
    }

  if (argc != 3)
    goto usage_error;

  if (default_archive && !arch_valid_archive_name (default_archive))
    {
      safe_printfmt (2, "%s: invalid archive name (%s)\n",
                     argv[0], default_archive);
      exit (1);
    }

  default_archive = arch_my_default_archive (default_archive);

  {
    t_uchar * from_spec;
    t_uchar * to_spec;
    t_uchar * changeset = 0;
    t_uchar * upon_root = 0;

    from_spec = argv[1];
    to_spec = argv[2];

    if (! upon)
      upon = str_save (0, ".");

    upon_root = arch_tree_root (0, upon, 0);

    if (dest)
      {
        t_uchar * dest_dir = file_name_directory_file (0, dest);

        if (! dest_dir)
          dest_dir = str_save (0, ".");

        changeset = tmp_file_name (dest_dir, ",,apply-delta-changeset");

        safe_printfmt (1, "* copying %s to %s\n", upon, dest);
        arch_copy_project_tree (upon, dest, 1, 1);

        lim_free (0, dest_dir);
      }
    else
      {
        dest = str_save (0, upon);
        changeset = tmp_file_name (dest, ",,apply-delta-changeset");
      }
    if (three_way)
      {
        do_three_way(1, cache_dir, default_archive, upon_root, from_spec, to_spec, escape_classes);
      }

    else
      {
        if (cache_dir)
          arch_call_cmd (arch_cmd_delta, argv[0],
                         "--cache", cache_dir, "-A", default_archive, from_spec, to_spec, changeset,
                         ((escape_classes == 0) ? "--unescaped" : (char*)0),
                         (char*)0);
        else
          arch_call_cmd (arch_cmd_delta, argv[0],
                         "-A", default_archive, from_spec, to_spec, changeset,
                         ((escape_classes == 0) ? "--unescaped" : (char*)0),
                         (char*)0);

        {
          struct arch_apply_changeset_report r = {0, };

          r.callback = apply_delta_callback;
          r.thunk = (void *)1;

          safe_printfmt (1, "* applying changeset\n");

          arch_apply_changeset (&r, changeset, dest, arch_unspecified_id_tagging, arch_inventory_unrecognized, 0, forward, 0, 0, 0, escape_classes);

          exit_status = arch_conflicts_occured (&r);

          arch_free_apply_changeset_report_data (&r);
        }

      }
    rmrf_file (changeset);

    lim_free (0, changeset);
    lim_free (0, upon_root);
  }

  lim_free (0, upon);
  lim_free (0, dest);
  lim_free (0, default_archive);
  lim_free (0, cache_dir);

  return exit_status;
}




static void
apply_delta_callback (void * vfd, const char * fmt, va_list ap)
{
  int fd;

  fd = (int)(t_ulong)vfd;
  safe_printfmt_va_list (fd, fmt, ap);
  safe_flush (fd);
}


static void 
do_three_way(int chatter_fd, t_uchar * cache_dir, t_uchar * default_archive, t_uchar * upon_root, t_uchar * from_spec, t_uchar * to_spec, int escape_classes)
{
  t_uchar * scratch_dir = tmp_file_name (".", ",,apply-delta-scratch");
  t_uchar * from_path = arch_interpret_delta_path (0, 0, scratch_dir, default_archive, from_spec, cache_dir);
  t_uchar * to_path = arch_interpret_delta_path (0, 0, scratch_dir, default_archive, to_spec, cache_dir);
  arch_merge3 (chatter_fd, upon_root, from_path, to_path, escape_classes);
  rmrf_file (scratch_dir);
}



/* tag: Tom Lord Wed Jun  4 15:40:44 2003 (cmd-deltapatch.c)
 */