git staging patches: with hunk smaller than a line
Asked Answered
L

1

7

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)

Lager answered 5/7, 2015 at 13:38 Comment(0)
E
3

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.

Erose answered 6/7, 2015 at 8:53 Comment(1)
Even if hunks are limited to one line, it would be nice to have an ouput similar to --word-diff=porcelain. For the work-flow: words are definitely not worth a commit but at best a stash. By the time during, as work-flow I will take care to have a sentence by line so that one line hunks are not too big.Lager

© 2022 - 2024 — McMap. All rights reserved.