Home > Mobile >  Correctly patching git hunks with index editing
Correctly patching git hunks with index editing

Time:11-15

Updated:

you can reproduce this problem with below commands:

mkdir test && cd test
git init
cat > sample <<EOD
:host {
    max-width: 20%;
    text-align: center;
    cursor: pointer;

    .svg-game-controller {
        width: 100%;
EOD

git add -A
git commit -m 'first'

cat > sample <<EOD
@import '../svg-icon.scss';

:host {
    @include svg-icon-default-style(20%);

    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    align-items: center;

    height: 100%;

    .svg-game-controller {
        width: 100%;
EOD


git diff # check changes

git add -p # patch about diff

(interactive mode) s
(interactive mode) n # skip first hunk

# then you can see editing window.



I faced a problem about git hunk editing.

It can be resolved with patching hard split, but I don't want that and hope to know about git purely.

Please anyone describes to me about hunk editing correctly.

Following my knowledge, On manual hunk editing window,

you can check hunk info like below:

<sign><start line in edit window>,<changed line counts (signed line with sources)>

and there are two part like this: @@ <delete change info> <add lines change info> @@

example: @@ -1,7 3,12 @@ means delete change is started on first line in edit window and lines including source content is 7 (all lines except signed liens).

Then here is my target file.

diff --git a/src/app/svg-icon/logo-svg/logo-svg.component.scss b/src/app/svg-icon/logo-svg/logo-svg.component.scss
index c0f0c72..9500e10 100644
--- a/src/app/svg-icon/logo-svg/logo-svg.component.scss
    b/src/app/svg-icon/logo-svg/logo-svg.component.scss
@@ -1,7  1,14 @@
 @import '../svg-icon.scss';
 
 :host {
-    max-width: 20%;
-    text-align: center;
-    cursor: pointer;
     @include svg-icon-default-style(20%);
 
     display: flex;
     flex-direction: column;
     justify-content: space-evenly;
     align-items: center;
 
     height: 100%;

     .svg-game-controller {
         width: 100%;

First, I splited the hunk to two part.

then skip first and edit second part.

diff --git a/src/app/svg-icon/logo-svg/logo-svg.component.scss b/src/app/svg-icon/logo-svg/logo-svg.component.scss
index c0f0c72..9500e10 100644
--- a/src/app/svg-icon/logo-svg/logo-svg.component.scss
    b/src/app/svg-icon/logo-svg/logo-svg.component.scss
@@ -1,7  1,14 @@
 @import '../svg-icon.scss';
 
 :host {
-    max-width: 20%;
-    text-align: center;
-    cursor: pointer;
     @include svg-icon-default-style(20%);
 
     display: flex;
     flex-direction: column;
     justify-content: space-evenly;
     align-items: center;
 
     height: 100%;

then I have removed 7 lines like below:

# Manual hunk edit mode -- see bottom for a quick guide.
@@ -1,7  3,12 @@
 :host {
-    max-width: 20%;
-    text-align: center;
-    cursor: pointer;
     @include svg-icon-default-style(20%);

     .svg-game-controller {
         width: 100%;
# ---
# 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.

I edited to -1,7 3,5 but got error.

(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? e
error: patch failed: src/app/svg-icon/logo-svg/logo-svg.component.scss:1
error: src/app/svg-icon/logo-svg/logo-svg.component.scss: patch does not apply

I cannot understand why git works like this

HOW CAN I SUCCESS THIS HUNK PATCH ?

CodePudding user response:

Updated: It was git bug on previous version (can be reproduced with 2.30.1)

on 2.38.1 bug is fixed


This is more a comment than an answer I can't make a clean comment-buffer version so:

First, I splited the hunk to two part.

then skip first and edit second part.

then I have removed 7 lines like below:

[(deleting all additions below the `@include`)]

When I do this, it works. Delete the additions you don't want to commit yet, :x, it applies. The working edit buffer I end with:

# Manual hunk edit mode -- see bottom for a quick guide.
@@ -1,7  3,12 @@
 :host {
-    max-width: 20%;
-    text-align: center;
-    cursor: pointer;
     @include svg-icon-default-style(20%);
 
     .svg-game-controller {
         width: 100%;
# ---
# 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.
  •  Tags:  
  • git
  • Related