summaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin/cvs/contrib/easy-import.pl
blob: e284216f6a899da2b88bec3ae4403f624c73e473 (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
#! xPERL_PATHx
#
# Support for importing a source collection into CVS.
# Tries to prevent the user from the most common pitfalls (like creating
# new top-level repositories or second-level areas accidentally), and
# cares to do some of the `dirty' work like maintaining the modules
# database accordingly.
#
# Written by Jörg Wunsch, 95/03/07, and placed in the public domain.
#
# $FreeBSD$

require "complete.pl";
require "getopts.pl";


sub scan_opts
{
    local($status);

    $status = &Getopts("nv");

    $dont_do_it = "-n" if $opt_n;
    if($opt_v) {
	print STDERR '$Source: /home/ncvs/src/gnu/usr.bin/cvs/contrib/easy-import.pl,v $ $Revision: 1.10 $' . "\n"; # 'emacs kludge
	exit 0;
    }
    die "usage: $0 [-v] [-n] [moduledir]\n" .
	"       -n: don't do any commit, show only\n" .
	"       -v: show program version\n"
	    unless $status && $#ARGV <= 0;

    if($#ARGV == 0) {
	$moduledir = $ARGV[0];
	shift;
    }
}

sub lsdir
{
    # find all subdirectories under @_
    # ignore all CVS entries, dot entries, and non-directories

    local($base) = @_;
    local(@ls, @rv, $fname);

    opendir(DIR, $base) || die "Cannot find dir $base.\n";

    @ls = readdir(DIR);
    closedir(DIR);

    @rv = ();

    foreach $fname (@ls) {
	next if $fname =~ /^CVS/ || $fname eq "Attic"
	    || $fname =~ /^\./ || ! -d "$base/$fname";
	@rv = (@rv, $fname);
    }

    return sort(@rv);
}


sub contains
{
    # look if the first parameter is contained in the list following it
    local($item, @list) = @_;
    local($found, $i);

    $found = 0;
    foreach $i (@list) {
	return 1 if $i eq $item;
    }
    return 0;
}



sub term_init
{
    # first, get some terminal attributes

    # try bold mode first
    $so = `tput md`; $se = `tput me`;

    # if no bold mode available, use standout mode
    if ($so eq "") {
	$so = `tput so`; $se = `tput se`;
    }

    # try if we can underscore
    $us = `tput us`; $ue = `tput ue`;
    # if we don't have it available, or same as bold/standout, disable it
    if ($us eq "" || $us eq $so) {
	$us = $ue = "";
    }

    # look how many columns we've got
    if($ENV{'COLUMNS'} ne "") {
	$columns = $ENV{'COLUMNS'};
    } elsif(-t STDIN) {		# if we operate on a terminal...
	local($word, $tmp);

	open(STTY, "stty -a|");
	$_ = <STTY>;		# try getting the tty win structure value
	close(STTY);
	chop;
	$columns = 0;
	foreach $word (split) {
	    $columns = $tmp if $word eq "columns;"; # the number preceding
	    $tmp = $word;
	}
    } else {
	$columns = 80;
    }
    # sanity
    $columns = 80 unless $columns >= 5;
}


sub list
{
    # pretty-print a list
    # imports: global variable $columns
    local(@items) = @_;
    local($longest,$i,$item,$cols,$width);

    # find the longest item
    $longest = 0;
    foreach $item (@items) {
	$i = length($item);
	$longest = $i if $longest < $i;
    }
    $width = $longest + 1;
    $cols = int($columns / $width);

    $i = 0;
    foreach $item (@items) {
	print $item;
	if(++$i == $cols) {
	    $i = 0; print "\n";
	} else {
	    print ' ' x ($width - length($item));
	}
    }
    print "\n" unless $i == 0;
}

sub cvs_init
{
    # get the CVS repository(s)

    die "You need to have the \$CVSROOT variable set.\n"
	unless $ENV{'CVSROOT'} ne "";

    # get the list of available repositories
    $cvsroot = $ENV{'CVSROOT'};
    @reps = &lsdir($cvsroot);
}


sub lsmodules
{
    # list all known CVS modules
    local(%rv, $mname, $mpath, $_);

    %rv = ();

    open(CVS, "cvs co -c|");
    while($_ = <CVS>) {
	chop;
	($mname,$mpath) = split;
	next if $mname eq "";
	$rv{$mname} = $mpath;
    }
    close(CVS);

    return %rv;
}


sub checktag
{
    # check a given string for tag rules
    local($s, $name) = @_;
    local($regexp);

    if($name eq "vendor") { $regexp = '^[A-Z][A-Z0-9_]*$'; }
    elsif($name eq "release") { $regexp = '^[a-z][a-z0-9_]*$'; }
    else {
	print STDERR "Internal error: unknown tag name $name\n";
	exit(2);
    }

    if($s !~ /$regexp/) {
	print "\a${us}Valid $name tags must match the regexp " .
	    "$regexp.${ue}\n";
	return 0;
    }
    if($s =~ /^RELENG/) {
	print "\a${us}Tags must not start with the word \"RELENG\".${ue}\n";
	return 0;
    }

    return 1;
}


&scan_opts;
&term_init;
&cvs_init;

if(! $moduledir) {
    @dirs = &lsdir(".");
    print "${so}Import from which directory?${se}\n";
    @dirs = (@dirs, ".");
    &list(@dirs);
    $moduledir = &Complete("Which? [.]: ", @dirs);
    $moduledir = "." unless $moduledir ne "";
}

chdir $moduledir || die "Cannot chdir to $moduledir\n";

print "${so}Available repositories:${se}\n";
&list(@reps);

# the following kludge prevents the Complete package from starting
# over with the string just selected; Complete should better provide
# some reinitialize method
$Complete'return = "";   $Complete'r = 0;

$selected =
    &Complete("Enter repository (<TAB>=complete, ^D=show): ",
	      @reps);

die "\aYou cannot create new repositories with this script.\n"
    unless &contains($selected, @reps);

$rep = $selected;

print "\n${so}Selected repository:${se} ${us}$rep${ue}\n";


@areas = &lsdir("$cvsroot/$rep");

print "${so}Existent areas in this repository:${se}\n";
&list(@areas);

$Complete'return = "";   $Complete'r = 0;

$selected =
    &Complete("Enter area name (<TAB>=complete, ^D=show): ",
	      @areas);

print "\a${us}Warning: this will create a new area.${ue}\n"
    unless &contains($selected, @areas);

$area = "$rep/$selected";

print "\n${so}[Working on:${se} ${us}$area${ue}${so}]${se}\n";

%cvsmods = &lsmodules();

for(;;) {
    $| = 1;
    print "${so}Gimme the module name:${se} ";
    $| = 0;
    $modname = <>;
    chop $modname;
    if ($modname eq "") {
	print "\a${us}You cannot use an empty module name.${ue}\n";
	next;
    }
    last if !$cvsmods{$modname};
    print "\a${us}This module name does already exist; do you intend to\n" .
	"perform a vendor-branch import to the existing sources?${ue}: ";
    $rep = <>;
    if ($rep =~ /\s*[yY]/) {
	($area,$modpath) = split(/\//,$cvsmods{$modname},2);
	$branchimport = 1;
	last;
    }
    print "${us}Choose another name.${ue}\n";
}


if(!$branchimport) {
    for(;;) {
	$| = 1;
	print "${so}Enter the module path:${se} $area/";
	$| = 0;
	$modpath = <>;
	chop $modpath;
	if ($modpath eq "") {
	    print "\a${us}You cannot use an empty module path.${ue}\n";
	    next;
	}
	last if ! -d "$cvsroot/$area/$modpath";
	print "\a${us}This module path does already exist; " .
	    "choose another one.${ue}\n";
    }


    @newdirs = ();
    $dir1 = "$cvsroot/$area";
    $dir2 = "$area";

    @newdirs = (@newdirs, "$dir2") if ! -d $dir1;

    foreach $ele (split(/\//, $modpath)) {
	$dir1 = "$dir1/$ele";
	$dir2 = "$dir2/$ele";
	@newdirs = (@newdirs, "$dir2") if ! -d $dir1;
    }

    print "${so}You're going to create the following new directories:${se}\n";

    &list(@newdirs);
}

for(;;) {
    $| = 1;
    print "${so}Enter a \`vendor\' tag (e. g. the authors ID):${se} ";
    $| = 0;
    $vtag = <>;
    chop $vtag;
    last if &checktag($vtag, "vendor");
}

for(;;) {
    $| = 1;
    print "${so}Enter a \`release\' tag (e. g. the version #):${se} ";
    $| = 0;
    $rtag = <>;
    chop $rtag;
    last if &checktag($rtag, "release");
}


$| = 1;
print "${so}This is your last chance to interrupt, " .
    "hit <return> to go on:${se} ";
$| = 0;
<>;

if (!$branchimport) {
    $mod = "";
    foreach $tmp (sort(keys(%cvsmods))) {
	if($tmp gt $modname) {
	    $mod = $tmp;
	    last;
	}
    }
    if($mod eq "") {
	# we are going to append our module
	$cmd = "\$\na\n";
    } else {
	# we can insert it
	$cmd = "/^${mod}[ \t]/\ni\n";
    }

    print "${so}Checking out the modules database...${se}\n";
    system("cvs co modules") && die "${us}failed.\n${ue}";

    print "${so}Inserting new module...${se}\n";
    open(ED, "|ed modules/modules") || die "${us}Cannot start ed${ue}\n";
    print(ED "${cmd}${modname} " . ' ' x (15 - length($modname)) .
	  "$area/${modpath}\n.\nw\nq\n");
    close(ED);

    print "${so}Commiting new modules database...${se}\n";
    system("cvs $dont_do_it commit -m \"  " .
	   "${modname} --> $area/${modpath}\" modules")
	&& die "Commit failed\n";

    system("cvs $dont_do_it -Q release -d modules");
}

print "${so}Importing source.  Enter a commit message in the editor.${se}\n";

system("cvs $dont_do_it import $area/$modpath $vtag $rtag");

print "${so}You are done now.  Go to a different directory, perform a${se}\n".
    "${us}cvs co ${modname}${ue} ${so}command, and see if your new module" .
    " builds ok.${se}\n";

print "\nPlease don't forget to edit the parent Makefile to add what you\n".
    "just imported.\n";

if($dont_do_it) {
print <<END


${so}Since you did not allow to commit anything, you'll have${se}
${so}to remove the edited modules' database yourself.${se}
${so}To do this, perform a${se}
${us}cd ${moduledir}; cvs -Q release -d modules${ue}
${so}command.${se}
END
;
}
OpenPOWER on IntegriCloud