Re: [PATCH] update-index: add --swap to swap index and worktree content

2 messages, 2 authors, 2016-06-15 · open the first message on its own page

Re: [PATCH] update-index: add --swap to swap index and worktree content

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:51:49

Jeff King [off-list ref] writes:
  # add some content with an error
  echo 'printf("hello word!\n")' >hello.c
  git add hello.c

  # work on it more, realizing the error
  echo 'printf("goodbye world!\n") >hello.c

  # now what? you want to stage the s/word/world/ fixup,
  # but you want to keep the hello/goodbye thing as a separate change.
  # Using anything line-based is going to conflate the two.
  # The change is simple, though, so you can just as easily edit the
  # index file, if only you could get to it. So you do:
  git update-index --swap hello.c
  sed -i s/word/world/ hello.c
  git update-index --swap hello.c

So the swap really functions as a toggle of "I would like to work on
the index version for a minute", and then you toggle back when you're
done.
And you have to redo what you did to the index version in the working tree
after the second "swap", no?

Re: [PATCH] update-index: add --swap to swap index and worktree content

From: Jeff King <hidden>
Date: 2016-06-15 22:51:49

On Tue, Aug 16, 2011 at 02:56:41PM -0700, Junio C Hamano wrote:
Jeff King [off-list ref] writes:
quoted
  # add some content with an error
  echo 'printf("hello word!\n")' >hello.c
  git add hello.c

  # work on it more, realizing the error
  echo 'printf("goodbye world!\n") >hello.c

  # now what? you want to stage the s/word/world/ fixup,
  # but you want to keep the hello/goodbye thing as a separate change.
  # Using anything line-based is going to conflate the two.
  # The change is simple, though, so you can just as easily edit the
  # index file, if only you could get to it. So you do:
  git update-index --swap hello.c
  sed -i s/word/world/ hello.c
  git update-index --swap hello.c

So the swap really functions as a toggle of "I would like to work on
the index version for a minute", and then you toggle back when you're
done.
And you have to redo what you did to the index version in the working tree
after the second "swap", no?
No. The point is that I _already_ did it in the working tree version
while doing my s/hello/goodbye/ change (let's call this the "new
change"). And ideally I would just use "git add -p" to stage only the
s/word/world/ change (let's call this the "fixup"). But they're tangled
in a single hunk, and I need some way of splitting them.

You could also use "add -e" to solve this situation. In this case, you
end up removing the "new change" from the patch, leaving only the fixup.
In this toy case, it would be pretty easy. But in more complex cases,
the fixup is a one-liner, but the "new change" is many lines. So rather
than edit out the new changes, it is simpler to recreate the fixup on
top of the index version.

-Peff
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help