Skip to content

Instantly share code, notes, and snippets.

@brandonkelly
Created June 8, 2024 13:58
Show Gist options
  • Select an option

  • Save brandonkelly/6a88599b254b755decb6d29f3d777651 to your computer and use it in GitHub Desktop.

Select an option

Save brandonkelly/6a88599b254b755decb6d29f3d777651 to your computer and use it in GitHub Desktop.

Revisions

  1. brandonkelly created this gist Jun 8, 2024.
    41 changes: 41 additions & 0 deletions cherrypath.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #!/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";
    }
    }