« Back to Index

[git edit hunk]

View original Gist on GitHub

Tags: #git #edit #hunk #patch #diff

1. original file.py

# https://stackoverflow.com/a/6508925

# CODE BEFORE CHANGES
37 argument = super(QueryArgumentMixin, self).get_query_argument(
38     name, default=default, strip=strip
39 )
40 logging.warn(f"argument: {argument}")
41 logging.warn(f"default: {type(default)}")
42 logging.warn(f"_ARG_DEFAULT: {type(_ARG_DEFAULT)}")
43 if not argument and default is self._ARG_DEFAULT:
44     logging.warn("raise missing arg error")
45     raise tornado.web.MissingArgumentError(name)
46 return argument
47   

# CODE AFTER CHANGES
37 argument = super(QueryArgumentMixin, self).get_query_argument(
38     name, default=default, strip=strip
39 )
40 if not argument and default is _ARG_DEFAULT:
41     raise tornado.web.MissingArgumentError(name)
42 return argument
43   

"""
-37,11 
-37 represents the file before the changes, and it says the hunk is starting from line 40 minus three (for extra context)
,11 represents the number of lines in this hunk before the changes

+36,7
+36 represents the starting point after ALL changes (not just this hunk) are applied.
,7  represents the number of lines in this hunk after the changes

So to modify the hunk as shown we'll need to modify the hunk header to:
-37,7 +36,7

NOTE: why the number +36 ??? it worked but I don't understand why?
"""

2. original.patch

# Manual hunk edit mode -- see bottom for a quick guide.
@@ -37,11 +36,7 @@ class QueryArgumentMixin(object):
         argument = super(QueryArgumentMixin, self).get_query_argument(
             name, default=default, strip=strip
         )
-        logging.warn(f"argument: {argument}")
-        logging.warn(f"default: {type(default)}")
-        logging.warn(f"_ARG_DEFAULT: {type(_ARG_DEFAULT)}")
-        if not argument and default is self._ARG_DEFAULT:
-            logging.warn("raise missing arg error")
+        if not argument and default is _ARG_DEFAULT:
             raise tornado.web.MissingArgumentError(name)
         return argument
 
# ---
# 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 staging.
# 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.

3. edited.patch

# Manual hunk edit mode -- see bottom for a quick guide.
@@ -37,7 +37,7 @@ class QueryArgumentMixin(object):
         argument = super(QueryArgumentMixin, self).get_query_argument(
             name, default=default, strip=strip
         )
+        if not argument and default is _ARG_DEFAULT:
             raise tornado.web.MissingArgumentError(name)
         return argument
 
# ---
# 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 staging.
# 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.

Example Part A.patch

$ git diff
diff --git a/foo b/foo
index b1e6722..d9a85ba 100644
--- a/foo
+++ b/foo
@@ -1,3 +1,3 @@
 A
-B
 C
+D

Example Part B.patch

# Manual hunk edit mode -- see bottom for a quick guide.
@@ -1,3 +1,4 @@
 A
 B
 C
+D
# ---
# 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 staging.
# 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.