|
| 1 | +#!/bin/sh |
| 2 | +# |
| 3 | +# git-submodule-purge - purges the copies of obsoleted submodule repositories |
| 4 | +# |
| 5 | +# Copyright (c) 2012 Akinori MUSHA |
| 6 | +# |
| 7 | +# All rights reserved. |
| 8 | +# |
| 9 | +# Redistribution and use in source and binary forms, with or without |
| 10 | +# modification, are permitted provided that the following conditions |
| 11 | +# are met: |
| 12 | +# 1. Redistributions of source code must retain the above copyright |
| 13 | +# notice, this list of conditions and the following disclaimer. |
| 14 | +# 2. Redistributions in binary form must reproduce the above copyright |
| 15 | +# notice, this list of conditions and the following disclaimer in the |
| 16 | +# documentation and/or other materials provided with the distribution. |
| 17 | +# |
| 18 | +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 19 | +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 22 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 | +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 | +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 | +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 | +# SUCH DAMAGE. |
| 29 | +# |
| 30 | + |
| 31 | +main () { |
| 32 | + if [ ! -d .git ]; then |
| 33 | + echo 'You need to run this command from the toplevel of the working tree.' >&2 |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | + |
| 37 | + [ -d .git/modules ] || return 0 |
| 38 | + |
| 39 | + repodir=`pwd -L`/.git/modules |
| 40 | + git submodule foreach --quiet --recursive ' |
| 41 | + [ -f .git ] && (while read -r key gitdir; do |
| 42 | + [ "$key" = "gitdir:" ] || continue |
| 43 | + cd -L -- "$gitdir" 2>/dev/null && pwd -L |
| 44 | + break |
| 45 | + done < .git) || :' | |
| 46 | + while read -r gitdir; do |
| 47 | + case "$gitdir" in |
| 48 | + "$repodir/"*) |
| 49 | + touch -- "$gitdir/.do_not_purge_me" |
| 50 | + ;; |
| 51 | + esac |
| 52 | + done |
| 53 | + |
| 54 | + find .git/modules -type f -name config -print | |
| 55 | + while read -r config; do |
| 56 | + gitdir="${config%/config}" |
| 57 | + if [ -f "$gitdir/.do_not_purge_me" ]; then |
| 58 | + rm -- "$gitdir/.do_not_purge_me" |
| 59 | + continue |
| 60 | + fi |
| 61 | + [ -f "$gitdir/index" -a -d "$gitdir/objects" ] || continue |
| 62 | + printf '%s\n' "$gitdir" |
| 63 | + done | sort -r | |
| 64 | + while read -r dir; do |
| 65 | + echo "Purging $dir" |
| 66 | + rm -rf -- "$dir"/* |
| 67 | + rmdir -p -- "$dir" 2>/dev/null || : |
| 68 | + done |
| 69 | +} |
| 70 | + |
| 71 | +main "$@" |
0 commit comments