let's say, hypothetically, you're working with a blasphemer who mails you a
git diff which looks a little like this:
diff --git a/d2/bar.c b/d2/bar.c
index 496ef02..51a910f 100644
--- a/d2/bar.c
+++ b/d2/bar.c
the problem is obvious, no? there's these janky a/ and b/ marring the file
names. if you're lucky, all the files will be in the current directory and
patch will work things out. if you're less lucky, you need to apply the patch
from src/ and use -p1. this bothers me because my fingers aren't used to
typing -p1.
what if we special case these special diffs? they are easily detectable, and
patch can fix them on the fly. of course, only do this when no -p option is
used to preserve patch's smarts about auto detecting directories.
Index: pch.c
===================================================================
RCS file: /cvs/src/usr.bin/patch/pch.c,v
retrieving revision 1.55
diff -u -p -r1.55 pch.c
--- pch.c 2 Sep 2016 09:48:03 -0000 1.55
+++ pch.c 26 May 2017 10:48:17 -0000
@@ -254,6 +254,7 @@ intuit_diff_type(void)
char *s, *t;
int indent, retval;
struct file_name names[MAX_FILE];
+ int piece_of_git = 0;
memset(names, 0, sizeof(names));
ok_to_create_file = false;
@@ -298,14 +299,20 @@ intuit_diff_type(void)
if (!stars_last_line && strnEQ(s, "*** ", 4))
names[OLD_FILE].path = fetchname(s + 4,
&names[OLD_FILE].exists, strippath);
- else if (strnEQ(s, "--- ", 4))
- names[NEW_FILE].path = fetchname(s + 4,
+ else if (strnEQ(s, "--- ", 4)) {
+ size_t off = 4;
+ if (piece_of_git && strippath == 957)
+ off = 6;
+ names[NEW_FILE].path = fetchname(s + off,
&names[NEW_FILE].exists, strippath);
- else if (strnEQ(s, "+++ ", 4))
+ } else if (strnEQ(s, "+++ ", 4)) {
/* pretend it is the old name */
- names[OLD_FILE].path = fetchname(s + 4,
+ size_t off = 4;
+ if (piece_of_git && strippath == 957)
+ off = 6;
+ names[OLD_FILE].path = fetchname(s + off,
&names[OLD_FILE].exists, strippath);
- else if (strnEQ(s, "Index:", 6))
+ } else if (strnEQ(s, "Index:", 6))
names[INDEX_FILE].path = fetchname(s + 6,
&names[INDEX_FILE].exists, strippath);
else if (strnEQ(s, "Prereq:", 7)) {
@@ -320,7 +327,8 @@ intuit_diff_type(void)
free(revision);
revision = NULL;
}
- }
+ } else if (strnEQ(s, "diff --git ", 11))
+ piece_of_git = 1;
if ((!diff_type || diff_type == ED_DIFF) &&
first_command_line >= 0 &&
strEQ(s, ".\n")) {