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
Line 
1#!/usr/bin/perl -w
2
3# $Id: dasscm 271 2009-03-04 20:40:36Z joergs $
4
5use strict;
6
7use Env
8 qw($DASSCM_PROD $DASSCM_REPO $USER $DASSCM_USERNAME $DASSCM_USER $DASSCM_PASSWORD $SHELL);
9use Cwd;
10use Getopt::Long;
11use File::Basename;
12use File::Compare;
13use File::Copy;
14use File::Find;
15use File::stat;
16use File::Path;
17use Term::ReadKey;
18
19#use Data::Dumper;
20
21#####################################################################
22#
23# global
24#
25
26# shell exit codes
27my $RETURN_OK = 0;
28my $RETURN_NOK = 1;
29
30# file to store permissions
31my $permissions_file = "/etc/permissions.d/dasscm.permission_backup";
32
33# documentation file (for usage)
34my $doc_file = "/usr/share/doc/packages/dasscm/dasscm_howto.txt";
35
36# commands that require write access (and therefore a login)
37# to the repository server
38my @COMMANDS_REQUIRE_WRITE = ( "add", "commit", "commitall" );
39
40# configuration file
41my $config_file = "/etc/dasscm.conf";
42my $config = get_config($config_file);
43my $DASSCM_LOCAL_REPOSITORY_BASE;
44my $DASSCM_REPOSITORY_NAME;
45my $DASSCM_SVN_REPOSITORY;
46my $DASSCM_CHECKOUT_USERNAME;
47my $DASSCM_CHECKOUT_PASSWORD;
48
49# current directory at program start
50my $StartDirectory = cwd();
51
52my $diff = "diff --exclude .svn ";
53my $SVN = "svn ";
54my $svnOptions = "";
55my $svnCheckoutCredentials = "";
56my $svnPasswordCredentials = "";
57# flag. Set to true by svn_update
58# This prevents, that svn_update is called multiple times
59my $svnRepositoryIsUptodate = 0;
60
61# command line options get stored in options hash
62my %options = ();
63
64# subcommand, that gets executed (add, commit, ...)
65my $command;
66
67
68my $verbose = 0;
69
70#####################################################################
71#
72# util functions
73#
74sub usage()
75{
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";
81 print " help <subcommand>\n";
82 print " init\n";
83 print " login\n";
84 print " up\n";
85 print " ls\n";
86 print " add <filename>\n";
87 print " commit <filename>\n";
88 print " commitall\n";
89 print " diff <filename>\n";
90 print " status\n";
91 print " permissions\n";
92 print "\n";
93 print "preparation:\n", " if dasscm is already configured,\n",
94 " use 'dasscm login' and then eg. 'add'.\n",
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",
98 " If dasscm is not yet configured, read", " $doc_file\n";
99}
100
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{
113 error(@_);
114
115 #print "Exiting\n";
116 exit 1;
117}
118
119#
120# reading config file and return key/value pairs as hash
121#
122sub get_config
123{
124 my $file = $_[0];
125
126 if ( !$file ) {
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 ) {
142
143 # splitting in 2 fields at maximum
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
163
164#
165# check and evaluate environment variables
166#
167sub check_env()
168{
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
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 }
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 }
196 if ($verbose) { print "DASSCM_REPO: " . $DASSCM_REPO . "\n"; }
197
198 #
199 # subversion checkout user
200 #
201 if ( !$DASSCM_CHECKOUT_USERNAME ) {
202 fatalerror(
203 "variable DASSCM_CHECKOUT_USERNAME is not defined.",
204 "Use file $config_file to configure it."
205 );
206 }
207
208 if ( !$DASSCM_CHECKOUT_PASSWORD ) {
209 fatalerror(
210 "variable DASSCM_CHECKOUT_PASSWORD is not defined.",
211 "Use file $config_file to configure it."
212 );
213 }
214
215 #
216 # check if local repository directory exist
217 # (if not creating by init)
218 #
219 if ( $command ne "init" ) {
220 if ( not -d $DASSCM_REPO ) {
221 die
222 "Can't access local repository DASSCM_REPO\n($DASSCM_REPO)\nCheck configuration and execute\n dasscm init\n";
223 }
224
225 #
226 # user settings
227 #
228
229 # DASSCM_USER is legacy. Use DASSCM_USERNAME instead
230 if ( !$DASSCM_USERNAME ) {
231 $DASSCM_USERNAME = $DASSCM_USER;
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" ) {
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 );
248 }
249 $svnOptions .= " --no-auth-cache ";
250 } elsif ( !$DASSCM_USERNAME ) {
251 $DASSCM_USERNAME = $USER;
252 }
253
254 #
255 # password
256 #
257 if ($DASSCM_PASSWORD) {
258 $svnPasswordCredentials = " --password '$DASSCM_PASSWORD' ";
259 }
260 }
261
262 #$svnOptions .= " --username $DASSCM_USERNAME "
263}
264
265
266
267#
268# has been intendend,
269# to check addtitional parameters.
270# Currently not used.
271#
272sub check_parameter(@)
273{
274}
275
276#
277# generate from (relative) filename
278# all required file and directory names:
279# $basename, $dirname_prod, $dirname_repo,
280# $filename_prod, $filename_repo
281#
282sub get_filenames(@)
283{
284 my $filename_prod = $_[0] || ".";
285
286 # make filename absolut
287 if ( !( $filename_prod =~ m/^\// ) ) {
288 $filename_prod = cwd() . '/' . $filename_prod;
289 }
290
291 if ( not -r $filename_prod ) {
292 fatalerror( $filename_prod . " is not accessable" );
293 }
294
295 # TODO: dirname buggy: eg. "/etc/" is reduced to "/",
296 # "/etc" is used as filename
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 }
302
303 ( my $basename, my $dirname_prod ) = fileparse($filename_prod);
304
305 # uses chdir to determine real directory in a unique way
306 chdir $dirname_prod or die $!;
307 $dirname_prod = cwd() . '/';
308 chdir $StartDirectory;
309
310 if ($verbose) {
311 print "dir: " . $dirname_prod . "\n";
312 print "fn: " . $basename . "\n";
313 }
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 );
322}
323
324
325sub copy_file_to_repository( $ )
326{
327 my $filename = shift;
328
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
346#
347# creates a file with permissions
348#
349sub generatePermissionList
350{
351
352 # generieren der Zeilen für Permission-Savefile
353 my @files = @_;
354 my @permlist = ();
355 foreach my $file (@files) {
356 $file = "/" . $file;
357 if ( -e $file ) {
358 my $info = stat($file) || die "failed to stat $file: aborting";
359 my $mode = get_type( $info->mode ) & 07777;
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 {
371 print
372 "failed to get status of $file. It exists in the repository, but not on the system\n";
373 }
374 }
375 return @permlist;
376}
377
378sub get_type
379{
380
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
390
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;
400}
401
402sub run_command
403{
404 my $command = shift;
405
406 #print "executing command: " . $command . "\n";
407
408 open( RESULT, $command . ' 2>&1 |' );
409 my @result = <RESULT>;
410 close(RESULT);
411 my $retcode = $? >> 8;
412
413 #print @result;
414 #if( $retcode ) { print "return code: " . $retcode . "\n"; }
415
416 return ( $retcode, @result );
417}
418
419sub run_interactive
420{
421
422 if ($verbose) {
423 print "run_interactive:" . join( " ", @_ ) . "\n";
424 }
425
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
438sub svn_check_credentials( $$;$$ )
439{
440 my $username = shift;
441 my $password = shift;
442
443 # check silently are allow user interaction?
444 my $interactive = shift || 0;
445
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
452 if ( !$username ) {
453 fatalerror("no username given");
454 }
455
456 if ( !$password ) {
457 fatalerror("no password given");
458 }
459
460 print "for " . $username . "@" . $DASSCM_SVN_REPOSITORY . ": ";
461
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
470 my $rc_update;
471 if ($interactive) {
472 $rc_update =
473 run_interactive(
474 "$SVN ls --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
475 );
476 } else {
477 ( $rc_update, my @result ) =
478 run_command(
479 "$SVN ls --non-interactive --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
480 );
481
482 if ( $rc_update != 0 ) {
483 print "\n", @result;
484 if ($fatalerror) {
485 fatalerror();
486 }
487 return;
488 }
489 }
490
491 # return success
492 return $rc_update == 0;
493}
494
495sub svn_update( ;$ )
496{
497 my $update_path = shift || "";
498
499 # use this flag to do only one update per run
500 if( ! $svnRepositoryIsUptodate ) {
501 ( my $rc_update, my @result ) =
502 run_command(
503 "$SVN update --non-interactive $svnCheckoutCredentials '$DASSCM_REPO/$update_path'");
504 print @result;
505 if ( $rc_update != 0 ) {
506 fatalerror();
507 } elsif ( not $update_path ) {
508 # set this flag if a full update is done
509 $svnRepositoryIsUptodate = 1;
510 }
511 }
512}
513
514
515
516sub svn_ls( ;@ )
517{
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] );
526
527 # svn ls -R is better, but much, much slower
528 # ( my $rc, my @result ) = run_command("$SVN ls --recursive $svnCheckoutCredentials $path");
529
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 );
566 }
567
568 return @files;
569}
570
571
572sub getModifiedFiles( ;$ )
573{
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] );
582
583 my @files = svn_ls( $filename_prod );
584
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
594 my $realfile = $dirname_prod . $file;
595 my $cvsworkfile = $dirname_repo . $file;
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
615#
616# from an array of files/dirs,
617# generates list of files
618# sorted by type
619#
620sub get_files( @ )
621{
622 my @files = ();
623 my @links = ();
624 my @dirs = ();
625 my @others = ();
626
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 $_ ) {
639
640 # directories
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 );
653 }
654
655 # don't rely on others.
656 # If more specific file types are needed,
657 # they will be added
658 return {
659 files => \@files,
660 links => \@links,
661 dirs => \@dirs,
662 others => \@others
663 };
664}
665
666#####################################################################
667#
668# functions
669
670sub help(;@)
671{
672 if ( @_ == 0 ) {
673 usage();
674 } else {
675 print "help for @_: ...\n";
676 usage();
677 }
678}
679
680sub login(@)
681{
682 check_parameter( @_, 1 );
683 check_env();
684
685 my $input_username = $_[0];
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);
696
697 $input_username = $input_username || $DASSCM_USERNAME;
698 }
699
700 # hidden password input
701 print "Enter password for $input_username: ";
702 ReadMode('noecho');
703 my $input_password = <STDIN>;
704 ReadMode('normal');
705 chomp($input_password);
706 print "\n";
707
708 # checking checkout username/password
709 svn_check_credentials( $DASSCM_CHECKOUT_USERNAME,
710 $DASSCM_CHECKOUT_PASSWORD );
711 print "checkout access okay\n";
712
713 svn_check_credentials( $input_username, $input_password );
714
715 #
716 # set environment variables
717 #
718 $ENV{'DASSCM_USERNAME'} = "$input_username";
719 $ENV{'DASSCM_PASSWORD'} = "$input_password";
720
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";
725
726 my $shell = $SHELL || "bash";
727 exec($shell) or die "failed to start new shell";
728}
729
730
731#
732# initialize local checkout directory (initial checkout)
733#
734sub init(@)
735{
736 check_parameter( @_, 1 );
737 check_env();
738
739 # don't do repository creation (svn mkdir) here,
740 # because then their must be a lot of prior checks
741
742 # update complete repository
743 # and create permission file
744 my $retcode =
745 run_interactive(
746 "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY; mkdir -p `dirname $permissions_file`; touch $permissions_file"
747 );
748}
749
750
751
752sub ls(@)
753{
754 check_parameter( @_, 1 );
755 check_env();
756
757 my @files = svn_ls(@_);
758
759 print join( "\n", @files );
760 print "\n";
761}
762
763
764
765sub update(@)
766{
767 check_parameter( @_, 1 );
768 check_env();
769
770 #
771 # update local repository
772 #
773 svn_update();
774}
775
776#
777# helper function for "add" command
778#
779sub add_helper(@)
780{
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] );
789
790 if ( $command eq "add" ) {
791 mkpath($dirname_repo);
792 }
793
794 # TODO: are permissions also copied?
795 copy( $filename_prod, $filename_repo )
796 or error "failed to copy $filename_prod to repository: $!";
797
798 if ( $command eq "add" ) {
799
800 # already checked in?
801 chdir $DASSCM_REPO;
802
803 # also add the path to filename.
804 for my $dir ( split( '/', $dirname_prod ) ) {
805 if ($dir) {
806 my ( $rc, @out ) =
807 run_command( "$SVN add --non-recursive \"" . $dir . "\"" );
808 if ( $rc > 0 ) {
809 print join( "\n", @out );
810 }
811 chdir $dir;
812 }
813 }
814 my ( $rc, @out ) = run_command( "$SVN add \"" . $basename . "\"" );
815 if ( $rc > 0 ) {
816 print join( "\n", @out );
817 }
818 chdir $StartDirectory;
819 }
820}
821
822
823
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
837 # get all regular files and links
838 my $href_files = get_files(@_);
839
840 #print Dumper( $href_files );
841
842 my @files = @{ $href_files->{files} };
843 my @links = @{ $href_files->{links} };
844
845 if (@files) {
846 my $number = $#files + 1;
847 print "files to check-in ($number): \n";
848 print join( "\n", @files );
849 print "\n";
850 }
851
852 # TODO: check in links and also link target? At least warn about link target
853 if (@links) {
854 my $number = $#links + 1;
855 print "\n";
856 print "ignoring links ($number):\n";
857 print join( "\n", @links );
858 print "\n";
859 }
860
861 # TODO: confirm
862
863 # copy files one by one to local repository
864 for my $file (@files) {
865
866 # add file
867 add_helper($file);
868 }
869
870 # create new permissions file
871 permissions();
872
873 # add permissions file
874 add_helper($permissions_file);
875
876 if ( $options{'message'} ) {
877 $svnOptions .= " --message \"$options{'message'}\" ";
878 }
879
880 # commit calls $EDITOR.
881 # use "interactive" here, to display output
882 my $retcode =
883 run_interactive(
884 "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
885 );
886}
887
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
903 ( my $refChangedFiles, my $refRemovedFiles ) = getModifiedFiles( "/" );
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 );
917}
918
919
920
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
981sub blame(@)
982{
983 check_parameter( @_, 1 );
984 check_env();
985
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");
996}
997
998sub diff(@)
999{
1000 check_parameter( @_, 1 );
1001 check_env();
1002
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
1020 ( my $rc_diff, my @diff_result ) =
1021 run_command( $diff . " $filename_repo $filename_prod" );
1022
1023 print @diff_result;
1024}
1025
1026sub status(@)
1027{
1028 check_parameter( @_, 1 );
1029 check_env();
1030
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
1041 # return code for the shell
1042 # default: error
1043 my $return_code = $RETURN_NOK;
1044
1045 #
1046 # update local repository
1047 #
1048 svn_update();
1049
1050 ( my $refChangedFiles, my $refRemovedFiles ) = getModifiedFiles( $dirname_prod );
1051 my %changedfiles = %{$refChangedFiles};
1052 my %removedfiles = %{$refRemovedFiles};
1053
1054 if( %removedfiles or %changedfiles ) {
1055 if (%removedfiles) {
1056 print "deleted files (found in repository, but not in system):\n";
1057 foreach my $key ( values %removedfiles ) {
1058 print "$key\n";
1059 }
1060 print "\n";
1061 }
1062
1063 if (%changedfiles) {
1064 print "modified files:\n";
1065 foreach my $key ( keys %changedfiles ) {
1066 print "$key\n";
1067 }
1068 }
1069 } else {
1070 print "no modified files found in $dirname_repo\n";
1071 $return_code = $RETURN_OK;
1072 }
1073
1074 return $return_code;
1075}
1076
1077
1078
1079sub permissions()
1080{
1081 check_parameter( @_, 1 );
1082 check_env();
1083
1084 #
1085 # update local repository
1086 #
1087 #svn_update();
1088
1089 my $dir = $DASSCM_REPO;
1090 my @files = svn_ls( "/" );
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
1098
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" )
1104 || die("failed to write to $permissions_file: $!");
1105 $tofile = 1; # Merken, daß in File geschrieben wird
1106 print OUTFILE "#\n";
1107 print OUTFILE "# created by dasscm permissions\n";
1108 print OUTFILE
1109 "# It is intended to be used for restoring permissions\n";
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
1120 if ($tofile) {
1121 close(OUTFILE);
1122 }
1123 }
1124}
1125
1126
1127
1128#####################################################################
1129#
1130# main
1131#
1132
1133my $return_code = $RETURN_OK;
1134my $number_arguments = @ARGV;
1135
1136if ( $number_arguments > 0 ) {
1137
1138 # get subcommand and remove it from @ARGV
1139 $command = $ARGV[0];
1140 shift @ARGV;
1141
1142 $DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
1143 $DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
1144
1145 # TODO: check variables
1146 $DASSCM_SVN_REPOSITORY =
1147 $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
1148
1149 $DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
1150 $DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
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
1166 my $result = GetOptions( \%options, 'verbose', 'message=s' );
1167
1168 # print options
1169 foreach my $option ( keys %options ) {
1170 print "${option}: $options{$option}\n";
1171 }
1172
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 #
1181 $_ = $command;
1182 if (m/help/i) {
1183 help(@ARGV);
1184 } elsif (m/login/i) {
1185 $command = "login";
1186 login(@ARGV);
1187 } elsif (m/init/i) {
1188 $command = "init";
1189 init(@ARGV);
1190 } elsif (m/ls/i) {
1191 $command = "ls";
1192 ls(@ARGV);
1193 } elsif ( (m/update/i) || (m/up/i) ) {
1194 $command = "update";
1195 update(@ARGV);
1196 } elsif ( (m/commitall/i) || (m/cia/i) ) {
1197 $command = "commitall";
1198 commitall(@ARGV);
1199 } elsif (m/add/i) {
1200 $command = "add";
1201 add(@ARGV);
1202 } elsif ( (m/commit/i) || (m/checkin/i) || (m/ci/i) ) {
1203 $command = "commit";
1204 commit(@ARGV);
1205 } elsif (m/blame/i) {
1206 $command = "blame";
1207 blame(@ARGV);
1208 } elsif (m/diff/i) {
1209 $command = "diff";
1210 diff(@ARGV);
1211 } elsif ( (m/status/i) || (m/st/i) ) {
1212 $command = "status";
1213 $return_code = status(@ARGV);
1214 } elsif (m/permissions/i) {
1215 $command = "permissions";
1216 permissions();
1217 } else {
1218 print "unknown command: $command\n\n";
1219 usage();
1220 check_env();
1221 $return_code = $RETURN_NOK;
1222 }
1223
1224 # cleanup (svn-commit.tmp)
1225 # revert
1226 # activate
1227 # rm
1228
1229}
1230
1231exit $return_code;
Note: See TracBrowser for help on using the repository browser.