Skip to contentSkip to conversation

gitk: Add options --select-file and --select-line

PATCH Superseded Stefan Haller 2 participants 5 messages Oct 14, 2020 – Dec 19, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gitk: Add options --select-file and --select-line

These can be used in combination with --select-commit to jump to a given line in a patch on startup. (They don't have any effect when --select-commit is not used.)

This is useful for the "Show History Context" command in Git Gui's blame window, which currently only jumps to the right commit in gitk, but doesn't select the line that the context menu was opened on.

Also, these options allow for powerful editor integration; they make it possible to jump into gitk right from a text editor. For example, here's a small ruby script that takes a file path and a line number and opens gitk with the commit selected that last modified that line. This can easily be mapped to a key in vim or other editors.

    #!/usr/bin/env ruby

    if ARGV.length != 2
        puts "Usage: #{$0} <file> <line>"
        exit 1
    end

    file, line = ARGV
    blame_output = `git blame -p -L#{line},+1 "#{file}"`
    exit 1 if $?.exitstatus != 0

    blame_output_lines = blame_output.split("\n")
    commit, line = blame_output_lines[0].split

    file = blame_output_lines.grep(/^filename /)[0][9..-1]
    date = blame_output_lines.grep(/^committer-time /)[0][15..-1]
    two_weeks_later = date.to_i + 60 * 60 * 24 * 7 * 2

    system "gitk --before='#{two_weeks_later}' \
                 --select-commit=#{commit} \
                 --select-file='#{file}' \
                 --select-line=#{line} &"
---
 gitk | 31 ++++++++++++++++++++++++++++---
 1 file changed, 28 insertions(+), 3 deletions(-)
gitk +28 -3
index 23d9dd1..cf70313 100755
@@ -475,12 +475,17 @@ proc stop_rev_list {view} {
 }
 
 proc reset_pending_select {selid} {
-    global pending_select mainheadid selectheadid
+    global pending_select pending_select_file pending_select_line
+    global mainheadid selectheadid select_file select_line
 
     if {$selid ne {}} {
         set pending_select $selid
     } elseif {$selectheadid ne {}} {
         set pending_select $selectheadid
+        if {$select_file ne {} && $select_line ne {}} {
+            set pending_select_file $select_file
+            set pending_select_line $select_line
+        }
     } else {
         set pending_select $mainheadid
     }
@@ -1612,6 +1617,16 @@ proc getcommitlines {fd inst view updating}  {
     return 2
 }
 
+proc select_pending_line {} {
+    global pending_select pending_select_file pending_select_line
+
+    set desired_loc [expr {[info exists pending_select_file]
+        ? [list $pending_select_file $pending_select_line]
+        : {}}]
+
+    selectline [rowofcommit $pending_select] 1 $desired_loc
+}
+
 proc chewcommits {} {
     global curview hlview viewcomplete
     global pending_select
@@ -1626,7 +1641,7 @@ proc chewcommits {} {
             reset_pending_select {}
 
             if {[commitinview $pending_select $curview]} {
-                selectline [rowofcommit $pending_select] 1
+                select_pending_line
             } else {
                 set row [first_real_row]
                 selectline $row 1
@@ -5244,7 +5259,7 @@ proc layoutmore {} {
     if {[info exists pending_select] &&
         [commitinview $pending_select $curview]} {
         update
-        selectline [rowofcommit $pending_select] 1
+        select_pending_line
     }
     drawvisible
 }
@@ -7325,6 +7340,8 @@ proc selectline {l isnew {desired_loc {}} {switch_to_patch 0}} {
     global vinlinediff
 
     unset -nocomplain pending_select
+    unset -nocomplain pending_select_file
+    unset -nocomplain pending_select_line
     $canv delete hover
     normalline
     unsel_reflist
@@ -12507,6 +12524,8 @@ if {[catch {set gitdir [exec git rev-parse --git-dir]}]} {
 
 set selecthead {}
 set selectheadid {}
+set select_file {}
+set select_line {}
 
 set revtreeargs {}
 set cmdline_files {}
@@ -12522,6 +12541,12 @@ foreach arg $argv {
         "--select-commit=*" {
             set selecthead [string range $arg 16 end]
         }
+        "--select-file=*" {
+            set select_file [string range $arg 14 end]
+        }
+        "--select-line=*" {
+            set select_line [string range $arg 14 end]
+        }
         "--argscmd=*" {
             set revtreeargscmd [string range $arg 10 end]
         }
Signature · 1 line
2.29.0.rc1.13.g3b7fca9674
#1 lore raw
Quoted 5 lines · Stefan Haller

These can be used in combination with --select-commit to jump to a given line in a patch on startup. (They don't have any effect when --select-commit is not used.)
[...]
---

Missing sign-off.

#2 lore raw

PATCH v2 1/1 gitk: Add options --select-file and --select-line

These can be used in combination with --select-commit to jump to a given line in a patch on startup. (They don't have any effect when --select-commit is not used.)

This is useful for the "Show History Context" command in Git Gui's blame window, which currently only jumps to the right commit in gitk, but doesn't select the line that the context menu was opened on.

Also, these options allow for powerful editor integration; they make it possible to jump into gitk right from a text editor. For example, here's a small ruby script that takes a file path and a line number and opens gitk with the commit selected that last modified that line. This can easily be mapped to a key in vim or other editors.

    #!/usr/bin/env ruby

    if ARGV.length != 2
        puts "Usage: #{$0} <file> <line>"
        exit 1
    end

    file, line = ARGV
    blame_output = `git blame -p -L#{line},+1 "#{file}"`
    exit 1 if $?.exitstatus != 0

    blame_output_lines = blame_output.split("\n")
    commit, line = blame_output_lines[0].split

    file = blame_output_lines.grep(/^filename /)[0][9..-1]
    date = blame_output_lines.grep(/^committer-time /)[0][15..-1]
    two_weeks_later = date.to_i + 60 * 60 * 24 * 7 * 2

    system "gitk --before='#{two_weeks_later}' \
                 --select-commit=#{commit} \
                 --select-file='#{file}' \
                 --select-line=#{line} &"

---
Second version: added Signed-off-by. No other changes.

 gitk | 31 ++++++++++++++++++++++++++++---
 1 file changed, 28 insertions(+), 3 deletions(-)
gitk +28 -3
index 23d9dd1..cf70313 100755
@@ -475,12 +475,17 @@ proc stop_rev_list {view} {
 }
 
 proc reset_pending_select {selid} {
-    global pending_select mainheadid selectheadid
+    global pending_select pending_select_file pending_select_line
+    global mainheadid selectheadid select_file select_line
 
     if {$selid ne {}} {
         set pending_select $selid
     } elseif {$selectheadid ne {}} {
         set pending_select $selectheadid
+        if {$select_file ne {} && $select_line ne {}} {
+            set pending_select_file $select_file
+            set pending_select_line $select_line
+        }
     } else {
         set pending_select $mainheadid
     }
@@ -1612,6 +1617,16 @@ proc getcommitlines {fd inst view updating}  {
     return 2
 }
 
+proc select_pending_line {} {
+    global pending_select pending_select_file pending_select_line
+
+    set desired_loc [expr {[info exists pending_select_file]
+        ? [list $pending_select_file $pending_select_line]
+        : {}}]
+
+    selectline [rowofcommit $pending_select] 1 $desired_loc
+}
+
 proc chewcommits {} {
     global curview hlview viewcomplete
     global pending_select
@@ -1626,7 +1641,7 @@ proc chewcommits {} {
             reset_pending_select {}
 
             if {[commitinview $pending_select $curview]} {
-                selectline [rowofcommit $pending_select] 1
+                select_pending_line
             } else {
                 set row [first_real_row]
                 selectline $row 1
@@ -5244,7 +5259,7 @@ proc layoutmore {} {
     if {[info exists pending_select] &&
         [commitinview $pending_select $curview]} {
         update
-        selectline [rowofcommit $pending_select] 1
+        select_pending_line
     }
     drawvisible
 }
@@ -7325,6 +7340,8 @@ proc selectline {l isnew {desired_loc {}} {switch_to_patch 0}} {
     global vinlinediff
 
     unset -nocomplain pending_select
+    unset -nocomplain pending_select_file
+    unset -nocomplain pending_select_line
     $canv delete hover
     normalline
     unsel_reflist
@@ -12507,6 +12524,8 @@ if {[catch {set gitdir [exec git rev-parse --git-dir]}]} {
 
 set selecthead {}
 set selectheadid {}
+set select_file {}
+set select_line {}
 
 set revtreeargs {}
 set cmdline_files {}
@@ -12522,6 +12541,12 @@ foreach arg $argv {
         "--select-commit=*" {
             set selecthead [string range $arg 16 end]
         }
+        "--select-file=*" {
+            set select_file [string range $arg 14 end]
+        }
+        "--select-line=*" {
+            set select_line [string range $arg 14 end]
+        }
         "--argscmd=*" {
             set revtreeargscmd [string range $arg 10 end]
         }

--
2.29.0.rc1.13.g3b7fca9674

#3 lore raw
Quoted 8 lines · Stefan Haller

These can be used in combination with --select-commit to jump to a given line in a patch on startup. (They don't have any effect when --select-commit is not used.)

This is useful for the "Show History Context" command in Git Gui's blame window, which currently only jumps to the right commit in gitk, but doesn't select the line that the context menu was opened on.

Quoted 5 lines

Also, these options allow for powerful editor integration; they make it possible to jump into gitk right from a text editor. For example, here's a small ruby script that takes a file path and a line number and opens gitk with the commit selected that last modified that line. This can easily be mapped to a key in vim or other editors.

Just a gentle ping. Any thoughts?

I have been running with this patch for a few years now, and I use the functionality daily. I'd love to get this applied so that I don't have to maintain this patch locally. Also, my co-workers are jealous every time they see what I can do with this. :-)

Thanks, Stefan.

Quoted 123 lines
    #!/usr/bin/env ruby

    if ARGV.length != 2
        puts "Usage: #{$0} <file> <line>"
        exit 1
    end

    file, line = ARGV
    blame_output = `git blame -p -L#{line},+1 "#{file}"`
    exit 1 if $?.exitstatus != 0

    blame_output_lines = blame_output.split("\n")
    commit, line = blame_output_lines[0].split

    file = blame_output_lines.grep(/^filename /)[0][9..-1]
    date = blame_output_lines.grep(/^committer-time /)[0][15..-1]
    two_weeks_later = date.to_i + 60 * 60 * 24 * 7 * 2

    system "gitk --before='#{two_weeks_later}' \
                 --select-commit=#{commit} \
                 --select-file='#{file}' \
                 --select-line=#{line} &"

---
Second version: added Signed-off-by. No other changes.

 gitk | 31 ++++++++++++++++++++++++++++---
 1 file changed, 28 insertions(+), 3 deletions(-)
gitk +28 -4
index 23d9dd1..cf70313 100755
@@ -475,12 +475,17 @@ proc stop_rev_list {view} {
 }
 
 proc reset_pending_select {selid} {
-    global pending_select mainheadid selectheadid
+    global pending_select pending_select_file pending_select_line
+    global mainheadid selectheadid select_file select_line
 
     if {$selid ne {}} {
         set pending_select $selid
     } elseif {$selectheadid ne {}} {
         set pending_select $selectheadid
+        if {$select_file ne {} && $select_line ne {}} {
+            set pending_select_file $select_file
+            set pending_select_line $select_line
+        }
     } else {
         set pending_select $mainheadid
     }
@@ -1612,6 +1617,16 @@ proc getcommitlines {fd inst view updating}  {
     return 2
 }
 
+proc select_pending_line {} {
+    global pending_select pending_select_file pending_select_line
+
+    set desired_loc [expr {[info exists pending_select_file]
+        ? [list $pending_select_file $pending_select_line]
+        : {}}]
+
+    selectline [rowofcommit $pending_select] 1 $desired_loc
+}
+
 proc chewcommits {} {
     global curview hlview viewcomplete
     global pending_select
@@ -1626,7 +1641,7 @@ proc chewcommits {} {
             reset_pending_select {}
 
             if {[commitinview $pending_select $curview]} {
-                selectline [rowofcommit $pending_select] 1
+                select_pending_line
             } else {
                 set row [first_real_row]
                 selectline $row 1
@@ -5244,7 +5259,7 @@ proc layoutmore {} {
     if {[info exists pending_select] &&
         [commitinview $pending_select $curview]} {
         update
-        selectline [rowofcommit $pending_select] 1
+        select_pending_line
     }
     drawvisible
 }
@@ -7325,6 +7340,8 @@ proc selectline {l isnew {desired_loc {}} {switch_to_patch 0}} {
     global vinlinediff
 
     unset -nocomplain pending_select
+    unset -nocomplain pending_select_file
+    unset -nocomplain pending_select_line
     $canv delete hover
     normalline
     unsel_reflist
@@ -12507,6 +12524,8 @@ if {[catch {set gitdir [exec git rev-parse --git-dir]}]} {
 
 set selecthead {}
 set selectheadid {}
+set select_file {}
+set select_line {}
 
 set revtreeargs {}
 set cmdline_files {}
@@ -12522,6 +12541,12 @@ foreach arg $argv {
         "--select-commit=*" {
             set selecthead [string range $arg 16 end]
         }
+        "--select-file=*" {
+            set select_file [string range $arg 14 end]
+        }
+        "--select-line=*" {
+            set select_line [string range $arg 14 end]
+        }
         "--argscmd=*" {
             set revtreeargscmd [string range $arg 10 end]
         }
--

2.29.0.rc1.13.g3b7fca9674

#4 lore raw
Quoted 15 lines · Stefan Haller
Quoted 8 lines · Stefan Haller

These can be used in combination with --select-commit to jump to a given line in a patch on startup. (They don't have any effect when --select-commit is not used.)

This is useful for the "Show History Context" command in Git Gui's blame window, which currently only jumps to the right commit in gitk, but doesn't select the line that the context menu was opened on.

Quoted 5 lines

Also, these options allow for powerful editor integration; they make it possible to jump into gitk right from a text editor. For example, here's a small ruby script that takes a file path and a line number and opens gitk with the commit selected that last modified that line. This can easily be mapped to a key in vim or other editors.

Quoted 8 lines

Just a gentle ping. Any thoughts?

I have been running with this patch for a few years now, and I use the functionality daily. I'd love to get this applied so that I don't have to maintain this patch locally. Also, my co-workers are jealous every time they see what I can do with this. :-)

Thanks, Stefan.

Another friendly ping; it's been two months since I submitted the patch. Is there anything else I can do to get this included?

Thanks, Stefan.

Quoted 123 lines
Quoted 123 lines
    #!/usr/bin/env ruby

    if ARGV.length != 2
        puts "Usage: #{$0} <file> <line>"
        exit 1
    end

    file, line = ARGV
    blame_output = `git blame -p -L#{line},+1 "#{file}"`
    exit 1 if $?.exitstatus != 0

    blame_output_lines = blame_output.split("\n")
    commit, line = blame_output_lines[0].split

    file = blame_output_lines.grep(/^filename /)[0][9..-1]
    date = blame_output_lines.grep(/^committer-time /)[0][15..-1]
    two_weeks_later = date.to_i + 60 * 60 * 24 * 7 * 2

    system "gitk --before='#{two_weeks_later}' \
                 --select-commit=#{commit} \
                 --select-file='#{file}' \
                 --select-line=#{line} &"

---
Second version: added Signed-off-by. No other changes.

 gitk | 31 ++++++++++++++++++++++++++++---
 1 file changed, 28 insertions(+), 3 deletions(-)
gitk +28 -4
index 23d9dd1..cf70313 100755
@@ -475,12 +475,17 @@ proc stop_rev_list {view} {
 }
 
 proc reset_pending_select {selid} {
-    global pending_select mainheadid selectheadid
+    global pending_select pending_select_file pending_select_line
+    global mainheadid selectheadid select_file select_line
 
     if {$selid ne {}} {
         set pending_select $selid
     } elseif {$selectheadid ne {}} {
         set pending_select $selectheadid
+        if {$select_file ne {} && $select_line ne {}} {
+            set pending_select_file $select_file
+            set pending_select_line $select_line
+        }
     } else {
         set pending_select $mainheadid
     }
@@ -1612,6 +1617,16 @@ proc getcommitlines {fd inst view updating}  {
     return 2
 }
 
+proc select_pending_line {} {
+    global pending_select pending_select_file pending_select_line
+
+    set desired_loc [expr {[info exists pending_select_file]
+        ? [list $pending_select_file $pending_select_line]
+        : {}}]
+
+    selectline [rowofcommit $pending_select] 1 $desired_loc
+}
+
 proc chewcommits {} {
     global curview hlview viewcomplete
     global pending_select
@@ -1626,7 +1641,7 @@ proc chewcommits {} {
             reset_pending_select {}
 
             if {[commitinview $pending_select $curview]} {
-                selectline [rowofcommit $pending_select] 1
+                select_pending_line
             } else {
                 set row [first_real_row]
                 selectline $row 1
@@ -5244,7 +5259,7 @@ proc layoutmore {} {
     if {[info exists pending_select] &&
         [commitinview $pending_select $curview]} {
         update
-        selectline [rowofcommit $pending_select] 1
+        select_pending_line
     }
     drawvisible
 }
@@ -7325,6 +7340,8 @@ proc selectline {l isnew {desired_loc {}} {switch_to_patch 0}} {
     global vinlinediff
 
     unset -nocomplain pending_select
+    unset -nocomplain pending_select_file
+    unset -nocomplain pending_select_line
     $canv delete hover
     normalline
     unsel_reflist
@@ -12507,6 +12524,8 @@ if {[catch {set gitdir [exec git rev-parse --git-dir]}]} {
 
 set selecthead {}
 set selectheadid {}
+set select_file {}
+set select_line {}
 
 set revtreeargs {}
 set cmdline_files {}
@@ -12522,6 +12541,12 @@ foreach arg $argv {
         "--select-commit=*" {
             set selecthead [string range $arg 16 end]
         }
+        "--select-file=*" {
+            set select_file [string range $arg 14 end]
+        }
+        "--select-line=*" {
+            set select_line [string range $arg 14 end]
+        }
         "--argscmd=*" {
             set revtreeargscmd [string range $arg 10 end]
         }
--

2.29.0.rc1.13.g3b7fca9674