| 1 | #!/usr/bin/perl |
| 2 | ################################################################################ |
| 3 | # |
| 4 | # File: RemoveEmptyBranch.pl,v |
| 5 | # Revision: 1.1.1.1 |
| 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 |
| 14 | # Language: Perl |
| 15 | # |
| 16 | # (c) Copyright 2004, ClearSCM, Inc., all rights reserved |
| 17 | # |
| 18 | ################################################################################ |
| 19 | use strict; |
| 20 | use warnings; |
| 21 | |
| 22 | my $debug = $ENV{TRIGGER_DEBUG}; |
| 23 | my $windows = ($^O || $ENV{OS}) =~ /MSWin32|Windows_NT/i ? "yes" : "no"; |
| 24 | my $separator = $windows eq "yes" ? "\\" : "/"; |
| 25 | my $null = $windows eq "yes" ? "NUL" : "/dev/null"; |
| 26 | my $trigger_file; |
| 27 | |
| 28 | sub InitDebug { |
| 29 | my $tmpdir = $ENV{TMP}; |
| 30 | my $trigger_debug_log = "$tmpdir${separator}trigger_debug.log"; |
| 31 | |
| 32 | open TRIGGER_DEBUG_LOG, ">>$trigger_debug_log" |
| 33 | or die "Unable to open $trigger_debug_log"; |
| 34 | |
| 35 | return *TRIGGER_DEBUG_LOG; |
| 36 | } # InitDebug |
| 37 | |
| 38 | sub debug { |
| 39 | my $msg = shift; |
| 40 | |
| 41 | return if !defined $debug; |
| 42 | |
| 43 | $trigger_file = InitDebug if !defined $trigger_file; |
| 44 | |
| 45 | print $trigger_file "$msg\n"; |
| 46 | } # debug |
| 47 | |
| 48 | # The following environment variables are set by Clearcase when this |
| 49 | # trigger is called |
| 50 | my $xname = $ENV{CLEARCASE_XPN}; |
| 51 | my $xn_sfx = $ENV{CLEARCASE_XN_SFX}; |
| 52 | my $opkind = $ENV{CLEARCASE_OP_KIND}; |
| 53 | my $brtype = $ENV{CLEARCASE_BRTYPE}; |
| 54 | my $view_type = $ENV{CLEARCASE_VIEW_KIND}; |
| 55 | |
| 56 | debug "RM_EMPTY_BRANCH Trigger:"; |
| 57 | debug "CLEARCASE_XPN = $xname"; |
| 58 | debug "CLEARCASE_XN_SFX = $xn_sfx"; |
| 59 | debug "CLEARCASE_OP_KIND = $opkind"; |
| 60 | debug "CLEARCASE_BRTYPE = $brtype"; |
| 61 | debug "CLEARCASE_VIEW_KIND = $view_type"; |
| 62 | |
| 63 | $xname =~ s/\\/\//g if $windows eq "yes"; |
| 64 | |
| 65 | # For uncheckout, if the remaining version is not 0 then we are done - |
| 66 | # the most common case... |
| 67 | exit 0 if ($opkind eq "uncheckout" && $xname !~ m/\/0$/); |
| 68 | |
| 69 | my $branch = $xname; |
| 70 | |
| 71 | if ($opkind eq "uncheckout") { |
| 72 | # Remove the last component |
| 73 | $branch =~ s/\/[^\/]*$//; |
| 74 | } # if |
| 75 | |
| 76 | # Don't try to remove the /main branch |
| 77 | exit 0 if $branch =~ m/$xn_sfx\/main$/; |
| 78 | |
| 79 | # Check if there are other versions, branches, labels or checked out versions |
| 80 | # on this branch. If so don't do anything. |
| 81 | if ($view_type eq "dynamic") { |
| 82 | opendir (DIR, $branch); |
| 83 | my @entries = readdir (DIR); |
| 84 | closedir (DIR); |
| 85 | |
| 86 | # In an empty branch there are four things: ".", "..", "0" an d"LATEST". |
| 87 | # If there are more then it isn't an empty branch |
| 88 | exit 0 if (scalar (@entries) != 4); |
| 89 | } else { |
| 90 | # Snapshot views. |
| 91 | my ($pname, $brpath) = split ($xn_sfx, $branch); |
| 92 | |
| 93 | # The rmbranch will not reload the element. This shows as "special |
| 94 | # selection, deleted version" in snapshot views This cleans that up. |
| 95 | if ($opkind eq "rmbranch") { |
| 96 | system "cleartool update -log $null \"$pname\"" if ($opkind eq "rmbranch"); |
| 97 | exit 0; # Nothing else to do here... |
| 98 | } # if |
| 99 | |
| 100 | my @vtree = `cleartool lsvtree -branch $brpath \"$pname\"`; |
| 101 | my $latest; |
| 102 | chomp ($latest = pop (@vtree)); |
| 103 | $latest =~ tr/\\/\// if $windows eq "yes"; |
| 104 | |
| 105 | exit 0 unless $latest =~ m/$brpath\/0$/; |
| 106 | } # if |
| 107 | |
| 108 | # Remove the branch! |
| 109 | debug "Removing empty branch $branch"; |
| 110 | system "cleartool rmbranch -force -nc \"$branch\""; |
| 111 | |
| 112 | exit 0; |