archives2git: --gitfilter
[archives2git.git] / archives2git
blob7714107e37af4d388d596411ccd36aeb6c964fec
1 #!/bin/sh
2 # archives2git - generate a Git history from a series of release tarballs
3 # Copyright © 2013 Géraud Meyer <graud@gmx.com>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation.
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 # for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with this program. If not, see <http://www.gnu.org/licenses/>.
17 PROGRAM_NAME="archives2git"
18 PROGRAM_VERSION="0"
19 # Usage:
20 # archives2git [<options>] [--] <archives>
22 # parameters
23 RENAME='echo "$file"'
24 GIT_WORK_TREE=${GIT_WORK_TREE-.}
25 FILTER='false $file'
26 DATE=''
27 TITLE='echo "$arch"'
28 BODY=
29 ADDARGS=
30 CIARGS=
31 FILTERHEAD=
32 while [ $# -gt 0 ]
34 case $1 in
35 --rename)
36 shift; RENAME=$1 ;;
37 --repo)
38 shift; GIT_WORK_TREE=$1 ;;
39 --keep-filter)
40 shift; FILTER=$1 ;;
41 --date)
42 shift; DATE=$1 ;;
43 --title)
44 shift; TITLE=$1 ;;
45 --body)
46 shift; BODY=$1 ;;
47 --gitadd-arg)
48 shift; ADDARGS="$ADDARGS $1" ;;
49 --gitci-arg)
50 shift; CIARGS="$CIARGS $1" ;;
51 --gitfilter)
52 shift; FILTERHEAD=$1 ;;
53 --version)
54 echo "$PROGRAM_NAME version $PROGRAM_VERSION"
55 exit ;;
56 --?*)
57 echo "$0: unknown option $1" >&2
58 exit 255 ;;
59 --)
60 shift; break ;;
62 break ;;
63 esac
64 shift
65 done
66 # setup
67 NL='
69 TMPDIR=$(mktemp -d)
70 WRKDIR=$(pwd)
71 # git repository check
72 cd "$GIT_WORK_TREE" &&
73 { test -d .git || git rev-parse --git-dir >/dev/null; } ||
75 echo "$0: error not in a git repository" >&2
76 exit 255
78 test -z "$(git status --porcelain)" &&
79 git update-index -q --refresh ||
81 echo "$0: unstaged files or dirty index" >&2
82 exit 254
85 # main
86 set -e
87 OLDIFS=$IFS
88 for arch
90 cd "$WRKDIR"
91 aunpack -X "$TMPDIR" "$arch"
92 cd "$GIT_WORK_TREE"
93 for file in * .*
95 [ -e "$file" ] || continue
96 [ "$file" = "." -o "$file" = ".." -o "$file" = ".git" ] && continue
97 eval "$FILTER" || ( git rm -r "$file" || rm -R "$file" )
98 done
99 for file in "$TMPDIR"/* "$TMPDIR"/.*
101 [ -e "$file" ] || continue
102 file=${file#$TMPDIR/}
103 [ "$file" = "." -o "$file" = ".." ] && continue
104 name=$(eval "$RENAME")
105 if [ -n "$name" ]
106 then
107 [ ! -e ./"$name" ] || rm -R ./"$name"
108 mv "$TMPDIR"/"$file" ./"$name"
109 git add $ADDARGS ./"$name"
110 else
111 rm -R "$TMPDIR"/"$file"
113 done
114 git commit -m "$(eval "$TITLE")${BODY:+$NL$BODY}" \
115 ${DATE:+--date "$(eval "$DATE")"} $CIARGS
116 if [ -n "$FILTERHEAD" ]
117 then
118 IFS=$NL
119 git filter-branch $FILTERHEAD -- HEAD^..HEAD
120 rm -R "$(git rev-parse --git-dir)"/refs/original/
121 IFS=$OLDIFS
123 done
126 rmdir "$TMPDIR"