2

I'm a relative newcomer to Git, but I've been learning some more advanced stuff as I've been working on a game design competition. In the process of trying to splice changes between two branches, I've been introduced to git checkout --patch, which would do exactly what I need it to. However, despite the fact that simply applying hunks with y or n will work, as will splitting, any time I choose to manually edit a hunk with e, it consistently fails. Take, for example, this patch:

# Manual hunk edit mode -- see bottom for a quick guide
@@ -102,12 +80,12 @@ enum Facing {
     int stunTimer;
     int m_health;

-    bool m_softStun = true;
     bool hitFlag;
     bool jumpFlag;
     SoundGroup contactSounds;

     float movementInput;
+    float verticalInput;

     [SerializeField]
     public GameObject opponent;
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for applying. If it does not apply cleanly, you will be given
# an opportunity to edit again. If all lines of the hunk are removed,
# then the edit is aborted and the hunk is left unchanged.

This is a simple patch whose problem I could solve by splitting, but I will use it as an example. Say I attempt to keep both the m_softStun bool, and verticalInput float variables. It should seem like all I need to do is this:

# Manual hunk edit mode -- see bottom for a quick guide
@@ -102,12 +80,12 @@ enum Facing {
     int stunTimer;
     int m_health;

     bool m_softStun = true;
     bool hitFlag;
     bool jumpFlag;
     SoundGroup contactSounds;

     float movementInput;
+    float verticalInput;

     [SerializeField]
     public GameObject opponent;
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for applying. If it does not apply cleanly, you will be given
# an opportunity to edit again. If all lines of the hunk are removed,
# then the edit is aborted and the hunk is left unchanged.

All I have done is remove the - in front of bool m_softStun and, as documentation and other answers have told me, add a space.

However, when I try to apply this patch, it returns to me an error:

error: patch failed: Assets/Scripts/Fighter.cs:23
error: Assets/Scripts/Fighter.cs: patch does not apply

This error, or variations with different line numbers, has returned to me through all of the following attempts to fix it:

  • I have used standard Windows 10 Notepad, Notepad++ and even Vim
  • I have checked to make sure there is no whitespace being consumed by a tab at the beginning, even going into Vim's hexadecimal analysis to ensure it
  • I have tried removing and keeping carriage returns from the ends of the lines
  • I have tried to encode in both ANSI and UTF-8 via Vim, Notepad and Notepad++
  • I have tried not editing the patch at all, which still gives me an error, leading me to suspect it is either an error in git, or in the way I'm saving files.

I'm practically at my wit's end trying to figure out what's gone wrong, but I don't have the technical knowledge to even start to find out. Even my local 'git guy', who suggested such things as the hexadecimal checking, has been unable to help, and sent me to e-mail a bug report which has so far turned up nothing. Does anyone have any clue why I can't make a manual patch?

MutantOctopus
  • 3,431
  • 4
  • 22
  • 31
  • If the line starts with a tab, and you replace the `-` with a space, then it will start with a space and a tab - in other words, you *need* to have "whitespace consumed by a tab" for it to be a correct context line. Besides that I wonder if you need to adjust the `@@` line counts –  Apr 11 '16 at 19:09
  • @Wumpus No, what I mean is, a more tech-savvy friend suggested that my text editor might be turning the leading spaces into *parts of the tab* - which is *bad*, because git can no longer read the leading space. In any case, that's not what's happening, because there's no tab to begin with. And I doubt the line counts are the issue, since as I posted, it even fails a patch when I *don't change anything* – MutantOctopus Apr 11 '16 at 19:15
  • @Wumpus - are you still there? I really want to figure out this problem – MutantOctopus Apr 12 '16 at 15:39
  • I've never used this feature of git. It looks like an interesting problem but I really don't know what's going on. Can you save the patch from the editor into a separate file, then try to apply it directly (i.e. using the standard `patch` command, not git) –  Apr 12 '16 at 16:08
  • Unfortunately I lack the technical knowledge to figure out how to even start - putting two copies of a file from two branches into one working directory... or something. In any case this question has lowered a bit in priority because after five days worrying, I hacked in the edits manually. Oh well. – MutantOctopus Apr 12 '16 at 16:10
  • I thought my starting suggestion was one you should be able to follow. You said you already had the patch in an editor, so you just need to save it somewhere outside the git area. If you have msysgit then you should have the `patch` command. Figuring out the arguments to `patch` isn't always easy but `patch -p1 --dry-run < thefilepath` is a good start –  Apr 12 '16 at 16:16
  • I mean, that sounds good in theory - but it would mean having to go through the `checkout --patch` process, edit all the patches, save them, and then try to re-apply them, which I feel still wouldn't work... If the problem ever comes up again, I'll try it, but I partly doubt that it will, and I'd much rather figure out whether *this* way of doing it is simply bugged or if I'm doing something wrong - so I don't mess up again in the future. Sorry. – MutantOctopus Apr 12 '16 at 16:19
  • Possible duplicate of [How do diff/patch work and how safe are they?](https://stackoverflow.com/questions/33545654/how-do-diff-patch-work-and-how-safe-are-they) – Paul Sweatte Aug 28 '17 at 23:11

0 Answers0