2 ################################################################################
4 # File: RemoveEmptyBranch.pl,v
6 # Description: This trigger script is remove empty branches. If a branch has
7 # no elements (except the 0 element of course) after an uncheckout
8 # remove it and the branch.
9 # Trigger Type: All element
10 # Operation: Postop rmbranch, uncheckout
11 # Author: Andrew@DeFaria.com
12 # Created: Fri Mar 12 10:17:44 PST 2004
13 # Modified: 2007/05/17 07:45:48
16 # (c) Copyright 2004, ClearSCM, Inc., all rights reserved
18 ################################################################################
24 my $debug = $ENV{TRIGGER_DEBUG};
25 my $windows = ($^O || $ENV{OS}) =~ /MSWin32|Windows_NT/i ? "yes" : "no";
26 my $SEPARATOR = $windows eq "yes" ? "\\" : "/";
27 my $null = $windows eq "yes" ? "NUL" : "/dev/null";
31 my $tmpdir = $ENV{TMP};
32 my $trigger_debug_log = "$tmpdir/trigger_debug.log";
34 open my $debugLog, '>>', $trigger_debug_log
35 or croak "Unable to open $trigger_debug_log";
43 return if !defined $debug;
45 $trigger_file = InitDebug if !defined $trigger_file;
47 print $trigger_file "$msg\n";
52 # The following environment variables are set by Clearcase when this
54 my $xname = $ENV{CLEARCASE_XPN};
55 my $xn_sfx = $ENV{CLEARCASE_XN_SFX};
56 my $opkind = $ENV{CLEARCASE_OP_KIND};
57 my $brtype = $ENV{CLEARCASE_BRTYPE};
58 my $view_type = $ENV{CLEARCASE_VIEW_KIND};
60 debug "RM_EMPTY_BRANCH Trigger:";
61 debug "CLEARCASE_XPN = $xname";
62 debug "CLEARCASE_XN_SFX = $xn_sfx";
63 debug "CLEARCASE_OP_KIND = $opkind";
64 debug "CLEARCASE_BRTYPE = $brtype";
65 debug "CLEARCASE_VIEW_KIND = $view_type";
67 $xname =~ s/\\/\//g if $windows eq "yes";
69 # For uncheckout, if the remaining version is not 0 then we are done -
70 # the most common case...
71 exit 0 if ($opkind eq "uncheckout" && $xname !~ m/\/0$/);
75 if ($opkind eq "uncheckout") {
76 # Remove the last component
77 $branch =~ s/\/[^\/]*$//;
80 # Don't try to remove the /main branch
81 exit 0 if $branch =~ m/$xn_sfx\/main$/;
83 # Check if there are other versions, branches, labels or checked out versions
84 # on this branch. If so don't do anything.
85 if ($view_type eq "dynamic") {
86 opendir (DIR, $branch);
87 my @entries = readdir (DIR);
90 # In an empty branch there are four things: ".", "..", "0" an d"LATEST".
91 # If there are more then it isn't an empty branch
92 exit 0 if (scalar (@entries) != 4);
95 my ($pname, $brpath) = split ($xn_sfx, $branch);
97 # The rmbranch will not reload the element. This shows as "special
98 # selection, deleted version" in snapshot views This cleans that up.
99 if ($opkind eq "rmbranch") {
100 system "cleartool update -log $null \"$pname\"" if ($opkind eq "rmbranch");
101 exit 0; # Nothing else to do here...
104 my @vtree = `cleartool lsvtree -branch $brpath \"$pname\"`;
106 chomp ($latest = pop (@vtree));
107 $latest =~ tr/\\/\// if $windows eq "yes";
109 exit 0 unless $latest =~ m/$brpath\/0$/;
113 debug "Removing empty branch $branch";
114 system "cleartool rmbranch -force -nc \"$branch\"";