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 | use Carp; |
23 | |
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"; |
28 | my $trigger_file; |
29 | |
30 | sub InitDebug { |
31 | my $tmpdir = $ENV{TMP}; |
32 | my $trigger_debug_log = "$tmpdir/trigger_debug.log"; |
33 | |
34 | open my $debugLog, '>>', $trigger_debug_log |
35 | or croak "Unable to open $trigger_debug_log"; |
36 | |
37 | return $debugLog |
38 | } # InitDebug |
39 | |
40 | sub debug { |
41 | my ($msg) = @_; |
42 | |
43 | return if !defined $debug; |
44 | |
45 | $trigger_file = InitDebug if !defined $trigger_file; |
46 | |
47 | print $trigger_file "$msg\n"; |
48 | |
49 | return; |
50 | } # debug |
51 | |
52 | # The following environment variables are set by Clearcase when this |
53 | # trigger is called |
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}; |
59 | |
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"; |
66 | |
67 | $xname =~ s/\\/\//g if $windows eq "yes"; |
68 | |
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$/); |
72 | |
73 | my $branch = $xname; |
74 | |
75 | if ($opkind eq "uncheckout") { |
76 | # Remove the last component |
77 | $branch =~ s/\/[^\/]*$//; |
78 | } # if |
79 | |
80 | # Don't try to remove the /main branch |
81 | exit 0 if $branch =~ m/$xn_sfx\/main$/; |
82 | |
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); |
88 | closedir (DIR); |
89 | |
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); |
93 | } else { |
94 | # Snapshot views. |
95 | my ($pname, $brpath) = split ($xn_sfx, $branch); |
96 | |
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... |
102 | } # if |
103 | |
104 | my @vtree = `cleartool lsvtree -branch $brpath \"$pname\"`; |
105 | my $latest; |
106 | chomp ($latest = pop (@vtree)); |
107 | $latest =~ tr/\\/\// if $windows eq "yes"; |
108 | |
109 | exit 0 unless $latest =~ m/$brpath\/0$/; |
110 | } # if |
111 | |
112 | # Remove the branch! |
113 | debug "Removing empty branch $branch"; |
114 | system "cleartool rmbranch -force -nc \"$branch\""; |
115 | |
116 | exit 0; |