Dmitry Minkovsky
Dmitry Minkovsky

Reputation: 38183

Why doesn't this patch apply?

I'm trying to use git add -p to stage a specific hunk of changes to the index.

Here's the hunk as Git suggests:

@@ -7,8 +7,15 @@
     <title>{{ page.title }}</title>
     <link rel="stylesheet" href="/css/style.css">
   </head>
-  <body>
-    <div class="content container">
+
+  <body class="container">
+    <ul class="navigation">
+      {% if page.url != '/index.html' %}
+      <li><a href="/">Home</a></li>
+      {% endif %}
+    </ul>
+
+    <div class="content">
       {{ content }}
     </div> 
   </body>

and here is my attempted edit of that patch:

@@ -7,5 +7,5 @@                                                                                                                                                                                                                                
     <title>{{ page.title }}</title>                                                                                                                                                                                                           
     <link rel="stylesheet" href="/css/style.css">                                                                                                                                                                                             
   </head>                                                                                                                                                                                                                                     
-  <body>                                                                                                                                                                                                                                      
-    <div class="content container">                                                                                                                                                                                                           
+                                                                                                                                                                                                                                              
+  <body class="container">   

Git rejects this patch, saying:

Your edited hunk does not apply. Edit again (saying "no" discards!) [y/n]?

I don't get it though:

When I look at the original file (below), and mentally attempt to apply my suggested patch, it seems to make sense. What's wrong?

For reference, here's the whole original file:

  1 <!DOCTYPE html>                                                                                                                                                                                                                            
  2 <html lang="en">                                                                                                                                                                                                                           
  3   <head>                                                                                                                                                                                                                                   
  4     <meta charset="utf-8">                                                                                                                                                                                                                 
  5     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">                                                                                                                                                                         
  6     <meta name="viewport" content="width=device-width, initial-scale=1.0">                                                                                                                                                                 
  7     <title>{{ page.title }}</title>                                                                                                                                                                                                        
  8     <link rel="stylesheet" href="/css/style.css">                                                                                                                                                                                          
  9   </head>                                                                                                                                                                                                                                  
 10   <body>                                                                                                                                                                                                                                   
 11     <div class="content container">                                                                                                                                                                                                        
 12       {{ content }}                                                                                                                                                                                                                        
 13     </div>                                                                                                                                                                                                                                 
 14   </body>                                                                                                                                                                                                                                  
 15 </html>  

Also, here is a patch I wrote that Git doesn't complain about:

@@ -7,8 +7,9 @@
     <title>{{ page.title }}</title>
     <link rel="stylesheet" href="/css/style.css">
   </head>
-  <body>
-    <div class="content container">
+
+  <body class="container">
+    <div class="content">
       {{ content }}
     </div> 
   </body>

It's actually a better patch in some ways, but I'd like to understand why Git doesn't like my first approach.

Upvotes: 2

Views: 181

Answers (1)

ssmith
ssmith

Reputation: 744

The context is indeed required in patches, at least by default. By deleting the bottom context the patch is saying that the changes are the last lines of the file.

By looking in the git help apply manual at the -C (context) option it says "by default no context is ever ignored.". You can ignore context with git apply -C0 my.patch, but of course this doesn't help with the interactive edit you're doing as it's seemingly using the default.

Further reading of git help add I notice a section named "EDITING PATCHES" with the following paragraphs that finally says it all:

There are also several operations which should be avoided entirely,
as they will make the patch impossible to apply:

   ·   adding context (" ") or removal ("-") lines

   ·   deleting context or removal lines

   ·   modifying the contents of context or removal lines

Upvotes: 1

Related Questions