1 | #!/usr/bin/perl -w
|
---|
2 |
|
---|
3 | # $Id: dasscm 239 2008-10-08 22:07:00Z joergs $
|
---|
4 |
|
---|
5 | use strict;
|
---|
6 |
|
---|
7 | use Env
|
---|
8 | qw($DASSCM_PROD $DASSCM_REPO $USER $DASSCM_USERNAME $DASSCM_USER $DASSCM_PASSWORD $SHELL);
|
---|
9 | use Cwd;
|
---|
10 | use Getopt::Long;
|
---|
11 | use File::Basename;
|
---|
12 | use File::Compare;
|
---|
13 | use File::Copy;
|
---|
14 | use File::Find;
|
---|
15 | use File::stat;
|
---|
16 | use File::Path;
|
---|
17 | use Term::ReadKey;
|
---|
18 |
|
---|
19 | #use Data::Dumper;
|
---|
20 |
|
---|
21 | #####################################################################
|
---|
22 | #
|
---|
23 | # global
|
---|
24 | #
|
---|
25 |
|
---|
26 | # file to store permissions
|
---|
27 | my $permissions_file = "/etc/permissions.d/dasscm.permission_backup";
|
---|
28 |
|
---|
29 | # documentation file (for usage)
|
---|
30 | my $doc_file = "/usr/share/doc/packages/dasscm/dasscm_howto.txt";
|
---|
31 |
|
---|
32 | # configuration file
|
---|
33 | my $config_file = "/etc/dasscm.conf";
|
---|
34 | my $config = get_config($config_file);
|
---|
35 | my $DASSCM_LOCAL_REPOSITORY_BASE;
|
---|
36 | my $DASSCM_REPOSITORY_NAME;
|
---|
37 | my $DASSCM_SVN_REPOSITORY;
|
---|
38 |
|
---|
39 | # current directory at program start
|
---|
40 | my $StartDirectory = cwd();
|
---|
41 |
|
---|
42 | my $SVN = "svn ";
|
---|
43 | my $svnOptions = "";
|
---|
44 | my $svnCheckoutCredentials = "";
|
---|
45 | my $svnPasswordCredentials = "";
|
---|
46 | my $diff = "diff --exclude .svn ";
|
---|
47 |
|
---|
48 | # command line options get stored in options hash
|
---|
49 | my %options = ();
|
---|
50 |
|
---|
51 | # subcommand, that gets executed (add, commit, ...)
|
---|
52 | my $command;
|
---|
53 |
|
---|
54 | my $verbose = 0;
|
---|
55 |
|
---|
56 | #####################################################################
|
---|
57 | #
|
---|
58 | # util functions
|
---|
59 | #
|
---|
60 | sub usage()
|
---|
61 | {
|
---|
62 | print "usage: dasscm <subcommand> [options] [args]\n";
|
---|
63 | print "\n";
|
---|
64 | print "dasscm is intended to help versioning configuration files\n";
|
---|
65 | print "\n";
|
---|
66 | print "Available subcommands:\n";
|
---|
67 | print " help <subcommand>\n";
|
---|
68 | print " init\n";
|
---|
69 | print " login\n";
|
---|
70 | print " up\n";
|
---|
71 | print " ls\n";
|
---|
72 | print " add <filename>\n";
|
---|
73 | print " commit <filename>\n";
|
---|
74 | print " diff <filename>\n";
|
---|
75 | print " status\n";
|
---|
76 | print " permissions\n";
|
---|
77 | print "\n";
|
---|
78 | print "preparation:\n", " if dasscm is already configured,\n",
|
---|
79 | " use 'dasscm login' and then eg. 'add'.\n",
|
---|
80 | " The environment variables\n", " DASSCM_REPO\n", " DASSCM_PROD\n",
|
---|
81 | " DASSCM_USERNAME\n", " DASSCM_PASSWORD\n",
|
---|
82 | " are evaluated, but set automatically by 'dasscm login'.\n", "\n",
|
---|
83 | " If dasscm is not yet configured, read", " $doc_file\n";
|
---|
84 | }
|
---|
85 |
|
---|
86 | sub warning(@)
|
---|
87 | {
|
---|
88 | print "Warning: " . join( "\n ", @_ ) . "\n";
|
---|
89 | }
|
---|
90 |
|
---|
91 | sub error(@)
|
---|
92 | {
|
---|
93 | print "Error: " . join( "\n ", @_ ) . "\n";
|
---|
94 | }
|
---|
95 |
|
---|
96 | sub fatalerror(@)
|
---|
97 | {
|
---|
98 | error(@_);
|
---|
99 |
|
---|
100 | #print "Exiting\n";
|
---|
101 | exit 1;
|
---|
102 | }
|
---|
103 |
|
---|
104 | #
|
---|
105 | # reading config file and return key/value pairs as hash
|
---|
106 | #
|
---|
107 | sub get_config
|
---|
108 | {
|
---|
109 | my $file = $_[0];
|
---|
110 |
|
---|
111 | if ( !$file ) {
|
---|
112 | fatalerror( "failed to open config file" . $file );
|
---|
113 | }
|
---|
114 |
|
---|
115 | my $data = {};
|
---|
116 |
|
---|
117 | # try to open config file
|
---|
118 | if ( !open( FH, $file ) ) {
|
---|
119 | fatalerror( "failed to open config file" . $file );
|
---|
120 | } else {
|
---|
121 | while (<FH>) {
|
---|
122 | chomp;
|
---|
123 | if (/^#/) {
|
---|
124 | next;
|
---|
125 | }
|
---|
126 | if ( $_ =~ /=/g ) {
|
---|
127 |
|
---|
128 | # splitting in 2 fields at maximum
|
---|
129 | my ( $option, $value ) = split( /=/, $_, 2 );
|
---|
130 | $option =~ s/^\s+//g;
|
---|
131 | $option =~ s/\s+$//g;
|
---|
132 | $option =~ s/\"+//g;
|
---|
133 | $value =~ s/^\s+//g;
|
---|
134 | $value =~ s/\s+$//g;
|
---|
135 | $value =~ s/\"+//g;
|
---|
136 |
|
---|
137 | if ( length($option) ) {
|
---|
138 | $data->{$option} = $value;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|
143 | close(FH);
|
---|
144 |
|
---|
145 | return $data;
|
---|
146 | }
|
---|
147 |
|
---|
148 | sub check_env()
|
---|
149 | {
|
---|
150 |
|
---|
151 | # DASSCM_PROD
|
---|
152 | if ( !$DASSCM_PROD ) {
|
---|
153 | $DASSCM_PROD = "/";
|
---|
154 | }
|
---|
155 |
|
---|
156 | if ( !-d $DASSCM_PROD ) {
|
---|
157 | die "DASSCM_PROD ($DASSCM_PROD) is not set to a directory.\n";
|
---|
158 | }
|
---|
159 | if ($verbose) { print "DASSCM_PROD: " . $DASSCM_PROD . "\n"; }
|
---|
160 |
|
---|
161 | # DASSCM_REPOSITORY_NAME
|
---|
162 | if ( !$DASSCM_REPOSITORY_NAME ) {
|
---|
163 | die
|
---|
164 | "Variable DASSCM_REPOSITORY_NAME is not defined.\nIt needs to be a unique name.\nNormally the full qualified host name is used.\nUse file $config_file to configure it.\n";
|
---|
165 | }
|
---|
166 |
|
---|
167 | # DASSCM_REPO
|
---|
168 | if ( !$DASSCM_REPO ) {
|
---|
169 | if ( $DASSCM_LOCAL_REPOSITORY_BASE && $DASSCM_REPOSITORY_NAME ) {
|
---|
170 | $DASSCM_REPO =
|
---|
171 | $DASSCM_LOCAL_REPOSITORY_BASE . "/" . $DASSCM_REPOSITORY_NAME;
|
---|
172 | } else {
|
---|
173 | die
|
---|
174 | "Envirnonment variable DASSCM_REPO not set.\nSet DASSCM_REPO to the directory of the versioning system checkout for this machine.\n";
|
---|
175 | }
|
---|
176 | }
|
---|
177 | if ($verbose) { print "DASSCM_REPO: " . $DASSCM_REPO . "\n"; }
|
---|
178 |
|
---|
179 | #
|
---|
180 | # check if local repository directory exist
|
---|
181 | # (if not creating by init)
|
---|
182 | #
|
---|
183 | if ( $command ne "init" ) {
|
---|
184 | if ( not -d $DASSCM_REPO ) {
|
---|
185 | die
|
---|
186 | "Can't access local repository DASSCM_REPO\n($DASSCM_REPO)\nCheck configuration and execute\n dasscm init\n";
|
---|
187 | }
|
---|
188 |
|
---|
189 | #
|
---|
190 | # user settings
|
---|
191 | #
|
---|
192 |
|
---|
193 | # DASSCM_USER is legacy. Use DASSCM_USERNAME instead
|
---|
194 | if ( !$DASSCM_USERNAME ) {
|
---|
195 | $DASSCM_USERNAME = $DASSCM_USER;
|
---|
196 | }
|
---|
197 |
|
---|
198 | # user root is not allowed for checkins.
|
---|
199 | # if user is root, DASSCM_USER has to be set,
|
---|
200 | # otherwise USER can be used
|
---|
201 | if ( "$USER" eq "root" ) {
|
---|
202 | if ( ( not $DASSCM_USERNAME ) and ( $command ne "login" ) ) {
|
---|
203 | die
|
---|
204 | "Envirnonment variable DASSCM_USERNAME not set.\nSet DASSCM_USERNAME to your subversion user account.\n";
|
---|
205 | }
|
---|
206 | $svnOptions .= " --no-auth-cache ";
|
---|
207 | } elsif ( !$DASSCM_USERNAME ) {
|
---|
208 | $DASSCM_USERNAME = $USER;
|
---|
209 | }
|
---|
210 |
|
---|
211 | #
|
---|
212 | # password
|
---|
213 | #
|
---|
214 | if ($DASSCM_PASSWORD) {
|
---|
215 | $svnPasswordCredentials = " --password $DASSCM_PASSWORD ";
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | #$svnOptions .= " --username $DASSCM_USERNAME "
|
---|
220 | }
|
---|
221 |
|
---|
222 | sub check_parameter(@)
|
---|
223 | {
|
---|
224 | }
|
---|
225 |
|
---|
226 | #
|
---|
227 | # generate from (relative) filename
|
---|
228 | # all required file and directory names:
|
---|
229 | # $basename, $dirname_prod, $dirname_repo,
|
---|
230 | # $filename_prod, $filename_repo
|
---|
231 | #
|
---|
232 | sub get_filenames(@)
|
---|
233 | {
|
---|
234 | my $filename_prod = $_[0];
|
---|
235 |
|
---|
236 | # make filename absolut
|
---|
237 | if ( !( $filename_prod =~ m/^\// ) ) {
|
---|
238 | $filename_prod = cwd() . "/" . $filename_prod;
|
---|
239 | }
|
---|
240 |
|
---|
241 | if ( not -r $filename_prod ) {
|
---|
242 | fatalerror( $filename_prod . " is not accessable" );
|
---|
243 | }
|
---|
244 |
|
---|
245 | # TODO: dirname buggy: eg. "/etc/" is reduced to "/",
|
---|
246 | # "/etc" is used as filename
|
---|
247 | my $dirname_prod = dirname($filename_prod);
|
---|
248 |
|
---|
249 | # uses chdir to determine real directory in a unique way
|
---|
250 | chdir $dirname_prod or die $!;
|
---|
251 | $dirname_prod = cwd();
|
---|
252 | chdir $StartDirectory;
|
---|
253 |
|
---|
254 | my $basename = basename($filename_prod);
|
---|
255 |
|
---|
256 | if ($verbose) {
|
---|
257 | print "dir: " . $dirname_prod . "\n";
|
---|
258 | print "fn: " . $basename . "\n";
|
---|
259 | }
|
---|
260 |
|
---|
261 | my $dirname_repo = $DASSCM_REPO . "/" . $dirname_prod;
|
---|
262 | my $filename_repo = "$dirname_repo/$basename";
|
---|
263 |
|
---|
264 | return (
|
---|
265 | $basename, $dirname_prod, $dirname_repo,
|
---|
266 | $filename_prod, $filename_repo
|
---|
267 | );
|
---|
268 | }
|
---|
269 |
|
---|
270 | sub generatePermissionList
|
---|
271 | {
|
---|
272 |
|
---|
273 | # generieren der Zeilen für Permission-Savefile
|
---|
274 | my @files = @_;
|
---|
275 | my @permlist = ();
|
---|
276 | foreach my $file (@files) {
|
---|
277 | $file = "/" . $file;
|
---|
278 | if ( -e $file ) {
|
---|
279 | my $info = stat($file) || die "failed to stat $file: aborting";
|
---|
280 | my $mode = get_type( $info->mode ) & 07777;
|
---|
281 | my $modestring = sprintf( "%04o", $mode );
|
---|
282 | my $uid = $info->uid;
|
---|
283 | my $uidname = getpwuid($uid);
|
---|
284 | my $gid = $info->gid;
|
---|
285 | my $gidname = getgrgid($gid);
|
---|
286 | push(
|
---|
287 | @permlist,
|
---|
288 | sprintf( "%-55s %-17s %4d",
|
---|
289 | $file, "${uidname}:${gidname}", $modestring )
|
---|
290 | );
|
---|
291 | } else {
|
---|
292 | print
|
---|
293 | "failed to get status of $file. It exists in the repository, but not on the system\n";
|
---|
294 | }
|
---|
295 | }
|
---|
296 | return @permlist;
|
---|
297 | }
|
---|
298 |
|
---|
299 | sub get_type
|
---|
300 | {
|
---|
301 |
|
---|
302 | # Funktion übernommen aus /usr/bin/chkstat
|
---|
303 | my $S_IFLNK = 0120000; # symbolic link
|
---|
304 | my $S_IFREG = 0100000; # regular file
|
---|
305 | my $S_IFDIR = 0040000; # directory
|
---|
306 | my $S_IFCHAR = 0020000; # character device
|
---|
307 | my $S_IFBLK = 0060000; # block device
|
---|
308 | my $S_IFFIFO = 0010000; # fifo
|
---|
309 | my $S_IFSOCK = 0140000; # socket
|
---|
310 | my $S_IFMT = 0170000; # type of file
|
---|
311 |
|
---|
312 | my $S_m;
|
---|
313 | if ( ( $_[0] & $S_IFMT ) == $S_IFLNK ) { $S_m = $_[0] - $S_IFLNK; }
|
---|
314 | elsif ( ( $_[0] & $S_IFMT ) == $S_IFREG ) { $S_m = $_[0] - $S_IFREG; }
|
---|
315 | elsif ( ( $_[0] & $S_IFMT ) == $S_IFDIR ) { $S_m = $_[0] - $S_IFDIR; }
|
---|
316 | elsif ( ( $_[0] & $S_IFMT ) == $S_IFCHAR ) { $S_m = $_[0] - $S_IFCHAR; }
|
---|
317 | elsif ( ( $_[0] & $S_IFMT ) == $S_IFBLK ) { $S_m = $_[0] - $S_IFBLK; }
|
---|
318 | elsif ( ( $_[0] & $S_IFMT ) == $S_IFFIFO ) { $S_m = $_[0] - $S_IFFIFO; }
|
---|
319 | elsif ( ( $_[0] & $S_IFMT ) == $S_IFSOCK ) { $S_m = $_[0] - $S_IFSOCK; }
|
---|
320 | $S_m;
|
---|
321 | }
|
---|
322 |
|
---|
323 | sub run_command
|
---|
324 | {
|
---|
325 | my $command = shift;
|
---|
326 |
|
---|
327 | #print "executing command: " . $command . "\n";
|
---|
328 |
|
---|
329 | open( RESULT, $command . ' 2>&1 |' );
|
---|
330 | my @result = <RESULT>;
|
---|
331 | close(RESULT);
|
---|
332 | my $retcode = $? >> 8;
|
---|
333 |
|
---|
334 | #print @result;
|
---|
335 | #if( $retcode ) { print "return code: " . $retcode . "\n"; }
|
---|
336 |
|
---|
337 | return ( $retcode, @result );
|
---|
338 | }
|
---|
339 |
|
---|
340 | sub run_interactive
|
---|
341 | {
|
---|
342 |
|
---|
343 | if ($verbose) {
|
---|
344 | print "run_interactive:" . join( " ", @_ ) . "\n";
|
---|
345 | }
|
---|
346 |
|
---|
347 | system(@_);
|
---|
348 | if ( $? == -1 ) {
|
---|
349 | printf "failed to execute: $!\n";
|
---|
350 | } elsif ( $? & 127 ) {
|
---|
351 | printf "child died with signal %d, %s coredump\n", ( $? & 127 ),
|
---|
352 | ( $? & 128 ) ? 'with' : 'without';
|
---|
353 | } elsif ( $? >> 8 != 0 ) {
|
---|
354 | printf "child exited with value %d\n", $? >> 8;
|
---|
355 | }
|
---|
356 | return ( $? >> 8 );
|
---|
357 | }
|
---|
358 |
|
---|
359 | sub svn_check_credentials( $$ )
|
---|
360 | {
|
---|
361 | my $username = shift;
|
---|
362 | my $password = shift;
|
---|
363 |
|
---|
364 | print "checking credentials ... ";
|
---|
365 |
|
---|
366 | if ( !$username ) {
|
---|
367 | fatalerror("no username given");
|
---|
368 | }
|
---|
369 |
|
---|
370 | if ( !$password ) {
|
---|
371 | fatalerror("no password given");
|
---|
372 | }
|
---|
373 |
|
---|
374 | # Options for "svn info" are not supported by subversion 1.0.0 (SLES9),
|
---|
375 | # therefore switching to "svn status"
|
---|
376 | # ( my $rc_update, my @result ) =
|
---|
377 | # run_command(
|
---|
378 | # "$SVN info --non-interactive --no-auth-cache --username $username --password $password $DASSCM_SVN_REPOSITORY"
|
---|
379 | # );
|
---|
380 | #print @result;
|
---|
381 |
|
---|
382 | ( my $rc_update, my @result ) =
|
---|
383 | run_command(
|
---|
384 | "$SVN ls --non-interactive --no-auth-cache --username $username --password $password $DASSCM_SVN_REPOSITORY"
|
---|
385 | );
|
---|
386 |
|
---|
387 | if ( $rc_update != 0 ) {
|
---|
388 | print "\n", @result;
|
---|
389 | fatalerror();
|
---|
390 | }
|
---|
391 |
|
---|
392 | }
|
---|
393 |
|
---|
394 | sub svn_update( ;$ )
|
---|
395 | {
|
---|
396 | my $update_path = shift || $DASSCM_REPO;
|
---|
397 | ( my $rc_update, my @result ) =
|
---|
398 | run_command(
|
---|
399 | "$SVN update --non-interactive $svnCheckoutCredentials $update_path");
|
---|
400 | print @result;
|
---|
401 | if ( $rc_update != 0 ) {
|
---|
402 | fatalerror();
|
---|
403 | }
|
---|
404 | }
|
---|
405 |
|
---|
406 | sub svn_getStoredFiles( ;$ )
|
---|
407 | {
|
---|
408 |
|
---|
409 | # TODO: get_filenames?
|
---|
410 | #my $rel_path = shift || "";
|
---|
411 | #my $path = "${DASSCM_REPO}/${rel_path}";
|
---|
412 | my $path = ${DASSCM_REPO};
|
---|
413 |
|
---|
414 | # svn ls -R is better, but much, much slower
|
---|
415 | # ( my $rc, my @result ) = run_command("$SVN ls --recursive $svnCheckoutCredentials $path");
|
---|
416 | ( my $rc, my @result ) =
|
---|
417 | run_command(
|
---|
418 | "cd $path && find | grep -v '/.svn' | sed -e 's/\.\\///' | grep -v '^\$'"
|
---|
419 | );
|
---|
420 | if ( $rc != 0 ) {
|
---|
421 | print @result;
|
---|
422 | fatalerror;
|
---|
423 | }
|
---|
424 | chomp(@result);
|
---|
425 | return @result;
|
---|
426 | }
|
---|
427 |
|
---|
428 | #
|
---|
429 | # from an array of files/dirs,
|
---|
430 | # generates list of files
|
---|
431 | # sorted by type
|
---|
432 | #
|
---|
433 | sub get_files( @ )
|
---|
434 | {
|
---|
435 | my @files = ();
|
---|
436 | my @links = ();
|
---|
437 | my @dirs = ();
|
---|
438 | my @others = ();
|
---|
439 |
|
---|
440 | if (@_) {
|
---|
441 | find(
|
---|
442 | {
|
---|
443 | wanted => sub {
|
---|
444 | my $fullname = cwd() . "/" . $_;
|
---|
445 | if ( -l $_ ) {
|
---|
446 |
|
---|
447 | # soft link
|
---|
448 | # important: check for links first
|
---|
449 | # to exclude them from further checks
|
---|
450 | push( @links, $fullname );
|
---|
451 | } elsif ( -d $_ ) {
|
---|
452 | push( @dirs, $fullname );
|
---|
453 | } elsif ( -f $_ ) {
|
---|
454 |
|
---|
455 | # regular file
|
---|
456 | push( @files, $fullname );
|
---|
457 | } else {
|
---|
458 | push( @others, $fullname );
|
---|
459 | }
|
---|
460 | }
|
---|
461 | },
|
---|
462 | @_
|
---|
463 | );
|
---|
464 | }
|
---|
465 |
|
---|
466 | # don't rely on others.
|
---|
467 | # If more specific file types are needed,
|
---|
468 | # they will be added
|
---|
469 | return {
|
---|
470 | files => \@files,
|
---|
471 | links => \@links,
|
---|
472 | dirs => \@dirs,
|
---|
473 | others => \@others
|
---|
474 | };
|
---|
475 | }
|
---|
476 |
|
---|
477 | #####################################################################
|
---|
478 | #
|
---|
479 | # functions
|
---|
480 |
|
---|
481 | sub help(;@)
|
---|
482 | {
|
---|
483 | if ( @_ == 0 ) {
|
---|
484 | usage();
|
---|
485 | } else {
|
---|
486 | print "help for @_: ...\n";
|
---|
487 | usage();
|
---|
488 | }
|
---|
489 | }
|
---|
490 |
|
---|
491 | sub login(@)
|
---|
492 | {
|
---|
493 | check_parameter( @_, 1 );
|
---|
494 | check_env();
|
---|
495 |
|
---|
496 | my $input_username = $_[0];
|
---|
497 |
|
---|
498 | if ( not $input_username ) {
|
---|
499 | my $output_username = "";
|
---|
500 | if ($DASSCM_USERNAME) {
|
---|
501 | $output_username = " ($DASSCM_USERNAME)";
|
---|
502 | }
|
---|
503 |
|
---|
504 | print "Enter DASSCM user name", $output_username, ": ";
|
---|
505 | $input_username = <STDIN>;
|
---|
506 | chomp($input_username);
|
---|
507 | }
|
---|
508 |
|
---|
509 | # hidden password input
|
---|
510 | print "Enter DASSCM user password: ";
|
---|
511 | ReadMode('noecho');
|
---|
512 | my $input_password = <STDIN>;
|
---|
513 | ReadMode('normal');
|
---|
514 | chomp($input_password);
|
---|
515 | print "\n";
|
---|
516 |
|
---|
517 | svn_check_credentials( $input_username || $DASSCM_USERNAME,
|
---|
518 | $input_password );
|
---|
519 |
|
---|
520 | #
|
---|
521 | # set environment variables
|
---|
522 | #
|
---|
523 | $ENV{'DASSCM_USERNAME'} = $input_username;
|
---|
524 | $ENV{'DASSCM_PASSWORD'} = $input_password;
|
---|
525 |
|
---|
526 | print "subversion access okay\n\n", "DASSCM_USERNAME: $input_username\n",
|
---|
527 | "DASSCM_PASSWORD: (hidden)\n", "DASSCM_PROD: $DASSCM_PROD\n",
|
---|
528 | "DASSCM_REPO: $DASSCM_REPO\n",
|
---|
529 | "Server Repository: $DASSCM_SVN_REPOSITORY\n", "\n", "[dasscm shell]\n\n";
|
---|
530 |
|
---|
531 | my $shell = $SHELL || "bash";
|
---|
532 | exec($shell) or die "failed to start new shell";
|
---|
533 | }
|
---|
534 |
|
---|
535 | sub init(@)
|
---|
536 | {
|
---|
537 | check_parameter( @_, 1 );
|
---|
538 | check_env();
|
---|
539 |
|
---|
540 | # don't do repository creation (svn mkdir) here,
|
---|
541 | # because then their must be a lot of prior checks
|
---|
542 |
|
---|
543 | # update complete repository
|
---|
544 | # and create permission file
|
---|
545 | my $retcode =
|
---|
546 | run_interactive(
|
---|
547 | "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY; touch $permissions_file"
|
---|
548 | );
|
---|
549 | }
|
---|
550 |
|
---|
551 | sub ls(@)
|
---|
552 | {
|
---|
553 | check_parameter( @_, 1 );
|
---|
554 | check_env();
|
---|
555 |
|
---|
556 | my @files = svn_getStoredFiles(@_);
|
---|
557 |
|
---|
558 | print join( "\n", @files );
|
---|
559 | print "\n";
|
---|
560 | }
|
---|
561 |
|
---|
562 | sub update(@)
|
---|
563 | {
|
---|
564 | check_parameter( @_, 1 );
|
---|
565 | check_env();
|
---|
566 |
|
---|
567 | #
|
---|
568 | # update local repository
|
---|
569 | #
|
---|
570 | svn_update();
|
---|
571 | }
|
---|
572 |
|
---|
573 | #
|
---|
574 | # helper function for "add" command
|
---|
575 | #
|
---|
576 | sub add_helper(@)
|
---|
577 | {
|
---|
578 | (
|
---|
579 | my $basename,
|
---|
580 | my $dirname_prod,
|
---|
581 | my $dirname_repo,
|
---|
582 | my $filename_prod,
|
---|
583 | my $filename_repo
|
---|
584 | )
|
---|
585 | = get_filenames( $_[0] );
|
---|
586 |
|
---|
587 | if ( $command eq "add" ) {
|
---|
588 | mkpath($dirname_repo);
|
---|
589 | }
|
---|
590 |
|
---|
591 | # TODO: are permissions also copied?
|
---|
592 | copy( $filename_prod, $filename_repo )
|
---|
593 | or error "failed to copy $filename_prod to repository: $!";
|
---|
594 |
|
---|
595 | if ( $command eq "add" ) {
|
---|
596 |
|
---|
597 | # already checked in?
|
---|
598 | chdir $DASSCM_REPO;
|
---|
599 |
|
---|
600 | # also add the path to filename.
|
---|
601 | for my $dir ( split( '/', $dirname_prod ) ) {
|
---|
602 | if ($dir) {
|
---|
603 | my ( $rc, @out ) =
|
---|
604 | run_command( "$SVN add --non-recursive \"" . $dir . "\"" );
|
---|
605 | if ( $rc > 0 ) {
|
---|
606 | print join( "\n", @out );
|
---|
607 | }
|
---|
608 | chdir $dir;
|
---|
609 | }
|
---|
610 | }
|
---|
611 | my ( $rc, @out ) = run_command( "$SVN add \"" . $basename . "\"" );
|
---|
612 | if ( $rc > 0 ) {
|
---|
613 | print join( "\n", @out );
|
---|
614 | }
|
---|
615 | chdir $StartDirectory;
|
---|
616 | }
|
---|
617 | }
|
---|
618 |
|
---|
619 | #
|
---|
620 | # add (is used for command add and commit)
|
---|
621 | #
|
---|
622 | sub add(@)
|
---|
623 | {
|
---|
624 | check_parameter( @_, 1 );
|
---|
625 | check_env();
|
---|
626 |
|
---|
627 | #
|
---|
628 | # update local repository
|
---|
629 | #
|
---|
630 | svn_update();
|
---|
631 |
|
---|
632 | # get all regular files and links
|
---|
633 | my $href_files = get_files(@_);
|
---|
634 |
|
---|
635 | #print Dumper( $href_files );
|
---|
636 |
|
---|
637 | my @files = @{ $href_files->{files} };
|
---|
638 | my @links = @{ $href_files->{links} };
|
---|
639 |
|
---|
640 | if (@files) {
|
---|
641 | my $number = $#files + 1;
|
---|
642 | print "files to check-in ($number): \n";
|
---|
643 | print join( "\n", @files );
|
---|
644 | print "\n";
|
---|
645 | }
|
---|
646 |
|
---|
647 | if (@links) {
|
---|
648 | my $number = $#links + 1;
|
---|
649 | print "\n";
|
---|
650 | print "ignoring links ($number):\n";
|
---|
651 | print join( "\n", @links );
|
---|
652 | print "\n";
|
---|
653 | }
|
---|
654 |
|
---|
655 | # TODO: confirm
|
---|
656 |
|
---|
657 | # copy files one by one to local repository
|
---|
658 | for my $file (@files) {
|
---|
659 |
|
---|
660 | # add file
|
---|
661 | add_helper($file);
|
---|
662 | }
|
---|
663 |
|
---|
664 | # create new permissions file
|
---|
665 | permissions();
|
---|
666 |
|
---|
667 | # add permissions file
|
---|
668 | add_helper($permissions_file);
|
---|
669 |
|
---|
670 | if ( $options{'message'} ) {
|
---|
671 | $svnOptions .= " --message \"$options{'message'}\" ";
|
---|
672 | }
|
---|
673 |
|
---|
674 | # commit calls $EDITOR.
|
---|
675 | # use "interactive" here, to display output
|
---|
676 | my $retcode =
|
---|
677 | run_interactive(
|
---|
678 | "$SVN commit $svnOptions --username $DASSCM_USERNAME $svnPasswordCredentials $DASSCM_REPO"
|
---|
679 | );
|
---|
680 |
|
---|
681 | #print $filename_prod. "\n";
|
---|
682 | #print $dirname_repo. "\n";
|
---|
683 | }
|
---|
684 |
|
---|
685 | sub blame(@)
|
---|
686 | {
|
---|
687 | check_parameter( @_, 1 );
|
---|
688 | check_env();
|
---|
689 |
|
---|
690 | (
|
---|
691 | my $basename,
|
---|
692 | my $dirname_prod,
|
---|
693 | my $dirname_repo,
|
---|
694 | my $filename_prod,
|
---|
695 | my $filename_repo
|
---|
696 | )
|
---|
697 | = get_filenames( $_[0] );
|
---|
698 |
|
---|
699 | my $retcode = run_interactive("$SVN blame $svnOptions $filename_repo");
|
---|
700 | }
|
---|
701 |
|
---|
702 | sub diff(@)
|
---|
703 | {
|
---|
704 | check_parameter( @_, 1 );
|
---|
705 | check_env();
|
---|
706 |
|
---|
707 | (
|
---|
708 | my $basename,
|
---|
709 | my $dirname_prod,
|
---|
710 | my $dirname_repo,
|
---|
711 | my $filename_prod,
|
---|
712 | my $filename_repo
|
---|
713 | )
|
---|
714 | = get_filenames( $_[0] );
|
---|
715 |
|
---|
716 | #print "$basename,$dirname_prod,$dirname_repo\n";
|
---|
717 |
|
---|
718 | ( my $rc_update, my @result ) = run_command("$SVN update $filename_repo");
|
---|
719 | if ( $rc_update != 0 ) {
|
---|
720 | print @result;
|
---|
721 | die;
|
---|
722 | }
|
---|
723 |
|
---|
724 | ( my $rc_diff, my @diff_result ) =
|
---|
725 | run_command( $diff . " $filename_repo $filename_prod" );
|
---|
726 |
|
---|
727 | print @diff_result;
|
---|
728 | }
|
---|
729 |
|
---|
730 | sub status(@)
|
---|
731 | {
|
---|
732 | check_parameter( @_, 1 );
|
---|
733 | check_env();
|
---|
734 |
|
---|
735 | #
|
---|
736 | # update local repository
|
---|
737 | #
|
---|
738 | svn_update();
|
---|
739 |
|
---|
740 | # TODO: start at subdirectories ?
|
---|
741 | my $dir = $DASSCM_REPO;
|
---|
742 | my @files = svn_getStoredFiles($dir);
|
---|
743 |
|
---|
744 | # Liste der geänderten Files ausgeben, falls nicht leer
|
---|
745 | if (@files) {
|
---|
746 |
|
---|
747 | # stores result from status (cvscheck)
|
---|
748 | my %removedfiles = ();
|
---|
749 | my %changedfiles = ();
|
---|
750 |
|
---|
751 | foreach my $file (@files) {
|
---|
752 |
|
---|
753 | my $realfile = "/" . $file;
|
---|
754 | my $cvsworkfile = "${DASSCM_REPO}/${file}";
|
---|
755 |
|
---|
756 | if ( -d $realfile ) {
|
---|
757 |
|
---|
758 | # directory. do nothing
|
---|
759 | } elsif ( !-r $realfile ) {
|
---|
760 | $removedfiles{"$realfile"} = $cvsworkfile;
|
---|
761 | } else {
|
---|
762 | ( -r "$cvsworkfile" )
|
---|
763 | || die("Fehler: $cvsworkfile ist nicht lesbar");
|
---|
764 | if ( compare( $cvsworkfile, $realfile ) != 0 ) {
|
---|
765 | $changedfiles{"$realfile"} = $cvsworkfile;
|
---|
766 | }
|
---|
767 | }
|
---|
768 | }
|
---|
769 |
|
---|
770 | if (%removedfiles) {
|
---|
771 | print "deleted files (found in repository, but not in system):\n";
|
---|
772 | foreach my $key ( values %removedfiles ) {
|
---|
773 | print "$key\n";
|
---|
774 | }
|
---|
775 | print "\n";
|
---|
776 | }
|
---|
777 |
|
---|
778 | if (%changedfiles) {
|
---|
779 | print "modified files:\n";
|
---|
780 | foreach my $key ( keys %changedfiles ) {
|
---|
781 | print "$key\n";
|
---|
782 | }
|
---|
783 | }
|
---|
784 | } else {
|
---|
785 | print "no modified files found in $dir\n";
|
---|
786 | }
|
---|
787 |
|
---|
788 | print "\n";
|
---|
789 | }
|
---|
790 |
|
---|
791 | sub permissions(@)
|
---|
792 | {
|
---|
793 | check_parameter( @_, 1 );
|
---|
794 | check_env();
|
---|
795 |
|
---|
796 | #
|
---|
797 | # update local repository
|
---|
798 | #
|
---|
799 | #svn_update();
|
---|
800 |
|
---|
801 | # TODO: start at subdirectories ?
|
---|
802 | my $dir = $DASSCM_REPO;
|
---|
803 | my @files = svn_getStoredFiles($dir);
|
---|
804 |
|
---|
805 | if (@files) {
|
---|
806 |
|
---|
807 | # generieren der Permissions
|
---|
808 | my @permissions = generatePermissionList(@files);
|
---|
809 | my $OUTFILE;
|
---|
810 | my $tofile = 0; # Status für schreiben in File
|
---|
811 |
|
---|
812 | if ( -w dirname($permissions_file) ) {
|
---|
813 |
|
---|
814 | # Verzeichnis existiert => schreiben
|
---|
815 | print "storing permissions in file $permissions_file\n";
|
---|
816 | open( OUTFILE, ">$permissions_file" )
|
---|
817 | || die("failed to write to $permissions_file: $!");
|
---|
818 | $tofile = 1; # Merken, daß in File geschrieben wird
|
---|
819 | print OUTFILE "#\n";
|
---|
820 | print OUTFILE "# created by dasscm permissions\n";
|
---|
821 | print OUTFILE
|
---|
822 | "# It is intended to be used for restoring permissions\n";
|
---|
823 | } else {
|
---|
824 |
|
---|
825 | # Pfad für Sicherungsdatei existiert nicht => schreiben auf stdout
|
---|
826 | # Alias Filehandle für stdout erzeugen
|
---|
827 | *OUTFILE = *STDOUT;
|
---|
828 | }
|
---|
829 | foreach my $line (@permissions) {
|
---|
830 | print OUTFILE "$line\n";
|
---|
831 | }
|
---|
832 |
|
---|
833 | if ($tofile) {
|
---|
834 | close(OUTFILE);
|
---|
835 | }
|
---|
836 | }
|
---|
837 | }
|
---|
838 |
|
---|
839 | #####################################################################
|
---|
840 | #
|
---|
841 | # main
|
---|
842 | #
|
---|
843 |
|
---|
844 | my $number_arguments = @ARGV;
|
---|
845 |
|
---|
846 | if ( $number_arguments > 0 ) {
|
---|
847 |
|
---|
848 | # get subcommand and remove it from @ARGV
|
---|
849 | $command = $ARGV[0];
|
---|
850 | shift @ARGV;
|
---|
851 |
|
---|
852 | $DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
|
---|
853 | $DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
|
---|
854 |
|
---|
855 | # TODO: check variables
|
---|
856 | $DASSCM_SVN_REPOSITORY =
|
---|
857 | $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
|
---|
858 |
|
---|
859 | my $DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
|
---|
860 | my $DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
|
---|
861 |
|
---|
862 | #
|
---|
863 | # if a user is given by dasscm configuration file, we use it.
|
---|
864 | # Otherwise we expect that read-only account is configured
|
---|
865 | # as local subversion configuration.
|
---|
866 | # If this is also not the case,
|
---|
867 | # user is required to type username and password.
|
---|
868 | # This will be stored as local subversion configuration thereafter.
|
---|
869 | #
|
---|
870 | if ( $DASSCM_CHECKOUT_USERNAME && $DASSCM_CHECKOUT_PASSWORD ) {
|
---|
871 | $svnCheckoutCredentials =
|
---|
872 | " --username $DASSCM_CHECKOUT_USERNAME --password $DASSCM_CHECKOUT_PASSWORD ";
|
---|
873 | }
|
---|
874 |
|
---|
875 | # get command line options and store them in options hash
|
---|
876 | my $result = GetOptions( \%options, 'verbose', 'message=s' );
|
---|
877 |
|
---|
878 | # print options
|
---|
879 | foreach my $option ( keys %options ) {
|
---|
880 | print "${option}: $options{$option}\n";
|
---|
881 | }
|
---|
882 |
|
---|
883 | # set verbose to command line option
|
---|
884 | $verbose = $options{'verbose'};
|
---|
885 |
|
---|
886 | #
|
---|
887 | # action accordinly to command are taken
|
---|
888 | # $command is rewritten in standard format,
|
---|
889 | # so we can test for it later on more simply
|
---|
890 | #
|
---|
891 | $_ = $command;
|
---|
892 | if (m/help/i) {
|
---|
893 | help(@ARGV);
|
---|
894 | } elsif (m/login/i) {
|
---|
895 | $command = "login";
|
---|
896 | login(@ARGV);
|
---|
897 | } elsif (m/init/i) {
|
---|
898 | $command = "init";
|
---|
899 | init(@ARGV);
|
---|
900 | } elsif (m/ls/i) {
|
---|
901 | $command = "ls";
|
---|
902 | ls(@ARGV);
|
---|
903 | } elsif ( (m/update/i) || (m/up/i) ) {
|
---|
904 | $command = "update";
|
---|
905 | update(@ARGV);
|
---|
906 | } elsif (m/add/i) {
|
---|
907 | $command = "add";
|
---|
908 | add(@ARGV);
|
---|
909 | } elsif (m/commit/i) {
|
---|
910 | $command = "commit";
|
---|
911 | add(@ARGV);
|
---|
912 | } elsif (m/blame/i) {
|
---|
913 | $command = "blame";
|
---|
914 | blame(@ARGV);
|
---|
915 | } elsif (m/diff/i) {
|
---|
916 | $command = "diff";
|
---|
917 | diff(@ARGV);
|
---|
918 | } elsif ( (m/status/i) || (m/st/i) ) {
|
---|
919 | $command = "status";
|
---|
920 | status(@ARGV);
|
---|
921 | } elsif (m/permissions/i) {
|
---|
922 | $command = "permissions";
|
---|
923 | permissions(@ARGV);
|
---|
924 | } else {
|
---|
925 | print "unknown command: $command\n\n";
|
---|
926 | usage();
|
---|
927 | check_env();
|
---|
928 | }
|
---|
929 |
|
---|
930 | # cleanup (svn-commit.tmp)
|
---|
931 | # commitall
|
---|
932 | # revert
|
---|
933 | # activate
|
---|
934 | # rm
|
---|
935 | }
|
---|