-
Notifications
You must be signed in to change notification settings - Fork 58
/
clean-modules
executable file
·155 lines (131 loc) · 4.48 KB
/
clean-modules
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/sh
#+
# Name:
# clean-modules
# Purpose:
# Clean git submodules.
# Type of Module:
# Bourne shell script
# Usage:
# clean-modules [-f] [submodule]
# Description:
# This script cleans the content of the given submodule, or all
# submodules if ran without any arguments. It is destructive as
# it does a "git clean -fdx" so should be used with care.
#
# If any submodule contains local changes then this script will refuse
# to run. You will need to correct the problem that represents before
# running this script.
#
# A typical use of this script is to clean out all submodules in a
# working tree when switching to a branch or you want a clean
# rebuild.
#
# In extreme circumstances when you do not have any changes to preserve
# and the submodules are in a mess the "-f" flag can be used to skip all
# safety checks.
# Authors:
# PWD: P.W. Draper (JAC, Durham University)
# {enter_new_authors_here}
# Copyright:
# Copyright (C) 2009 Science and Technology Facilities Council.
# All Rights Reserved.
# Licence:
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street,Fifth Floor, Boston, MA
# 02110-1301, USA
# History:
# 17-JUL-2009: (PWD):
# Original version, based on remove-modules.
# {enter_further_changes_here}
#-
echo ""
echo "Clean all local repository submodules:"
# Parse any command-line arguments. We just have two optional ones,
# -f and a submodule.
check=1
if test "$1" != ""; then
if test "$1" = "-f"; then
check=0
shift
fi
submodule="$1"
else
submodule=""
fi
# Get a list of all the paths to the submodules or check that the given
# submodule is valid.
if test "$submodule" = ""; then
submodule_list="`git ls-files --stage | grep ^160000| awk '{print $4}'`"
else
submodule_list="$submodule"
# Verify this a submodule.
found="`git ls-files --stage | grep ^160000| grep $submodule| awk '{print $4}'`"
if test "$found" != "$submodule"; then
echo "ERROR: no such submodule: ... $submodule"
exit 1
fi
fi
# Get a summary of the submodules. If this returns any content then one or
# more have been modified and committed and should be pushed or reset before
# proceeding.
if test "$check" = "1"; then
summary=`git submodule summary $submodule`
if test "$summary" != ""; then
echo "ERROR: submodule(s) are modified, will not remove"
echo "$summary"
exit 1
fi
# The above will not report modified files that have not been committed.
# This isn't important, but probably indicates some activity in the
# submodule so refuse to proceed when they are present as well.
if test "$check" = "1"; then
for m in $submodule_list; do
# May not be checked in yet, in that case we see the modified files
# in the full repository. Avoid that case.
nfiles=`cd $m && ls -a | wc -w`
if test $nfiles -gt 2 ; then
modified=`cd $m && git diff-index --name-status HEAD`
if test "$modified" != ""; then
echo "WARNING: submodule contains modified files, will not proceed"
echo "... $m"
echo "$modified" | head
exit 1
fi
else
echo "... found uninitialised submodule: $m"
fi
done
fi
fi
# No modifications or told to proceed so get on with removal of contents.
# Since this is destructive, ask one more time.
read -p "Are you sure you want to proceed (y/N)? " answer
if test "$answer" = "y"; then
for m in $submodule_list; do
nfiles=`cd $m && ls -a | wc -w`
if test $nfiles -gt 2 ; then
(cd $m && git clean -fdx) > /dev/null 2>&1
echo "... cleaned: $m"
else
echo "... ignored: $m"
fi
done
echo "Clean complete:"
git status
echo ""
else
echo "Nothing done"
fi
exit