Skip to content

Commit

Permalink
tg: initial Git sha256 hash algorithm support
Browse files Browse the repository at this point in the history
The primary issue that arises when supporting a different hash
algorithm is that the "constant" hash values used for different
items such as the "null" object and the "empty" blob and tree all
change.

Update the code to automatically use the correct "constant" hash
values for the hash algorithm that's currently in use.

Also make sure the wayback machine preserves the correct hash
algorithm when activating wayback mode.

Signed-off-by: Kyle J. McKay <[email protected]>
  • Loading branch information
mackyle committed Aug 21, 2021
1 parent 83ec23f commit 746c77a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
10 changes: 7 additions & 3 deletions tg--index-merge-one-file.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/sh

# tg--index-merge-one-file -- auto merge a file without touching working tree
# Copyright (C) 2017 Kyle J. McKay
# All rights reserved.
# Copyright (C) 2017,2021 Kyle J. McKay
# All rights reserved
# License GPLv2+

# $TG_TMP_DIR => location to store temporary files
Expand All @@ -15,7 +15,6 @@
# $6 => stage 2 mode
# $7 => stage 3 mode

nullsha="0000000000000000000000000000000000000000"
tab=' '

if [ "$1" = "-h" ] && [ $# -eq 1 ]; then
Expand All @@ -31,6 +30,11 @@ esac; fi

[ $# -eq 7 ] || exit 1

mtblob="$(git hash-object -t blob --stdin </dev/null 2>/dev/null)" || :
nullsha="0000000000000000000000000000000000000000"
test "${#mtblob}" != "64" ||
nullsha="0000000000000000000000000000000000000000000000000000000000000000"

# The read-tree --aggressive option handles three cases that may end up in
# here:
#
Expand Down
59 changes: 49 additions & 10 deletions tg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ octet='[0-9a-f][0-9a-f]'
octet4="$octet$octet$octet$octet"
octet19="$octet4$octet4$octet4$octet4$octet$octet$octet"
octet20="$octet4$octet4$octet4$octet4$octet4"
nullsha="0000000000000000000000000000000000000000" # :|git mktree|tr 0-9a-f 0
mtblob="e69de29bb2d1d6434b8b29ae775ad8c2e48c5391" # :|git hash-object --stdin -w
tab=' '
lf='
'
Expand Down Expand Up @@ -2099,6 +2097,46 @@ setup_git_dirs()
fi
[ -n "$git_dir" ] && [ -n "$git_common_dir" ] &&
[ -d "$git_dir" ] && [ -d "$git_common_dir" ] || die "Not a git repository"
activate_awksome $1
}

mtblob=
setup_hashalgo_vars()
{
test -z "$mtblob" || return 0

# the empty blob will match the hash algorithm of the repository (if any)

mtblob="$(git hash-object -t blob --stdin </dev/null 2>/dev/null)" ||
die "git hash-object failed"
case "${#mtblob}" in
40)
nullsha="0000000000000000000000000000000000000000"
;;
64)
nullsha="0000000000000000000000000000000000000000000000000000000000000000"
;;
*)
die "git hash-object failed"
esac
}

## Include awk scripts and their utility functions (separated for easier debugging)
awksome_loaded=
activate_awksome()
{
test -z "$awksome_loaded" || return 0

# tg--awksome requires that mtblob be set before sourcing it;
# mtblob is configured by setup_hashalgo_vars;
# therefore run setup_hashalgo_vars first.

setup_hashalgo_vars $1
[ -f "$TG_INST_CMDDIR/tg--awksome" ] && [ -r "$TG_INST_CMDDIR/tg--awksome" ] ||
die "Missing awk scripts: '$TG_INST_CMDDIR/tg--awksome'"
. "$TG_INST_CMDDIR/tg--awksome"

awksome_loaded=1
}

basic_setup_remote()
Expand Down Expand Up @@ -2340,14 +2378,20 @@ activate_wayback_machine()
qpesc="$(printf '%s\n' "$git_common_dir" | sed -e 's/\([\\""]\)/\\\1/g' -e '$!s/$/\\n/' | tr -d '\n')"
laru="false"
[ -z "$2" ] || laru="true"
rfv=0
ofmt=
if [ "${#mtblob}" = 64 ]; then
rfv=1
ofmt="$lf${tab}objectFormat = sha256"
fi
printf '%s' "\
[include]
path = \"$qpesc/config\"
[core]
bare = false
logAllRefUpdates = $laru
repositoryFormatVersion = 0
[extensions]
repositoryFormatVersion = $rfv
[extensions]$ofmt
preciousObjects = true
[gc]
auto = 0
Expand Down Expand Up @@ -2417,6 +2461,7 @@ set_topbases()

[ -z "$tg_topbases_set" ] || return 0

activate_awksome # may not have been loaded yet
topbases_implicit_default=1
# See if topgit.top-bases is set to heads or refs
tgtb="$(git config "topgit.top-bases" 2>/dev/null)" || :
Expand Down Expand Up @@ -2556,12 +2601,6 @@ v_get_abs_path()
[ -d "$TG_INST_CMDDIR" ] ||
die "No command directory: '$TG_INST_CMDDIR'"

## Include awk scripts and their utility functions (separated for easier debugging)

[ -f "$TG_INST_CMDDIR/tg--awksome" ] && [ -r "$TG_INST_CMDDIR/tg--awksome" ] ||
die "Missing awk scripts: '$TG_INST_CMDDIR/tg--awksome'"
. "$TG_INST_CMDDIR/tg--awksome"

if [ -n "$tg__include" ]; then

# We were sourced from another script for our utility functions;
Expand Down

0 comments on commit 746c77a

Please sign in to comment.