Skip to contentSkip to conversation

Add git-edit-index.perl

PATCH In review Neil Roberts 5 participants 8 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add git-edit-index.perl

This script can be used to edit a file in the index without affecting your working tree. It checkouts a copy of the file to a temporary file and runs an editor on it. If the editor completes successfully with a non-empty file then it updates the index with the new data.

This is useful to fine tune the results from git add -p. For example sometimes your unrelated changes are too close together and git-add--interactive will refuse to split them up. Using this script you can add both the changes and later edit the index file to temporarily remove one of the changes.

---
 .gitignore          |    1 +
 Makefile            |    1 +
 git-edit-index.perl |   98 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 100 insertions(+), 0 deletions(-)
 create mode 100755 git-edit-index.perl
.gitignore +1 -0
index d9adce5..251d90b 100644
@@ -36,6 +36,7 @@ git-diff-files
 git-diff-index
 git-diff-tree
 git-describe
+git-edit-index
 git-fast-export
 git-fast-import
 git-fetch
Makefile +1 -0
index 5158197..77ee97f 100644
@@ -275,6 +275,7 @@ SCRIPT_PERL += git-archimport.perl
 SCRIPT_PERL += git-cvsexportcommit.perl
 SCRIPT_PERL += git-cvsimport.perl
 SCRIPT_PERL += git-cvsserver.perl
+SCRIPT_PERL += git-edit-index.perl
 SCRIPT_PERL += git-relink.perl
 SCRIPT_PERL += git-send-email.perl
 SCRIPT_PERL += git-svn.perl
git-edit-index.perl new file +98 -0

98 lines not on this page · raw message

Signature · 1 line
1.5.6.3
#1 lore raw
Quoted 4 lines · Neil Roberts

This script can be used to edit a file in the index without affecting your working tree. It checkouts a copy of the file to a temporary file and runs an editor on it. If the editor completes successfully with a non-empty file then it updates the index with the new data.

Hmm. Neat idea. I have used add-interactive's "e"dit patch option to do a similar thing, but it is often unwieldy (e.g., just yesterday I had a patch that removed about 30 lines and added 1 -- rather than munging the diff, it would have been simpler to re-add the line in a staged version).

Thinking out loud: One thing that would make this more useful would be providing the content of the work tree file in some way. Like seeing the whole file but with "conflict markers" showing two versions of each hunk, like:

a line in both files
<<<<<<< staged
a line only in the staged version
=======
a line only in the working tree version

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

working tree

and then you can edit around that. Of course, then you _have_ to edit every hunk since you have just stuck the conflict marker cruft. I guess a savvy user could just open both versions in their editor and pull content from one buffer to the other other.

Quoted 5 lines

This is useful to fine tune the results from git add -p. For example sometimes your unrelated changes are too close together and git-add--interactive will refuse to split them up. Using this script you can add both the changes and later edit the index file to temporarily remove one of the changes.

Have you tried using the edit-patch option in "git add -p"? I'm curious if you would like that better for your cases, or if you find this way more natural.

Quoted 3 lines
 .gitignore          |    1 +
 Makefile            |    1 +
 git-edit-index.perl |   98 +++++++++++++++++++++++++++++++++++++++++++++++++++

I have to wonder if this wouldn't be better as part of git-add--interactive? I guess technically you aren't "adding" from the work tree since it is purely looking at the staged version. But it seems to be part of the same workflow, as you are munging content in the index.

git-edit-index.perl line 38

Quoted 7 lines
git-edit-index.perl new file +7 -0
+sub check_file_size {
+        my ($fn) = @_;
+        my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
+            $atime, $mtime, $ctime, $blksize, $blocks) = stat($fn);
+
+        $size;
+}

FYI, a shorthand for this is:

  (stat $fn))[7];

or you may consider:

  use File::stat;
  stat($fn)->size;

which is nicely readable.

git-edit-index.perl line 78

Quoted 6 lines
git-edit-index.perl new file +6 -0
+        unless (system($editor, $tmp_fn) == 0
+                && check_file_size($tmp_fn)) {
+                # If the editor failed, the file has disappeared or it
+                # has zero size then give up
+                delete_temp_files(@file_list);
+                die("Editor failed or file has zero size");

I guess you are aborting on a zero-size file to allow the user a chance to say "oops, I want to scrap the changes I've saved so far". But you are disallowing making a file empty by this process. Which I guess is not all that common, but it still seems restrictive. I wonder if asking for confirmation might make more sense.

Also, does it make sense to delete the temp files if the editor failed? The user may have put work into the file, but we are not successfully updating the index; so we may be deleting useful work that could be recovered.

git-edit-index.perl line 90

Quoted 3 lines
git-edit-index.perl new file +3 -0
+        unless (defined($hash) && $hash =~ /\A[0-9a-f]{40}\z/) {
+                delete_temp_files(@file_list);
+                die("Failed to add new file");

Again, if we fail to hash for whatever reason, we should not delete the useful work that the user might have done.

-Peff

#2 lore raw

Hi,

Quoted 8 lines · Jeff King
Quoted 4 lines · Neil Roberts

This script can be used to edit a file in the index without affecting your working tree. It checkouts a copy of the file to a temporary file and runs an editor on it. If the editor completes successfully with a non-empty file then it updates the index with the new data.

Hmm. Neat idea.

Yes, it is a neat idea. But I always keep in mind what Junio had to say about my "add -e" thing (that I use pretty frequently myself): you will put something into the index that has _never_ been tested.

Would we really want to bless such a workflow with "official" support?

Ciao,
Dscho

#3 lore raw
Quoted 5 lines · Johannes Schindelin

Yes, it is a neat idea. But I always keep in mind what Junio had to say about my "add -e" thing (that I use pretty frequently myself): you will put something into the index that has _never_ been tested.

Would we really want to bless such a workflow with "official" support?

That is definitely something to be concerned about. Which is why my workflow is something like:

  $ hack hack hack
  $ while ! git diff; do
      git add -p
      git commit
    done
  $ for i in `git rev-list origin..`; do
      git checkout $i && make test || barf
    done

That is, it is not inherently a problem to put something untested into the index as long as you are doing it so that you can go back and test later.

It _would_ be a nicer workflow to say "I don't want these changes yet" and selectively put them elsewhere, test what's in the working tree, commit, and then grab some more changes from your stash. But we don't have interactive stashing and unstashing yet, which would be required for that.

-Peff

#4 lore raw

Hi,

Quoted 5 lines · Jeff King

It _would_ be a nicer workflow to say "I don't want these changes yet" and selectively put them elsewhere, test what's in the working tree, commit, and then grab some more changes from your stash. But we don't have interactive stashing and unstashing yet, which would be required for that.

git stash -i... Yes, I'd like that!

Ciao,
Dscho

#5 lore raw
Quoted 11 lines · Johannes Schindelin

Hi,

Quoted 5 lines · Jeff King

It _would_ be a nicer workflow to say "I don't want these changes yet" and selectively put them elsewhere, test what's in the working tree, commit, and then grab some more changes from your stash. But we don't have interactive stashing and unstashing yet, which would be required for that.

git stash -i... Yes, I'd like that!

Or git checkout -i?

For the cases when you did two changes in a file, you realize one of them is not yet necessary, but you don't want to stage/commit the other change yet.

#6 lore raw

Hi,

Quoted 10 lines · Miklos Vajna
Quoted 8 lines · Johannes Schindelin
Quoted 5 lines · Jeff King

It _would_ be a nicer workflow to say "I don't want these changes yet" and selectively put them elsewhere, test what's in the working tree, commit, and then grab some more changes from your stash. But we don't have interactive stashing and unstashing yet, which would be required for that.

Quoted 3 lines
Quoted 1 line

git stash -i... Yes, I'd like that!

Or git checkout -i?

Frankly, I need to move changes away much more often. Plus, you could have what you wished for with a "git checkout -- <path> && git stash -i". It's just that you would move out the changes you would not want yet.

Ciao,
Dscho

#7 lore raw
In reply to Jeff King
Quoted 7 lines · Jeff King
Quoted 5 lines · Johannes Schindelin

Yes, it is a neat idea. But I always keep in mind what Junio had to say about my "add -e" thing (that I use pretty frequently myself): you will put something into the index that has _never_ been tested.

Would we really want to bless such a workflow with "official" support?

Back in stone ages of git, there wasn't usable tool support to make random unproven commits, later to be tested separately before releasing. The old aversion to committing something that has never existed as a whole in the work tree comes from those days.

The world has changed quite a bit since then, and I do not think the argument holds anymore when better tool support for "commit first, validate and fix-up as needed later" workflow is available.

Quoted 12 lines

That is definitely something to be concerned about. Which is why my workflow is something like:

  $ hack hack hack
  $ while ! git diff; do
      git add -p
      git commit
    done
  $ for i in `git rev-list origin..`; do
      git checkout $i && make test || barf
    done
Quoted 3 lines

That is, it is not inherently a problem to put something untested into the index as long as you are doing it so that you can go back and test later.

Yeah, I do not think there is anything inherently wrong about it, either.