Created
November 30, 2013 09:03
-
-
Save azat/7716818 to your computer and use it in GitHub Desktop.
git_history.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
function findGitDir($start) | |
{ | |
$dir = $start; | |
while (true) { | |
$dir = realpath($dir); | |
if (preg_match('@/.git$@', $dir)) { | |
return $dir; | |
} | |
if (is_dir($dir . '/.git/')) { | |
return realpath($dir . '/.git/'); | |
} | |
$dir .= '/../'; | |
} | |
} | |
/* TODO: getopts */ | |
$dir = findGitdir(getcwd()); | |
$format = '%s (%h)'; | |
$range = ($argc > 1) ? $argv[1] : ''; | |
$output = array(); | |
exec(sprintf('env GIT_DIR=%s git log --format="%s" %s', escapeshellarg($dir), $format, $range), $output); | |
$history = array(); | |
foreach ($output as $line) { | |
preg_match('@^\[(?P<group>.*)\](?P<subject>.*) \((?P<sha>[0-9a-f]*)\)@', $line, $m); | |
if (empty($history[$m['group']])) { | |
$history[$m['group']] = array(); | |
} | |
$history[$m['group']][] = sprintf('%s (%s)', $m['subject'], $m['sha']); | |
} | |
foreach ($history as $group => $changes) { | |
printf("%s: \n", $group); | |
foreach ($changes as $change) { | |
printf("- %s\n", trim($change)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment