forked from majnemer/davesdots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-untrack-ignored
executable file
·53 lines (43 loc) · 1.16 KB
/
git-untrack-ignored
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
#! /usr/bin/env perl
# moderately tested script to untrack files that are listed in .gitignore
use strict;
use warnings;
use Getopt::Long;
my $dry_run = 0;
my $help_me = 0;
GetOptions(
'dry-run!' => \$dry_run,
'h|help|?' => \$help_me,
);
if ($help_me) {
print "$0: runs 'git rm --cached' for all the files in your .gitignore\n";
print "(This makes them untracked but not deleted)\n";
print "Options: --dry-run: just print out which files we would remove\n";
exit;
}
open my $fh, '<', '.gitignore' or die "Couldn't find/open .gitignore: $!";
my @patterns = <$fh>;
close $fh;
chomp @patterns;
apply_patterns('.', @patterns);
sub apply_patterns {
my $dir = shift;
#print "at $dir\n";
my @patterns = @_;
for my $pattern (@patterns) {
if ($pattern =~ /^\s*#/ || $pattern =~ /^\s*$/) {
next;
} elsif ($pattern =~ /^\s*!/) {
print "ignoring negating pattern\n";
}
my @files = glob($pattern);
for my $file (grep {-e $_} map {"$dir/$_"} @files) {
if ($dry_run) {
print "unstage $file\n";
} else {
system('git', 'rm', '-q', '--ignore-unmatch', '--cached', $file);
}
}
}
apply_patterns($_, @patterns) for grep {-d} glob($dir . '/*');
}