shlib.sh: move vcnt function into shlib as v_cnt
[girocco.git] / jobd / gc-util-functions.sh
blob71c64f703aab5825e35232533f99013cb3a53e6c
1 #!/bin/sh
3 # This is a shell library for common gc related functions
4 # used by various Girocco scripts.
6 # shlib.sh always sets this, it's an error to source
7 # this script without having already sourced shlib.sh
8 [ -n "$var_git_exec_path" ] || exit 2
10 # default packing options
11 packopts="--depth=50 --window=50 --window-memory=${var_window_memory:-1g}"
12 quiet="-q"; [ "${show_progress:-0}" = "0" ] || quiet=
14 # make sure combine-packs uses the correct Git executable
15 run_combine_packs() {
16 PATH="$var_git_exec_path:$cfg_basedir/bin:$PATH" @basedir@/jobd/combine-packs.sh "$@"
19 # duplicate the first file to the name given by the second file making sure that
20 # the second file appears atomically all-at-once after the copy has been completed
21 # and does not appear at all if the copy fails (in which case this function fails)
22 # if the second file already exists this function fails with status 1
23 # if the file names are the same this function returns immediately with success
24 dupe_file() {
25 [ "$1" != "$2" ] || return 0
26 ! [ -e "$2" ] || return 1
27 case "$2" in
28 *?/?*) _tmpdir="${2%/*}";;
29 *) _tmpdir=".";;
30 esac
31 _tmpfile="$(mktemp "${_tmpdir:-.}/packtmp-XXXXXX")" || return 1
32 cp -fp "$1" "$_tmpfile" || return 1
33 mv -f "$_tmpfile" "$2"
36 # rename_pack oldnamepath newnamepath
37 # note that .keep and .bndl files are left untouched and not moved at all!
38 rename_pack() {
39 [ $# -eq 2 ] && [ "$1" != "$2" ] || {
40 echo >&2 "[$proj] incorrect use of rename_pack function"
41 exit 1
43 # Git assumes that if the destination of the rename already exists
44 # that it is, in fact, a copy of the same bytes so silently succeeds
45 # without doing anything. We duplicate that logic here.
46 # Git checks for the .idx file first before even trying to use a pack
47 # so it should be the last moved and the first removed.
48 for ext in pack bitmap idx; do
49 [ -f "$1.$ext" ] || continue
50 ln "$1.$ext" "$2.$ext" >/dev/null 2>&1 ||
51 dupe_file "$1.$ext" "$2.$ext" >/dev/null 2>&1 ||
52 [ -f "$2.$ext" ] || {
53 echo >&2 "[$proj] unable to move $1.$ext to $2.$ext"
54 exit 1
56 done
57 for ext in idx pack bitmap; do
58 rm -f "$1.$ext"
59 done
60 return 0
63 # current directory must already be set to the $GIT_DIR
64 # see if there are "lotsa" loose objects
65 # "lotsa" is defined as the 17, 68, 71 and 86 object directories existing
66 # and there being at least 5 total objects between them which corresponds
67 # to an approximate average of 320 loose objects before this function starts
68 # returning true and triggering a "mini" gc to pack up loose objects
69 lotsa_loose_objects() {
70 [ -d objects/17 ] && [ -d objects/68 ] && [ -d objects/71 ] && [ -d objects/86 ] || return 1
71 _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) ))
72 [ ${_objs:-0} -ge 5 ]
75 # pack any existing, non-packed loose objects into a new _l.pack file then run prune-packed
76 # note that prune-packed is NOT run beforehand -- the caller must do that if needed
77 # loose objects need not be part of complete commits/trees as --weak-naming is used
78 pack_incremental_loose_objects() {
79 _lpacks="$(run_combine_packs </dev/null --names --loose --weak-naming --incremental --non-empty --all-progress-implied ${quiet:---progress} $packopts)"
80 if [ -n "$_lpacks" ]; then
81 # We need to identify these packs later so we don't combine_packs them
82 for _objpack in $_lpacks; do
83 rename_pack "objects/pack/pack-$_objpack" "objects/pack/pack-${_objpack}_l" || :
84 done
85 git prune-packed $quiet