Big coding style shakeup, use Titlecase and lowercase variables

This commit is contained in:
root 2013-02-14 00:00:00 +00:00
parent d342bbceab
commit ca6a984195

View file

@ -9,12 +9,12 @@
#set -x #set -x
set -e set -e
DID_FIND_REPO= # yes for connected, no for no repo Did_find_repo= # yes for connected, no for no repo
LOCALDIR="${GIT_DIR:-.git}/remote-gcrypt" Localdir="${GIT_DIR:-.git}/remote-gcrypt"
export GITCEPTION="$GITCEPTION+" # Reuse $GREF except when stacked export GITCEPTION="$GITCEPTION+" # Reuse $Gref except when stacked
GREF="refs/gcrypt/gitception$GITCEPTION" Gref="refs/gcrypt/gitception$GITCEPTION"
REPOID= Repoid=
PACKPFX="pack :SHA224:" Packpfx="pack :SHA224:"
isurl() { test -z "${2%%$1://*}" ; } isurl() { test -z "${2%%$1://*}" ; }
@ -30,17 +30,14 @@ splitcolon()
gitception_get() gitception_get()
{ {
# Take care to preserve FETCH_HEAD # Take care to preserve FETCH_HEAD
local FHEAD local ret_=: obj_id= f_head="$GIT_DIR/FETCH_HEAD"
local RETVAL [ -e "$f_head" ] && command mv -f "$f_head" "$f_head.$$~" || :
FHEAD="$GIT_DIR/FETCH_HEAD" git fetch -q -f "$1" HEAD:"$Gref" 2>/dev/tty >/dev/null &&
[ -e "$FHEAD" ] && command mv -f "$FHEAD" "$FHEAD.$$~" || : obj_id="$(git ls-tree "$Gref" | xgrep -E '\b'"$2"'$' | awk '{print $3}')" &&
git fetch -q -f "$1" HEAD:"$GREF" 2>/dev/tty >/dev/null && [ -n "$obj_id" ] && git cat-file blob "$obj_id" && ret_=: ||
OBJID="$(git ls-tree "$GREF" | { ret_=false && : ; }
xgrep -E '\b'"$2"'$' | awk '{print $3}')" && [ -e "$f_head.$$~" ] && command mv -f "$f_head.$$~" "$f_head" || :
[ -n "$OBJID" ] && git cat-file blob "$OBJID" && RETVAL=: || $ret_
{ RETVAL=false && : ; }
[ -e "$FHEAD.$$~" ] && command mv -f "$FHEAD.$$~" "$FHEAD" || :
$RETVAL
} }
anon_commit() anon_commit()
@ -61,13 +58,14 @@ update_tree()
} }
# Put giturl $1, file $2 # Put giturl $1, file $2
# depends on previous GET to set $GREF and depends on PUT_FINAL later # depends on previous GET to set $Gref and depends on PUT_FINAL later
gitception_put() gitception_put()
{ {
OBJID=$(git hash-object -w --stdin) && local obj_id= tree_id= commit_id=
TREEID=$(update_tree "$GREF" "$2" "$OBJID") && obj_id=$(git hash-object -w --stdin) &&
COMMITID=$(anon_commit "$TREEID" -m "x") && tree_id=$(update_tree "$Gref" "$2" "$obj_id") &&
git update-ref "$GREF" "$COMMITID" commit_id=$(anon_commit "$tree_id" -m "x") &&
git update-ref "$Gref" "$commit_id"
} }
## end gitception ## end gitception
@ -115,7 +113,7 @@ PUT_FINAL()
{ {
if isurl gitception "$1" if isurl gitception "$1"
then then
git push --quiet -f "${1#gitception://}" "$GREF":master git push --quiet -f "${1#gitception://}" "$Gref":master
else else
: :
fi fi
@ -142,19 +140,19 @@ PUTREPO()
CLEAN_FINAL() CLEAN_FINAL()
{ {
isurl gitception "$1" && git update-ref -d "$GREF" || : isurl gitception "$1" && git update-ref -d "$Gref" || :
} }
ENCRYPT() ENCRYPT()
{ {
(printf "%s" "$MASTERKEY" | (printf "%s" "$Masterkey" |
gpg --batch --force-mdc --compress-algo none \ gpg --batch --force-mdc --compress-algo none \
--passphrase-fd 0 --output - -c /dev/fd/3) 3<&0 --passphrase-fd 0 --output - -c /dev/fd/3) 3<&0
} }
DECRYPT() DECRYPT()
{ {
(printf "%s" "$MASTERKEY" | (printf "%s" "$Masterkey" |
gpg -q --batch --no-default-keyring --secret-keyring /dev/null \ gpg -q --batch --no-default-keyring --secret-keyring /dev/null \
--keyring /dev/null \ --keyring /dev/null \
--passphrase-fd 0 --output - -d /dev/fd/3) 3<&0 --passphrase-fd 0 --output - -d /dev/fd/3) 3<&0
@ -163,18 +161,18 @@ DECRYPT()
# Encrypt to recipients $1 # Encrypt to recipients $1
PRIVENCRYPT() PRIVENCRYPT()
{ {
gpg --no-default-keyring --keyring "$CONF_KEYRING" \ gpg --no-default-keyring --keyring "$Conf_keyring" \
--compress-algo none -se $1 --compress-algo none -se $1
} }
PRIVDECRYPT() PRIVDECRYPT()
{ {
local STATUS local status_=
exec 4>&1 && exec 4>&1 &&
STATUS=$(gpg --no-default-keyring --keyring "$CONF_KEYRING" \ status_=$(gpg --no-default-keyring --keyring "$Conf_keyring" \
--status-fd 3 -q -d 3>&1 1>&4) && --status-fd 3 -q -d 3>&1 1>&4) &&
printf "%s" "$STATUS" | grep "^\[GNUPG:\] ENC_TO " >/dev/null && printf "%s" "$status_" | grep "^\[GNUPG:\] ENC_TO " >/dev/null &&
(printf "%s" "$STATUS" | grep "^\[GNUPG:\] GOODSIG " >/dev/null || { (printf "%s" "$status_" | grep "^\[GNUPG:\] GOODSIG " >/dev/null || {
echo_info "Failed to verify manifest signature!" && return 1 echo_info "Failed to verify manifest signature!" && return 1
}) })
} }
@ -186,8 +184,8 @@ genkey()
pack_hash() pack_hash()
{ {
local HASH=$(gpg --with-colons --print-md SHA224 | tr A-F a-f) local hash_="$(gpg --with-colons --print-md SHA224 | tr A-F a-f)"
HASH=${HASH#:*:}; printf "%s" "${HASH%:}" hash_=${hash_#:*:}; printf "%s" "${hash_%:}"
} }
@ -205,11 +203,11 @@ echo_die() { echo_info "$@" ; exit 1; }
check_recipients() check_recipients()
{ {
RECIPIENTS="$(gpg --no-default-keyring --keyring "$CONF_KEYRING" \ Recipients="$(gpg --no-default-keyring --keyring "$Conf_keyring" \
--with-colons -k | xgrep ^pub | cut -f5 -d: | tr '\n' ' ')" --with-colons -k | xgrep ^pub | cut -f5 -d: | tr '\n' ' ')"
# Split recipients by space, example "a b c" => -R a -R b -R c # Split recipients by space, example "a b c" => -R a -R b -R c
RECIPIENTS=$(printf "%s" "$RECIPIENTS" | sed -e 's/\([^ ]\+\)/-R &/g') Recipients=$(printf "%s" "$Recipients" | sed -e 's/\([^ ]\+\)/-R &/g')
if [ -z "$RECIPIENTS" ] if [ -z "$Recipients" ]
then then
echo_info "You must configure a keyring for the repository." echo_info "You must configure a keyring for the repository."
echo_info "Use ::" echo_info "Use ::"
@ -221,75 +219,72 @@ check_recipients()
make_new_repo() make_new_repo()
{ {
local URLID= local urlid_= fix_config=
local FIXCONFIG=
echo_info "Setting up new repository at $URL" echo_info "Setting up new repository at $URL"
PUTREPO "$URL" PUTREPO "$URL"
MASTERKEY="$(genkey)" Masterkey="$(genkey)"
# We need a relatively short ID for URL+REPO # We need a relatively short ID for URL+REPO
# The manifest will be stored at SHA224(URLID) # The manifest will be stored at SHA224(urlid_)
# Needed assumption: the same user should have no duplicate URLID # Needed assumption: the same user should have no duplicate urlid_
# For now, we use 20 random hex digits (80 bits), can be increased # For now, we use 20 random hex digits (80 bits), can be increased
URLID=$(printf "%.20s" "$(genkey | pack_hash)") urlid_=$(printf "%.20s" "$(genkey | pack_hash)")
REPOID=$(printf "%s" "$URLID" | pack_hash) Repoid=$(printf "%s" "$urlid_" | pack_hash)
echo_info "Repository ID is" "$URLID" echo_info "Repository ID is" "$urlid_"
[ "${NAME#gcrypt::}" != "$URL" ] && { [ "${NAME#gcrypt::}" != "$URL" ] && {
git config "remote.$NAME.url" "gcrypt::$URL/G/$URLID" git config "remote.$NAME.url" "gcrypt::$URL/G/$urlid_"
FIXCONFIG=1 fix_config=1
} || : } || :
echo_info "Repository URL is" "gcrypt::$URL/G/$URLID" echo_info "Repository URL is" "gcrypt::$URL/G/$urlid_"
[ -n "$FIXCONFIG" ] && echo_info "(configuration for $NAME updated)" ||: [ -n "$fix_config" ] && echo_info "(configuration for $NAME updated)"||:
} }
read_config() read_config()
{ {
CONF_KEYRING=$(git config --path gcrypt.keyring || printf "/dev/null") Conf_keyring=$(git config --path gcrypt.keyring || printf "/dev/null")
} }
ensure_connected() ensure_connected()
{ {
local MANIFEST local manifest_= rcv_repoid= url_id=
local RCVREPOID
local URLID
if [ -n "$DID_FIND_REPO" ] if [ -n "$Did_find_repo" ]
then then
return return
fi fi
DID_FIND_REPO=no Did_find_repo=no
read_config read_config
# split out REPOID from URL # split out Repoid from URL
URLID=${URL##*/G/} url_id=${URL##*/G/}
[ "$URLID" = "$URL" ] && URLID= && return 0 || : [ "$url_id" = "$URL" ] && url_id= && return 0 || :
URL=${URL%/G/"$URLID"} URL=${URL%/G/"$url_id"}
REPOID=$(printf "%s" "$URLID" | pack_hash) Repoid=$(printf "%s" "$url_id" | pack_hash)
TMPMANIFEST_ENC="$LOCALDIR/manifest.$$" TmpManifest_Enc="$Localdir/manifest.$$"
trap 'rm -f "$TMPMANIFEST_ENC"' EXIT trap 'rm -f "$TmpManifest_Enc"' EXIT
GET "$URL" "$REPOID" 2>/dev/null > "$TMPMANIFEST_ENC" || GET "$URL" "$Repoid" 2>/dev/null > "$TmpManifest_Enc" ||
echo_die "Repository not found: $URLID at $URL" echo_die "Repository not found: $url_id at $URL"
DID_FIND_REPO=yes Did_find_repo=yes
echo_info "Decrypting manifest" echo_info "Decrypting manifest"
MANIFEST=$(PRIVDECRYPT < "$TMPMANIFEST_ENC") && [ -n "$MANIFEST" ] || { manifest_=$(PRIVDECRYPT < "$TmpManifest_Enc") &&[ -n "$manifest_" ] || {
echo_info "Failed to decrypt manifest!" echo_info "Failed to decrypt manifest!"
echo_info "Using keyring $CONF_KEYRING" echo_info "Using keyring $Conf_keyring"
if [ "$CONF_KEYRING" = "/dev/null" ] ; then if [ "$Conf_keyring" = "/dev/null" ] ; then
echo_info "NOTE: Please configure gcrypt.keyring" echo_info "NOTE: Please configure gcrypt.keyring"
fi fi
exit 1 exit 1
} }
rm -f "$TMPMANIFEST_ENC" rm -f "$TmpManifest_Enc"
trap 0 trap 0
MASTERKEY=$(printf "%s\n" "$MANIFEST" | head -n 1) Masterkey=$(printf "%s\n" "$manifest_" | head -n 1)
BRANCHLIST=$(printf "%s\n" "$MANIFEST" | xgrep -E '^[0-9a-f]{40} ') Branchlist=$(printf "%s\n" "$manifest_" | xgrep -E '^[0-9a-f]{40} ')
PACKLIST=$(printf "%s\n" "$MANIFEST" | xgrep "^$PACKPFX") Packlist=$(printf "%s\n" "$manifest_" | xgrep "^$Packpfx")
RCVREPOID=$(printf "%s\n" "$MANIFEST" | xgrep "^repo ") rcv_repoid=$(printf "%s\n" "$manifest_" | xgrep "^repo ")
[ "repo $REPOID" = "$RCVREPOID" ] || echo_die "Repository id mismatch!" [ "repo $Repoid" = "$rcv_repoid" ] || echo_die "Repository id mismatch!"
} }
do_capabilities() do_capabilities()
@ -301,17 +296,16 @@ do_capabilities()
do_list() do_list()
{ {
local OBJID local obj_id= ref_name= line_=
local REFNAME
ensure_connected ensure_connected
printf "%s\n" "$BRANCHLIST" | while read LINE printf "%s\n" "$Branchlist" | while read line_
do do
[ -z "$LINE" ] && break [ -z "$line_" ] && break
OBJID=${LINE%% *} obj_id=${line_%% *}
REFNAME=${LINE##* } ref_name=${line_##* }
echo "$OBJID" "$REFNAME" echo "$obj_id" "$ref_name"
if [ "$REFNAME" = "refs/heads/master" ] if [ "$ref_name" = "refs/heads/master" ]
then then
echo "@refs/heads/master HEAD" echo "@refs/heads/master HEAD"
fi fi
@ -327,42 +321,43 @@ do_fetch()
# The PACK id is the SHA-1 of the encrypted git packfile. # The PACK id is the SHA-1 of the encrypted git packfile.
# We only download packs mentioned in the encrypted manifest, # We only download packs mentioned in the encrypted manifest,
# and check their digest when received. # and check their digest when received.
local PNEED local pack_= rcv_id= packline_= pneed_= pboth_= phave_=
local PBOTH
local PHAVE
ensure_connected ensure_connected
if [ -z "$PACKLIST" ] if [ -z "$Packlist" ]
then then
echo # end with blank line echo # end with blank line
return return
fi fi
TMPPACK_ENCRYPTED="$LOCALDIR/tmp_pack_ENCRYPTED_.$$" TmpPack_Encrypted="$Localdir/tmp_pack_ENCRYPTED_.$$"
trap 'rm -f "$TMPPACK_ENCRYPTED"' EXIT trap 'rm -f "$TmpPack_Encrypted"' EXIT
# Needed packs is REMOTE - (HAVE & REMOTE) # Needed packs is Packlist - (phave & Packlist)
# The `+` for $GITCEPTION is pointless but we will be safe for stacking # The `+` for $GITCEPTION is pointless but we will be safe for stacking
PHAVE="$(cat "$LOCALDIR/have_packs+" 2>/dev/null || :)" phave_="$(cat "$Localdir/have_packs+" 2>/dev/null || :)"
PBOTH="$(printf "%s\n%s" "$PACKLIST" "$PHAVE" | sort_C | uniq -d)" pboth_="$(printf "%s\n%s" "$Packlist" "$phave_" | sort_C | uniq -d)"
PNEED="$(printf "%s\n%s" "$PACKLIST" "$PBOTH" | sort_C | uniq -u)" pneed_="$(printf "%s\n%s" "$Packlist" "$pboth_" | sort_C | uniq -u)"
printf "%s\n" "$PNEED" | while read PACKLINE printf "%s\n" "$pneed_" | while read packline_
do do
[ -z "$PACKLINE" ] && break [ -z "$packline_" ] && break
PACK=${PACKLINE#"$PACKPFX"} pack_=${packline_#"$Packpfx"}
RCVID="$(GET "$URL" "$PACK" | tee "$TMPPACK_ENCRYPTED" | pack_hash)" rcv_id="$(GET "$URL" "$pack_" | \
if [ "$RCVID" != "$PACK" ] tee "$TmpPack_Encrypted" | pack_hash)"
if [ "$rcv_id" != "$pack_" ]
then then
echo_die "Packfile $PACK does not match digest!" echo_die "Packfile $pack_ does not match digest!"
fi fi
DECRYPT < "$TMPPACK_ENCRYPTED" | DECRYPT < "$TmpPack_Encrypted" |
git index-pack -v --stdin >/dev/null git index-pack -v --stdin >/dev/null
# add to local pack list # add to local pack list
printf "$PACKPFX%s\n" "$PACK">>"$LOCALDIR/have_packs$GITCEPTION" printf "%s%s\n" "$Packpfx" "$pack_" \
>> "$Localdir/have_packs$GITCEPTION"
done done
rm -f "$TMPPACK_ENCRYPTED" rm -f "$TmpPack_Encrypted"
trap 0 trap 0
echo # end with blank line echo # end with blank line
} }
@ -374,30 +369,27 @@ do_push()
# Each git packfile is encrypted and then named for the encrypted # Each git packfile is encrypted and then named for the encrypted
# file's hash. The manifest is updated with the pack id. # file's hash. The manifest is updated with the pack id.
# The manifest is encrypted. # The manifest is encrypted.
local REMOTEHAS local remote_has= remote_want= prefix_= suffix_=
local SIGNMANIFEST
local REMOTEWANT
local prefix_
local suffix_
ensure_connected ensure_connected
check_recipients check_recipients
if [ "$DID_FIND_REPO" = "no" ] if [ "$Did_find_repo" = "no" ]
then then
make_new_repo make_new_repo
fi fi
trap 'rm -f "$TMPMANIFEST" "$TMPPACK_ENCRYPTED" "$TMPOBJLIST"' EXIT trap 'rm -f "$TmpManifest" "$TmpPack_Encrypted" "$TmpObjlist"' EXIT
TMPMANIFEST="$LOCALDIR/tmp_new_manifest_.$$" TmpManifest="$Localdir/tmp_new_manifest_.$$"
touch "$TMPMANIFEST" touch "$TmpManifest"
if [ -n "$BRANCHLIST" ] if [ -n "$Branchlist" ]
then then
printf "%s\n" "$BRANCHLIST" >"$TMPMANIFEST" printf "%s\n" "$Branchlist" >"$TmpManifest"
REMOTEHAS="$(printf "%s" "$BRANCHLIST" | \ remote_has=$(printf "%s" "$Branchlist" | \
cut -f1 -d' ' | sed -e s/^/^/ | tr '\n' ' ')" cut -f1 -d' ' | sed -e s/^/^/ | tr '\n' ' ')
fi fi
REMOTEWANT="$(printf "%s\n" "$1" | while read LINE remote_want="$(printf "%s\n" "$1" | while read LINE
do do
# +src:dst -- remove leading + then split at : # +src:dst -- remove leading + then split at :
splitcolon "${LINE#+}" splitcolon "${LINE#+}"
@ -405,46 +397,46 @@ do_push()
then then
printf "%s " "$prefix_" printf "%s " "$prefix_"
printf "%s %s\n" "$(git rev-parse "$prefix_")" \ printf "%s %s\n" "$(git rev-parse "$prefix_")" \
"$suffix_" >> "$TMPMANIFEST" "$suffix_" >> "$TmpManifest"
# else delete # else delete
fi fi
done)" done)"
# POSIX compat issue: sort -s (stable), but supported in bsd and gnu # POSIX compat issue: sort -s (stable), but supported in bsd and gnu
BRANCHLIST="$(sort_C -k2 -s "$TMPMANIFEST" | tac | uniq -s40)" Branchlist="$(sort_C -k2 -s "$TmpManifest" | tac | uniq -s40)"
TMPPACK_ENCRYPTED="$LOCALDIR/tmp_pack_ENCRYPTED_.$$" TmpPack_Encrypted="$Localdir/tmp_pack_ENCRYPTED_.$$"
TMPOBJLIST="$LOCALDIR/tmp_packrevlist.$$" TmpObjlist="$Localdir/tmp_packrevlist.$$"
git rev-list --objects $REMOTEHAS $REMOTEWANT -- | \ git rev-list --objects $remote_has $remote_want -- | \
tee "$TMPOBJLIST" | \ tee "$TmpObjlist" | \
git pack-objects --stdout | ENCRYPT > "$TMPPACK_ENCRYPTED" git pack-objects --stdout | ENCRYPT > "$TmpPack_Encrypted"
# Only send pack if we have any objects to send # Only send pack if we have any objects to send
if [ -s "$TMPOBJLIST" ] if [ -s "$TmpObjlist" ]
then then
PACKID=$(pack_hash < "$TMPPACK_ENCRYPTED") pack_id=$(pack_hash < "$TmpPack_Encrypted")
PACKLIST=$(append "$PACKLIST" "$PACKPFX$PACKID") Packlist=$(append "$Packlist" "$Packpfx$pack_id")
PUT "$URL" "$PACKID" < "$TMPPACK_ENCRYPTED" PUT "$URL" "$pack_id" < "$TmpPack_Encrypted"
fi fi
rm -f "$TMPPACK_ENCRYPTED" rm -f "$TmpPack_Encrypted"
rm -f "$TMPMANIFEST" rm -f "$TmpManifest"
rm -f "$TMPOBJLIST" rm -f "$TmpObjlist"
trap 0 trap 0
# Update manifest # Update manifest
echo_info "Encrypting manifest to \"$RECIPIENTS\"" echo_info "Encrypting manifest to \"$Recipients\""
echo_info "Requesting manifest key signature" echo_info "Requesting manifest key signature"
TMPMANIFEST_ENC="$LOCALDIR/manifest.$$" TmpManifest_Enc="$Localdir/manifest.$$"
trap 'rm -f "$TMPMANIFEST_ENC"' EXIT trap 'rm -f "$TmpManifest_Enc"' EXIT
printf "%s\n%s\n%s\n%s\n" "$MASTERKEY" "$BRANCHLIST" "$PACKLIST" \ printf "%s\n%s\n%s\n%s\n" "$Masterkey" "$Branchlist" "$Packlist" \
"repo $REPOID" | PRIVENCRYPT "$RECIPIENTS" > "$TMPMANIFEST_ENC" "repo $Repoid" | PRIVENCRYPT "$Recipients" > "$TmpManifest_Enc"
PUT "$URL" "$REPOID" < "$TMPMANIFEST_ENC" PUT "$URL" "$Repoid" < "$TmpManifest_Enc"
PUT_FINAL "$URL" PUT_FINAL "$URL"
rm -f "$TMPMANIFEST_ENC" rm -f "$TmpManifest_Enc"
trap 0 trap 0
# ok all updates (not deletes) # ok all updates (not deletes)
@ -470,12 +462,12 @@ URL=$2
isurl gitception "$URL" || test -z ${URL##/*} ) || isurl gitception "$URL" || test -z ${URL##/*} ) ||
echo_die "Supported URLs: gitception://<giturl>, Absolute path, sftp://, ssh://" echo_die "Supported URLs: gitception://<giturl>, Absolute path, sftp://, ssh://"
mkdir -p "$LOCALDIR" mkdir -p "$Localdir"
while read INPUT while read Input
do do
#echo_info "Got: $INPUT ($GITCEPTION)" #echo_info "Got: $Input ($GITCEPTION)"
case "$INPUT" in case "$Input" in
capabilities) capabilities)
do_capabilities do_capabilities
;; ;;
@ -483,35 +475,35 @@ do
do_list do_list
;; ;;
fetch\ *) fetch\ *)
FETCH_ARGS="${INPUT##fetch }" args_="${Input##fetch }"
while read INPUTX while read InputX
do do
case "$INPUTX" in case "$InputX" in
fetch*) fetch*)
FETCH_ARGS= #ignored args_= #ignored
;; ;;
*) *)
break break
;; ;;
esac esac
done done
do_fetch "$FETCH_ARGS" do_fetch "$args_"
;; ;;
push\ *) push\ *)
PUSH_ARGS="${INPUT##push }" args_="${Input##push }"
while read INPUTX while read InputX
do do
#echo_info "Got: (for push) $INPUTX" #echo_info "Got: (for push) $InputX"
case "$INPUTX" in case "$InputX" in
push\ *) push\ *)
PUSH_ARGS=$(append "$PUSH_ARGS" "${INPUTX#push }") args_=$(append "$args_" "${InputX#push }")
;; ;;
*) *)
break break
;; ;;
esac esac
done done
do_push "$PUSH_ARGS" do_push "$args_"
;; ;;
?*) ?*)
echo_die "Unknown input!" echo_die "Unknown input!"