Skip to content

Instantly share code, notes, and snippets.

@reekoheek
Created August 11, 2014 11:42
Show Gist options
  • Save reekoheek/3be625adbca076354a50 to your computer and use it in GitHub Desktop.
Save reekoheek/3be625adbca076354a50 to your computer and use it in GitHub Desktop.
Show recursive statuses of directory
#!/usr/bin/env php
<?php
/**
* How to install
* --------------
*
* Write and put this file to directory in PATH with name git-statuses
*
* How to use
* ----------
*
* Show statuses of git project from current directory
*
* ```
* cd $parent_directory_path
* git statuses
* ```
*
* Show statuses of git project from specified directory
*
* ```
* git statuses $specified_directory_path
* ```
*
* Show statuses that DIRTY
*
* ```
* git statuses | grep DIRTY
* ```
*/
function showStatus($baseDir)
{
$baseDir = rtrim($baseDir, '/');
if (is_readable($baseDir.'/.git')) {
echo $baseDir;
$code = '';
$result = '';
$last = exec('cd "'.$baseDir.'" && git status', $result);
$data = array(
'branch' => '',
'status' => 'DIRTY',
'remote' => '-',
);
if (!empty($result[0])) {
$matches = '';
preg_match('/^On branch (.*)$/', $result[0], $matches);
$data['branch'] = $matches[1];
}
foreach ($result as $line) {
if (strpos($line, 'nothing to commit, working directory clean') !== false) {
$data['status'] = 'CLEAN';
} elseif (strpos($line, 'Your branch is up-to-date') !== false) {
$data['remote'] = 'up-to-date';
} elseif (strpos($line, 'Your branch is ahead') !== false) {
$matches = '';
preg_match('/by (\d+) commit/', $line, $matches);
$data['remote'] = 'ahead '.$matches[1].' commits';
}
}
echo " branch: ${data['branch']},";
echo " status: ${data['status']},";
echo " remote: ${data['remote']}\n";
} else {
$dirs = array_filter(glob($baseDir.'/*'), 'is_dir');
foreach ($dirs as $dir) {
if (!is_link($dir)) {
showStatus($dir);
}
}
}
}
echo "GIT (recursive) statuses:\n";
$dir = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '.';
showStatus($dir);
echo "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment