Skip to content

Instantly share code, notes, and snippets.

@brandonkelly
Created June 8, 2024 13:58
Show Gist options
  • Save brandonkelly/6a88599b254b755decb6d29f3d777651 to your computer and use it in GitHub Desktop.
Save brandonkelly/6a88599b254b755decb6d29f3d777651 to your computer and use it in GitHub Desktop.
cherrypath: git-cherry filtered by path
#!/usr/bin/env php
<?php
// -----------------------------------------------------------------------------
// Usage: `cherrypath.php <upstream> <head> <path>`
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Runs `git cherry -v <upstream> <head>` and filters the output
// to only include SHAs where <path> was modified.
// -----------------------------------------------------------------------------
if (!isset($argv[1], $argv[2], $argv[3]) || isset($argv[4])) {
echo "Usage: cherrypath.php <upstream> <head> <path>\n";
exit(1);
}
exec(sprintf('git cherry -v %s %s', $argv[1], $argv[2]), $cherryOutput, $cherryCode);
if ($cherryCode !== 0) {
echo sprintf("git-cherry failed: %s\n", implode("\n", $cherryOutput));
exit(1);
}
exec(sprintf('git log --format=oneline -- %s', $argv[3]), $logOutput, $logCode);
if ($logCode !== 0) {
echo sprintf("git-log failed: %s\n", implode("\n", $logOutput));
exit(1);
}
// index the shas that involved the <path>
$logShas = [];
foreach ($logOutput as $line) {
$sha = substr($line, 0, 40);
$logShas[$sha] = true;
}
// output the git-cherry lines for the filtered shas
foreach ($cherryOutput as $line) {
$sha = substr($line, 2, 40);
if (isset($logShas[$sha])) {
echo "$line\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment