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
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"
20 # archives2git [<options>] [--] <archives>
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
28 TITLE
='echo "$arch"' # $arch
29 BODY
="" # message taken as is
32 FILTERHEAD
= # line separated arguments
39 shift; GIT_WORK_TREE
=$1 ;;
49 shift; ADDARGS
="$ADDARGS $1" ;;
51 shift; CIARGS
="$CIARGS $1" ;;
53 shift; FILTERHEAD
=$1 ;;
55 echo "$PROGRAM_NAME version $PROGRAM_VERSION"
58 echo "$0: unknown option $1" >&2
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
79 test -z "$(git status --porcelain)" &&
80 git update-index
-q --refresh ||
82 echo "$0: unstaged files or dirty index" >&2
91 # extract the archive in a temp dir
93 aunpack
-X "$TMPDIR" "$arch"
94 # remove (almost) everything from the repository dir
98 [ -e "$file" ] ||
continue
99 [ "$file" = "." -o "$file" = ".." -o "$file" = ".git" ] && continue
100 eval "$FILTER" ||
( git
rm -r "$file" ||
rm -R "$file" )
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")
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"
116 rm -R "$TMPDIR"/"$file"
119 # commit, filter and clean up
121 git commit
-m "$(eval "$TITLE")${BODY:+$NL$BODY}" \
122 ${DATE:+--date "$(eval "$DATE")"} $CIARGS
123 if [ -n "$FILTERHEAD" ]
126 git filter-branch
$FILTERHEAD -- HEAD^..HEAD
127 rm -R "$(git rev-parse --git-dir)"/refs
/original
/