Skip to contentSkip to conversation

best way to show diff of commit

RFC In review Pierre Habouzit 6 participants 14 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

Hi there,

There is specific script I run in my vim with git, that tries to show
from the 'status' git commit shows in the buffer which list of files has changed, and builds a diff from it quite clumsily[0].

I wonder how hard it would be for git commit to "stash" the current
commit being sent somewhere editors would be able to have a look at (an alternate index is probably fine). Note that maybe I'm stupid and overlooked that such a thing already exists. I'd like to have it in two
flavors: normal and amend mode. normal mode would show what the resulting commit diff looks like, and the amend mode only shows the incrementall diff the amend adds to the previous commit.

My question is: what do you think is the best way to do that, and
where ?

[0] the issue with this approach is that it's completely broken in amending mode (does not shows the proper thing), and the generated diffs aren't excellent, because as an editor plugin, it's hard to treat renames and copies easily, so I generate really really nasty diffs in that case too.

Signature · 3 lines
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
#1 lore raw
Quoted 12 lines · Pierre Habouzit

There is specific script I run in my vim with git, that tries to show
from the 'status' git commit shows in the buffer which list of files has changed, and builds a diff from it quite clumsily[0].

I wonder how hard it would be for git commit to "stash" the current
commit being sent somewhere editors would be able to have a look at (an alternate index is probably fine). Note that maybe I'm stupid and overlooked that such a thing already exists. I'd like to have it in two
flavors: normal and amend mode. normal mode would show what the resulting commit diff looks like, and the amend mode only shows the incrementall diff the amend adds to the previous commit.

Quoted 2 lines

My question is: what do you think is the best way to do that, and
where ?

Have you checked whether "git-commit -v" already does what you want?

--b.

#2 lore raw
Quoted 13 lines · J. Bruce Fields
Quoted 12 lines · Pierre Habouzit

There is specific script I run in my vim with git, that tries to show
from the 'status' git commit shows in the buffer which list of files has changed, and builds a diff from it quite clumsily[0].

I wonder how hard it would be for git commit to "stash" the current
commit being sent somewhere editors would be able to have a look at (an alternate index is probably fine). Note that maybe I'm stupid and overlooked that such a thing already exists. I'd like to have it in two
flavors: normal and amend mode. normal mode would show what the resulting commit diff looks like, and the amend mode only shows the incrementall diff the amend adds to the previous commit.

Quoted 4 lines
Quoted 2 lines

My question is: what do you think is the best way to do that, and
where ?

Have you checked whether "git-commit -v" already does what you want?

Hmm it doesn't because I would have to call git commit -v each time I
commit and well I _like_ having the status better. And moreover I want the diff to go in a separated buffer too.

I'd rather have some kind of way to have git-commit let a trail of
what he's doing if the user validate the commit, and use that from the editor. I'm sure it would be useful to simple GUIs as well (most advanced GUIs would like to have fine control to what gets commited and are likely to have the command like used quite right).

Signature · 3 lines
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
#3 lore raw
Quoted 3 lines · Pierre Habouzit

Hmm it doesn't because I would have to call git commit -v each time I
commit and well I _like_ having the status better. And moreover I want the diff to go in a separated buffer too.

I've never felt it a problem while editing the log message in Emacs.

Don't enhanced vi implementations let you split the same buffer into two allowing you to view different portions of it these days?

#4 lore raw
Quoted 10 lines · Junio C Hamano
Quoted 3 lines · Pierre Habouzit

Hmm it doesn't because I would have to call git commit -v each time I
commit and well I _like_ having the status better. And moreover I want the diff to go in a separated buffer too.

I've never felt it a problem while editing the log message in Emacs.

Don't enhanced vi implementations let you split the same buffer into two allowing you to view different portions of it these days?

Well, maybe I could write my plugin so that it cuts the diff out from
the main buffer indeed, though I will have to learn using git commit -v instead of git commit :P

That and the fact that the syntax colorization of the diff doesn't
work, but it's probably not up to git to fix that.

Signature · 3 lines
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
#5 lore raw
Quoted 12 lines · Pierre Habouzit
Quoted 10 lines · Junio C Hamano
Quoted 3 lines · Pierre Habouzit

Hmm it doesn't because I would have to call git commit -v each time I
commit and well I _like_ having the status better. And moreover I want the diff to go in a separated buffer too.

I've never felt it a problem while editing the log message in Emacs.

Don't enhanced vi implementations let you split the same buffer into two allowing you to view different portions of it these days?

Quoted 6 lines

Well, maybe I could write my plugin so that it cuts the diff out from
the main buffer indeed, though I will have to learn using git commit -v instead of git commit :P

That and the fact that the syntax colorization of the diff doesn't
work, but it's probably not up to git to fix that.

Or you can use pre-commit hook (which will be run with GIT_INDEX_FILE set to the index file used for the commit) to generate the diff in a separate file, and set EDITOR (or GIT_EDITOR) to a script around vim to open the given COMMIT_EDITMSG as well as that file you create.

#6 lore raw
Quoted 18 lines · Junio C Hamano
Quoted 16 lines · Pierre Habouzit
Quoted 10 lines · Junio C Hamano
Quoted 3 lines · Pierre Habouzit

Hmm it doesn't because I would have to call git commit -v each time I
commit and well I _like_ having the status better. And moreover I want the diff to go in a separated buffer too.

I've never felt it a problem while editing the log message in Emacs.

Don't enhanced vi implementations let you split the same buffer into two allowing you to view different portions of it these days?

Well, maybe I could write my plugin so that it cuts the diff out from
the main buffer indeed, though I will have to learn using git commit -v instead of git commit :P

Quoted 7 lines
Quoted 2 lines

That and the fact that the syntax colorization of the diff doesn't
work, but it's probably not up to git to fix that.

Or you can use pre-commit hook (which will be run with GIT_INDEX_FILE set to the index file used for the commit) to generate the diff in a separate file, and set EDITOR (or GIT_EDITOR) to a script around vim to open the given COMMIT_EDITMSG as well as that file you create.

*ha* _that_ sounds more like the kind of things I was lurking for.
Sounds like a plan thanks :)

Signature · 3 lines
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
#7 lore raw
In reply to Junio C Hamano
Quoted 10 lines · Junio C Hamano
Quoted 3 lines · Pierre Habouzit

Hmm it doesn't because I would have to call git commit -v each time I
commit and well I _like_ having the status better. And moreover I want the diff to go in a separated buffer too.

I've never felt it a problem while editing the log message in Emacs.

Don't enhanced vi implementations let you split the same buffer into two allowing you to view different portions of it these days?

Yes, certainly with vim, which is what most distributions seem to include as the enhanced vim. I have noticed that there seem to be fewer vim users that figure out these kinds of things than emacs users, though.

Dave

#8 lore raw
In reply to Pierre Habouzit
Quoted 6 lines · Pierre Habouzit

Hi there,

There is specific script I run in my vim with git, that tries to show
from the 'status' git commit shows in the buffer which list of files has changed, and builds a diff from it quite clumsily[0].

Quoted 7 lines

I wonder how hard it would be for git commit to "stash" the current
commit being sent somewhere editors would be able to have a look at (an alternate index is probably fine). Note that maybe I'm stupid and overlooked that such a thing already exists. I'd like to have it in two
flavors: normal and amend mode. normal mode would show what the resulting commit diff looks like, and the amend mode only shows the incrementall diff the amend adds to the previous commit.

Such thing does already exist. When the editor is called, the index pointed to by GIT_INDEX_FILE (or the default index if unset) is exactly what will be in the commit.

Hm, looking at the builtin-commit.c that is currently cooking in next, it does not seem to be the case there. It probably should, so the editor can inspect what will be commited.

Quoted 2 lines

My question is: what do you think is the best way to do that, and
where ?

Are you talking about the ftplugins/git.vim script from vim-scripts package, right?

All it needs to do is call 'git diff --cached'. The below patch seems to work here (tried commit, commit -a, commit --amend, commit -a --amend and commit from subdirectory and they seemed to all work).

Quoted 2 lines

[0] the issue with this approach is that it's completely broken in amending mode (does not shows the proper thing), and the generated

I didn't find any issue with amending mode (showed incremental diff for me all right). The problem was when a file was being touched by the commit but work tree had unstaged changes. Such changes were shown when they shouldn't.

Quoted 3 lines

diffs aren't excellent, because as an editor plugin, it's hard to treat renames and copies easily, so I generate really really nasty diffs in that case too.

With --cached, detecting copies is just a matter of adding -C. In the patch below I added only one -C, but it should probably be user-configurable to use no, one or two (equivalent of --find-copies-harder).

Signature · 65 lines
						 Jan 'Bulb' Hudec <[email protected]>

---8<---
--- vim-scripts/ftplugin/git.vim.orig	2007-07-24 09:46:19.000000000 +0200
+++ vim-scripts/ftplugin/git.vim	2007-11-26 01:01:59.000000000 +0100
@@ -14,41 +14,6 @@
 "{{{ function Git_diff_windows
 
 function! Git_diff_windows(vertsplit, auto)
-    let i = 0
-    let list_of_files = ''
-
-    " drop everything until '#  (will commit)' and the next empty line
-    while i <= line('$')
-        let line = getline(i)
-        if line == '# Changes to be committed:'
-            let i = i + 3
-            break
-        endif
-
-        let i = i + 1
-    endwhile
-
-    " read file names until we have EOF or an empty line
-    while i <= line('$')
-        let line = getline(i)
-        if line =~ '^#\s*[a-z ]*:.*->.*$'
-            let file = substitute(line, '^#[^:]*:.*->\s*\(.*\)\s*$', '\1', '')
-            let list_of_files = list_of_files . ' '.file
-            let file = substitute(line, '^#[^:]*:\s*\(.*\)\s*->.*$', '\1', '')
-            let list_of_files = list_of_files . ' '.file
-        elseif line =~ '^#\s*[a-z ]*:'
-            let file = substitute(line, '^#[^:]*:\s*\(.*\)\s*$', '\1', '')
-            let list_of_files = list_of_files . ' '.file
-        elseif line =~ '^#\s*$'
-            break
-        endif
-
-        let i = i + 1
-    endwhile
-
-    if list_of_files == ""
-        return
-    endif
 
     if a:vertsplit
         rightbelow vnew
@@ -56,15 +21,8 @@
         rightbelow new
     endif
     silent! setlocal ft=diff previewwindow bufhidden=delete nobackup noswf nobuflisted nowrap buftype=nofile
-    let gitDir = system('git rev-parse --git-dir 2>/dev/null')
-    let gitDir = substitute(gitDir, '.git\n', '', '')
-    let wd = getcwd()
-    if gitDir != ''
-        exe 'cd '.gitDir
-    endif
-    exe 'normal :r!LANG=C git diff HEAD -- ' . list_of_files . "\n1Gdd"
-    exe 'normal :r!LANG=C git diff --stat HEAD -- ' . list_of_files . "\no\<esc>1GddO\<esc>"
-    exe 'cd '.wd
+    exe "normal :r!LANG=C git diff --cached -C\n1Gdd"
+    exe "normal :r!LANG=C git diff --stat --cached -C\no\<esc>1GddO\<esc>"
     setlocal nomodifiable
     noremap <buffer> q :bw<cr>
     if a:auto
#9 lore raw
Quoted 6 lines · Jan Hudec
Quoted 2 lines · Pierre Habouzit

My question is: what do you think is the best way to do that, and
where ?

Are you talking about the ftplugins/git.vim script from vim-scripts package, right?

Yes, its author.

Quoted 3 lines

All it needs to do is call 'git diff --cached'. The below patch seems to work here (tried commit, commit -a, commit --amend, commit -a --amend and commit from subdirectory and they seemed to all work).

Damn if that's so simple, I really feel like a big fool now.

Quoted 6 lines
Quoted 2 lines

[0] the issue with this approach is that it's completely broken in amending mode (does not shows the proper thing), and the generated

I didn't find any issue with amending mode (showed incremental diff for me all right). The problem was when a file was being touched by the commit but work tree had unstaged changes. Such changes were shown when they shouldn't.

Yep, that's the point.

Quoted 7 lines
Quoted 3 lines

diffs aren't excellent, because as an editor plugin, it's hard to treat renames and copies easily, so I generate really really nasty diffs in that case too.

With --cached, detecting copies is just a matter of adding -C. In the patch below I added only one -C, but it should probably be user-configurable to use no, one or two (equivalent of --find-copies-harder).

Sure, I'll do that obviously.

Thanks a lot for the tip.

Quoted 67 lines
Signature · 4 lines
-- 
						 Jan 'Bulb' Hudec <[email protected]>

---8<---
ftplugin/git.vim renamed from ftplugin/git.vim.orig +2 -44
@@ -14,41 +14,6 @@
 "{{{ function Git_diff_windows
 
 function! Git_diff_windows(vertsplit, auto)
-    let i = 0
-    let list_of_files = ''
-
-    " drop everything until '#  (will commit)' and the next empty line
-    while i <= line('$')
-        let line = getline(i)
-        if line == '# Changes to be committed:'
-            let i = i + 3
-            break
-        endif
-
-        let i = i + 1
-    endwhile
-
-    " read file names until we have EOF or an empty line
-    while i <= line('$')
-        let line = getline(i)
-        if line =~ '^#\s*[a-z ]*:.*->.*$'
-            let file = substitute(line, '^#[^:]*:.*->\s*\(.*\)\s*$', '\1', '')
-            let list_of_files = list_of_files . ' '.file
-            let file = substitute(line, '^#[^:]*:\s*\(.*\)\s*->.*$', '\1', '')
-            let list_of_files = list_of_files . ' '.file
-        elseif line =~ '^#\s*[a-z ]*:'
-            let file = substitute(line, '^#[^:]*:\s*\(.*\)\s*$', '\1', '')
-            let list_of_files = list_of_files . ' '.file
-        elseif line =~ '^#\s*$'
-            break
-        endif
-
-        let i = i + 1
-    endwhile
-
-    if list_of_files == ""
-        return
-    endif
 
     if a:vertsplit
         rightbelow vnew
@@ -56,15 +21,8 @@
         rightbelow new
     endif
     silent! setlocal ft=diff previewwindow bufhidden=delete nobackup noswf nobuflisted nowrap buftype=nofile
-    let gitDir = system('git rev-parse --git-dir 2>/dev/null')
-    let gitDir = substitute(gitDir, '.git\n', '', '')
-    let wd = getcwd()
-    if gitDir != ''
-        exe 'cd '.gitDir
-    endif
-    exe 'normal :r!LANG=C git diff HEAD -- ' . list_of_files . "\n1Gdd"
-    exe 'normal :r!LANG=C git diff --stat HEAD -- ' . list_of_files . "\no\<esc>1GddO\<esc>"
-    exe 'cd '.wd
+    exe "normal :r!LANG=C git diff --cached -C\n1Gdd"
+    exe "normal :r!LANG=C git diff --stat --cached -C\no\<esc>1GddO\<esc>"
     setlocal nomodifiable
     noremap <buffer> q :bw<cr>
     if a:auto
Signature · 3 lines
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
#10 lore raw
In reply to Jan Hudec
Quoted 3 lines · Jan Hudec

Hm, looking at the builtin-commit.c that is currently cooking in next, it does not seem to be the case there. It probably should, so the editor can inspect what will be commited.

I confirm that it's broken now, but that it works at the top of main.

Signature · 3 lines
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
#11 lore raw

PATCH 1/1 Make builtin-commit.c export GIT_INDEX_FILE for launch_editor as well.

---

And here's the fix for this tiny problem …

 builtin-commit.c |    9 ++++++---
 builtin-tag.c    |    6 +++---
 strbuf.h         |    2 +-
 3 files changed, 10 insertions(+), 7 deletions(-)
builtin-commit.c +6 -3
index 5d27102..6e6b9f2 100644
@@ -748,9 +748,12 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 
 	/* Get the commit message and validate it */
 	header_len = sb.len;
-	if (!no_edit)
-		launch_editor(git_path(commit_editmsg), &sb);
-	else if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
+	if (!no_edit) {
+		char index[PATH_MAX];
+		const char *env[2] = { index, NULL };
+		snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
+		launch_editor(git_path(commit_editmsg), &sb, env);
+	} else if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
 		rollback_index_files();
 		die("could not read commit message\n");
 	}
builtin-tag.c +3 -3
index e89b201..8cc7f9c 100644
@@ -23,7 +23,7 @@ static const char * const git_tag_usage[] = {
 
 static char signingkey[1000];
 
-void launch_editor(const char *path, struct strbuf *buffer)
+void launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
 {
 	const char *editor, *terminal;
 
@@ -49,7 +49,7 @@ void launch_editor(const char *path, struct strbuf *buffer)
 	if (strcmp(editor, ":")) {
 		const char *args[] = { editor, path, NULL };
 
-		if (run_command_v_opt(args, 0))
+		if (run_command_v_opt_cd_env(args, 0, NULL, env))
 			die("There was a problem with the editor %s.", editor);
 	}
 
@@ -318,7 +318,7 @@ static void create_tag(const unsigned char *object, const char *tag,
 			write_or_die(fd, tag_template, strlen(tag_template));
 		close(fd);
 
-		launch_editor(path, buf);
+		launch_editor(path, buf, NULL);
 
 		unlink(path);
 		free(path);
strbuf.h +1 -1
index 8334a9b..36d61db 100644
@@ -117,6 +117,6 @@ extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
 extern int strbuf_getline(struct strbuf *, FILE *, int);
 
 extern void stripspace(struct strbuf *buf, int skip_comments);
-extern void launch_editor(const char *path, struct strbuf *buffer);
+extern void launch_editor(const char *path, struct strbuf *buffer, const char *const *env);
 
 #endif /* STRBUF_H */
Signature · 1 line
1.5.3.6.2015.g9baba-dirty
#12 lore raw

Re: [PATCH] Make builtin-commit.c export GIT_INDEX_FILE for launch_editor as well.

Quoted 4 lines · Pierre Habouzit

---

And here's the fix for this tiny problem …

That looks good to me.

Quoted 70 lines

 builtin-commit.c |    9 ++++++---
 builtin-tag.c    |    6 +++---
 strbuf.h         |    2 +-
 3 files changed, 10 insertions(+), 7 deletions(-)
builtin-commit.c +6 -3
index 5d27102..6e6b9f2 100644
@@ -748,9 +748,12 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 
 	/* Get the commit message and validate it */
 	header_len = sb.len;
-	if (!no_edit)
-		launch_editor(git_path(commit_editmsg), &sb);
-	else if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
+	if (!no_edit) {
+		char index[PATH_MAX];
+		const char *env[2] = { index, NULL };
+		snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
+		launch_editor(git_path(commit_editmsg), &sb, env);
+	} else if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
 		rollback_index_files();
 		die("could not read commit message\n");
 	}
builtin-tag.c +3 -3
index e89b201..8cc7f9c 100644
@@ -23,7 +23,7 @@ static const char * const git_tag_usage[] = {
 
 static char signingkey[1000];
 
-void launch_editor(const char *path, struct strbuf *buffer)
+void launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
 {
 	const char *editor, *terminal;
 
@@ -49,7 +49,7 @@ void launch_editor(const char *path, struct strbuf *buffer)
 	if (strcmp(editor, ":")) {
 		const char *args[] = { editor, path, NULL };
 
-		if (run_command_v_opt(args, 0))
+		if (run_command_v_opt_cd_env(args, 0, NULL, env))
 			die("There was a problem with the editor %s.", editor);
 	}
 
@@ -318,7 +318,7 @@ static void create_tag(const unsigned char *object, const char *tag,
 			write_or_die(fd, tag_template, strlen(tag_template));
 		close(fd);
 
-		launch_editor(path, buf);
+		launch_editor(path, buf, NULL);
 
 		unlink(path);
 		free(path);
strbuf.h +1 -1
index 8334a9b..36d61db 100644
@@ -117,6 +117,6 @@ extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
 extern int strbuf_getline(struct strbuf *, FILE *, int);
 
 extern void stripspace(struct strbuf *buf, int skip_comments);
-extern void launch_editor(const char *path, struct strbuf *buffer);
+extern void launch_editor(const char *path, struct strbuf *buffer, const char *const *env);
 
 #endif /* STRBUF_H */