Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git staging patches: with hunk smaller than a line

Tags:

git

patch

staging

I am looking to make an interactive patch from a unique commit. I would like to use an output similar as

git diff --word-diff=porcelain [file]

for an interactive patching. The command

git add --edit

propose only hunk that are as least as one line?

Aim: I have a patch for a text files and I would like to stage the changing like in "LibreOffice" accept or reject changes.


Example:

A text file in one line:

echo "Thiis is a sentence with one wrong word. This is a clean sentence. Thhe laast one iss alwaays morre dificult" > text.txt

to commit

git add text.txt
git commit -m "one"

Modifiying the file

echo "This is a sentence with one wrong word. This is a clean sentence. The last one is always more difficult" > text.txt

git add -e

should be automatically cut in 3 hunks

"Thiis" > "This"
"Thhe laast" > "The last"
"iss alwaays morre dificult"> "is always more difficult"

I can just obtain

--- a/text.txt
+++ b/text.txt
@@ -1 +1 @@
-Thiis is a sentence with one wrong word. This is a clean sentence. Thhe laast one iss alwaays morre dificult
+This is a sentence with one wrong word. This is a clean sentence. The last one is always more difficult

Note: at the best "without introduc[ing] confusing changes to the index" (# EDITING PATCHES)

like image 750
nebi Avatar asked Jul 05 '15 13:07

nebi


1 Answers

No, the standard tools for git hunks are to use lines (or set of changed lines in the near vicinity). However, all that git is doing is taking a snapshot of the file in each of three states; so you could easily replay that if you have access to your changes. First, do one of the changes (then do a commit), then replay the second change (then do a commit) and so on.

It's probably not worth doing this on a word-by-word basis though - you'll end up with many commits with no real standalone relevance.

like image 154
AlBlue Avatar answered Nov 15 '22 11:11

AlBlue