source: trunk/dasscm/dasscm@ 196

Last change on this file since 196 was 196, checked in by joergs, on Dec 6, 2004 at 5:42:11 PM

add -m Option. Unified add and commit. svn update without --username to prevent that the password had to be typed every time

  • Property keyword set to id
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 6.2 KB
Line 
1#!/usr/bin/perl -w
2
3# $Id: dasscm 196 2004-12-06 16:42:11Z joergs $
4
5use strict;
6
7use Env qw($DASSCM_PROD $DASSCM_REPO $USER $DASSCM_USER $DASSCM_PW);
8use Cwd;
9use File::Basename;
10use File::stat;
11use File::Path;
12use File::Copy;
13#use POSIX qw/getpgrp tcgetpgrp/;
14use Getopt::Long;
15
16#####################################################################
17#
18# global
19#
20my $SVN = "svn ";
21my $svnOptions = "";
22# command line options get stored in options hash
23my %options = ();
24my $command;
25
26#####################################################################
27#
28# util functions
29#
30sub usage()
31{
32 print "usage: dasscm <subcommand> [options] [args]\n";
33 print "\n";
34 print "dasscm is intended to help versioning configuration files\n";
35 print "\n";
36 print "Available subcommands:\n";
37 print " add <filename>\n";
38 print " commit <filename>\n";
39 print " diff <filename>\n";
40 print " help <subcommand>\n";
41 print "\n";
42 print "preperation:\n";
43 print "check out the configuration repository, e.g.\n";
44 print "svn checkout --no-auth-cache --username USERNAME https://dass-it.de/svn/lvermgeo/technical/config/\n";
45 print "environment variables DASSCM_REPO, DASSCM_PROD and DASSCM_USER are evaluated.\n";
46 print "\n";
47}
48
49sub check_env()
50{
51 # DASSCM_PROD
52 if ( ! $DASSCM_PROD ) {
53 $DASSCM_PROD = "/";
54 }
55 print "DASSCM_PROD: ".$DASSCM_PROD."\n";
56 if ( ! -d $DASSCM_PROD ) {
57 die "DASSCM_PROD is not set to a directory.\n";
58 }
59
60 # DASSCM_REPO
61 if ( ! $DASSCM_REPO ) {
62 die "Envirnonment variable DASSCM_REPO not set.\nSet DASSCM_REPO to the directory of the versioning system checkout for this machine.\n";
63 }
64 print "DASSCM_REPO: ".$DASSCM_REPO."\n";
65 if ( ! -d $DASSCM_REPO ) {
66 die "DASSCM_REPO must be is not set to the directory of the versioning system checkout for this machine.\n";
67 }
68
69 # User settings
70 # user root is not allowed for checkins.
71 # if user is root, DASSCM_USER has to be set,
72 # otherwise USER can be used
73 if ( "$USER" eq "root" ) {
74 if ( ! $DASSCM_USER ) {
75 die "Envirnonment variable DASSCM_USER not set.\nSet DASSCM_USER to your subversion user account.\n";
76 }
77 $svnOptions .= " --no-auth-cache "
78 } elsif ( ! $DASSCM_USER ) {
79 $DASSCM_USER=$USER;
80 }
81 #$svnOptions .= " --username $DASSCM_USER "
82}
83
84sub check_parameter(@)
85{
86}
87
88sub get_filenames(@)
89{
90 my $filename_prod = $_[0];
91 if ( !($filename_prod =~ m/^\//) ) {
92 $filename_prod = cwd()."/".$filename_prod;
93 }
94
95 -r $filename_prod or die "$filename_prod is not accessable";
96
97 # TODO: dirname buggy: eg. "/etc/" is reduced to "/",
98 # "/etc" is used as filename
99 my $dirname_prod = dirname($filename_prod);
100 chdir $dirname_prod or die $!;
101 $dirname_prod = cwd();
102 my $basename = basename($filename_prod);
103
104 print "dir: ".$dirname_prod."\n";
105 print "fn: ".$basename."\n";
106
107 my $dirname_repo = $DASSCM_REPO."/".$dirname_prod;
108 my $filename_repo = "$dirname_repo/$basename";
109
110 return ($basename,$dirname_prod,$dirname_repo,$filename_prod,$filename_repo);
111}
112
113
114sub run_command
115{
116 my $command = shift;
117
118 #print "executing command: " . $command . "\n";
119
120 open(RESULT, $command . ' 2>&1 |' );
121 my @result = <RESULT>;
122 close(RESULT);
123 my $retcode = $?>>8;
124
125 #print @result;
126 #if( $retcode ) { print "return code: " . $retcode . "\n"; }
127
128 return($retcode, @result);
129}
130
131
132
133sub run_interactive
134{
135 system( @_ );
136 if ($? == -1) {
137 printf "failed to execute: $!\n";
138 } elsif ($? & 127) {
139 printf "child died with signal %d, %s coredump\n",
140 ($? & 127), ($? & 128) ? 'with' : 'without';
141 } elsif( $? >> 8 != 0 ) {
142 printf "child exited with value %d\n", $? >> 8;
143 }
144 }
145 return( $? >> 8 );
146}
147
148
149#####################################################################
150#
151# functions
152
153sub help(;@)
154{
155 if( @_ == 0 ) {
156 usage();
157 } else {
158 print "help for @_: ...\n";
159 }
160}
161
162#
163# add (is used for command add and commit)
164#
165sub add(@)
166{
167 check_parameter(@_,1);
168 check_env();
169
170 (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
171
172 if( $command eq "add" ) {
173 mkpath($dirname_repo);
174 }
175
176 # update complete repository
177 my $retcode = run_interactive( "$SVN update $svnOptions $DASSCM_REPO" );
178
179 copy( $filename_prod, $filename_repo ) or die $!;
180
181 if( $command eq "add" ) {
182 # already checked in?
183 chdir($DASSCM_REPO);
184 # also add the path to filename.
185 for my $dir (split('/', $dirname_prod) ) {
186 if( $dir ) {
187 run_command( "$SVN add --non-recursive $dir" );
188 chdir $dir;
189 }
190 }
191 run_command( "$SVN add $basename" );
192 }
193
194 if( $options{'message'} ) {
195 $svnOptions .= " --message $options{'message'} ";
196 }
197
198 # commit calls $EDITOR. uses "system" here, to display output
199 my $retcode = run_interactive( "$SVN commit $svnOptions --username $DASSCM_USER $DASSCM_REPO" );
200
201 print $filename_prod."\n";
202 print $dirname_repo."\n";
203}
204
205
206sub blame(@)
207{
208 check_parameter(@_,1);
209 check_env();
210
211 (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
212
213 my $retcode = run_interactive( "$SVN blame $svnOptions $filename_repo" );
214}
215
216
217
218sub diff(@)
219{
220 check_parameter(@_,1);
221 check_env();
222
223 (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
224
225 #print "$basename,$dirname_prod,$dirname_repo\n";
226
227 (my $rc_update, my @result)=run_command( "$SVN update $filename_repo" );
228 if( $rc_update != 0 ) {
229 print @result;
230 die;
231 }
232
233 (my $rc_diff, my @diff)=run_command( "diff $filename_repo $filename_prod" );
234 print @diff;
235}
236
237#####################################################################
238#
239# main
240#
241
242my $number_arguments = @ARGV;
243#print "$number_arguments\n";
244
245if ($number_arguments > 0) {
246 # get subcommand and remove it from @ARGV
247 $command = $ARGV[0];
248 shift @ARGV;
249
250
251 # get command line options and store them in options hash
252 my $result = GetOptions ( \%options, 'message=s' );
253 foreach my $option (keys %options) {
254 print $option.": ".$options{$option}."\n";
255 }
256
257 $_=$command;
258 if (m/help/i) {
259 help(@ARGV);
260 } elsif (m/add/i) {
261 # rewrite command. just to make sure
262 $command = "add";
263 add( @ARGV);
264 } elsif (m/commit/i) {
265 # rewrite command. just to make sure
266 $command = "commit";
267 add( @ARGV);
268 } elsif (m/blame/i) {
269 blame(@ARGV);
270 } elsif (m/diff/i) {
271 diff(@ARGV);
272 } elsif (m/activate/i) {
273 activate(@ARGV);
274 } else {
275 usage();
276 check_env();
277 }
278}
Note: See TracBrowser for help on using the repository browser.