update readme and add gitignore
[client-tools.git] / tools / stage_label_automatic_update.pl
blobb3c3347375850a712b3345bed645f1c81ea12a3e
1 #! /usr/bin/perl
3 use warnings;
4 use strict;
5 use Socket;
7 die "usage: $0 [max_changelist]\n\tProcess change lists and automatically push appropriate ones to the stage label\n" if (@ARGV > 1 || !($ARGV[0] =~ /^\d+$/));
9 # ======================================================================
10 # set up global constants
12 my $counter = "swg-stage-counter";
13 my $label = "swg-stage-label";
14 my $group = "swg_stage_group";
16 # ======================================================================
18 my $maxChangeList = shift;
19 if (!defined $maxChangeList)
21 open(P4, "p4 counter change |") || die "$0: p4 review failed";
22 $maxChangeList = <P4>;
23 chomp $maxChangeList;
24 close(P4);
27 # get the list of changelists to review
28 open(P4, "p4 review -t $counter |") || die "$0: p4 review failed";
29 my @review = <P4>;
30 close(P4);
32 # bail out fast if there are no new change lists
33 if (@review == 0)
35 print STDERR "No new changelists, done.\n";
36 exit(0);
39 # get the stage group membership from perforce
40 my %stageUsers;
41 open(P4, "p4 group -o $group |") || die "$0: p4 group failed";
42 while (<P4>)
44 last if (/^Users:/);
46 while (<P4>)
48 chomp;
49 s/\s+//g;
50 $stageUsers{$_} = 1;
52 close(P4);
54 # process all change lists
55 my %sync;
56 my $changeList;
57 while (@review)
59 # process this submitted change list
60 $_ = shift @review;
61 chomp;
62 my $user;
64 my $junk;
65 my $rest;
66 ($junk, $changeList, $user, $rest) = split;
69 # keep to out max changelist
70 last if ($changeList > $maxChangeList);
72 # read the change description
73 open(P4, "p4 -ztag describe -s $changeList |") || die "$0: p4 change failed";
74 my @describe = <P4>;
75 chomp @describe;
76 close(P4);
78 # check if this change list should go to stage
79 my $update = defined($stageUsers{$user}) ? 1 : 0;
80 $update = 1 if (grep /\[stage\]/i, @describe);
81 $update = 0 if (grep /\[no[ _]*stage\]/i, @describe);
83 if ($update)
85 my $update = 0;
86 my $file;
87 foreach (@describe)
89 $file = $_ if (s/^\.\.\.\s+depotFile\d+\s+// && (m%^//depot/swg/current/data/% || m%^//depot/swg/current/dsrc/%));
90 if (defined $file && s/^\.\.\.\s+rev\d+\s+//)
92 $update += 1;
93 $sync{$file} = $_;
94 undef $file;
98 print STDERR "\t$changeList [stage] $user ($update files)\n";
100 else
102 print STDERR "\t$changeList [no stage] $user\n";
106 # see if we need to update any files
107 my $sync = scalar keys %sync;
108 if ($sync != 0)
110 # figure out my perforce user name
111 my $userName;
112 open(P4, "p4 info |");
113 while (<P4>)
115 chomp;
116 $userName = $_ if (s/User name: //);
118 close(P4);
120 # check who owns the label
121 my $isOwner = 1;
122 my @label;
123 open(P4, "p4 label -o $label |");
124 while (<P4>)
126 chomp;
127 if (/^Owner:\t/ && $_ ne "Owner:\t$userName")
129 $_ = "Owner:\t$userName";
130 $isOwner = 0;
132 push(@label, $_);
134 close(P4);
136 # make me the owner of the label if I wasn't already
137 if (!$isOwner)
139 open(P4, "| p4 label -i > nul");
140 foreach (@label)
142 print P4 $_, "\n";
144 close(P4);
147 my $start = time;
148 print STDERR "Updating the label... (", $sync, " files)\n";
149 open(P4, "| p4 -x - labelsync -l $label > nul") || die "$0: labelsync failed\n";
150 foreach (sort keys %sync)
152 print P4 $_, "#", $sync{$_}, "\n";
154 close(P4);
155 my $elapsed = time - $start;
156 print STDERR "Update took ", $elapsed, " seconds\n";
159 # update the counter to the last processed change list
160 system("p4 counter $counter $changeList > nul") == 0 || die "$0: counter update failed\n";
162 print STDERR "Stage label update complete to changelist $changeList.\n";