-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean
executable file
·105 lines (76 loc) · 2.03 KB
/
clean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
cd "$(dirname "$0")" ; here="$(pwd)"
source "$here/common"
declare -A references
reference_build() {
local file="$1"
local branch="$2"
local path="${file%.ref}"
local name="${path##*/}"
local commit="$(cat "$file" 2> /dev/null)"
if ! in_array "$name" "${build_types[@]}" ; then
echo "Removing obsolete build type ${esc}[32m$name${esc}[0m in ${esc}[34m$branch${esc}[0m"
rmbuild "$logs/branches/$branch/$name"
return
fi
references["$commit"]=1
local old_file="$path.old"
if [ -f "$old_file" ] ; then
local old_commit="$(cat "$old_file" 2> /dev/null)"
references["$old_commit"]=1
fi
}
reference_branch() {
local dir="$1"
[ -d "$dir" ] || return
# Remove obsolete branches.
branch="${f##*/}"
if ! is_valid_branch "$branch" ; then
echo "Removing obsolete branch ${esc}[34m$branch${esc}[0m"
rm -r "$dir"
return
fi
for f in `dir -d $dir/*.ref 2> /dev/null` ; do
reference_build "$f" "$branch"
done
}
clean_commit() {
local dir="$1"
[ -d "$dir" ] || return
local commit="${dir##*/}"
if [ -z "$commit" ] ; then
# Empty commit.
return
fi
if [ ! -z "${references["$commit"]}" ] ; then
# Referenced, don't delete
return
fi
echo "Archiving unreferenced commit dir ${esc}[34m$commit${esc}[0m"
# Remove empty directories.
local archive="$dir.tar.xz"
pushd "$logs/commits/" > /dev/null
if ! tar -cJf "$archive" "$commit" || [ ! -f "$archive" ] ; then
popd > /dev/null
return
fi
rm -r "$commit"
popd > /dev/null
}
echo "Cleaning unreferenced commit logs..."
branches=(`dir -d $repo/.git/logs/refs/remotes/origin/* 2> /dev/null`)
if [ $build_latest_tag = 1 ] ; then
latest_tag="$(
git --git-dir="$repo/.git" describe --tags $(git --git-dir="$repo/.git" rev-list --tags --max-count=1)
)"
[ -z "$latest_tag" ] || branches+=( "$latest_tag" )
fi
# Collect referenced commits.
for f in `dir -d $logs/branches/* 2> /dev/null` ; do
reference_branch "$f"
done
# Clean unreferenced commits.
for f in `dir -d $logs/commits/* 2> /dev/null` ; do
clean_commit "$f"
done
echo "Done."