-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-brclean
executable file
·179 lines (142 loc) · 5.85 KB
/
git-brclean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
use Term::ReadKey; # for better input handling
my $dry_run = 0;
my $debug = 0;
GetOptions(
"dry-run|n" => \$dry_run,
"debug|d" => \$debug
) or die "Error in command line arguments\n";
# Input handling functions
sub get_number_input {
my ($prompt, $max) = @_;
# Clear any leftover input
while (Term::ReadKey::ReadKey(-1)) {} # non-blocking read to clear buffer
print "\n$prompt";
my $input = <STDIN>;
print "Debug: input received, raw length: ", length($input // ''), "\n" if $debug;
chomp $input;
print "You entered: '$input'\n" if $debug;
return undef unless defined($input) && length($input);
return undef unless $input =~ /^\d+$/ && $input > 0 && $input <= $max;
return $input;
}
sub get_yes_no {
my ($prompt) = @_;
print "$prompt";
# Clear any leftover input
while (Term::ReadKey::ReadKey(-1)) {}
my $confirm = lc(ReadKey(0));
print "You entered: '$confirm'\n" if $debug;
print "$confirm\n";
# Clear any remaining input after confirmation
while (Term::ReadKey::ReadKey(-1)) {}
return $confirm eq 'y';
}
sub pause_for_enter {
print "Press Enter to continue...";
# Clear any leftover input
while (Term::ReadKey::ReadKey(-1)) {}
my $pause = <STDIN>;
print "Debug: pause received, raw length: ", length($pause // ''), "\n" if $debug;
chomp $pause;
print "You entered for pause: '$pause'\n" if $debug;
}
# Check if brt command exists
system('git config --get-regexp \'^alias\.brt\' >/dev/null 2>&1') == 0
or system('which git-brt >/dev/null 2>&1') == 0
or die "Error: git brt command not found. Please define the alias:\n\n" .
" git config --global alias.brt 'branch -a --format=\"%(committerdate:format:%Y%m%d_%H%M%S) %(refname:short)\" | sort -r'\n\n";
# Get list of remotes and sync remote-tracking branches
my @remotes = `git remote`;
chomp @remotes;
die "No remotes configured!\n" unless @remotes;
print "Syncing remote-tracking branches:\n";
foreach my $remote (@remotes) {
print " Pruning $remote... ";
if ($dry_run) {
print "DRY RUN - would execute: git remote prune --dry-run $remote\n";
system('git', 'remote', 'prune', '--dry-run', $remote);
} else {
system('git', 'remote', 'prune', $remote) == 0
or warn "Failed to prune $remote\n";
print "done\n";
}
}
print "\n";
# Get current branch name
chomp(my $current_branch = `git rev-parse --abbrev-ref HEAD`);
die "Failed to get current branch name\n" unless $current_branch;
my @protected_branches = ('HEAD', 'main', 'master', 'develop', $current_branch);
print "Protected branches (including current branch '$current_branch'): @protected_branches\n" if $debug;
while (1) {
# Get fresh branch list and detect remotes
my @branches = `git brt`;
chomp @branches;
# Convert timestamp-prefixed branches to branch-only format for processing
my @branch_names = map { /^\d{8}_\d{6}\s+(.+)$/; $1 } @branches;
my %remotes = map { (split m{/}, $_)[0] => 1 }
grep { m{/} } @branch_names;
die "No remote branches found!\n" unless %remotes;
# Build regex to exclude protected branches
my $protected_re = join '|',
@protected_branches, # local branches
(map { my $r = $_; map { "$r/$_" } @protected_branches } keys %remotes); # remote branches
$protected_re = '(?:' . $protected_re . ')';
print "Protected branches regex: $protected_re\n" if $debug;
# Filter out protected branches but keep original format for display
my @available_branches = grep {
my ($branch) = /^\d{8}_\d{6}\s+(.+)$/;
$branch !~ /^${protected_re}$/;
} @branches;
# Show available branches
my $num_width = length(scalar @available_branches); # width needed for highest number
printf "\nAvailable branches for deletion:\n";
printf " %${num_width}s Branch Info (timestamp branch_name)\n", '#';
printf " %${num_width}s -------------------------------\n", '-' x $num_width;
for my $i (0 .. $#available_branches) {
printf " %${num_width}d: %s\n", $i + 1, $available_branches[$i];
}
my $input = get_number_input("Enter branch number to delete, or empty to exit: ", scalar @available_branches);
exit unless defined $input;
# If input is invalid, show error and continue
unless ($input > 0 && $input <= @available_branches) {
print "Invalid selection. Please enter a number between 1 and ", scalar @available_branches, "\n";
pause_for_enter();
next;
}
my $idx = $input - 1;
my ($branch) = $available_branches[$idx] =~ /^\d{8}_\d{6}\s+(.+)$/;
if ($branch =~ m{^([^/]+)/(.+)$}) {
my ($remote, $branch_name) = ($1, $2);
my @cmd = ('git', 'push', $remote, '--delete', $branch_name);
print "\nRemote branch: $branch_name from $remote\n";
print "Command: @cmd\n";
if ($dry_run) {
print "DRY RUN: Would execute above command\n";
next;
}
next unless get_yes_no("Execute this command? (y/N) ");
if (system(@cmd) != 0) {
print "Remote branch deletion failed. Attempting to remove local remote-tracking reference...\n";
my @cleanup_cmd = ('git', 'branch', '-dr', "$remote/$branch_name");
system(@cleanup_cmd) == 0
or warn "Failed to delete branch $branch\n";
}
} else {
my @cmd = ('git', 'branch', '-D', $branch);
print "\nLocal branch: $branch\n";
print "Command: @cmd\n";
if ($dry_run) {
print "DRY RUN: Would execute above command\n";
next;
}
next unless get_yes_no("Execute this command? (y/N) ");
system(@cmd) == 0
or warn "Failed to delete branch $branch\n";
}
print "\n";
}
print "\nDone.\n";