Skip to content

Instantly share code, notes, and snippets.

@garex
Last active April 18, 2019 15:13
Show Gist options
  • Save garex/50d858c4e4fad36264c97bca9ba93e6c to your computer and use it in GitHub Desktop.
Save garex/50d858c4e4fad36264c97bca9ba93e6c to your computer and use it in GitHub Desktop.
Git interactive rebase with changed files below commits
#!/usr/bin/env php
<?php
//
// To use this file run this:
// git config --global sequence.editor git-commit-file.php
//
// Then just:
// git rebase -i develop
//
$core = trim(`git config core.editor`);
$commits = $_SERVER['argv'][1];
echo "Processing commits @ $commits\n";
$contents = file_get_contents($commits);
$contentLines = explode(PHP_EOL, $contents);
$changedLines = [];
foreach ($contentLines as $line) {
$changedLines[] = $line;
if (preg_match('/^pick ([^ ]+)/', $line, $m)) {
$commit = $m[1];
$diff = shell_exec("git diff --name-status $commit^..$commit | sed -e 's/\\t/ /' -e 's/^[A-Z]/ # \\0/'");
$changedLines[] = $diff;
}
}
$changedContents = implode(PHP_EOL, $changedLines);
file_put_contents($commits, $changedContents);
echo "Starting default editor $core\n";
exec("$core $commits > `tty`", $nothing, $exitCode);
echo "Exiting with code $exitCode\n";
exit($exitCode);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment