archives2git: --repo, $GIT_WORK_TREE as a means to control the archives' names
[archives2git.git] / archives2git
bloba224cd74a9cee2f2333ceee46e5f71ad37c38a24
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='true $file'
26 DATE=''
27 TITLE='echo "$arch"'
28 BODY=
29 ARGS=
30 while [ $# -gt 0 ]
32 case $1 in
33 --rename)
34 shift; RENAME=$1 ;;
35 --repo)
36 shift; GIT_WORK_TREE=$1 ;;
37 --filter-file)
38 shift; FILTER=$1 ;;
39 --date)
40 shift; DATE=$1 ;;
41 --title)
42 shift; TITLE=$1 ;;
43 --body)
44 shift; BODY=$1 ;;
45 --git-arg)
46 shift; ARGS="$ARGS $1" ;;
47 --version)
48 echo "$PROGRAM_NAME version $PROGRAM_VERSION"
49 exit ;;
50 --?*)
51 echo "$0: unknown option $1" >&2
52 exit 255 ;;
53 --)
54 shift; break ;;
56 break ;;
57 esac
58 shift
59 done
60 # setup
61 NL='
63 TMPDIR=$(mktemp -d)
64 WRKDIR=$(pwd)
65 # git repository check
66 cd "$GIT_WORK_TREE" &&
67 { test -d .git || git rev-parse --git-dir >/dev/null; } ||
69 echo "$0: error not in a git repository" >&2
70 exit 255
73 # main
74 set -e
75 for arch
77 cd "$WRKDIR"
78 aunpack -X "$TMPDIR" "$arch"
79 cd "$GIT_WORK_TREE"
80 for file in * .*
82 [ -e "$file" ] || continue
83 [ "$file" = "." -o "$file" = ".." -o "$file" = ".git" ] && continue
84 git rm -r "$file" || rm -R "$file"
85 done
86 for file in "$TMPDIR"/* "$TMPDIR"/.*
88 [ -e "$file" ] || continue
89 file=${file#$TMPDIR/}
90 [ "$file" = "." -o "$file" = ".." ] && continue
91 name=$(eval "$RENAME")
92 if [ -n "$name" ]
93 then mv "$TMPDIR"/"$file" ./"$name"
94 else rm "$TMPDIR"/"$file"
96 done
97 for file in * .*
99 [ -e "$file" ] || continue
100 [ "$file" = "." -o "$file" = ".." -o "$file" = ".git" ] && continue
101 eval "$FILTER" && git add "$file"
102 done
103 git commit -m "$(eval "$TITLE")${BODY:+$NL$BODY}" \
104 ${DATE:+--date "$(eval "$DATE")"} $ARGS
105 done
108 rmdir "$TMPDIR"