Initial add of defaria.com
[clearscm.git] / defaria.com / Computers / code / bin / clearcase / triggers / RemoveEmptyBranch.pl
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         RemoveEmptyBranch.pl
5 # Description:  This trigger script is remove empty branches. If a branch has
6 #               no elements (except the 0 element of course) after an uncheckout
7 #               or rmver, or the parent of a just-rmbranched branch is now empty,
8 #               remove it.
9 #
10 #               Install like this:
11 #
12 #               ct mktrtype -element -global -postop uncheckout,rmver,rmbranch \
13 #               -c "Remove empty branches after uncheckout, rmver, or rmbranch" \
14 #               -exec T:/Triggers/RemoveEmptyBranch RM_EMPTY_BRANCH
15 # Assumptions:  Clearprompt is in the users PATH
16 # Author:       Andrew@DeFaria.com
17 # Created:      Fri May 23 13:23:47 PDT 2003
18 # Language:     Perl
19 # Modifications:
20 #
21 # (c) Copyright 2003, Andrew@DeFaria.com, all rights reserved
22 #
23 ################################################################################
24 use strict;
25
26 BEGIN {
27   # Add the appropriate path to our modules to @INC array. We use ipconfig to
28   # get the current host's IP address then determine whether we are in the US
29   # or China. If neither then we fallback to using T:/Triggers.
30   my @ipconfig = grep (/IP Address/, `ipconfig`);
31   my ($ipaddr) = ($ipconfig[0] =~ /(\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3})/);
32
33   # US is in the subnets of 192 and 172 while China is in the subnet of 10
34   if ($ipaddr =~ /^192|^172/) {
35     unshift (@INC, "//sons-clearcase/Views/official/Tools/lib");
36   } elsif ($ipaddr =~ /^10/) {
37     unshift (@INC, "//sons-cc/Views/official/Tools/lib");
38   } else {
39     die "Internal Error: Unable to find our modules!\n"
40   } # if
41 } # BEGIN
42
43 use TriggerUtils;
44
45 # The following environment variables are set by Clearcase when this
46 # trigger is called
47 my $xname  = $ENV{CLEARCASE_XPN};
48 my $opkind = $ENV{CLEARCASE_OP_KIND};
49 my $xn_sfx = $ENV{CLEARCASE_XN_SFX};
50 my $os     = $ENV{OS};
51 my $brtype = $ENV{CLEARCASE_BRTYPE};
52 #clearlog "Checking to see if the branch is empty and needs to be removed";
53 #clearlog "xname  = $xname";
54 #clearlog "opkind = $opkind";
55 #clearlog "xn_sfx = $xn_sfx";
56 #clearlog "os     = $os";
57
58 $xname =~ s/\\/\//g if $ENV{OS} eq "Windows_NT";
59
60 # For uncheckout, if the remaining version is not 0 then we are done;
61 exit 0 if ($opkind eq "uncheckout" && $xname !~ m/\/0$/);
62
63 #clearlog "Continuing...";
64 my $branch;
65
66 ($branch = $xname) =~ s/\/[^\/]*$//;
67
68 #clearlog "branch = $branch; xname = $xname";
69
70 # Don't try to remove the /main branch
71 exit 0 if $branch =~ m/\@\@\/main$/;
72
73 # Check if there are other versions, branches, labels or checked out versions
74 # on this branch. If so don't do anything.
75 if (opendir (D, $branch)) {
76   # This opendir succeeds only in a dynamic view
77   #clearlog "In dynamic view!";
78   my @other_stuff = readdir (D);
79   closedir (D);
80
81   # In an empty branch there are four things: ".", "..", "0" an d"LATEST".
82   # If there are more then it isn't an empty branch
83   exit if (scalar (@other_stuff) != 4);
84 } else {
85   # Snapshot views.
86   #clearlog "In snapshot view!";
87   my ($pname, $brpath) = split ($xn_sfx, $branch);
88   #clearlog "pname = $pname; brpath = $brpath";
89   # rmbranch will not reload the element...
90   system "cleartool update -log /dev/null \"$pname\"" if ($opkind eq "rmbranch");
91   my @vtree = `cleartool lsvtree -branch $brpath \"$pname\"`;
92   my $latest;
93   chomp ($latest = pop (@vtree));
94   $latest =~ tr/\\/\// if $os eq "Windows_NT";
95   #clearlog "latest = $latest";
96   exit 0 unless $latest =~ m/$brpath\/0$/;
97 } # if
98
99 # Remove the branch!
100 clearlog "After $opkind branch is empty - removing empty branch $brtype";
101 #clearlog "About to cleartool rmbranch -force -nc \"$branch\"";
102 system "cleartool rmbranch -force -nc \"$branch\"";
103
104 exit 0;