From e3e8f7fc77127ef1f0d9980fae444c978facbcea Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Tue, 9 Jun 2020 20:46:33 -0700 Subject: [PATCH] gc-util-functions.sh: extract common gc-related functions Move the run_combine_packs, dupe_file, rename_pack and lotsa_loose_objects functions into a new gc-related script library file. This avoids code duplication for functions that need to be used by different gc scripts but are not otherwise general purpose enough to be moved into shlib.sh. Signed-off-by: Kyle J. McKay --- jobd/gc-util-functions.sh | 69 +++++++++++++++++++++++++++++++++++++++++++++++ jobd/gc.sh | 61 +---------------------------------------- 2 files changed, 70 insertions(+), 60 deletions(-) create mode 100755 jobd/gc-util-functions.sh diff --git a/jobd/gc-util-functions.sh b/jobd/gc-util-functions.sh new file mode 100755 index 0000000..7040434 --- /dev/null +++ b/jobd/gc-util-functions.sh @@ -0,0 +1,69 @@ +#!/bin/sh +# +# This is a shell library for common gc related functions +# used by various Girocco scripts. + +# shlib.sh always sets this, it's an error to source +# this script without having already sourced shlib.sh +[ -n "$var_git_exec_path" ] || exit 2 + +# make sure combine-packs uses the correct Git executable +run_combine_packs() { + PATH="$var_git_exec_path:$cfg_basedir/bin:$PATH" @basedir@/jobd/combine-packs.sh "$@" +} + +# duplicate the first file to the name given by the second file making sure that +# the second file appears atomically all-at-once after the copy has been completed +# and does not appear at all if the copy fails (in which case this function fails) +# if the second file already exists this function fails with status 1 +# if the file names are the same this function returns immediately with success +dupe_file() { + [ "$1" != "$2" ] || return 0 + ! [ -e "$2" ] || return 1 + case "$2" in + *?/?*) _tmpdir="${2%/*}";; + *) _tmpdir=".";; + esac + _tmpfile="$(mktemp "${_tmpdir:-.}/packtmp-XXXXXX")" || return 1 + cp -fp "$1" "$_tmpfile" || return 1 + mv -f "$_tmpfile" "$2" +} + +# rename_pack oldnamepath newnamepath +# note that .keep and .bndl files are left untouched and not moved at all! +rename_pack() { + [ $# -eq 2 ] && [ "$1" != "$2" ] || { + echo >&2 "[$proj] incorrect use of rename_pack function" + exit 1 + } + # Git assumes that if the destination of the rename already exists + # that it is, in fact, a copy of the same bytes so silently succeeds + # without doing anything. We duplicate that logic here. + # Git checks for the .idx file first before even trying to use a pack + # so it should be the last moved and the first removed. + for ext in pack bitmap idx; do + [ -f "$1.$ext" ] || continue + ln "$1.$ext" "$2.$ext" >/dev/null 2>&1 || + dupe_file "$1.$ext" "$2.$ext" >/dev/null 2>&1 || + [ -f "$2.$ext" ] || { + echo >&2 "[$proj] unable to move $1.$ext to $2.$ext" + exit 1 + } + done + for ext in idx pack bitmap; do + rm -f "$1.$ext" + done + return 0 +} + +# current directory must already be set to the $GIT_DIR +# see if there are "lotsa" loose objects +# "lotsa" is defined as the 17, 68, 71 and 86 object directories existing +# and there being at least 5 total objects between them which corresponds +# to an approximate average of 320 loose objects before this function starts +# returning true and triggering a "mini" gc to pack up loose objects +lotsa_loose_objects() { + [ -d objects/17 ] && [ -d objects/68 ] && [ -d objects/71 ] && [ -d objects/86 ] || return 1 + _objs=$(( $(find -L objects/17 objects/68 objects/71 objects/86 -maxdepth 1 -name "$octet19*" -type f -print 2>/dev/null | LC_ALL=C wc -l) )) + [ ${_objs:-0} -ge 5 ] +} diff --git a/jobd/gc.sh b/jobd/gc.sh index 51e4798..ce60cb1 100755 --- a/jobd/gc.sh +++ b/jobd/gc.sh @@ -5,6 +5,7 @@ # gc.sh my-project -f . @basedir@/shlib.sh +. @basedir@/jobd/gc-util-functions.sh set -e @@ -194,55 +195,6 @@ is_dirty() { is_empty_refs_dir } -# make sure combine-packs uses the correct Git executable -run_combine_packs() { - PATH="$var_git_exec_path:$cfg_basedir/bin:$PATH" @basedir@/jobd/combine-packs.sh "$@" -} - -# duplicate the first file to the name given by the second file making sure that -# the second file appears atomically all-at-once after the copy has been completed -# and does not appear at all if the copy fails (in which case this function fails) -# if the second file already exists this function fails with status 1 -# if the file names are the same this function returns immediately with success -dupe_file() { - [ "$1" != "$2" ] || return 0 - ! [ -e "$2" ] || return 1 - case "$2" in - *?/?*) _tmpdir="${2%/*}";; - *) _tmpdir=".";; - esac - _tmpfile="$(mktemp "${_tmpdir:-.}/packtmp-XXXXXX")" || return 1 - cp -fp "$1" "$_tmpfile" || return 1 - mv -f "$_tmpfile" "$2" -} - -# rename_pack oldnamepath newnamepath -# note that .keep and .bndl files are left untouched and not moved at all! -rename_pack() { - [ $# -eq 2 ] && [ "$1" != "$2" ] || { - echo >&2 "[$proj] incorrect use of rename_pack function" - exit 1 - } - # Git assumes that if the destination of the rename already exists - # that it is, in fact, a copy of the same bytes so silently succeeds - # without doing anything. We duplicate that logic here. - # Git checks for the .idx file first before even trying to use a pack - # so it should be the last moved and the first removed. - for ext in pack bitmap idx; do - [ -f "$1.$ext" ] || continue - ln "$1.$ext" "$2.$ext" >/dev/null 2>&1 || - dupe_file "$1.$ext" "$2.$ext" >/dev/null 2>&1 || - [ -f "$2.$ext" ] || { - echo >&2 "[$proj] unable to move $1.$ext to $2.$ext" - exit 1 - } - done - for ext in idx pack bitmap; do - rm -f "$1.$ext" - done - return 0 -} - # combine the input pack(s) into a new pack (or possibly packs if packSizeLimit set) # input pack names are read from standard input one per line delimited by the first # ':', ' ' or '\n' character on the line (which allows gfi-packs to be read directly) @@ -268,17 +220,6 @@ repack_gfi_packs() { return 0 } -# see if there are "lotsa" loose objects -# "lotsa" is defined as the 17, 68, 71 and 86 object directories existing -# and there being at least 5 total objects between them which corresponds -# to an approximate average of 320 loose objects before this function starts -# returning true and triggering a "mini" gc to pack up loose objects -lotsa_loose_objects() { - [ -d objects/17 ] && [ -d objects/68 ] && [ -d objects/71 ] && [ -d objects/86 ] || return 1 - _objs=$(( $(find -L objects/17 objects/68 objects/71 objects/86 -maxdepth 1 -name "$octet19*" -type f -print 2>/dev/null | LC_ALL=C wc -l) )) - [ ${_objs:-0} -ge 5 ] -} - # pack any existing loose objects into a new _l.pack file then run prune-packed # note that prune-packed is NOT run beforehand -- the caller must do that if needed # loose objects need not be part of complete commits/trees as --weak-naming is used -- 2.11.4.GIT