Skip to content

Instantly share code, notes, and snippets.

@oliverde8
Last active September 29, 2022 15:02
Show Gist options
  • Save oliverde8/1e4f1ebe061a0ea3e63faf9f38f4c2fb to your computer and use it in GitHub Desktop.
Save oliverde8/1e4f1ebe061a0ea3e63faf9f38f4c2fb to your computer and use it in GitHub Desktop.
Quick stats from a git diff file
<?php
// STEP 1: Generate Diff:
// - git diff <old-hash> <newer-hash> > my.diff
// STEP 2: Change the path to the file here:
$filePath = "my.diff";
// STEP 3: List extensions you would like to ignore when doing your stat.
$ignoredExtensions = ['csv', 'txt', 'jar', '.properties'];
// STEP 4: Try to guess a proportion of comment/non logical code (50 for 50%)
$commentProportion = 80;
/**
* *****************************************************
* Don't change the code beyond this point
* *****************************************************
*/
$linesPerDay = 100;
$handle = fopen(__DIR__ . "/" . $filePath, "r");
$stats = [
'nbFiles' => 0,
'nbNewFiles' => 0,
'nbDeletedFiles' => 0,
'added' => 0,
'removed' => 0,
'changed' => 0
];
$currentFileStat = getCurrentStat("", $ignoredExtensions);
while (($line = fgets($handle)) !== false) {
if (strpos($line, "diff ") === 0) {
$data = [];
$result = preg_match("/^diff --git a\/(?'name'.*) .*/", $line, $data);
$stats['nbFiles']++;
$diff = $currentFileStat["added"] - $currentFileStat['removed'];
if ($diff > 0) {
$stats['added'] += $diff;
} elseif ($diff < 0) {
$stats['removed'] += -1 * $diff;
}
$stats['changed'] += $currentFileStat["added"] + $currentFileStat['removed'];
$currentFileStat = getCurrentStat($data['name'], $ignoredExtensions);
continue;
} elseif (strpos($line, "+++ ") === 0 || strpos($line, "--- ") === 0) {
continue;
}
if (!$currentFileStat['ignored']) {
if (strpos($line, "+") === 0) {
$currentFileStat['added']++;
} elseif (strpos($line, "-") === 0) {
$currentFileStat['removed']++;
} elseif (strpos($line, "deleted file mode") === 0) {
$stats['nbDeletedFiles']++;
} elseif (strpos($line, "new file mode") === 0) {
$stats['nbNewFiles']++;
}
}
}
echo "== FILES ==\n";
echo "Number of files concerned: " . $stats['nbFiles'] . "\n";
echo "Number of new files: " . $stats['nbNewFiles'] . "\n";
echo "Number of deleted files: " . $stats['nbDeletedFiles'] . "\n";
echo "\n== LINES OF CODE ==\n";
echo "Number of lines added: " . $stats['added'] . "\n";
echo "Number of lines removed: " . $stats['removed'] . "\n";
echo "Number of lines changed: " . $stats['changed'] . " (contains added & removed lines as well)\n";
echo "\n== TIME ESTIMATION ==\n";
echo "Estimated time spent : " . floor($stats['changed']/$linesPerDay/($commentProportion/100)) . " days \n";
echo "\n== Information ==\n";
echo "Diff file: $filePath\n";
echo "Percent of non logical code used for estimation: " . $commentProportion . "%\n";
echo "Ignored extensions: " . implode(", ", $ignoredExtensions) . "\n\n";
fclose($handle);
function getCurrentStat($fileName, $ignoredExtensions)
{
$info = pathinfo($fileName);
$extension = "";
if (isset($info['extension'])) {
$extension = $info['extension'];
}
return [
'fileName' => $fileName,
'ignored' => in_array($extension, $ignoredExtensions),
'added' => 0,
'removed' => 0,
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment