source: trunk/dasscm/dasscm@ 269

Last change on this file since 269 was 268, checked in by joergs, on Mar 3, 2009 at 6:18:03 PM

added command commitall

  • Property keyword set to id
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 26.8 KB
Line 
1#!/usr/bin/perl -w
2
3# $Id: dasscm 268 2009-03-03 17:18:03Z 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
163sub check_env()
164{
165
166 # DASSCM_PROD
167 if ( !$DASSCM_PROD ) {
168 $DASSCM_PROD = "/";
169 }
170
171 if ( !-d $DASSCM_PROD ) {
172 die "DASSCM_PROD ($DASSCM_PROD) is not set to a directory.\n";
173 }
174 if ($verbose) { print "DASSCM_PROD: " . $DASSCM_PROD . "\n"; }
175
176 # DASSCM_REPOSITORY_NAME
177 if ( !$DASSCM_REPOSITORY_NAME ) {
178 die
179 "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";
180 }
181
182 # DASSCM_REPO
183 if ( !$DASSCM_REPO ) {
184 if ( $DASSCM_LOCAL_REPOSITORY_BASE && $DASSCM_REPOSITORY_NAME ) {
185 $DASSCM_REPO =
186 $DASSCM_LOCAL_REPOSITORY_BASE . "/" . $DASSCM_REPOSITORY_NAME;
187 } else {
188 die
189 "Envirnonment variable DASSCM_REPO not set.\nSet DASSCM_REPO to the directory of the versioning system checkout for this machine.\n";
190 }
191 }
192 if ($verbose) { print "DASSCM_REPO: " . $DASSCM_REPO . "\n"; }
193
194 #
195 # subversion checkout user
196 #
197 if ( !$DASSCM_CHECKOUT_USERNAME ) {
198 fatalerror(
199 "variable DASSCM_CHECKOUT_USERNAME is not defined.",
200 "Use file $config_file to configure it."
201 );
202 }
203
204 if ( !$DASSCM_CHECKOUT_PASSWORD ) {
205 fatalerror(
206 "variable DASSCM_CHECKOUT_PASSWORD is not defined.",
207 "Use file $config_file to configure it."
208 );
209 }
210
211 #
212 # check if local repository directory exist
213 # (if not creating by init)
214 #
215 if ( $command ne "init" ) {
216 if ( not -d $DASSCM_REPO ) {
217 die
218 "Can't access local repository DASSCM_REPO\n($DASSCM_REPO)\nCheck configuration and execute\n dasscm init\n";
219 }
220
221 #
222 # user settings
223 #
224
225 # DASSCM_USER is legacy. Use DASSCM_USERNAME instead
226 if ( !$DASSCM_USERNAME ) {
227 $DASSCM_USERNAME = $DASSCM_USER;
228 }
229
230 # user root is not allowed for checkins.
231 # if user is root, DASSCM_USER has to be set,
232 # otherwise USER can be used
233 if ( "$USER" eq "root" ) {
234 if ( ( not $DASSCM_USERNAME )
235 and ( grep { m|^$command$| } @COMMANDS_REQUIRE_WRITE ) )
236 {
237
238 #( $command ne "login" ) and ( $command ne "status" ) ) {
239 fatalerror(
240 "Envirnonment variable DASSCM_USERNAME not set.",
241 "Set DASSCM_USERNAME to your subversion user account or",
242 "use 'dasscm login'"
243 );
244 }
245 $svnOptions .= " --no-auth-cache ";
246 } elsif ( !$DASSCM_USERNAME ) {
247 $DASSCM_USERNAME = $USER;
248 }
249
250 #
251 # password
252 #
253 if ($DASSCM_PASSWORD) {
254 $svnPasswordCredentials = " --password '$DASSCM_PASSWORD' ";
255 }
256 }
257
258 #$svnOptions .= " --username $DASSCM_USERNAME "
259}
260
261sub check_parameter(@)
262{
263}
264
265#
266# generate from (relative) filename
267# all required file and directory names:
268# $basename, $dirname_prod, $dirname_repo,
269# $filename_prod, $filename_repo
270#
271sub get_filenames(@)
272{
273 my $filename_prod = $_[0] || ".";
274
275 # make filename absolut
276 if ( !( $filename_prod =~ m/^\// ) ) {
277 $filename_prod = cwd() . "/" . $filename_prod;
278 }
279
280 if ( not -r $filename_prod ) {
281 fatalerror( $filename_prod . " is not accessable" );
282 }
283
284 # TODO: dirname buggy: eg. "/etc/" is reduced to "/",
285 # "/etc" is used as filename
286 my $dirname_prod = dirname($filename_prod);
287
288 # uses chdir to determine real directory in a unique way
289 chdir $dirname_prod or die $!;
290 $dirname_prod = cwd();
291 chdir $StartDirectory;
292
293 my $basename = basename($filename_prod);
294
295 if ($verbose) {
296 print "dir: " . $dirname_prod . "\n";
297 print "fn: " . $basename . "\n";
298 }
299
300 my $dirname_repo = $DASSCM_REPO . "/" . $dirname_prod;
301 my $filename_repo = "$dirname_repo/$basename";
302
303 return (
304 $basename, $dirname_prod, $dirname_repo,
305 $filename_prod, $filename_repo
306 );
307}
308
309
310
311#
312# creates a file with permissions
313#
314sub generatePermissionList
315{
316
317 # generieren der Zeilen für Permission-Savefile
318 my @files = @_;
319 my @permlist = ();
320 foreach my $file (@files) {
321 $file = "/" . $file;
322 if ( -e $file ) {
323 my $info = stat($file) || die "failed to stat $file: aborting";
324 my $mode = get_type( $info->mode ) & 07777;
325 my $modestring = sprintf( "%04o", $mode );
326 my $uid = $info->uid;
327 my $uidname = getpwuid($uid);
328 my $gid = $info->gid;
329 my $gidname = getgrgid($gid);
330 push(
331 @permlist,
332 sprintf( "%-55s %-17s %4d",
333 $file, "${uidname}:${gidname}", $modestring )
334 );
335 } else {
336 print
337 "failed to get status of $file. It exists in the repository, but not on the system\n";
338 }
339 }
340 return @permlist;
341}
342
343sub get_type
344{
345
346 # Funktion übernommen aus /usr/bin/chkstat
347 my $S_IFLNK = 0120000; # symbolic link
348 my $S_IFREG = 0100000; # regular file
349 my $S_IFDIR = 0040000; # directory
350 my $S_IFCHAR = 0020000; # character device
351 my $S_IFBLK = 0060000; # block device
352 my $S_IFFIFO = 0010000; # fifo
353 my $S_IFSOCK = 0140000; # socket
354 my $S_IFMT = 0170000; # type of file
355
356 my $S_m;
357 if ( ( $_[0] & $S_IFMT ) == $S_IFLNK ) { $S_m = $_[0] - $S_IFLNK; }
358 elsif ( ( $_[0] & $S_IFMT ) == $S_IFREG ) { $S_m = $_[0] - $S_IFREG; }
359 elsif ( ( $_[0] & $S_IFMT ) == $S_IFDIR ) { $S_m = $_[0] - $S_IFDIR; }
360 elsif ( ( $_[0] & $S_IFMT ) == $S_IFCHAR ) { $S_m = $_[0] - $S_IFCHAR; }
361 elsif ( ( $_[0] & $S_IFMT ) == $S_IFBLK ) { $S_m = $_[0] - $S_IFBLK; }
362 elsif ( ( $_[0] & $S_IFMT ) == $S_IFFIFO ) { $S_m = $_[0] - $S_IFFIFO; }
363 elsif ( ( $_[0] & $S_IFMT ) == $S_IFSOCK ) { $S_m = $_[0] - $S_IFSOCK; }
364 $S_m;
365}
366
367sub run_command
368{
369 my $command = shift;
370
371 #print "executing command: " . $command . "\n";
372
373 open( RESULT, $command . ' 2>&1 |' );
374 my @result = <RESULT>;
375 close(RESULT);
376 my $retcode = $? >> 8;
377
378 #print @result;
379 #if( $retcode ) { print "return code: " . $retcode . "\n"; }
380
381 return ( $retcode, @result );
382}
383
384sub run_interactive
385{
386
387 if ($verbose) {
388 print "run_interactive:" . join( " ", @_ ) . "\n";
389 }
390
391 system(@_);
392 if ( $? == -1 ) {
393 printf "failed to execute: $!\n";
394 } elsif ( $? & 127 ) {
395 printf "child died with signal %d, %s coredump\n", ( $? & 127 ),
396 ( $? & 128 ) ? 'with' : 'without';
397 } elsif ( $? >> 8 != 0 ) {
398 printf "child exited with value %d\n", $? >> 8;
399 }
400 return ( $? >> 8 );
401}
402
403sub svn_check_credentials( $$;$$ )
404{
405 my $username = shift;
406 my $password = shift;
407
408 # check silently are allow user interaction?
409 my $interactive = shift || 0;
410
411 # default: exit program, if repository is not accessable
412 # (do not exit for 'init')
413 my $fatalerror = shift || 1;
414
415 print "checking credentials ";
416
417 if ( !$username ) {
418 fatalerror("no username given");
419 }
420
421 if ( !$password ) {
422 fatalerror("no password given");
423 }
424
425 print "for " . $username . "@" . $DASSCM_SVN_REPOSITORY . ": ";
426
427 # Options for "svn info" are not supported by subversion 1.0.0 (SLES9),
428 # therefore switching to "svn status"
429 # ( my $rc_update, my @result ) =
430 # run_command(
431 # "$SVN info --non-interactive --no-auth-cache --username $username --password $password $DASSCM_SVN_REPOSITORY"
432 # );
433 #print @result;
434
435 my $rc_update;
436 if ($interactive) {
437 $rc_update =
438 run_interactive(
439 "$SVN ls --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
440 );
441 } else {
442 ( $rc_update, my @result ) =
443 run_command(
444 "$SVN ls --non-interactive --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
445 );
446
447 if ( $rc_update != 0 ) {
448 print "\n", @result;
449 if ($fatalerror) {
450 fatalerror();
451 }
452 return;
453 }
454 }
455
456 # return success
457 return $rc_update == 0;
458}
459
460sub svn_update( ;$ )
461{
462 if( ! $svnRepositoryIsUptodate ) {
463 my $update_path = shift || $DASSCM_REPO;
464 ( my $rc_update, my @result ) =
465 run_command(
466 "$SVN update --non-interactive $svnCheckoutCredentials $update_path");
467 print @result;
468 if ( $rc_update != 0 ) {
469 fatalerror();
470 } else {
471 $svnRepositoryIsUptodate = 1;
472 }
473 }
474}
475
476
477
478sub svn_getStoredFiles( ;$ )
479{
480
481 # TODO: get_filenames?
482 #my $rel_path = shift || "";
483 #my $path = "${DASSCM_REPO}/${rel_path}";
484 my $path = ${DASSCM_REPO};
485
486 # svn ls -R is better, but much, much slower
487 # ( my $rc, my @result ) = run_command("$SVN ls --recursive $svnCheckoutCredentials $path");
488 ( my $rc, my @result ) =
489 run_command(
490 "cd $path && find | grep -v '/.svn' | sed -e 's/\.\\///' | grep -v '^[\.]\$'"
491 );
492 if ( $rc != 0 ) {
493 print @result;
494 fatalerror;
495 }
496 chomp(@result);
497 return @result;
498}
499
500
501sub getModifiedFiles( ;$ )
502{
503 # TODO: get_filenames?
504 #my $rel_path = shift || "";
505 #my $path = "${DASSCM_REPO}/${rel_path}";
506 my $path = ${DASSCM_REPO};
507 my @files = svn_getStoredFiles($path);
508
509 # stores result from status (cvscheck)
510 my %removedfiles = ();
511 my %changedfiles = ();
512
513 # create list of modified files
514 if (@files) {
515
516 foreach my $file (@files) {
517
518 my $realfile = "/" . $file;
519 my $cvsworkfile = "${DASSCM_REPO}/${file}";
520
521 if ( -d $realfile ) {
522 ## directory. do nothing
523 } elsif ( !-r $realfile ) {
524 $removedfiles{"$realfile"} = $cvsworkfile;
525 } else {
526 ( -r "$cvsworkfile" )
527 || die("Fehler: $cvsworkfile ist nicht lesbar");
528 if ( compare( $cvsworkfile, $realfile ) != 0 ) {
529 $changedfiles{"$realfile"} = $cvsworkfile;
530 }
531 }
532 }
533 }
534
535 return ( \%changedfiles, \%removedfiles );
536}
537
538
539#
540# from an array of files/dirs,
541# generates list of files
542# sorted by type
543#
544sub get_files( @ )
545{
546 my @files = ();
547 my @links = ();
548 my @dirs = ();
549 my @others = ();
550
551 if (@_) {
552 find(
553 {
554 wanted => sub {
555 my $fullname = cwd() . "/" . $_;
556 if ( -l $_ ) {
557
558 # soft link
559 # important: check for links first
560 # to exclude them from further checks
561 push( @links, $fullname );
562 } elsif ( -d $_ ) {
563 push( @dirs, $fullname );
564 } elsif ( -f $_ ) {
565
566 # regular file
567 push( @files, $fullname );
568 } else {
569 push( @others, $fullname );
570 }
571 }
572 },
573 @_
574 );
575 }
576
577 # don't rely on others.
578 # If more specific file types are needed,
579 # they will be added
580 return {
581 files => \@files,
582 links => \@links,
583 dirs => \@dirs,
584 others => \@others
585 };
586}
587
588#####################################################################
589#
590# functions
591
592sub help(;@)
593{
594 if ( @_ == 0 ) {
595 usage();
596 } else {
597 print "help for @_: ...\n";
598 usage();
599 }
600}
601
602sub login(@)
603{
604 check_parameter( @_, 1 );
605 check_env();
606
607 my $input_username = $_[0];
608
609 if ( not $input_username ) {
610 my $output_username = "";
611 if ($DASSCM_USERNAME) {
612 $output_username = " ($DASSCM_USERNAME)";
613 }
614
615 print "Enter DASSCM user name", $output_username, ": ";
616 $input_username = <STDIN>;
617 chomp($input_username);
618
619 $input_username = $input_username || $DASSCM_USERNAME;
620 }
621
622 # hidden password input
623 print "Enter password for $input_username: ";
624 ReadMode('noecho');
625 my $input_password = <STDIN>;
626 ReadMode('normal');
627 chomp($input_password);
628 print "\n";
629
630 # checking checkout username/password
631 svn_check_credentials( $DASSCM_CHECKOUT_USERNAME,
632 $DASSCM_CHECKOUT_PASSWORD );
633 print "checkout access okay\n";
634
635 svn_check_credentials( $input_username, $input_password );
636
637 #
638 # set environment variables
639 #
640 $ENV{'DASSCM_USERNAME'} = "$input_username";
641 $ENV{'DASSCM_PASSWORD'} = "$input_password";
642
643 print "subversion access okay\n\n", "DASSCM_USERNAME: $input_username\n",
644 "DASSCM_PASSWORD: (hidden)\n", "DASSCM_PROD: $DASSCM_PROD\n",
645 "DASSCM_REPO: $DASSCM_REPO\n",
646 "Server Repository: $DASSCM_SVN_REPOSITORY\n", "\n", "[dasscm shell]\n\n";
647
648 my $shell = $SHELL || "bash";
649 exec($shell) or die "failed to start new shell";
650}
651
652
653#
654# initialize local checkout directory (initial checkout)
655#
656sub init(@)
657{
658 check_parameter( @_, 1 );
659 check_env();
660
661 # don't do repository creation (svn mkdir) here,
662 # because then their must be a lot of prior checks
663
664 # update complete repository
665 # and create permission file
666 my $retcode =
667 run_interactive(
668 "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY; mkdir -p `dirname $permissions_file`; touch $permissions_file"
669 );
670}
671
672
673
674sub ls(@)
675{
676 check_parameter( @_, 1 );
677 check_env();
678
679 my @files = svn_getStoredFiles(@_);
680
681 print join( "\n", @files );
682 print "\n";
683}
684
685
686
687sub update(@)
688{
689 check_parameter( @_, 1 );
690 check_env();
691
692 #
693 # update local repository
694 #
695 svn_update();
696}
697
698#
699# helper function for "add" command
700#
701sub add_helper(@)
702{
703 (
704 my $basename,
705 my $dirname_prod,
706 my $dirname_repo,
707 my $filename_prod,
708 my $filename_repo
709 )
710 = get_filenames( $_[0] );
711
712 if ( $command eq "add" ) {
713 mkpath($dirname_repo);
714 }
715
716 # TODO: are permissions also copied?
717 copy( $filename_prod, $filename_repo )
718 or error "failed to copy $filename_prod to repository: $!";
719
720 if ( $command eq "add" ) {
721
722 # already checked in?
723 chdir $DASSCM_REPO;
724
725 # also add the path to filename.
726 for my $dir ( split( '/', $dirname_prod ) ) {
727 if ($dir) {
728 my ( $rc, @out ) =
729 run_command( "$SVN add --non-recursive \"" . $dir . "\"" );
730 if ( $rc > 0 ) {
731 print join( "\n", @out );
732 }
733 chdir $dir;
734 }
735 }
736 my ( $rc, @out ) = run_command( "$SVN add \"" . $basename . "\"" );
737 if ( $rc > 0 ) {
738 print join( "\n", @out );
739 }
740 chdir $StartDirectory;
741 }
742}
743
744
745
746#
747# add (is used for command add and commit)
748#
749sub add(@)
750{
751 check_parameter( @_, 1 );
752 check_env();
753
754 #
755 # update local repository
756 #
757 svn_update();
758
759 # get all regular files and links
760 my $href_files = get_files(@_);
761
762 #print Dumper( $href_files );
763
764 my @files = @{ $href_files->{files} };
765 my @links = @{ $href_files->{links} };
766
767 if (@files) {
768 my $number = $#files + 1;
769 print "files to check-in ($number): \n";
770 print join( "\n", @files );
771 print "\n";
772 }
773
774 # TODO: check in links and also link target? At least warn about link target
775 if (@links) {
776 my $number = $#links + 1;
777 print "\n";
778 print "ignoring links ($number):\n";
779 print join( "\n", @links );
780 print "\n";
781 }
782
783 # TODO: confirm
784
785 # copy files one by one to local repository
786 for my $file (@files) {
787
788 # add file
789 add_helper($file);
790 }
791
792 # create new permissions file
793 permissions();
794
795 # add permissions file
796 add_helper($permissions_file);
797
798 if ( $options{'message'} ) {
799 $svnOptions .= " --message \"$options{'message'}\" ";
800 }
801
802 # commit calls $EDITOR.
803 # use "interactive" here, to display output
804 my $retcode =
805 run_interactive(
806 "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
807 );
808}
809
810
811
812#
813# checks in all modified files
814#
815sub commitall(@)
816{
817 check_parameter( @_, 1 );
818 check_env();
819
820 # TODO: get_filenames?
821 #my $rel_path = shift || "";
822 #my $path = "${DASSCM_REPO}/${rel_path}";
823 my $dir = ${DASSCM_REPO};
824
825 #
826 # update local repository
827 #
828 svn_update();
829
830 ( my $refChangedFiles, my $refRemovedFiles ) = getModifiedFiles( $dir );
831 my %changedfiles = %{$refChangedFiles};
832 my %removedfiles = %{$refRemovedFiles};
833
834 if( %removedfiles ) {
835 my $removedFilesString = '"' . join( '" "', values(%removedfiles) ) . '"';
836 my ( $rc, @out ) = run_command( "$SVN rm $removedFilesString" );
837 if ( $rc > 0 ) {
838 print join( "\n", @out );
839 }
840 }
841
842 # add will care about the rest, including permission file and commiting
843 add( keys %changedfiles );
844}
845
846
847
848sub blame(@)
849{
850 check_parameter( @_, 1 );
851 check_env();
852
853 (
854 my $basename,
855 my $dirname_prod,
856 my $dirname_repo,
857 my $filename_prod,
858 my $filename_repo
859 )
860 = get_filenames( $_[0] );
861
862 my $retcode = run_interactive("$SVN blame $svnOptions $filename_repo");
863}
864
865sub diff(@)
866{
867 check_parameter( @_, 1 );
868 check_env();
869
870 (
871 my $basename,
872 my $dirname_prod,
873 my $dirname_repo,
874 my $filename_prod,
875 my $filename_repo
876 )
877 = get_filenames( $_[0] );
878
879 #print "$basename,$dirname_prod,$dirname_repo\n";
880
881 ( my $rc_update, my @result ) = run_command("$SVN update $filename_repo");
882 if ( $rc_update != 0 ) {
883 print @result;
884 die;
885 }
886
887 ( my $rc_diff, my @diff_result ) =
888 run_command( $diff . " $filename_repo $filename_prod" );
889
890 print @diff_result;
891}
892
893sub status(@)
894{
895 check_parameter( @_, 1 );
896 check_env();
897
898 # return code for the shell
899 # default: error
900 my $return_code = $RETURN_NOK;
901
902 #
903 # update local repository
904 #
905 svn_update();
906
907 # TODO: start at subdirectories ?
908 my $dir = $DASSCM_REPO;
909 ( my $refChangedFiles, my $refRemovedFiles ) = getModifiedFiles( $dir );
910 my %changedfiles = %{$refChangedFiles};
911 my %removedfiles = %{$refRemovedFiles};
912
913 if( %removedfiles or %changedfiles ) {
914 if (%removedfiles) {
915 print "deleted files (found in repository, but not in system):\n";
916 foreach my $key ( values %removedfiles ) {
917 print "$key\n";
918 }
919 print "\n";
920 }
921
922 if (%changedfiles) {
923 print "modified files:\n";
924 foreach my $key ( keys %changedfiles ) {
925 print "$key\n";
926 }
927 }
928 } else {
929 print "no modified files found in $dir\n";
930 $return_code = $RETURN_OK;
931 }
932
933 return $return_code;
934}
935
936
937
938sub permissions(@)
939{
940 check_parameter( @_, 1 );
941 check_env();
942
943 #
944 # update local repository
945 #
946 #svn_update();
947
948 # TODO: start at subdirectories ?
949 my $dir = $DASSCM_REPO;
950 my @files = svn_getStoredFiles($dir);
951
952 if (@files) {
953
954 # generieren der Permissions
955 my @permissions = generatePermissionList(@files);
956 my $OUTFILE;
957 my $tofile = 0; # Status für schreiben in File
958
959 if ( -w dirname($permissions_file) ) {
960
961 # Verzeichnis existiert => schreiben
962 print "storing permissions in file $permissions_file\n";
963 open( OUTFILE, ">$permissions_file" )
964 || die("failed to write to $permissions_file: $!");
965 $tofile = 1; # Merken, daß in File geschrieben wird
966 print OUTFILE "#\n";
967 print OUTFILE "# created by dasscm permissions\n";
968 print OUTFILE
969 "# It is intended to be used for restoring permissions\n";
970 } else {
971
972 # Pfad für Sicherungsdatei existiert nicht => schreiben auf stdout
973 # Alias Filehandle für stdout erzeugen
974 *OUTFILE = *STDOUT;
975 }
976 foreach my $line (@permissions) {
977 print OUTFILE "$line\n";
978 }
979
980 if ($tofile) {
981 close(OUTFILE);
982 }
983 }
984}
985
986
987
988#####################################################################
989#
990# main
991#
992
993my $return_code = $RETURN_OK;
994my $number_arguments = @ARGV;
995
996if ( $number_arguments > 0 ) {
997
998 # get subcommand and remove it from @ARGV
999 $command = $ARGV[0];
1000 shift @ARGV;
1001
1002 $DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
1003 $DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
1004
1005 # TODO: check variables
1006 $DASSCM_SVN_REPOSITORY =
1007 $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
1008
1009 $DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
1010 $DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
1011
1012 #
1013 # if a user is given by dasscm configuration file, we use it.
1014 # Otherwise we expect that read-only account is configured
1015 # as local subversion configuration.
1016 # If this is also not the case,
1017 # user is required to type username and password.
1018 # This will be stored as local subversion configuration thereafter.
1019 #
1020 if ( $DASSCM_CHECKOUT_USERNAME && $DASSCM_CHECKOUT_PASSWORD ) {
1021 $svnCheckoutCredentials =
1022 " --username $DASSCM_CHECKOUT_USERNAME --password $DASSCM_CHECKOUT_PASSWORD ";
1023 }
1024
1025 # get command line options and store them in options hash
1026 my $result = GetOptions( \%options, 'verbose', 'message=s' );
1027
1028 # print options
1029 foreach my $option ( keys %options ) {
1030 print "${option}: $options{$option}\n";
1031 }
1032
1033 # set verbose to command line option
1034 $verbose = $options{'verbose'};
1035
1036 #
1037 # action accordinly to command are taken
1038 # $command is rewritten in standard format,
1039 # so we can test for it later on more simply
1040 #
1041 $_ = $command;
1042 if (m/help/i) {
1043 help(@ARGV);
1044 } elsif (m/login/i) {
1045 $command = "login";
1046 login(@ARGV);
1047 } elsif (m/init/i) {
1048 $command = "init";
1049 init(@ARGV);
1050 } elsif (m/ls/i) {
1051 $command = "ls";
1052 ls(@ARGV);
1053 } elsif ( (m/update/i) || (m/up/i) ) {
1054 $command = "update";
1055 update(@ARGV);
1056 } elsif ( (m/commitall/i) || (m/cia/i) ) {
1057 $command = "commitall";
1058 commitall(@ARGV);
1059 } elsif (m/add/i) {
1060 $command = "add";
1061 add(@ARGV);
1062 } elsif ( (m/commit/i) || (m/checkin/i) || (m/ci/i) ) {
1063 $command = "commit";
1064 add(@ARGV);
1065 } elsif (m/blame/i) {
1066 $command = "blame";
1067 blame(@ARGV);
1068 } elsif (m/diff/i) {
1069 $command = "diff";
1070 diff(@ARGV);
1071 } elsif ( (m/status/i) || (m/st/i) ) {
1072 $command = "status";
1073 $return_code = status(@ARGV);
1074 } elsif (m/permissions/i) {
1075 $command = "permissions";
1076 permissions(@ARGV);
1077 } else {
1078 print "unknown command: $command\n\n";
1079 usage();
1080 check_env();
1081 $return_code = $RETURN_NOK;
1082 }
1083
1084 # cleanup (svn-commit.tmp)
1085 # revert
1086 # activate
1087 # rm
1088
1089}
1090
1091exit $return_code;
Note: See TracBrowser for help on using the repository browser.