From 3bd1b84759194eb802da38471414ffe4d778ab39 Mon Sep 17 00:00:00 2001 From: Niklas Fiekas Date: Fri, 4 May 2012 00:48:00 +0200 Subject: [PATCH] Add git-rebase-patch --- AUTHORS | 3 +- Readme.md | 16 +++++ bin/git-rebase-patch | 63 ++++++++++++++++++ man/git-rebase-patch.1 | 60 ++++++++++++++++++ man/git-rebase-patch.html | 130 ++++++++++++++++++++++++++++++++++++++ man/git-rebase-patch.md | 47 ++++++++++++++ 6 files changed, 318 insertions(+), 1 deletion(-) create mode 100755 bin/git-rebase-patch create mode 100644 man/git-rebase-patch.1 create mode 100644 man/git-rebase-patch.html create mode 100644 man/git-rebase-patch.md diff --git a/AUTHORS b/AUTHORS index 2300015..8f4d6e6 100644 --- a/AUTHORS +++ b/AUTHORS @@ -22,4 +22,5 @@ Patches and Suggestions - David Baumgold - Leila Muhtasib - Duane Johnson -- Todd Wolfson \ No newline at end of file +- Todd Wolfson +- Niklas Fiekas diff --git a/Readme.md b/Readme.md index 1fbf3a8..a1cf012 100644 --- a/Readme.md +++ b/Readme.md @@ -533,3 +533,19 @@ $ git missing master < d14b8f0 only on current checked out branch > 97ef387 only on master ``` + +## git-rebase-patch patch-file + +Given a patch that doesn't apply to the current HEAD, find the latest commit +it applies to and do a rebase. For example: + +```bash +$ git rebase-patch test.patch +Trying to find a commit the patch applies to... +Patch applied to dbcf408dd26 as 7dc8b23ae1a +First, rewinding head to replay your work on top of it... +Applying: test.patch +Using index info to reconstruct a base tree... +Falling back to patching base and 3-way merge... +Auto-merging README.txt +``` diff --git a/bin/git-rebase-patch b/bin/git-rebase-patch new file mode 100755 index 0000000..3dbf4e9 --- /dev/null +++ b/bin/git-rebase-patch @@ -0,0 +1,63 @@ +#!/bin/sh + +# Warn on a dirty work tree. +git rev-parse --verify HEAD >/dev/null || exit 1 +git update-index -q --ignore-submodules --refresh +if ! git diff-files --quiet --ignore-submodules +then + echo "WARNING (dirty work tree): The patch will only be checked against actual commits." +fi + +# Warn on a dirty index. +if ! git diff-index --cached --quiet --ignore-submodules HEAD -- +then + echo "WARNING (dirty index): The patch will only be checked against actual commits." +fi + +# Use a temporary index. +index="/tmp/$(basename $0).$$.tmp" +cleanup() { + rm $index + exit 2 +} +trap cleanup 2 + +# Go back in history while parent commits are available. +echo "Trying to find a commit the patch applies to..." +rev=$(git rev-parse HEAD) +while [ $? = 0 ] +do + GIT_INDEX_FILE=$index git read-tree $rev + + # Try to apply the patch. + GIT_INDEX_FILE=$index git apply --cached $1 >/dev/null 2>&1 + patch_failed=$? + + # Do it again, but show the error, if the problem is the patch itself. + if [ $patch_failed = 128 ] + then + GIT_INDEX_FILE=$index git apply --index --check $1 + exit $patch_failed + fi + + # The patch applied. Commit and rebase. + if [ $patch_failed = 0 ] + then + # Manufacture a commit. + tree=$(GIT_INDEX_FILE=$index git write-tree) + commit=$(git commit-tree $tree -p $rev -m "$1") + rm $index + + echo "Patch applied to $(git rev-parse --short $rev) as $(git rev-parse --short $commit)" + + git cherry-pick $commit + exit $? + fi + + rev=$(git rev-parse --verify -q $rev^) +done + +# No compatible commit found. Restore. +echo "Failed to find a commit the patch applies to." +rm $index +exit 1 diff --git a/man/git-rebase-patch.1 b/man/git-rebase-patch.1 new file mode 100644 index 0000000..747f930 --- /dev/null +++ b/man/git-rebase-patch.1 @@ -0,0 +1,60 @@ +.\" generated with Ronn/v0.7.3 +.\" http://github.com/rtomayko/ronn/tree/0.7.3 +. +.TH "GIT\-REBASE\-PATCH" "" "July 2014" "" "Git Extras" +. +.SH "SYNOPSIS" +\fBgit\-rebase\-patch\fR +. +.SH "DESCRIPTION" +Given you have a patch that doesn\'t apply to the current HEAD, but you know it applied to some commit in the past, \fBgit\-rebase\-patch\fR will help you find that commit and do a rebase\. +. +.SH "OPTIONS" +. +.TP + +The patch to be applied\. +. +.SH "EXAMPLES" +Executing +. +.IP "" 4 +. +.nf + +$ git rebase\-patch test\.patch +. +.fi +. +.IP "" 0 +. +.P +could give you something like that: +. +.IP "" 4 +. +.nf + +Trying to find a commit the patch applies to\.\.\. +Patch applied to dbcf408dd26 as 7dc8b23ae1a +First, rewinding head to replay your work on top of it\.\.\. +Applying: test\.patch +Using index info to reconstruct a base tree\.\.\. +Falling back to patching base and 3\-way merge\.\.\. +Auto\-merging README\.txt +. +.fi +. +.IP "" 0 +. +.P +Then your last commit has the changes of the patch and is named \fItest\.patch\fR\. +. +.SH "AUTHOR" +Written by Niklas Fiekas <\fIniklas\.fiekas@tu\-clausthal\.de\fR> +. +.SH "REPORTING BUGS" +<\fIhttp://github\.com/visionmedia/git\-extras/issues\fR> +. +.SH "SEE ALSO" +<\fIhttp://github\.com/visionmedia/git\-extras\fR> diff --git a/man/git-rebase-patch.html b/man/git-rebase-patch.html new file mode 100644 index 0000000..2f6b21b --- /dev/null +++ b/man/git-rebase-patch.html @@ -0,0 +1,130 @@ + + + + + + <git-rebase-patch>(1) -- Rebases a patch + + + + +
+ + + +
    +
  1. git-rebase-patch
  2. +
  3. Git Extras
  4. +
  5. git-rebase-patch
  6. +
+ +

(1) -- Rebases a patch

+

SYNOPSIS

+ +

git-rebase-patch <patch-file>

+ +

DESCRIPTION

+ +

Given you have a patch that doesn't apply to the current HEAD, but you know it +applied to some commit in the past, git-rebase-patch will help you find that +commit and do a rebase.

+ +

OPTIONS

+ +
+
<patch-file>
The patch to be applied.
+
+ + +

EXAMPLES

+ +

Executing

+ +
$ git rebase-patch test.patch
+
+ +

could give you something like that:

+ +
Trying to find a commit the patch applies to...
+Patch applied to dbcf408dd26 as 7dc8b23ae1a
+First, rewinding head to replay your work on top of it...
+Applying: test.patch
+Using index info to reconstruct a base tree...
+Falling back to patching base and 3-way merge...
+Auto-merging README.txt
+
+ +

Then your last commit has the changes of the patch and is named test.patch.

+ +

AUTHOR

+ +

Written by Niklas Fiekas <niklas.fiekas@tu-clausthal.de>

+ +

REPORTING BUGS

+ +

<http://github.com/visionmedia/git-extras/issues>

+ +

SEE ALSO

+ +

<http://github.com/visionmedia/git-extras>

+ + +
    +
  1. +
  2. July 2014
  3. +
  4. git-rebase-patch
  5. +
+ +
+ + diff --git a/man/git-rebase-patch.md b/man/git-rebase-patch.md new file mode 100644 index 0000000..cf1799a --- /dev/null +++ b/man/git-rebase-patch.md @@ -0,0 +1,47 @@ +(1) -- Rebases a patch +================================ + +## SYNOPSIS + +`git-rebase-patch` <patch-file> + +## DESCRIPTION + +Given you have a patch that doesn't apply to the current HEAD, but you know it +applied to some commit in the past, `git-rebase-patch` will help you find that +commit and do a rebase. + +## OPTIONS + + * <patch-file>: + The patch to be applied. + +## EXAMPLES + + Executing + + $ git rebase-patch test.patch + + could give you something like that: + + Trying to find a commit the patch applies to... + Patch applied to dbcf408dd26 as 7dc8b23ae1a + First, rewinding head to replay your work on top of it... + Applying: test.patch + Using index info to reconstruct a base tree... + Falling back to patching base and 3-way merge... + Auto-merging README.txt + + Then your last commit has the changes of the patch and is named *test.patch*. + +## AUTHOR + +Written by Niklas Fiekas <> + +## REPORTING BUGS + +<> + +## SEE ALSO + +<>