1 /* cmd-join-branch.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/libarch/namespace.h"
20 #include "tla/libarch/project-tree.h"
21 #include "tla/libarch/my.h"
22 #include "tla/libarch/archive.h"
23 #include "tla/libarch/pristines.h"
24 #include "tla/libarch/build-revision.h"
25 #include "tla/libarch/patch-logs.h"
26 #include "tla/libarch/cmd.h"
27 #include "tla/libarch/cmd-replay.h"
28 #include "tla/libarch/copy-project-tree.h"
29 #include "tla/libarch/cmd-join-branch.h"
30 
31 
32 
33 static t_uchar * usage = "[options] version";
34 static t_uchar * version_string = (cfg__std__package " from regexps.com\n"
35                                    "\n"
36                                    "Copyright 2003 Tom Lord\n"
37                                    "\n"
38                                    "This is free software; see the source for copying conditions.\n"
39                                    "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n"
40                                    "PARTICULAR PURPOSE.\n"
41                                    "\n"
42                                    "Report bugs to " cfg__tla_bug_mail ".\n"
43                                    "\n"
44                                    cfg__std__release_id_string
45                                    "\n");
46 
47 #define OPTS(OP) \
48   OP (opt_help_msg, "h", "help", 0, \
49       "Display a help message and exit.") \
50   OP (opt_long_help, "H", 0, 0, \
51       "Display a verbose help message and exit.") \
52   OP (opt_version, "V", "version", 0, \
53       "Display a release identifier string\n" \
54       "and exit.") \
55   OP (opt_archive, "A", "archive", 1, \
56       "Override `my-default-archive'") \
57   OP (opt_dir, "d", "dir DIR", 1, \
58       "Operate on project tree in DIR (default `.')") \
59   OP (opt_dest, 0, "dest DEST", 1, \
60       "Instead of modifying the project tree in-place,\n" \
61       "make a copy of it to DEST and apply the result to that") \
62   OP (opt_unescaped, 0, "unescaped", 0, \
63       "show filenames in unescaped form")
64 
65 
66 t_uchar arch_cmd_join_branch_help[] = ("add a version as an ancestor of a project tree\n"
67 
68                                        "VERSION--base-0 must be a continuation (e.g. tag). The command replays the\n"
69                                        "changeset for VERSION--base-0 in the project tree, which has the effect of\n"
70                                        "adding the log for the branch (making the tag an ancestor of the resulting\n"
71                                        "tree).\n");
72 
73 enum options
74 {
75   OPTS (OPT_ENUM)
76 };
77 
78 static struct opt_desc opts[] =
79 {
80   OPTS (OPT_DESC)
81     {-1, 0, 0, 0, 0}
82 };
83 
84 
85 
86 int
arch_cmd_join_branch(t_uchar * program_name,int argc,char * argv[])87 arch_cmd_join_branch (t_uchar * program_name, int argc, char * argv[])
88 {
89   int o;
90   struct opt_parsed * option;
91   t_uchar * default_archive = 0;
92   t_uchar * upon = 0;
93   t_uchar * dest = 0;
94   int escape_classes = arch_escape_classes;
95 
96   safe_buffer_fd (1, 0, O_WRONLY, 0);
97 
98   option = 0;
99 
100   while (1)
101     {
102       o = opt_standard (lim_use_must_malloc, &option, opts, &argc, argv, program_name, usage, version_string, arch_cmd_join_branch_help, opt_help_msg, opt_long_help, opt_version);
103       if (o == opt_none)
104         break;
105       switch (o)
106         {
107         default:
108           safe_printfmt (2, "unhandled option `%s'\n", option->opt_string);
109           panic ("internal error parsing arguments");
110 
111         usage_error:
112           opt_usage (2, argv[0], program_name, usage, 1);
113           exit (1);
114 
115           /* bogus_arg: */
116           safe_printfmt (2, "ill-formed argument for `%s' (`%s')\n", option->opt_string, option->arg_string);
117           goto usage_error;
118 
119         case opt_archive:
120           {
121             lim_free (0, default_archive);
122             default_archive = str_save (0, option->arg_string);
123             break;
124           }
125 
126         case opt_dir:
127           {
128             lim_free (0, upon);
129             upon = str_save (0, option->arg_string);
130             break;
131           }
132 
133         case opt_dest:
134           {
135             lim_free (0, dest);
136             dest = str_save (0, option->arg_string);
137             break;
138           }
139 
140 	case opt_unescaped:
141 	  {
142 	    escape_classes = 0;
143 	    break;
144 	  }
145         }
146     }
147 
148   if (argc != 2)
149     goto usage_error;
150 
151   if (default_archive && !arch_valid_archive_name (default_archive))
152     {
153       safe_printfmt (2, "%s: invalid archive name (%s)\n",
154                      argv[0], default_archive);
155       exit (1);
156     }
157 
158   {
159     t_uchar * vsn_spec;
160     t_uchar * archive = 0;
161     t_uchar * version = 0;
162     t_uchar * base0_revision = 0;
163     t_uchar * fqbase0 = 0;
164     t_uchar * upon_root = 0;
165     struct arch_archive * arch = 0;
166 
167     if (! upon)
168       upon = str_save (0, ".");
169 
170     vsn_spec = argv[1];
171 
172     if (!arch_valid_package_name (vsn_spec, arch_maybe_archive, arch_req_version, 0))
173       {
174         safe_printfmt (2, "%s: invalid version name (%s)\n",
175                        argv[0], vsn_spec);
176         exit (1);
177       }
178 
179     archive = arch_parse_package_name (arch_ret_archive, default_archive, vsn_spec);
180     version = arch_parse_package_name (arch_ret_non_archive, 0, vsn_spec);
181     base0_revision = str_alloc_cat (0, version, "--base-0");
182     fqbase0 = arch_fully_qualify (archive, base0_revision);
183 
184     upon_root = arch_tree_root (0, upon, 0);
185     if (!upon_root)
186       {
187         safe_printfmt (2, "%s: not in a project tree -- %s\n",
188                        argv[0], upon);
189         exit (1);
190       }
191 
192     if (arch_tree_has_log (upon_root, archive, base0_revision))
193       {
194         safe_printfmt (2, "%s: tree already has log for %s/%s\n",
195                        argv[0], archive, base0_revision);
196         exit (1);
197       }
198 
199     arch = arch_archive_connect (archive, 0);
200 
201     {
202       enum arch_revision_type type;
203 
204       arch_revision_type (&type, 0, arch, base0_revision);
205 
206       if (type != arch_continuation_revision)
207         {
208           safe_printfmt (2, "%s: not a tag revision (%s/%s)\n",
209                          argv[0], archive, base0_revision);
210           exit (1);
211         }
212     }
213 
214 
215     if (dest)
216       {
217         safe_printfmt (1, "* copying %s to %s\n", upon, dest);
218         safe_flush (1);
219         arch_copy_project_tree (upon, dest, 1, 1);
220       }
221     else
222       dest = str_save (0, upon);
223 
224     arch_call_cmd (arch_cmd_replay, argv[0], "--dir", dest, fqbase0, ((escape_classes == 0) ? "--unescaped" : 0), (char*)0);
225 
226 
227     lim_free (0, archive);
228     lim_free (0, version);
229     lim_free (0, base0_revision);
230     lim_free (0, fqbase0);
231 
232     arch_archive_close (arch);
233   }
234 
235   lim_free (0, upon);
236   lim_free (0, dest);
237   lim_free (0, default_archive);
238 
239   return 0;
240 }
241 
242 
243 
244 
245 /* tag: Tom Lord Wed Jun  4 18:23:19 2003 (cmd-join-branch.c)
246  */
247