Skip to content

Commit dab92fe

Browse files
tiwaij6t
authored andcommitted
git-gui: Add support of SHA256 repo
This patch adds the basic support of SHA256 Git repositories. Most of changes are idiomatic replacement of the hard-coded hash ID length, but there are subtle things: * The hash length is determined on startup, and stored in $hashlength global variable (either 40 or 64). * The hard-coded "40" are replaced with $hashlength; for regexp patterns, the ugly string map is used. * Some code have the fixed numbers like 39 and 45, and those are replaced with the $hashlength and the offset correction. * $nullid and $nullid2 are generated for the hash length. A caveat is that repository picker dialog is performed before evaluating the repo type, hence $hashlength isn't set there yet. So the code dealing with the hard-coded "40" are handled differently; namely, the regexp range is expanded, and the null id is generated from the HEAD id length locally. Signed-off-by: Takashi Iwai <[email protected]> Signed-off-by: Johannes Sixt <[email protected]>
1 parent 532a054 commit dab92fe

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

git-gui.sh

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,17 @@ if {[catch {
12981298
set picked 1
12991299
}
13001300

1301+
# Use object format as hash algorithm (either "sha1" or "sha256")
1302+
set hashalgorithm [git rev-parse --show-object-format]
1303+
if {$hashalgorithm eq "sha1"} {
1304+
set hashlength 40
1305+
} elseif {$hashalgorithm eq "sha256"} {
1306+
set hashlength 64
1307+
} else {
1308+
puts stderr "Unknown hash algorithm: $hashalgorithm"
1309+
exit 1
1310+
}
1311+
13011312
# we expand the _gitdir when it's just a single dot (i.e. when we're being
13021313
# run from the .git dir itself) lest the routines to find the worktree
13031314
# get confused
@@ -1391,8 +1402,8 @@ set is_conflict_diff 0
13911402
set last_revert {}
13921403
set last_revert_enc {}
13931404

1394-
set nullid "0000000000000000000000000000000000000000"
1395-
set nullid2 "0000000000000000000000000000000000000001"
1405+
set nullid [string repeat 0 $hashlength]
1406+
set nullid2 "[string repeat 0 [expr $hashlength - 1]]1"
13961407

13971408
######################################################################
13981409
##
@@ -3202,7 +3213,7 @@ blame {
32023213
if {$head eq {}} {
32033214
load_current_branch
32043215
} else {
3205-
if {[regexp {^[0-9a-f]{1,39}$} $head]} {
3216+
if {[regexp [string map "@@ [expr $hashlength - 1]" {^[0-9a-f]{1,@@}$}] $head]} {
32063217
if {[catch {
32073218
set head [git rev-parse --verify $head]
32083219
} err]} {

lib/blame.tcl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ method _kill {} {
426426

427427
method _load {jump} {
428428
variable group_colors
429+
global hashlength
429430

430431
_hide_tooltip $this
431432

@@ -436,7 +437,7 @@ method _load {jump} {
436437
$i conf -state normal
437438
$i delete 0.0 end
438439
foreach g [$i tag names] {
439-
if {[regexp {^g[0-9a-f]{40}$} $g]} {
440+
if {[regexp [string map "@@ $hashlength" {^g[0-9a-f]{@@}$}] $g]} {
440441
$i tag delete $g
441442
}
442443
}
@@ -500,6 +501,8 @@ method _load {jump} {
500501
}
501502

502503
method _history_menu {} {
504+
global hashlength
505+
503506
set m $w.backmenu
504507
if {[winfo exists $m]} {
505508
$m delete 0 end
@@ -513,7 +516,7 @@ method _history_menu {} {
513516
set c [lindex $e 0]
514517
set f [lindex $e 1]
515518

516-
if {[regexp {^[0-9a-f]{40}$} $c]} {
519+
if {[regexp [string map "@@ $hashlength" {^[0-9a-f]{@@}$}] $c]} {
517520
set t [string range $c 0 8]...
518521
} elseif {$c eq {}} {
519522
set t {Working Directory}
@@ -627,6 +630,7 @@ method _exec_blame {cur_w cur_d options cur_s} {
627630
method _read_blame {fd cur_w cur_d} {
628631
upvar #0 $cur_d line_data
629632
variable group_colors
633+
global hashlength nullid
630634

631635
if {$fd ne $current_fd} {
632636
catch {close $fd}
@@ -635,7 +639,7 @@ method _read_blame {fd cur_w cur_d} {
635639

636640
$cur_w conf -state normal
637641
while {[gets $fd line] >= 0} {
638-
if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
642+
if {[regexp [string map "@@ $hashlength" {^([a-z0-9]{@@}) (\d+) (\d+) (\d+)$}] $line line \
639643
cmit original_line final_line line_count]} {
640644
set r_commit $cmit
641645
set r_orig_line $original_line
@@ -648,7 +652,7 @@ method _read_blame {fd cur_w cur_d} {
648652
set oln $r_orig_line
649653
set cmit $r_commit
650654

651-
if {[regexp {^0{40}$} $cmit]} {
655+
if {$cmit eq $nullid} {
652656
set commit_abbr work
653657
set commit_type curr_commit
654658
} elseif {$cmit eq $commit} {

lib/choose_repository.tcl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ method _do_clone_full_end {ok} {
879879
if {[file exists [gitdir FETCH_HEAD]]} {
880880
set fd [safe_open_file [gitdir FETCH_HEAD] r]
881881
while {[gets $fd line] >= 0} {
882-
if {[regexp "^(.{40})\t\t" $line line HEAD]} {
882+
if {[regexp "^(.{40,64})\t\t" $line line HEAD]} {
883883
break
884884
}
885885
}
@@ -987,8 +987,9 @@ method _readtree_wait {fd} {
987987

988988
# -- Run the post-checkout hook.
989989
#
990-
set fd_ph [githook_read post-checkout [string repeat 0 40] \
991-
[git rev-parse HEAD] 1]
990+
set head_id [git rev-parse HEAD]
991+
set fd_ph [githook_read post-checkout \
992+
[string repeat 0 [string length $head_id]] $head_id 1]
992993
if {$fd_ph ne {}} {
993994
global pch_error
994995
set pch_error {}

lib/commit.tcl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ proc commit_committree {fd_wt curHEAD msg_p} {
346346
global file_states selected_paths rescan_active
347347
global repo_config
348348
global env
349+
global hashlength
349350

350351
gets $fd_wt tree_id
351352
if {[catch {close $fd_wt} err]} {
@@ -365,7 +366,7 @@ proc commit_committree {fd_wt curHEAD msg_p} {
365366
close $fd_ot
366367

367368
if {[string equal -length 5 {tree } $old_tree]
368-
&& [string length $old_tree] == 45} {
369+
&& [string length $old_tree] == [expr {$hashlength + 5}]} {
369370
set old_tree [string range $old_tree 5 end]
370371
} else {
371372
error [mc "Commit %s appears to be corrupt" $PARENT]

lib/remote_branch_delete.tcl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,16 @@ method _load {cache uri} {
323323
}
324324

325325
method _read {cache fd} {
326+
global hashlength
327+
326328
if {$fd ne $active_ls} {
327329
catch {close $fd}
328330
return
329331
}
330332

331333
while {[gets $fd line] >= 0} {
332334
if {[string match {*^{}} $line]} continue
333-
if {[regexp {^([0-9a-f]{40}) (.*)$} $line _junk obj ref]} {
335+
if {[regexp [string map "@@ $hashlength" {^([0-9a-f]{@@}) (.*)$}] $line _junk obj ref]} {
334336
if {[regsub ^refs/heads/ $ref {} abr]} {
335337
lappend head_list $abr
336338
lappend head_cache($cache) $abr

0 commit comments

Comments
 (0)