git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[GIT GUI PATCH 2/2] git-gui: add "open in editor" diff context menu entry

From
Heiko Voigt <hvoigt@hvoigt.net>
Date
Mar 23, 2012, 17:30 UTC
Message-ID
<f035d9318ecd77bba8942fc6879f6c43f3c48112.1332523097.git.hvoigt@hvoigt.net>
In-Reply-To
<cover.1332523097.git.hvoigt@hvoigt.net>

When cleaning up changes it is helpful to start an editor on the file viewed. Since we roughly know the line number it is also possible to feed the editor with that. An example implementation for some popular editors is contained in this patch.

Signed-off-by: Heiko Voigt <heiko.voigt@mahr.de>
---
 git-gui/git-gui.sh |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
diff --git a/git-gui/git-gui.sh b/git-gui/git-gui.sh
index 35cdee8..c80bbff 100755
--- a/git-gui/git-gui.sh
+++ b/git-gui/git-gui.sh
@@ -2237,6 +2237,49 @@ proc do_explore {} {
 	eval exec $explorer [list [file nativename $_gitworktree]] &
 }
 
+proc do_editor {file x y} {
+	global repo_config ui_diff
+
+	set filearg "$file"
+	set _e [string map {' \"} $repo_config(core.editor)]
+	set editor [lindex $_e 0]
+	lappend exe [_which $editor]
+	if {[llength $_e] > 1} {
+		lappend args [lrange $_e 1 end]
+	}
+
+	set line_no {}
+	catch {
+		set s_lno [lindex [split [$ui_diff index @$x,$y] .] 0]
+		set s_lno [$ui_diff search -backwards -regexp ^@@ $s_lno.0 0.0]
+		set hunk_header [$ui_diff get $s_lno "$s_lno lineend"]
+
+		regexp {^@@@? -([0-9]+),[0-9]+} $hunk_header -> line_no
+	}
+
+	if {$line_no ne {}} {
+		switch -glob -- $editor {
+			*notepad++* {
+				lappend args -n$line_no
+			}
+			*textpad* {
+				set filearg "$filearg\($line_no,0\)"
+			}
+			*emacs* -
+			*vim* {
+				lappend args +$line_no
+			}
+		}
+	}
+	lappend args $filearg
+
+	if {[lindex $exe 0] eq {}} {
+		error_popup [mc "Couldn't find %s in PATH" $repo_config(core.editor)]
+	} else {
+		eval exec $exe $args &
+	}
+}
+
 set is_quitting 0
 set ret_code    1
 
@@ -3516,6 +3559,14 @@ $ctxm add command \
 	-label [mc "Show More Context"] \
 	-command show_more_context
 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
+set editor ""
+catch {set editor $repo_config(core.editor)}
+if {$editor ne {}} {
+	$ctxm add command \
+		-label [mc "Open in Editor"] \
+		-command {do_editor $current_diff_path $cursorX $cursorY}
+	lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
+}
 $ctxm add separator
 create_common_diff_popup $ctxm
 
-- 
1.7.10.rc1.29.gf035d
Previous: Heiko VoigtNext: Bert Wesarg
Message 5 of 8 in “teach git gui to open the configured editor”
  1. 0/2 teach git gui to open the configured editorHeiko Voigt, Mar 23, 2012
  2. 1/2 git-gui: teach _which procedure to work with absolute pathsHeiko Voigt, Mar 23, 2012
  3. Junio C HamanoMar 23, 2012
  4. Heiko VoigtMar 25, 2012
  5. 2/2 git-gui: add "open in editor" diff context menu entryHeiko Voigt, Mar 23, 2012
  6. Bert WesargMar 23, 2012
  7. Heiko VoigtMar 25, 2012
  8. Bert WesargMar 25, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.