Skip to content

Instantly share code, notes, and snippets.

@joachim-n
Created February 3, 2015 13:08
Show Gist options
  • Save joachim-n/989eb0d1ca54ce756350 to your computer and use it in GitHub Desktop.
Save joachim-n/989eb0d1ca54ce756350 to your computer and use it in GitHub Desktop.
Drush shell script to diff and write a patch file.
#!/usr/bin/env drush
<?php
/**
* @file
* Drush shell script for creating a patch.
* Requires the following setup:
* - You are on a git feature branch for your issue.
* - The branch name is of the format '1234-description-of-feature' where 1234
* is the drupal.org issue number.
* - The feature branch is rebased to the major development branch you want to
* diff against.
* - All your work is committed.
*
* Pass a parameter to give a comment number for the issue.
* TODO: use d.org API to get this direct.
*/
// Gets args.
while ($arg = drush_shift()) {
$comment_number = $arg;
}
// Change directory to the actual place this script was run from, as Drush puts
// us in the Drupal root.
chdir(drush_cwd());
$current_module = basename(drush_cwd());
// Get the branches that are reachable.
$branch_list = explode("\n", shell_exec("git branch --merged"));
$branches = array();
// Analyse the list of branches obtained from git.
foreach ($branch_list as $branch) {
//print "$branch\n";
$matches = array();
// preg_match('/(?P<word>t[^s]+)/',$test,$matches);
//preg_match("@^\s+(?P<mark>.)\s+(?P<name>\w+)@", $branch, $matches);
preg_match("@^(?P<mark>.)\s+(?P<name>\S+)@", $branch, $matches);
// There's a blank line in the output from git.
if (empty($matches['name'])) {
continue;
}
$branch_name = $matches['name'];
//drush_print_r($matches);
$branches[$matches['name']] = array();
// Identify the current branch.
if ($matches['mark'] == '*') {
$current_branch = $branch_name;
}
// Identify the master branch.
if (preg_match("@(\d.x-\d+-x|\d.x|\d.\d+.x)@", $branch_name)) {
$master_branch = $branch_name;
print "master branch: $master_branch\n";
}
}
// Unpick the name of the current branch: can an issue number be deduced?
$matches = array();
preg_match("@(?P<issue>\d{5,})-?(?P<description>.+)@", $current_branch, $matches);
drush_print_r($matches);
if (!empty($matches['issue'])) {
$issue_number = $matches['issue'];
}
if (!empty($matches['issue'])) {
$branch_description = $matches['description'];
}
// still need folder name for module name
//drush_print_r($branches);
if (isset($comment_number)) {
$number = "$issue_number-$comment_number";
}
else {
$number = $issue_number;
}
$patch_name = "$number.$current_module.$branch_description.patch";
shell_exec("git diff $master_branch > $patch_name");
drush_print_r("Written diff from $master_branch to patch file $patch_name.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment