diff mbox series

[RFC] gitk: avoid hardcoded 40 characters for oid

Message ID 20211011114723.204-1-carenas@gmail.com (mailing list archive)
State New, archived
Headers show
Series [RFC] gitk: avoid hardcoded 40 characters for oid | expand

Commit Message

Carlo Marcelo Arenas Belón Oct. 11, 2021, 11:47 a.m. UTC
Current git could return ids larger than 40 characters if using
SHA-256.

Detect if the current repository does and set the oid_size to 64
instead, but fallback to 40 for backward compatibility, as well
as matching ids in a hash agnostic way.

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
---
With this patch, I was able to open gitk in a SHA-256 repository
without an error, and still do the same in a regular one.

Sending it as an RFC, since I am not a gitk user and haven't done
much testing of it, and because it is likely incomplete, as they
are still variables and other labels referring to SHA1 which would
need to be updated as well.

There is also a related set of changes needed for git-gui that
will be send independently.

 gitk | 45 +++++++++++++++++++++++++++++----------------
 1 file changed, 29 insertions(+), 16 deletions(-)
diff mbox series

Patch

diff --git a/gitk b/gitk
index 23d9dd1..5087543 100755
--- a/gitk
+++ b/gitk
@@ -1418,6 +1418,7 @@  proc getcommitlines {fd inst view updating}  {
     global parents children curview hlview
     global idpending ordertok
     global varccommits varcid varctok vtokmod vfilelimit vshortids
+    global oid_size
 
     set stuff [read $fd 500000]
     # git log doesn't terminate the last commit with a null...
@@ -1500,7 +1501,7 @@  proc getcommitlines {fd inst view updating}  {
             }
             set ok 1
             foreach id $ids {
-                if {[string length $id] != 40} {
+                if {[string length $id] != $oid_size} {
                     set ok 0
                     break
                 }
@@ -3925,7 +3926,7 @@  proc stopblaming {} {
 }
 
 proc read_line_source {fd inst} {
-    global blamestuff curview commfd blameinst nullid nullid2
+    global blamestuff curview commfd blameinst nullid nullid2 oid_size
 
     while {[gets $fd line] >= 0} {
         lappend blamestuff($inst) $line
@@ -3946,7 +3947,7 @@  proc read_line_source {fd inst} {
     set line [split [lindex $blamestuff($inst) 0] " "]
     set id [lindex $line 0]
     set lnum [lindex $line 1]
-    if {[string length $id] == 40 && [string is xdigit $id] &&
+    if {[string length $id] == $oid_size && [string is xdigit $id] &&
         [string is digit -strict $lnum]} {
         # look for "filename" line
         foreach l $blamestuff($inst) {
@@ -5094,7 +5095,7 @@  proc shortids {ids} {
     foreach id $ids {
         if {[llength $id] > 1} {
             lappend res [shortids $id]
-        } elseif {[regexp {^[0-9a-f]{40}$} $id]} {
+        } elseif {[regexp {^[0-9a-f]{40}(?:[0-9a-f]{24})?$} $id]} {
             lappend res [string range $id 0 7]
         } else {
             lappend res $id
@@ -5269,13 +5270,14 @@  proc get_viewmainhead {view} {
 # git rev-list should give us just 1 line to use as viewmainheadid($view)
 proc getviewhead {fd inst view} {
     global viewmainheadid commfd curview viewinstances showlocalchanges
+    global oid_size
 
     set id {}
     if {[gets $fd line] < 0} {
         if {![eof $fd]} {
             return 1
         }
-    } elseif {[string length $line] == 40 && [string is xdigit $line]} {
+    } elseif {[string length $line] == $oid_size && [string is xdigit $line]} {
         set id $line
     }
     set viewmainheadid($view) $id
@@ -7070,13 +7072,14 @@  proc appendwithlinks {text tags} {
 proc setlink {id lk} {
     global curview ctext pendinglinks
     global linkfgcolor
+    global oid_size
 
     if {[string range $id 0 1] eq "-g"} {
       set id [string range $id 2 end]
     }
 
     set known 0
-    if {[string length $id] < 40} {
+    if {[string length $id] < $oid_size} {
         set matches [longid $id]
         if {[llength $matches] > 0} {
             if {[llength $matches] > 1} return
@@ -8715,8 +8718,8 @@  proc incrfont {inc} {
 }
 
 proc clearsha1 {} {
-    global sha1entry sha1string
-    if {[string length $sha1string] == 40} {
+    global sha1entry sha1string oid_size
+    if {[string length $sha1string] == $oid_size} {
         $sha1entry delete 0 end
     }
 }
@@ -9445,11 +9448,11 @@  proc mktaggo {} {
 }
 
 proc copyreference {} {
-    global rowmenuid autosellen
+    global rowmenuid autosellen oid_size
 
     set format "%h (\"%s\", %ad)"
     set cmd [list git show -s --pretty=format:$format --date=short]
-    if {$autosellen < 40} {
+    if {$autosellen < $oid_size} {
         lappend cmd --abbrev=$autosellen
     }
     set reference [eval exec $cmd $rowmenuid]
@@ -9459,7 +9462,7 @@  proc copyreference {} {
 }
 
 proc writecommit {} {
-    global rowmenuid wrcomtop commitinfo wrcomcmd NS
+    global rowmenuid wrcomtop commitinfo wrcomcmd NS oid_size
 
     set top .writecommit
     set wrcomtop $top
@@ -9469,7 +9472,7 @@  proc writecommit {} {
     ${NS}::label $top.title -text [mc "Write commit to file"]
     grid $top.title - -pady 10
     ${NS}::label $top.id -text [mc "ID:"]
-    ${NS}::entry $top.sha1 -width 40
+    ${NS}::entry $top.sha1 -width $oid_size
     $top.sha1 insert 0 $rowmenuid
     $top.sha1 conf -state readonly
     grid $top.id $top.sha1 -sticky w
@@ -9548,7 +9551,7 @@  proc mvbranch {} {
 }
 
 proc branchdia {top valvar uivar} {
-    global NS commitinfo
+    global NS commitinfo oid_size
     upvar $valvar val $uivar ui
 
     catch {destroy $top}
@@ -9557,7 +9560,7 @@  proc branchdia {top valvar uivar} {
     ${NS}::label $top.title -text $ui(title)
     grid $top.title - -pady 10
     ${NS}::label $top.id -text [mc "ID:"]
-    ${NS}::entry $top.sha1 -width 40
+    ${NS}::entry $top.sha1 -width $oid_size
     $top.sha1 insert 0 $val(id)
     $top.sha1 conf -state readonly
     grid $top.id $top.sha1 -sticky w
@@ -12565,8 +12568,18 @@  if {$i >= [llength $argv] && $revtreeargs ne {}} {
     }
 }
 
-set nullid "0000000000000000000000000000000000000000"
-set nullid2 "0000000000000000000000000000000000000001"
+if {[catch {exec git config --get extensions.objectformat} hash]} {
+    set oid_size 40
+} else {
+    if { $hash eq "sha256" } {
+        set oid_size 64
+    } else {
+        set oid_size 40
+    }
+}
+
+set nullid %-0*s $oid_size 0
+set nullid2 %-0*s $oid_size 1
 set nullfile "/dev/null"
 
 set have_tk85 [expr {[package vcompare $tk_version "8.5"] >= 0}]