.gitignore
[archives2git.git] / archives2git
blob50087ccb2d4f09da6a21b7fa5d3a48e12ee8c59c
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"' # $file $arch
24 GIT_WORK_TREE=${GIT_WORK_TREE-.}
25 # may be a subdir of the top level
26 FILTER='false' # $file $arch
27 DATE='' # $arch
28 TITLE='echo "$arch"' # $arch
29 BODY="" # message taken as is
30 ADDARGS=
31 CIARGS=
32 FILTERHEAD= # line separated arguments
33 while [ $# -gt 0 ]
35 case $1 in
36 --rename)
37 shift; RENAME=$1 ;;
38 --repo)
39 shift; GIT_WORK_TREE=$1 ;;
40 --keep-filter)
41 shift; FILTER=$1 ;;
42 --date)
43 shift; DATE=$1 ;;
44 --title)
45 shift; TITLE=$1 ;;
46 --body)
47 shift; BODY=$1 ;;
48 --gitadd-arg)
49 shift; ADDARGS="$ADDARGS $1" ;;
50 --gitci-arg)
51 shift; CIARGS="$CIARGS $1" ;;
52 --gitfilter)
53 shift; FILTERHEAD=$1 ;;
54 --version)
55 echo "$PROGRAM_NAME version $PROGRAM_VERSION"
56 exit ;;
57 --?*)
58 echo "$0: unknown option $1" >&2
59 exit 255 ;;
60 --)
61 shift; break ;;
63 break ;;
64 esac
65 shift
66 done
67 # setup
68 NL='
70 TMPDIR=$(mktemp -d)
71 WRKDIR=$(pwd)
72 # git repository check
73 cd "$GIT_WORK_TREE" &&
74 { test -d .git || git rev-parse --git-dir >/dev/null; } ||
76 echo "$0: error not in a git repository" >&2
77 exit 255
79 test -z "$(git status --porcelain)" &&
80 git update-index -q --refresh ||
82 echo "$0: unstaged files or dirty index" >&2
83 exit 254
86 # main
87 set -e
88 OLDIFS=$IFS
89 for arch
91 # extract the archive in a temp dir
92 cd "$WRKDIR"
93 aunpack -X "$TMPDIR" "$arch"
94 # remove (almost) everything from the repository dir
95 cd "$GIT_WORK_TREE"
96 for file in * .*
98 [ -e "$file" ] || continue
99 [ "$file" = "." -o "$file" = ".." -o "$file" = ".git" ] && continue
100 eval "$FILTER" || ( git rm -r "$file" || rm -R "$file" )
101 done
102 # move the content of the temp dir to the repository and stage it
103 for file in "$TMPDIR"/* "$TMPDIR"/.*
105 [ -e "$file" ] || continue
106 file=${file#$TMPDIR/}
107 [ "$file" = "." -o "$file" = ".." ] && continue
108 name=$(eval "$RENAME")
109 if [ -n "$name" ]
110 then
111 [ ! -e ./"$name" ] || rm -R ./"$name"
112 # remove conflicting file (previously kept or renamed to the same name)
113 mv "$TMPDIR"/"$file" ./"$name"
114 git add $ADDARGS ./"$name"
115 else
116 rm -R "$TMPDIR"/"$file"
118 done
119 # commit, filter and clean up
120 file=
121 git commit -m "$(eval "$TITLE")${BODY:+$NL$BODY}" \
122 ${DATE:+--date "$(eval "$DATE")"} $CIARGS
123 if [ -n "$FILTERHEAD" ]
124 then
125 IFS=$NL
126 git filter-branch $FILTERHEAD -- HEAD^..HEAD
127 rm -R "$(git rev-parse --git-dir)"/refs/original/
128 IFS=$OLDIFS
130 done
132 # cleanup
133 rmdir "$TMPDIR"