Skip to content

Instantly share code, notes, and snippets.

@ibejohn818
Last active August 29, 2015 14:01
Show Gist options
  • Save ibejohn818/e07b281e3f8b1209022b to your computer and use it in GitHub Desktop.
Save ibejohn818/e07b281e3f8b1209022b to your computer and use it in GitHub Desktop.
Delete DTT Local Branches | Will skip master and any branch you are currently on and ask you to delete, or, pass in -y to just delete
#!/usr/bin/env php
<?php
# delete DTT branches
# will go through all repos in /home/websites
# and ask if you want to delete the branch
# will skip master and any branch you are currently on
# pass in -y to skip asking and just delete
###############################################################
# don't forget to make this file executable (chmod a+x {file})
###############################################################
$ask = true;
if(isset($argv[1]) && $argv[1] == "-y") {
$ask = false;
}
chdir("/home/websites");
$dirs = scandir(".");
$path = getcwd();
foreach($dirs as $dir) {
if(!is_dir("{$path}/{$dir}") || in_array($dir,array(".",".."))) {
continue;
}
//move into the directory
$newPath = "{$path}/{$dir}";
chdir($newPath);
if(!is_dir("{$newPath}/.git")) { //not a git repo!
chdir("..");
continue;
}
out("-----------------------------");
out("In Repo: {$newPath}");
out("-----------------------------");
out("-----------------------------");
$branches = array();
exec("git branch",$branches);
if(count($branches)<=0) {
out("No Branches To Delete In Repo");
continue;
}
foreach($branches as $branch) {
$branch = trim($branch);
if($branch == 'master' || preg_match('/^(\*)/',$branch)) {
continue;
}
if($ask) {
out("-----------------------------");
out("Do you want to delete {$branch}?");
out("y = delete it | n = skip ");
out("----------------------------");
out("Waddaya wanna do?:",0);
$answer = read();
if($answer == 'y') {
out("Deleting {$branch}");
deleteBranch($branch);
} else {
out("Skipping {$branch}");
}
} else {
out("Deleting {$branch}");
deleteBranch($branch);
}
}
}
# helper functions
function deleteBranch($branch) {
exec("git branch -D {$branch}");
}
function read() {
return trim(fgets(STDIN));
}
function out($s,$lines = 1) {
echo $s;
if($lines) {
for($i=0;$i<$lines;$i++) {
echo "\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment