1 /* cmd-delta.c:
2  *
3  ****************************************************************
4  * Copyright (C) 2003 Tom Lord
5  *
6  * See the file "COPYING" for further information about
7  * the copyright and warranty status of this work.
8  */
9 
10 
11 
12 
13 #include "config-options.h"
14 #include "hackerlab/cmd/main.h"
15 #include "hackerlab/fs/file-names.h"
16 #include "hackerlab/fs/cwd.h"
17 #include "tla/libfsutils/tmp-files.h"
18 #include "tla/libfsutils/rmrf.h"
19 #include "tla/libfsutils/dir-as-cwd.h"
20 #include "tla/libarch/namespace.h"
21 #include "tla/libarch/project-tree.h"
22 #include "tla/libarch/my.h"
23 #include "tla/libarch/archive.h"
24 #include "tla/libarch/pristines.h"
25 #include "tla/libarch/local-cache.h"
26 #include "tla/libarch/make-changeset.h"
27 #include "tla/libarch/changeset-report.h"
28 #include "tla/libarch/inode-sig.h"
29 #include "tla/libarch/cmdutils.h"
30 #include "tla/libarch/cmd.h"
31 #include "tla/libarch/cmd-delta.h"
32 
33 
34 /* __STDC__ prototypes for static functions */
35 static t_uchar * interpret_delta_path (t_uchar ** arch, t_uchar ** rev, t_uchar * scratch_dir, t_uchar * default_archive, t_uchar * spec, t_uchar * cache_dir);
36 static void delta_callback (void * vfd, const char * fmt, va_list ap);
37 
38 
39 
40 static t_uchar * usage = "[options] (REVISION|TREE)-A (REVISION|TREE)-B [DESTDIR]";
41 static t_uchar * version_string = (cfg__std__package " from regexps.com\n"
42                                    "\n"
43                                    "Copyright 2003 Tom Lord\n"
44                                    "\n"
45                                    "This is free software; see the source for copying conditions.\n"
46                                    "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n"
47                                    "PARTICULAR PURPOSE.\n"
48                                    "\n"
49                                    "Report bugs to " cfg__tla_bug_mail ".\n"
50                                    "\n"
51                                    cfg__std__release_id_string
52                                    "\n");
53 
54 #define OPTS(OP) \
55   OP (opt_help_msg, "h", "help", 0, \
56       "Display a help message and exit.") \
57   OP (opt_long_help, "H", 0, 0, \
58       "Display a verbose help message and exit.") \
59   OP (opt_version, "V", "version", 0, \
60       "Display a release identifier string\n" \
61       "and exit.") \
62   OP (opt_archive, "A", "archive", 1, \
63       "Override `my-default-archive'") \
64   OP (opt_cache, 0, "cache DIR", 1, \
65       "specify a cache root for pristine copies") \
66   OP (opt_no_changeset, "n" , "no-changeset", 0, \
67       "do not generate a changeset") \
68   OP (opt_diff, 0, "diffs", 0, \
69       "print changeset report with diffs (implies -n)") \
70   OP (opt_unescaped, 0, "unescaped", 0, \
71       "show filenames in unescaped form")
72 
73 
74 t_uchar arch_cmd_delta_help[] = ("compute a changeset (or diff) between any two trees or revisions\n"
75                                  "\n"
76                                  "Given (REVISION|TREE)-A and (REVISION|TREE)-B, tla will build a changeset\n"
77                                  "that comprises the changes between REVISION-A and REVISION-B\n"
78                                  "\n"
79                                  "Example:\n"
80                                  "  tla delta tla--devo--1.1--patch-6 \\\n"
81                                  "            tla--devo--1.1--patch-8 ,,changes\n"
82                                  "\n"
83                                  "  Will pull patch-6 and patch-8 from tla--devo--1.1 and compute\n"
84                                  "  a changeset, which will be saved in a newly created ,,changes\n"
85                                  "  directory. If you would like a report instead,\n"
86                                  "  append the --diffs option");
87 
88 enum options
89 {
90   OPTS (OPT_ENUM)
91 };
92 
93 static struct opt_desc opts[] =
94 {
95   OPTS (OPT_DESC)
96     {-1, 0, 0, 0, 0}
97 };
98 
99 
100 
101 int
arch_cmd_delta(t_uchar * program_name,int argc,char * argv[])102 arch_cmd_delta (t_uchar * program_name, int argc, char * argv[])
103 {
104   int o;
105   struct opt_parsed * option;
106   t_uchar * default_archive = 0;
107   t_uchar * cache_dir = 0;
108   int no_changeset = 0;
109   int report = 0;
110   int escape_classes = arch_escape_classes;
111 
112   safe_buffer_fd (1, 0, O_WRONLY, 0);
113 
114   option = 0;
115 
116   while (1)
117     {
118       o = opt_standard (lim_use_must_malloc, &option, opts, &argc, argv, program_name, usage, version_string, arch_cmd_delta_help, opt_help_msg, opt_long_help, opt_version);
119       if (o == opt_none)
120         break;
121       switch (o)
122         {
123         default:
124           safe_printfmt (2, "unhandled option `%s'\n", option->opt_string);
125           panic ("internal error parsing arguments");
126 
127         usage_error:
128           opt_usage (2, argv[0], program_name, usage, 1);
129           exit (1);
130 
131           /* bogus_arg: */
132           safe_printfmt (2, "ill-formed argument for `%s' (`%s')\n", option->opt_string, option->arg_string);
133           goto usage_error;
134 
135         case opt_archive:
136           {
137             lim_free (0, default_archive);
138             default_archive = str_save (0, option->arg_string);
139             break;
140           }
141 
142         case opt_cache:
143           {
144             lim_free (0, cache_dir);
145             cache_dir = str_save (0, option->arg_string);
146             break;
147           }
148 
149         case opt_diff:
150           {
151             report = 1;
152             no_changeset = 1;
153             break;
154           }
155 
156         case opt_no_changeset:
157           {
158             no_changeset = 1;
159             break;
160           }
161 
162 	case opt_unescaped:
163 	  {
164 	    escape_classes = 0;
165 	    break;
166 	  }
167         }
168     }
169 
170   if (argc < 3 || argc > 4)
171     goto usage_error;
172 
173   if (default_archive && !arch_valid_archive_name (default_archive))
174     {
175       safe_printfmt (2, "%s: invalid archive name (%s)\n",
176                      argv[0], default_archive);
177       exit (1);
178     }
179 
180   default_archive = arch_my_default_archive (default_archive);
181 
182   {
183     t_uchar * a_spec;
184     t_uchar * b_spec;
185     t_uchar * dest = 0;
186     t_uchar * scratch_dir = 0;
187     t_uchar * orig = 0;
188     t_uchar * mod = 0;
189     struct arch_make_changeset_report make_report = {0, };
190     t_uchar * orig_archive = 0;
191     t_uchar * orig_revision = 0;
192     assoc_table inode_shortcut = 0;
193 
194     a_spec = argv[1];
195     b_spec = argv[2];
196 
197     if (argc == 4)
198       {
199         dest = str_save (0, argv[3]);
200       }
201     else
202       {
203         dest = tmp_file_name (".", ",,delta");
204         rmrf_file (dest);
205       }
206 
207     scratch_dir = tmp_file_name (".", ",,delta-scratch");
208 
209     orig = interpret_delta_path (&orig_archive, &orig_revision, scratch_dir, default_archive, a_spec, cache_dir);
210     mod = interpret_delta_path (0, 0, scratch_dir, default_archive, b_spec, cache_dir);
211 
212     safe_printfmt (1, "* computing changeset\n");
213 
214     make_report.callback = delta_callback;
215     make_report.thunk = (void *)1;
216 
217     if (orig_archive)
218       {
219         arch_read_inode_sig (0, &inode_shortcut, mod, orig_archive, orig_revision);
220       }
221 
222     arch_make_changeset (&make_report, orig, mod, dest, arch_unspecified_id_tagging, arch_inventory_unrecognized, rel_table_nil, inode_shortcut, 0, escape_classes);
223 
224     if (report)
225       {
226         struct arch_changeset_report r = {rel_table_nil, };
227 
228         safe_printfmt (1, "* changeset report\n");
229         arch_evaluate_changeset (&r, dest);
230         arch_print_changeset (1, &r, 1, escape_classes);
231 
232         arch_free_changeset_report_data (&r);
233       }
234 
235     rmrf_file (scratch_dir);
236 
237     if (no_changeset)
238       rmrf_file (dest);
239     else
240       safe_printfmt (1, "* changeset: %s\n", dest);
241 
242     lim_free (0, dest);
243     lim_free (0, scratch_dir);
244     lim_free (0, orig);
245     lim_free (0, mod);
246     lim_free (0, orig_archive);
247     lim_free (0, orig_revision);
248     free_assoc_table (inode_shortcut);
249     arch_free_make_changeset_report_data (&make_report);
250   }
251 
252   lim_free (0, default_archive);
253 
254   return 0;
255 }
256 
257 
258 
259 
260 static t_uchar *
interpret_delta_path(t_uchar ** arch,t_uchar ** rev,t_uchar * scratch_dir,t_uchar * default_archive,t_uchar * spec,t_uchar * cache_dir)261 interpret_delta_path (t_uchar ** arch, t_uchar ** rev, t_uchar * scratch_dir, t_uchar * default_archive, t_uchar * spec, t_uchar * cache_dir)
262 {
263   t_uchar * answer = 0;
264 
265   if (arch_valid_package_name (spec, arch_maybe_archive, arch_req_patch_level, 0))
266     {
267       t_uchar * archive = 0;
268       t_uchar * revision = 0;
269       struct arch_archive *arch_struct = 0;
270 
271       archive = arch_parse_package_name (arch_ret_archive, default_archive, spec);
272       revision = arch_parse_package_name (arch_ret_non_archive, 0, spec);
273 
274       arch_struct = arch_archive_connect (archive, 0);
275       arch_check_for (arch_struct, arch_req_patch_level, revision);
276 
277       safe_printfmt (1, "* finding or making %s\n", spec);
278 
279       answer = arch_find_or_make_tmp_local_copy (1, scratch_dir, 0, cache_dir, arch_struct, archive, revision);
280       arch_archive_close (arch_struct);
281 
282       if (arch)
283         *arch = archive;
284       else
285         lim_free (0, archive);
286 
287       if (rev)
288         *rev = revision;
289       else
290         lim_free (0, revision);
291     }
292   else if (arch_valid_patch_level_name (spec))
293     {
294       t_uchar * archive = 0;
295       t_uchar * revision = 0;
296       t_uchar * fqvsn = 0;
297       t_uchar * tree_version = 0;
298       t_uchar * tree_root = 0;
299 
300       tree_root = arch_tree_root (0, ".", 0);
301 
302       if (!tree_root)
303         {
304           safe_printfmt (2, "tla delta: not in a project tree\n  dir: %s\n", directory_as_cwd ("."));
305           exit (2);
306         }
307 
308       fqvsn = arch_tree_version (tree_root);
309       if (!fqvsn)
310 	{
311 	  safe_printfmt (2, "tla delta: no tree-version set\n  tree: %s\n", tree_root);
312 	  exit (2);
313 	}
314 
315       archive = arch_parse_package_name (arch_ret_archive, 0, fqvsn);
316       tree_version = arch_parse_package_name (arch_ret_non_archive, 0, fqvsn);
317       revision = str_alloc_cat_many (0, tree_version, "--", spec, str_end);
318 
319       safe_printfmt (1, "* finding or making %s\n", spec);
320       safe_flush (1);
321 
322       answer = arch_find_or_make_tmp_local_copy (1, scratch_dir, 0, cache_dir, 0, archive, revision);
323 
324       if (arch)
325         *arch = archive;
326       else
327         lim_free (0, archive);
328 
329       if (rev)
330         *rev = revision;
331       else
332         lim_free (0, revision);
333 
334       lim_free (0, fqvsn);
335       lim_free (0, tree_version);
336       lim_free (0, tree_root);
337     }
338   else
339     {
340       arch_check_directory (spec, 0);
341       answer = directory_as_cwd (spec);
342     }
343 
344   return answer;
345 }
346 
347 
348 static void
delta_callback(void * vfd,const char * fmt,va_list ap)349 delta_callback (void * vfd, const char * fmt, va_list ap)
350 {
351   int fd;
352 
353   fd = (int)(t_ulong)vfd;
354   safe_printfmt_va_list (fd, fmt, ap);
355   safe_flush (fd);
356 }
357 
358 
359 
360 
361 
362 /* tag: Tom Lord Wed Jun  4 13:44:58 2003 (cmd-revdelta.c)
363  */
364