source: trunk/dasscm/dasscm@ 271

Last change on this file since 271 was 271, checked in by joergs, on Mar 4, 2009 at 9:40:36 PM

added specific function commit, to handle things more like plain svn

  • Property keyword set to id
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 30.0 KB
RevLine 
[186]1#!/usr/bin/perl -w
2
3# $Id: dasscm 271 2009-03-04 20:40:36Z joergs $
4
5use strict;
6
[208]7use Env
[235]8 qw($DASSCM_PROD $DASSCM_REPO $USER $DASSCM_USERNAME $DASSCM_USER $DASSCM_PASSWORD $SHELL);
[186]9use Cwd;
[214]10use Getopt::Long;
[186]11use File::Basename;
[209]12use File::Compare;
13use File::Copy;
[237]14use File::Find;
[186]15use File::stat;
16use File::Path;
[214]17use Term::ReadKey;
[239]18
[234]19#use Data::Dumper;
[186]20
[189]21#####################################################################
22#
[186]23# global
[189]24#
[205]25
[253]26# shell exit codes
[252]27my $RETURN_OK = 0;
28my $RETURN_NOK = 1;
29
[215]30# file to store permissions
31my $permissions_file = "/etc/permissions.d/dasscm.permission_backup";
[220]32
[238]33# documentation file (for usage)
34my $doc_file = "/usr/share/doc/packages/dasscm/dasscm_howto.txt";
35
[253]36# commands that require write access (and therefore a login)
37# to the repository server
[268]38my @COMMANDS_REQUIRE_WRITE = ( "add", "commit", "commitall" );
[252]39
[215]40# configuration file
[205]41my $config_file = "/etc/dasscm.conf";
[234]42my $config = get_config($config_file);
[205]43my $DASSCM_LOCAL_REPOSITORY_BASE;
44my $DASSCM_REPOSITORY_NAME;
45my $DASSCM_SVN_REPOSITORY;
[247]46my $DASSCM_CHECKOUT_USERNAME;
47my $DASSCM_CHECKOUT_PASSWORD;
[205]48
[238]49# current directory at program start
50my $StartDirectory = cwd();
51
[268]52my $diff = "diff --exclude .svn ";
[205]53my $SVN = "svn ";
54my $svnOptions = "";
55my $svnCheckoutCredentials = "";
56my $svnPasswordCredentials = "";
[268]57# flag. Set to true by svn_update
58# This prevents, that svn_update is called multiple times
59my $svnRepositoryIsUptodate = 0;
[205]60
[196]61# command line options get stored in options hash
[205]62my %options = ();
63
[197]64# subcommand, that gets executed (add, commit, ...)
[196]65my $command;
[186]66
[268]67
[205]68my $verbose = 0;
69
[189]70#####################################################################
71#
[186]72# util functions
[189]73#
[187]74sub usage()
75{
[205]76 print "usage: dasscm <subcommand> [options] [args]\n";
77 print "\n";
78 print "dasscm is intended to help versioning configuration files\n";
79 print "\n";
80 print "Available subcommands:\n";
[215]81 print " help <subcommand>\n";
[205]82 print " init\n";
83 print " login\n";
[215]84 print " up\n";
85 print " ls\n";
[205]86 print " add <filename>\n";
87 print " commit <filename>\n";
[268]88 print " commitall\n";
[205]89 print " diff <filename>\n";
[236]90 print " status\n";
[215]91 print " permissions\n";
[205]92 print "\n";
[238]93 print "preparation:\n", " if dasscm is already configured,\n",
[236]94 " use 'dasscm login' and then eg. 'add'.\n",
[220]95 " The environment variables\n", " DASSCM_REPO\n", " DASSCM_PROD\n",
96 " DASSCM_USERNAME\n", " DASSCM_PASSWORD\n",
97 " are evaluated, but set automatically by 'dasscm login'.\n", "\n",
[239]98 " If dasscm is not yet configured, read", " $doc_file\n";
[187]99}
100
[233]101sub warning(@)
102{
103 print "Warning: " . join( "\n ", @_ ) . "\n";
104}
105
106sub error(@)
107{
108 print "Error: " . join( "\n ", @_ ) . "\n";
109}
110
111sub fatalerror(@)
112{
[239]113 error(@_);
114
[233]115 #print "Exiting\n";
[239]116 exit 1;
[233]117}
118
[238]119#
120# reading config file and return key/value pairs as hash
121#
[234]122sub get_config
123{
[239]124 my $file = $_[0];
[234]125
[239]126 if ( !$file ) {
[234]127 fatalerror( "failed to open config file" . $file );
128 }
129
130 my $data = {};
131
132 # try to open config file
133 if ( !open( FH, $file ) ) {
134 fatalerror( "failed to open config file" . $file );
135 } else {
136 while (<FH>) {
137 chomp;
138 if (/^#/) {
139 next;
140 }
141 if ( $_ =~ /=/g ) {
[239]142
[238]143 # splitting in 2 fields at maximum
[234]144 my ( $option, $value ) = split( /=/, $_, 2 );
145 $option =~ s/^\s+//g;
146 $option =~ s/\s+$//g;
147 $option =~ s/\"+//g;
148 $value =~ s/^\s+//g;
149 $value =~ s/\s+$//g;
150 $value =~ s/\"+//g;
151
152 if ( length($option) ) {
153 $data->{$option} = $value;
154 }
155 }
156 }
157 }
158 close(FH);
159
160 return $data;
161}
162
[270]163
164#
165# check and evaluate environment variables
166#
[186]167sub check_env()
168{
[205]169
170 # DASSCM_PROD
171 if ( !$DASSCM_PROD ) {
172 $DASSCM_PROD = "/";
173 }
174
175 if ( !-d $DASSCM_PROD ) {
176 die "DASSCM_PROD ($DASSCM_PROD) is not set to a directory.\n";
177 }
178 if ($verbose) { print "DASSCM_PROD: " . $DASSCM_PROD . "\n"; }
179
180 # DASSCM_REPOSITORY_NAME
[208]181 if ( !$DASSCM_REPOSITORY_NAME ) {
182 die
183 "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";
184 }
[205]185
186 # DASSCM_REPO
187 if ( !$DASSCM_REPO ) {
188 if ( $DASSCM_LOCAL_REPOSITORY_BASE && $DASSCM_REPOSITORY_NAME ) {
189 $DASSCM_REPO =
190 $DASSCM_LOCAL_REPOSITORY_BASE . "/" . $DASSCM_REPOSITORY_NAME;
191 } else {
192 die
193 "Envirnonment variable DASSCM_REPO not set.\nSet DASSCM_REPO to the directory of the versioning system checkout for this machine.\n";
194 }
195 }
[215]196 if ($verbose) { print "DASSCM_REPO: " . $DASSCM_REPO . "\n"; }
[205]197
198 #
[248]199 # subversion checkout user
200 #
[252]201 if ( !$DASSCM_CHECKOUT_USERNAME ) {
202 fatalerror(
[248]203 "variable DASSCM_CHECKOUT_USERNAME is not defined.",
[252]204 "Use file $config_file to configure it."
205 );
[248]206 }
207
[252]208 if ( !$DASSCM_CHECKOUT_PASSWORD ) {
209 fatalerror(
[248]210 "variable DASSCM_CHECKOUT_PASSWORD is not defined.",
[252]211 "Use file $config_file to configure it."
212 );
[248]213 }
214
215 #
[239]216 # check if local repository directory exist
[235]217 # (if not creating by init)
[205]218 #
219 if ( $command ne "init" ) {
220 if ( not -d $DASSCM_REPO ) {
221 die
[208]222 "Can't access local repository DASSCM_REPO\n($DASSCM_REPO)\nCheck configuration and execute\n dasscm init\n";
[205]223 }
[208]224
[205]225 #
226 # user settings
227 #
[208]228
[205]229 # DASSCM_USER is legacy. Use DASSCM_USERNAME instead
[208]230 if ( !$DASSCM_USERNAME ) {
231 $DASSCM_USERNAME = $DASSCM_USER;
[205]232 }
233
234 # user root is not allowed for checkins.
235 # if user is root, DASSCM_USER has to be set,
236 # otherwise USER can be used
237 if ( "$USER" eq "root" ) {
[252]238 if ( ( not $DASSCM_USERNAME )
239 and ( grep { m|^$command$| } @COMMANDS_REQUIRE_WRITE ) )
240 {
241
242 #( $command ne "login" ) and ( $command ne "status" ) ) {
243 fatalerror(
244 "Envirnonment variable DASSCM_USERNAME not set.",
245 "Set DASSCM_USERNAME to your subversion user account or",
246 "use 'dasscm login'"
247 );
[205]248 }
249 $svnOptions .= " --no-auth-cache ";
250 } elsif ( !$DASSCM_USERNAME ) {
251 $DASSCM_USERNAME = $USER;
252 }
253
254 #
255 # password
256 #
[208]257 if ($DASSCM_PASSWORD) {
[267]258 $svnPasswordCredentials = " --password '$DASSCM_PASSWORD' ";
[205]259 }
260 }
261
262 #$svnOptions .= " --username $DASSCM_USERNAME "
[186]263}
264
[270]265
266
267#
268# has been intendend,
269# to check addtitional parameters.
270# Currently not used.
271#
[186]272sub check_parameter(@)
273{
274}
275
[238]276#
[239]277# generate from (relative) filename
[238]278# all required file and directory names:
279# $basename, $dirname_prod, $dirname_repo,
280# $filename_prod, $filename_repo
281#
[187]282sub get_filenames(@)
283{
[250]284 my $filename_prod = $_[0] || ".";
[237]285
[238]286 # make filename absolut
[205]287 if ( !( $filename_prod =~ m/^\// ) ) {
[270]288 $filename_prod = cwd() . '/' . $filename_prod;
[205]289 }
[187]290
[239]291 if ( not -r $filename_prod ) {
292 fatalerror( $filename_prod . " is not accessable" );
[233]293 }
[205]294
295 # TODO: dirname buggy: eg. "/etc/" is reduced to "/",
296 # "/etc" is used as filename
[270]297 # therefore make sure, that if filename is a directory,
298 # it will end by "/"
299 if( ( -d $filename_prod ) and ( !( $filename_prod =~ m/\/$/ ) ) ) {
300 $filename_prod = $filename_prod . '/';
301 }
[238]302
[270]303 ( my $basename, my $dirname_prod ) = fileparse($filename_prod);
304
[238]305 # uses chdir to determine real directory in a unique way
[205]306 chdir $dirname_prod or die $!;
[270]307 $dirname_prod = cwd() . '/';
[238]308 chdir $StartDirectory;
309
[214]310 if ($verbose) {
311 print "dir: " . $dirname_prod . "\n";
312 print "fn: " . $basename . "\n";
313 }
[205]314
315 my $dirname_repo = $DASSCM_REPO . "/" . $dirname_prod;
316 my $filename_repo = "$dirname_repo/$basename";
317
318 return (
319 $basename, $dirname_prod, $dirname_repo,
320 $filename_prod, $filename_repo
321 );
[187]322}
323
[256]324
[271]325sub copy_file_to_repository( $ )
326{
327 my $filename = shift;
[256]328
[271]329 (
330 my $basename,
331 my $dirname_prod,
332 my $dirname_repo,
333 my $filename_prod,
334 my $filename_repo
335 )
336 = get_filenames( $filename );
337
338 # TODO: are permissions also copied?
339
340 print "$filename: $filename_prod, $filename_repo\n";
341 copy( $filename_prod, $filename_repo )
342 or error "failed to copy $filename_prod to repository: $!";
343}
344
345
[256]346#
347# creates a file with permissions
348#
[215]349sub generatePermissionList
[209]350{
351
[215]352 # generieren der Zeilen für Permission-Savefile
353 my @files = @_;
354 my @permlist = ();
355 foreach my $file (@files) {
[227]356 $file = "/" . $file;
[239]357 if ( -e $file ) {
358 my $info = stat($file) || die "failed to stat $file: aborting";
359 my $mode = get_type( $info->mode ) & 07777;
[227]360 my $modestring = sprintf( "%04o", $mode );
361 my $uid = $info->uid;
362 my $uidname = getpwuid($uid);
363 my $gid = $info->gid;
364 my $gidname = getgrgid($gid);
365 push(
366 @permlist,
367 sprintf( "%-55s %-17s %4d",
368 $file, "${uidname}:${gidname}", $modestring )
369 );
370 } else {
[239]371 print
372 "failed to get status of $file. It exists in the repository, but not on the system\n";
[227]373 }
[215]374 }
375 return @permlist;
376}
[209]377
[215]378sub get_type
379{
[209]380
[215]381 # Funktion übernommen aus /usr/bin/chkstat
382 my $S_IFLNK = 0120000; # symbolic link
383 my $S_IFREG = 0100000; # regular file
384 my $S_IFDIR = 0040000; # directory
385 my $S_IFCHAR = 0020000; # character device
386 my $S_IFBLK = 0060000; # block device
387 my $S_IFFIFO = 0010000; # fifo
388 my $S_IFSOCK = 0140000; # socket
389 my $S_IFMT = 0170000; # type of file
[209]390
[215]391 my $S_m;
392 if ( ( $_[0] & $S_IFMT ) == $S_IFLNK ) { $S_m = $_[0] - $S_IFLNK; }
393 elsif ( ( $_[0] & $S_IFMT ) == $S_IFREG ) { $S_m = $_[0] - $S_IFREG; }
394 elsif ( ( $_[0] & $S_IFMT ) == $S_IFDIR ) { $S_m = $_[0] - $S_IFDIR; }
395 elsif ( ( $_[0] & $S_IFMT ) == $S_IFCHAR ) { $S_m = $_[0] - $S_IFCHAR; }
396 elsif ( ( $_[0] & $S_IFMT ) == $S_IFBLK ) { $S_m = $_[0] - $S_IFBLK; }
397 elsif ( ( $_[0] & $S_IFMT ) == $S_IFFIFO ) { $S_m = $_[0] - $S_IFFIFO; }
398 elsif ( ( $_[0] & $S_IFMT ) == $S_IFSOCK ) { $S_m = $_[0] - $S_IFSOCK; }
399 $S_m;
[209]400}
401
[186]402sub run_command
403{
[205]404 my $command = shift;
[186]405
[205]406 #print "executing command: " . $command . "\n";
[186]407
[205]408 open( RESULT, $command . ' 2>&1 |' );
409 my @result = <RESULT>;
410 close(RESULT);
411 my $retcode = $? >> 8;
[186]412
[205]413 #print @result;
414 #if( $retcode ) { print "return code: " . $retcode . "\n"; }
[186]415
[205]416 return ( $retcode, @result );
[186]417}
418
[205]419sub run_interactive
420{
[186]421
[208]422 if ($verbose) {
[205]423 print "run_interactive:" . join( " ", @_ ) . "\n";
424 }
[196]425
[205]426 system(@_);
427 if ( $? == -1 ) {
428 printf "failed to execute: $!\n";
429 } elsif ( $? & 127 ) {
430 printf "child died with signal %d, %s coredump\n", ( $? & 127 ),
431 ( $? & 128 ) ? 'with' : 'without';
432 } elsif ( $? >> 8 != 0 ) {
433 printf "child exited with value %d\n", $? >> 8;
434 }
435 return ( $? >> 8 );
436}
437
[247]438sub svn_check_credentials( $$;$$ )
[196]439{
[205]440 my $username = shift;
441 my $password = shift;
442
[252]443 # check silently are allow user interaction?
[247]444 my $interactive = shift || 0;
[220]445
[247]446 # default: exit program, if repository is not accessable
447 # (do not exit for 'init')
448 my $fatalerror = shift || 1;
449
450 print "checking credentials ";
451
[239]452 if ( !$username ) {
453 fatalerror("no username given");
[238]454 }
455
[239]456 if ( !$password ) {
457 fatalerror("no password given");
[238]458 }
459
[247]460 print "for " . $username . "@" . $DASSCM_SVN_REPOSITORY . ": ";
461
[220]462 # Options for "svn info" are not supported by subversion 1.0.0 (SLES9),
463 # therefore switching to "svn status"
464 # ( my $rc_update, my @result ) =
465 # run_command(
466 # "$SVN info --non-interactive --no-auth-cache --username $username --password $password $DASSCM_SVN_REPOSITORY"
467 # );
468 #print @result;
469
[247]470 my $rc_update;
[252]471 if ($interactive) {
472 $rc_update =
473 run_interactive(
[267]474 "$SVN ls --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
[252]475 );
[247]476 } else {
477 ( $rc_update, my @result ) =
[252]478 run_command(
[267]479 "$SVN ls --non-interactive --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
[252]480 );
481
[247]482 if ( $rc_update != 0 ) {
483 print "\n", @result;
[252]484 if ($fatalerror) {
[247]485 fatalerror();
486 }
487 return;
488 }
[205]489 }
490
[247]491 # return success
492 return $rc_update == 0;
[196]493}
494
[205]495sub svn_update( ;$ )
496{
[270]497 my $update_path = shift || "";
498
499 # use this flag to do only one update per run
[268]500 if( ! $svnRepositoryIsUptodate ) {
501 ( my $rc_update, my @result ) =
502 run_command(
[270]503 "$SVN update --non-interactive $svnCheckoutCredentials '$DASSCM_REPO/$update_path'");
[268]504 print @result;
505 if ( $rc_update != 0 ) {
506 fatalerror();
[270]507 } elsif ( not $update_path ) {
508 # set this flag if a full update is done
[268]509 $svnRepositoryIsUptodate = 1;
510 }
[205]511 }
[215]512}
[196]513
[268]514
515
[271]516sub svn_ls( ;@ )
[215]517{
[270]518 (
519 my $basename,
520 my $dirname_prod,
521 my $dirname_repo,
522 my $filename_prod,
523 my $filename_repo
524 )
525 = get_filenames( $_[0] );
[220]526
[218]527 # svn ls -R is better, but much, much slower
528 # ( my $rc, my @result ) = run_command("$SVN ls --recursive $svnCheckoutCredentials $path");
[270]529
[271]530 my @files = ();
531 my @links = ();
532 my @dirs = ();
533 my @others = ();
534
535 if( -f $filename_prod ) {
536 @files = ( $filename_prod );
537 } elsif ( -d $dirname_repo ) {
538 find(
539 {
540 wanted => sub {
541 my $name = $File::Find::name;
542 $name =~ s|^$dirname_repo||;
543 if ( $name =~ m/\.svn/ ) {
544 # skip svn meta data
545 } elsif ( -l $_ ) {
546
547 # soft link
548 # important: check for links first
549 # to exclude them from further checks
550 push( @links, $name );
551 } elsif ( -d $_ ) {
552
553 # directories
554 push( @dirs, $name );
555 } elsif ( -f $_ ) {
556
557 # regular file
558 push( @files, $name );
559 } else {
560 push( @others, $name );
561 }
562 }
563 },
564 ( $dirname_repo )
565 );
[215]566 }
[271]567
568 return @files;
[205]569}
570
[268]571
572sub getModifiedFiles( ;$ )
573{
[270]574 (
575 my $basename,
576 my $dirname_prod,
577 my $dirname_repo,
578 my $filename_prod,
579 my $filename_repo
580 )
581 = get_filenames( $_[0] );
[268]582
[271]583 my @files = svn_ls( $filename_prod );
[270]584
[268]585 # stores result from status (cvscheck)
586 my %removedfiles = ();
587 my %changedfiles = ();
588
589 # create list of modified files
590 if (@files) {
591
592 foreach my $file (@files) {
593
[271]594 my $realfile = $dirname_prod . $file;
595 my $cvsworkfile = $dirname_repo . $file;
[268]596
597 if ( -d $realfile ) {
598 ## directory. do nothing
599 } elsif ( !-r $realfile ) {
600 $removedfiles{"$realfile"} = $cvsworkfile;
601 } else {
602 ( -r "$cvsworkfile" )
603 || die("Fehler: $cvsworkfile ist nicht lesbar");
604 if ( compare( $cvsworkfile, $realfile ) != 0 ) {
605 $changedfiles{"$realfile"} = $cvsworkfile;
606 }
607 }
608 }
609 }
610
611 return ( \%changedfiles, \%removedfiles );
612}
613
614
[238]615#
616# from an array of files/dirs,
617# generates list of files
618# sorted by type
619#
[237]620sub get_files( @ )
621{
622 my @files = ();
623 my @links = ();
624 my @dirs = ();
625 my @others = ();
626
[239]627 if (@_) {
628 find(
629 {
630 wanted => sub {
631 my $fullname = cwd() . "/" . $_;
632 if ( -l $_ ) {
633
634 # soft link
635 # important: check for links first
636 # to exclude them from further checks
637 push( @links, $fullname );
638 } elsif ( -d $_ ) {
[271]639
640 # directories
[239]641 push( @dirs, $fullname );
642 } elsif ( -f $_ ) {
643
644 # regular file
645 push( @files, $fullname );
646 } else {
647 push( @others, $fullname );
648 }
649 }
650 },
651 @_
652 );
[237]653 }
654
[239]655 # don't rely on others.
[237]656 # If more specific file types are needed,
657 # they will be added
658 return {
[239]659 files => \@files,
660 links => \@links,
661 dirs => \@dirs,
662 others => \@others
663 };
[237]664}
665
[189]666#####################################################################
667#
[186]668# functions
669
670sub help(;@)
671{
[205]672 if ( @_ == 0 ) {
673 usage();
674 } else {
675 print "help for @_: ...\n";
[214]676 usage();
[205]677 }
[186]678}
679
[203]680sub login(@)
681{
[205]682 check_parameter( @_, 1 );
683 check_env();
[203]684
[235]685 my $input_username = $_[0];
[214]686
687 if ( not $input_username ) {
688 my $output_username = "";
689 if ($DASSCM_USERNAME) {
690 $output_username = " ($DASSCM_USERNAME)";
691 }
692
693 print "Enter DASSCM user name", $output_username, ": ";
694 $input_username = <STDIN>;
695 chomp($input_username);
[247]696
697 $input_username = $input_username || $DASSCM_USERNAME;
[205]698 }
[203]699
[205]700 # hidden password input
[247]701 print "Enter password for $input_username: ";
[205]702 ReadMode('noecho');
703 my $input_password = <STDIN>;
704 ReadMode('normal');
705 chomp($input_password);
[220]706 print "\n";
[203]707
[247]708 # checking checkout username/password
[252]709 svn_check_credentials( $DASSCM_CHECKOUT_USERNAME,
710 $DASSCM_CHECKOUT_PASSWORD );
[247]711 print "checkout access okay\n";
[205]712
[247]713 svn_check_credentials( $input_username, $input_password );
714
[205]715 #
716 # set environment variables
717 #
[267]718 $ENV{'DASSCM_USERNAME'} = "$input_username";
719 $ENV{'DASSCM_PASSWORD'} = "$input_password";
[205]720
[209]721 print "subversion access okay\n\n", "DASSCM_USERNAME: $input_username\n",
722 "DASSCM_PASSWORD: (hidden)\n", "DASSCM_PROD: $DASSCM_PROD\n",
723 "DASSCM_REPO: $DASSCM_REPO\n",
724 "Server Repository: $DASSCM_SVN_REPOSITORY\n", "\n", "[dasscm shell]\n\n";
[205]725
[235]726 my $shell = $SHELL || "bash";
727 exec($shell) or die "failed to start new shell";
[203]728}
729
[260]730
731#
732# initialize local checkout directory (initial checkout)
733#
[205]734sub init(@)
735{
736 check_parameter( @_, 1 );
737 check_env();
738
[235]739 # don't do repository creation (svn mkdir) here,
740 # because then their must be a lot of prior checks
741
[205]742 # update complete repository
[216]743 # and create permission file
[208]744 my $retcode =
745 run_interactive(
[252]746 "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY; mkdir -p `dirname $permissions_file`; touch $permissions_file"
[208]747 );
[205]748}
749
[260]750
751
[215]752sub ls(@)
[186]753{
[205]754 check_parameter( @_, 1 );
755 check_env();
[186]756
[271]757 my @files = svn_ls(@_);
[215]758
759 print join( "\n", @files );
760 print "\n";
761}
762
[267]763
764
[215]765sub update(@)
766{
767 check_parameter( @_, 1 );
768 check_env();
769
770 #
771 # update local repository
772 #
773 svn_update();
774}
775
[237]776#
777# helper function for "add" command
778#
[215]779sub add_helper(@)
780{
[205]781 (
782 my $basename,
783 my $dirname_prod,
784 my $dirname_repo,
785 my $filename_prod,
786 my $filename_repo
787 )
788 = get_filenames( $_[0] );
[186]789
[205]790 if ( $command eq "add" ) {
791 mkpath($dirname_repo);
792 }
[186]793
[238]794 # TODO: are permissions also copied?
[239]795 copy( $filename_prod, $filename_repo )
796 or error "failed to copy $filename_prod to repository: $!";
[205]797
798 if ( $command eq "add" ) {
799
800 # already checked in?
[238]801 chdir $DASSCM_REPO;
[205]802
803 # also add the path to filename.
804 for my $dir ( split( '/', $dirname_prod ) ) {
805 if ($dir) {
[239]806 my ( $rc, @out ) =
807 run_command( "$SVN add --non-recursive \"" . $dir . "\"" );
808 if ( $rc > 0 ) {
[226]809 print join( "\n", @out );
[239]810 }
[205]811 chdir $dir;
812 }
813 }
[239]814 my ( $rc, @out ) = run_command( "$SVN add \"" . $basename . "\"" );
815 if ( $rc > 0 ) {
[226]816 print join( "\n", @out );
[239]817 }
[238]818 chdir $StartDirectory;
[205]819 }
[215]820}
[205]821
[268]822
823
[215]824#
825# add (is used for command add and commit)
826#
827sub add(@)
828{
829 check_parameter( @_, 1 );
830 check_env();
831
832 #
833 # update local repository
834 #
835 svn_update();
836
[237]837 # get all regular files and links
[239]838 my $href_files = get_files(@_);
[220]839
[237]840 #print Dumper( $href_files );
841
[239]842 my @files = @{ $href_files->{files} };
843 my @links = @{ $href_files->{links} };
[237]844
[239]845 if (@files) {
[237]846 my $number = $#files + 1;
847 print "files to check-in ($number): \n";
848 print join( "\n", @files );
849 print "\n";
850 }
851
[268]852 # TODO: check in links and also link target? At least warn about link target
[239]853 if (@links) {
[237]854 my $number = $#links + 1;
855 print "\n";
856 print "ignoring links ($number):\n";
857 print join( "\n", @links );
858 print "\n";
859 }
860
[238]861 # TODO: confirm
862
[237]863 # copy files one by one to local repository
864 for my $file (@files) {
[239]865
[233]866 # add file
[239]867 add_helper($file);
[233]868 }
869
[215]870 # create new permissions file
871 permissions();
[220]872
[215]873 # add permissions file
[220]874 add_helper($permissions_file);
[215]875
[205]876 if ( $options{'message'} ) {
877 $svnOptions .= " --message \"$options{'message'}\" ";
878 }
879
[239]880 # commit calls $EDITOR.
[237]881 # use "interactive" here, to display output
[215]882 my $retcode =
[205]883 run_interactive(
[267]884 "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
[208]885 );
[268]886}
[205]887
[268]888
889
890#
891# checks in all modified files
892#
893sub commitall(@)
894{
895 check_parameter( @_, 1 );
896 check_env();
897
898 #
899 # update local repository
900 #
901 svn_update();
902
[270]903 ( my $refChangedFiles, my $refRemovedFiles ) = getModifiedFiles( "/" );
[268]904 my %changedfiles = %{$refChangedFiles};
905 my %removedfiles = %{$refRemovedFiles};
906
907 if( %removedfiles ) {
908 my $removedFilesString = '"' . join( '" "', values(%removedfiles) ) . '"';
909 my ( $rc, @out ) = run_command( "$SVN rm $removedFilesString" );
910 if ( $rc > 0 ) {
911 print join( "\n", @out );
912 }
913 }
914
915 # add will care about the rest, including permission file and commiting
916 add( keys %changedfiles );
[186]917}
918
[268]919
920
[271]921#
922# checks in all modified files
923#
924sub commit(@)
925{
926 check_parameter( @_, 1 );
927 check_env();
928
929 (
930 my $basename,
931 my $dirname_prod,
932 my $dirname_repo,
933 my $filename_prod,
934 my $filename_repo
935 )
936 = get_filenames( $_[0] );
937
938
939 #
940 # update local repository
941 #
942 svn_update();
943
944 ( my $refChangedFiles, my $refRemovedFiles ) = getModifiedFiles( $filename_prod );
945 my %changedfiles = %{$refChangedFiles};
946 my %removedfiles = %{$refRemovedFiles};
947
948 if( %removedfiles ) {
949 my $removedFilesString = '"' . join( '" "', values(%removedfiles) ) . '"';
950 my ( $rc, @out ) = run_command( "$SVN rm $removedFilesString" );
951 if ( $rc > 0 ) {
952 print join( "\n", @out );
953 }
954 }
955
956 # copy files one by one to local repository
957 for my $file ( keys(%changedfiles) ) {
958 copy_file_to_repository( $file );
959 }
960
961 # create new permissions file
962 permissions();
963
964 # add permissions file
965 add_helper($permissions_file);
966
967 if ( $options{'message'} ) {
968 $svnOptions .= " --message \"$options{'message'}\" ";
969 }
970
971 # commit calls $EDITOR.
972 # use "interactive" here, to display output
973 my $retcode =
974 run_interactive(
975 "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
976 );
977}
978
979
980
[193]981sub blame(@)
982{
[205]983 check_parameter( @_, 1 );
984 check_env();
[193]985
[205]986 (
987 my $basename,
988 my $dirname_prod,
989 my $dirname_repo,
990 my $filename_prod,
991 my $filename_repo
992 )
993 = get_filenames( $_[0] );
994
995 my $retcode = run_interactive("$SVN blame $svnOptions $filename_repo");
[193]996}
997
[187]998sub diff(@)
999{
[205]1000 check_parameter( @_, 1 );
1001 check_env();
[187]1002
[205]1003 (
1004 my $basename,
1005 my $dirname_prod,
1006 my $dirname_repo,
1007 my $filename_prod,
1008 my $filename_repo
1009 )
1010 = get_filenames( $_[0] );
1011
1012 #print "$basename,$dirname_prod,$dirname_repo\n";
1013
1014 ( my $rc_update, my @result ) = run_command("$SVN update $filename_repo");
1015 if ( $rc_update != 0 ) {
1016 print @result;
1017 die;
1018 }
1019
[238]1020 ( my $rc_diff, my @diff_result ) =
[239]1021 run_command( $diff . " $filename_repo $filename_prod" );
[238]1022
1023 print @diff_result;
[187]1024}
1025
[209]1026sub status(@)
1027{
1028 check_parameter( @_, 1 );
1029 check_env();
1030
[270]1031 (
1032 my $basename,
1033 my $dirname_prod,
1034 my $dirname_repo,
1035 my $filename_prod,
1036 my $filename_repo
1037 )
1038 = get_filenames( $_[0] || "/" );
1039
1040
[252]1041 # return code for the shell
1042 # default: error
1043 my $return_code = $RETURN_NOK;
1044
[209]1045 #
1046 # update local repository
1047 #
1048 svn_update();
1049
[270]1050 ( my $refChangedFiles, my $refRemovedFiles ) = getModifiedFiles( $dirname_prod );
[268]1051 my %changedfiles = %{$refChangedFiles};
1052 my %removedfiles = %{$refRemovedFiles};
[209]1053
[268]1054 if( %removedfiles or %changedfiles ) {
[215]1055 if (%removedfiles) {
1056 print "deleted files (found in repository, but not in system):\n";
1057 foreach my $key ( values %removedfiles ) {
[209]1058 print "$key\n";
1059 }
1060 print "\n";
1061 }
1062
[215]1063 if (%changedfiles) {
[209]1064 print "modified files:\n";
[215]1065 foreach my $key ( keys %changedfiles ) {
[209]1066 print "$key\n";
1067 }
1068 }
1069 } else {
[270]1070 print "no modified files found in $dirname_repo\n";
[252]1071 $return_code = $RETURN_OK;
[209]1072 }
[215]1073
[252]1074 return $return_code;
[215]1075}
[209]1076
[268]1077
1078
[270]1079sub permissions()
[215]1080{
1081 check_parameter( @_, 1 );
1082 check_env();
1083
1084 #
1085 # update local repository
1086 #
1087 #svn_update();
1088
[220]1089 my $dir = $DASSCM_REPO;
[271]1090 my @files = svn_ls( "/" );
[215]1091
1092 if (@files) {
1093
1094 # generieren der Permissions
1095 my @permissions = generatePermissionList(@files);
1096 my $OUTFILE;
1097 my $tofile = 0; # Status für schreiben in File
[220]1098
[215]1099 if ( -w dirname($permissions_file) ) {
1100
1101 # Verzeichnis existiert => schreiben
1102 print "storing permissions in file $permissions_file\n";
1103 open( OUTFILE, ">$permissions_file" )
[216]1104 || die("failed to write to $permissions_file: $!");
[215]1105 $tofile = 1; # Merken, daß in File geschrieben wird
1106 print OUTFILE "#\n";
1107 print OUTFILE "# created by dasscm permissions\n";
[220]1108 print OUTFILE
1109 "# It is intended to be used for restoring permissions\n";
[215]1110 } else {
1111
1112 # Pfad für Sicherungsdatei existiert nicht => schreiben auf stdout
1113 # Alias Filehandle für stdout erzeugen
1114 *OUTFILE = *STDOUT;
1115 }
1116 foreach my $line (@permissions) {
1117 print OUTFILE "$line\n";
1118 }
1119
[220]1120 if ($tofile) {
[215]1121 close(OUTFILE);
1122 }
1123 }
[209]1124}
1125
[268]1126
1127
[189]1128#####################################################################
1129#
[186]1130# main
[189]1131#
[186]1132
[252]1133my $return_code = $RETURN_OK;
[186]1134my $number_arguments = @ARGV;
1135
[205]1136if ( $number_arguments > 0 ) {
[186]1137
[205]1138 # get subcommand and remove it from @ARGV
1139 $command = $ARGV[0];
1140 shift @ARGV;
[196]1141
[205]1142 $DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
1143 $DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
[196]1144
[205]1145 # TODO: check variables
1146 $DASSCM_SVN_REPOSITORY =
1147 $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
1148
[247]1149 $DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
1150 $DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
[205]1151
1152 #
1153 # if a user is given by dasscm configuration file, we use it.
1154 # Otherwise we expect that read-only account is configured
1155 # as local subversion configuration.
1156 # If this is also not the case,
1157 # user is required to type username and password.
1158 # This will be stored as local subversion configuration thereafter.
1159 #
1160 if ( $DASSCM_CHECKOUT_USERNAME && $DASSCM_CHECKOUT_PASSWORD ) {
1161 $svnCheckoutCredentials =
1162 " --username $DASSCM_CHECKOUT_USERNAME --password $DASSCM_CHECKOUT_PASSWORD ";
1163 }
1164
1165 # get command line options and store them in options hash
[214]1166 my $result = GetOptions( \%options, 'verbose', 'message=s' );
[205]1167
1168 # print options
1169 foreach my $option ( keys %options ) {
[215]1170 print "${option}: $options{$option}\n";
[205]1171 }
1172
[214]1173 # set verbose to command line option
1174 $verbose = $options{'verbose'};
1175
1176 #
1177 # action accordinly to command are taken
1178 # $command is rewritten in standard format,
1179 # so we can test for it later on more simply
1180 #
[205]1181 $_ = $command;
1182 if (m/help/i) {
1183 help(@ARGV);
1184 } elsif (m/login/i) {
[208]1185 $command = "login";
[205]1186 login(@ARGV);
1187 } elsif (m/init/i) {
[208]1188 $command = "init";
[205]1189 init(@ARGV);
[215]1190 } elsif (m/ls/i) {
1191 $command = "ls";
1192 ls(@ARGV);
[235]1193 } elsif ( (m/update/i) || (m/up/i) ) {
[215]1194 $command = "update";
1195 update(@ARGV);
[268]1196 } elsif ( (m/commitall/i) || (m/cia/i) ) {
1197 $command = "commitall";
1198 commitall(@ARGV);
[205]1199 } elsif (m/add/i) {
1200 $command = "add";
1201 add(@ARGV);
[268]1202 } elsif ( (m/commit/i) || (m/checkin/i) || (m/ci/i) ) {
[205]1203 $command = "commit";
[271]1204 commit(@ARGV);
[205]1205 } elsif (m/blame/i) {
[208]1206 $command = "blame";
[205]1207 blame(@ARGV);
1208 } elsif (m/diff/i) {
[208]1209 $command = "diff";
[205]1210 diff(@ARGV);
[235]1211 } elsif ( (m/status/i) || (m/st/i) ) {
[252]1212 $command = "status";
1213 $return_code = status(@ARGV);
[215]1214 } elsif (m/permissions/i) {
1215 $command = "permissions";
[270]1216 permissions();
[205]1217 } else {
[215]1218 print "unknown command: $command\n\n";
[205]1219 usage();
1220 check_env();
[252]1221 $return_code = $RETURN_NOK;
[205]1222 }
1223
[209]1224 # cleanup (svn-commit.tmp)
[205]1225 # revert
[215]1226 # activate
[227]1227 # rm
[252]1228
[186]1229}
[252]1230
1231exit $return_code;
Note: See TracBrowser for help on using the repository browser.