]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - purge-packages.sh
When tripping over a GIT_ var, give a useful error
[packages/rpm-build-tools.git] / purge-packages.sh
1 #!/bin/sh
2 # Purges packages/ checkouts
3 #
4 # - if package has clean state, the dir is cleaned up (moved to purge dir)
5 # - otherwise git gc is called if CALL_GC=yes
6 # Stashes aside packages/ that do not have .git dir
7 # - these dirs are usually created by rpmbuild if Name does not match .spec file
8
9 set -e
10
11 CALL_GC=${CALL_GC:-no}
12
13 topdir="${1:-$(rpm -E %_topdir)}"
14 topdir=$(readlink -f "$topdir")
15 purgedir=$topdir/purged
16 stashdir=$topdir/stashed
17 cd "$topdir"
18
19 echo "Purging in $topdir, press ENTER to continue"
20 read a
21
22 if [ -d "$purgedir" ]; then
23         echo >&2 "Previous pruge dir exists: $purgedir, remove it to continue"
24         exit 1
25 fi
26
27 install -d $purgedir
28 for pkg in */.git; do
29         continue
30         pkg=${pkg%/.git}
31         cd "$pkg"
32         purge='yes'
33
34         status=$(git status --porcelain)
35         stash=$(git stash list)
36
37         # FIXME: does not currently handle if some pushes are not made!
38         if [ -n "$status" ] || [ -n "$stash" ]; then
39                 cat <<-EOF
40                 * Package $pkg - Untracked files or stash not empty.
41                 $status
42                 EOF
43                 purge='no'
44                 [ "$CALL_GC" != 'no' ] && git gc
45         fi
46         git show-ref --heads |\
47         { while read sha1 branch; do
48                 short_branch=${branch#refs/heads/}
49                 if ! upstream=$(git rev-parse -q --verify $short_branch@{u}) 2>/dev/null; then
50                         echo "* Package $pkg - Branch $short_branch has not defined upstream"
51                         purge='no'
52                         continue
53                 fi
54                 if [ -n "$(git rev-list "$upstream..$branch")" ]; then
55                         echo "* Package $pkg - Branch $short_branch is not fully merged to its upstream"
56                         purge='no'
57                         continue
58                 fi
59         done
60         if [ "$purge" = 'yes' ]; then
61                 cat <<-EOF
62                 * Package $pkg - State clean. Purging
63                 EOF
64                 mv ../$pkg $purgedir
65         fi }
66         cd ..
67 done
68
69 rmdir --ignore-fail-on-non-empty $purgedir
70
71 # go over packages that do not have .git
72 if [ -d "$stashdir" ]; then
73         echo >&2 "Previous stash dir exists: $stashdir, remove it to continue"
74         exit 1
75 fi
76 install -d $stashdir
77 stashdir=$(readlink -f $stashdir)
78 for pkg in */; do
79         # skip symlinks
80         test -L "${pkg%/}" && continue
81         # skip packages which do have .git
82         test -d "$pkg/.git" && continue
83         # skip if it's the stash dir itself
84         pkg=$(readlink -f $pkg)
85         test "$pkg" = "$stashdir" && continue
86
87         echo "* Package $pkg does not have .git, stashing"
88         mv $pkg $stashdir
89 done
90 rmdir --ignore-fail-on-non-empty $stashdir
91
92 # vi:syntax=sh:ts=4:sw=4:noet
This page took 0.105143 seconds and 3 git commands to generate.