Skip to content

Instantly share code, notes, and snippets.

@BiviaBen
Created January 13, 2015 23:02
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 BiviaBen/0a2bb025e6387d1d17e3 to your computer and use it in GitHub Desktop.
Save BiviaBen/0a2bb025e6387d1d17e3 to your computer and use it in GitHub Desktop.
Git My Pants
#!/opt/local/bin/php
<?php
$baseShas = array(
'master' => 'a89471b',
'develop' => '38d6412',
);
$permaBranches = array(
'master',
'staging',
'develop',
'v3beta',
'api',
);
$branchBases = array();
foreach ($baseShas as $base => $sha) {
$branchBases = array_merge(
$branchBases,
array_fill_keys(
explode("\n", preg_replace('/^[ \*]*/m', '', trim(`git branch --contains $sha`))),
$base
));
}
$gitList = `git for-each-ref --sort='authordate' --format='%(authordate:short):%(refname):%(upstream:short)' refs/heads | sed -e 's-refs/heads/--'`;
$branches = explode("\n", trim($gitList));
$pants = array();
$maxMsgLength = 0;
$maxActLength = 0;
$maxBranchLength = 0;
foreach ($branches as $dateBranch) {
list($date, $branch, $remote) = explode(':', $dateBranch);
if (array_key_exists($branch, $baseShas)) {
continue;
}
$maxBranchLength = max($maxBranchLength, strlen($branch));
$msg = 'unknown';
$action = 'wtf';
if (strtotime($date) < strtotime("-270 days")) {
$msg = "freakin ancient, buddy";
$action = 'reconsider';
} elseif (array_key_exists($branch, $branchBases)) {
$base = $branchBases[$branch];
$aheadBehind = trim(`git rev-list --left-right $branch...$base --`);
$ahead = substr_count($aheadBehind, '<');
$behind = substr_count($aheadBehind, '>');
if ($ahead && $behind) {
$msg = "$base: $ahead ahead, $behind behind";
$action = 'rebase';
if ($remote) {
$action = 'pull / merge';
}
} elseif ($ahead) {
$msg = "$base: $ahead ahead";
$action = 'share / PR';
if (
('develop' == $base && 'v3beta' == $branch)
||
('master' == $base && 'staging' == $branch)
) {
$action = 'merge / deploy';
}
} elseif ($behind) {
$msg = "$base: $behind behind";
$action = 'delete';
if (in_array($branch, $permaBranches)) {
$action = 'backport';
}
} else {
$msg = "$base: spot on";
$action = 'delete';
}
if ('delete' == $action && in_array($branch, $permaBranches)) {
$action = 'just chill';
}
}
$pants[] = array(
'date' => $date,
'branch' => $branch,
'msg' => $msg,
'action' => $action,
);
$maxMsgLength = max($maxMsgLength, strlen($msg));
$maxActLength = max($maxActLength, strlen($action));
}
$cols = "%-". $maxBranchLength ."s %-". ($maxActLength +2) ."s %s %-". $maxMsgLength ."s\n";
$header = sprintf(
$cols,
"branch",
" action",
"last work ",
"state"
);
echo $header . str_repeat('-', strlen($header)) ."\n";
$pants = array_reverse($pants);
foreach ($pants as $foundMyPants) {
printf(
$cols,
$foundMyPants['branch'],
'['. $foundMyPants['action'] .']',
$foundMyPants['date'],
$foundMyPants['msg']
);
}
echo <<<EOS
TODO:
√ 1. reorg columns: branch, action, date, comparison status
2. compare to the remote branch, to know if it should be pushed or pulled, etc.
3. compare the stashes to the branches, and include possible stash actions in
the action recommendation
4. crop display width to window width (preferably by trimming the branch name)
EOS;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment