Skip to content

Instantly share code, notes, and snippets.

@andyscott
Last active August 7, 2019 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyscott/0d18557fcaedbfa11e305492e6ebeb92 to your computer and use it in GitHub Desktop.
Save andyscott/0d18557fcaedbfa11e305492e6ebeb92 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use File::Basename;
my $bfg_jar_url = "https://repo1.maven.org/maven2/com/madgag/bfg/1.13.0/bfg-1.13.0.jar";
sub trim {
my $s = shift;
$s =~ s/^\s+|\s+$//g;
return $s
};
my $is_bare = trim(`git rev-parse --is-bare-repository`) eq "true";
if (!$is_bare) {
print "You may only run this tool in a bare git repository\n";
print "Please clone your repo with --mirror\n";
print "and try again.\n";
die;
}
my $dirname = dirname(__FILE__);
my $bfg_file = "${dirname}/bfg.jar";
if (!-e $bfg_file) {
print "File missing!\n";
print "${bfg_file}\n";
system "curl -o ${bfg_file} ${bfg_jar_url}";
}
sub prompt {
my ($query) = @_;
local $| = 1;
print $query;
chomp(my $answer = <STDIN>);
return $answer;
}
sub prompt_yn {
my ($query) = @_;
my $answer = prompt("$query [Y/n]: ");
return lc($answer) eq 'y';
}
print "Which files would you like to purge?\n";
my @index_filters = ();
my $nuke_files = prompt_yn(" - remove large files (history only)");
my $nuke_min_file_size = "";
if ($nuke_files) {
$nuke_min_file_size = prompt(" - how big? (10M/1M/128K/etc) ");
}
if (prompt_yn(" - 3rdparty/workspace.bzl")) {
push @index_filters, 'git rm --cached --ignore-unmatch 3rdparty/workspace.bzl';
}
if (prompt_yn(" - 3rdparty/jvm/...")) {
push @index_filters, 'git rm -r --cached --ignore-unmatch 3rdparty/jvm';
}
if (prompt_yn(" - shared_tools/...")) {
push @index_filters, 'git rm -r --cached --ignore-unmatch shared_tools';
}
if (prompt_yn(" - WORKSPACE")) {
push @index_filters, 'git rm --cached --ignore-unmatch WORKSPACE';
}
if (prompt_yn(" - stripe-build.yaml")) {
push @index_filters, 'git rm --cached --ignore-unmatch stripe-build.yaml';
}
if (prompt_yn(" - Dockerfile")) {
push @index_filters, 'git rm --cached --ignore-unmatch Dockerfile';
}
if (prompt_yn(" - jvm_dependencies.yaml")) {
push @index_filters, 'git rm --cached --ignore-unmatch bazel_deps.yaml';
push @index_filters, 'git rm --cached --ignore-unmatch jvm_dependencies.yaml';
}
if (prompt_yn(" - .bazelrc")) {
push @index_filters, 'git rm --cached --ignore-unmatch .bazelrc';
push @index_filters, 'git rm --cached --ignore-unmatch .bazelrc-ci-startup-options';
}
if (prompt_yn(" - .gitignore")) {
push @index_filters, 'git rm --cached --ignore-unmatch .gitignore';
}
if (prompt_yn(" - bazel")) {
push @index_filters, 'git rm --cached --ignore-unmatch bazel';
push @index_filters, 'git rm --cached --ignore-unmatch tools/bazel';
}
if (prompt_yn(" - tools/coursier")) {
push @index_filters, 'git rm --cached --ignore-unmatch tools/coursier';
}
print "\n";
my $move_to_subdir = prompt_yn("move to a subdir?");
my $subdir_name = "";
if ($move_to_subdir) {
$subdir_name = prompt("name? ");
}
if ($nuke_files) {
system "java -jar ${bfg_file} -b ${nuke_min_file_size}"
}
if (@index_filters) {
my $filter_command = join ';', @index_filters;
print "filter command:\n";
print "${filter_command}\n";
system "git filter-branch -f --index-filter '${filter_command}' HEAD"
}
if ($move_to_subdir) {
system <<"EOF";
git worktree add -f tmp
cd tmp
git ls-tree -dr --name-only head | xargs -I {} mkdir -p ./${subdir_name}/{}
git ls-files | xargs -I {} git mv {} ./${subdir_name}/{}
git commit -m 'move all files into directory: ${subdir_name}'
git clean -df
git worktree remove tmp
EOF
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment