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