Created
July 19, 2019 09:25
-
-
Save SwimmingTiger/129946ec9e02656d28921c6d28858d81 to your computer and use it in GitHub Desktop.
按提交注释和提交时间比较两个分支的差异,仅列出不同的部分
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 usage() { | |
global $argv; | |
die("Usage: $argv[0] <branch1> <branch2>\n"); | |
} | |
if ($argc < 3) { | |
usage(); | |
} | |
$branch1 = $argv[1]; | |
$branch2 = $argv[2]; | |
ob_start(); | |
passthru("git log --format='%H %ai [%an] %s' -b ".escapeshellarg($branch1)); | |
$gitLog1 = ob_get_clean(); | |
ob_start(); | |
passthru("git log --format='%H %ai [%an] %s' -b ".escapeshellarg($branch2)); | |
$gitLog2 = ob_get_clean(); | |
$gitLog1 = gitLogToArray($gitLog1, $branch1); | |
$gitLog2 = gitLogToArray($gitLog2, $branch2); | |
$gitLog1Size = count($gitLog1); | |
$gitLog2Size = count($gitLog2); | |
$maxSize = max($gitLog1Size, $gitLog2Size); | |
// merge two branch commits | |
$commentIndex = []; | |
for ($i=0; $i<$maxSize; $i++) { | |
if (isset($gitLog1[$i])) { | |
$log = $gitLog1[$i]; | |
$comment = $log['comment']; | |
if (!isset($commentIndex[$comment])) { | |
$commentIndex[$comment] = []; | |
} | |
$commentIndex[$comment][] = $log; | |
} | |
if (isset($gitLog2[$i])) { | |
$log = $gitLog2[$i]; | |
$comment = $log['comment']; | |
if (!isset($commentIndex[$comment])) { | |
$commentIndex[$comment] = []; | |
} | |
$commentIndex[$comment][] = $log; | |
} | |
} | |
// remove the same code base | |
foreach ($commentIndex as $comment => $commits) { | |
$commitCount = count($commits); | |
if ($commitCount < 2) { | |
continue; | |
} | |
$hashIndex = []; | |
$timeIndex = []; | |
foreach ($commits as $commit) { | |
$hash = $commit['hash']; | |
$time = $commit['date']; | |
if (!isset($hashIndex[$hash])) { | |
$hashIndex[$hash] = 0; | |
} | |
if (!isset($timeIndex[$time])) { | |
$timeIndex[$time] = 0; | |
} | |
$hashIndex[$hash]++; | |
$timeIndex[$time]++; | |
} | |
// If all the committing times are exactly the same, they are considered the same commit. | |
if (count($timeIndex) > 1) { | |
foreach ($hashIndex as $times) { | |
// If it is odd, only one branch has a commit for the hash. | |
if ($times % 2 != 0) { | |
continue 2; | |
} | |
} | |
} | |
// All hashes are evenly times, indicating that both branches have the same commit. | |
unset($commentIndex[$comment]); | |
} | |
foreach ($commentIndex as $comment => $commits) { | |
echo $comment, "\n"; | |
foreach ($commits as $commit) { | |
echo "\t$commit[branch]\t$commit[date]\t$commit[hash]\n"; | |
} | |
echo "\n"; | |
} | |
function gitLogToArray($lines, $branch) { | |
$arr = []; | |
$lines = explode("\n", $lines); | |
foreach ($lines as $line) { | |
$line = trim($line); | |
if (empty($line)) { | |
continue; | |
} | |
$hash = substr($line, 0, 40); | |
$date = substr($line, 41, 25); | |
$comment = substr($line, 67); | |
$arr[] = [ | |
'hash' => $hash, | |
'date' => $date, | |
'comment' => $comment, | |
'branch' => $branch, | |
]; | |
} | |
return $arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment