2 # Copyright © 2013,2014 Géraud Meyer <graud@gmx.com>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 2 as
6 # published by the Free Software Foundation.
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # You should have received a copy of the GNU General Public License along
14 # with this program. If not, see <http://www.gnu.org/licenses/>.
16 PROGRAM_NAME
="archives2git"
17 PROGRAM_VERSION
="0.1+"
21 $PROGRAM_NAME - generate a Git history from a series of release tarballs
23 $PROGRAM_NAME [<options>] [--] <archives_and_dirs>
25 --repo <work_tree_subdir>
27 --keep-filter <sh_code>
33 --gitfilter <nl_separated_args>
40 The following shell variables are usable if appropriate in the shell code
41 snippets passed as command line options: \$arch, \$archname, \$archdir,
50 An example file is distributed.
54 git-commit-tree(1), git_load_dirs(1)
59 RENAME
='echo "$file"' # $file $arch $archname
60 FILTER
='false' # $file $arch $archname
61 DATE
='' # $arch $archname
62 TITLE
='echo "$arch"' # $arch $archname
63 BODY
="" # message taken as is
66 FILTERHEAD
= # line separated arguments
68 TAGDEF
='printf version/; printf "%s\n" "$archname" |
69 LC_ALL=C sed -e '\''s/^.*[-_]\([0-9][-0-9.ab]*\)$/\1/'\'
70 # config (file, environment, command line)
71 [ -f "$HOME"/.archives2gitrc
] && .
"$HOME"/.archives2gitrc
72 GIT_WORK_TREE
=${GIT_WORK_TREE-.}
73 # may be a subdir of the top level
78 shift; GIT_WORK_TREE
=$1 ;;
90 shift; ADDARGS
="$ADDARGS $1" ;;
92 shift; CIARGS
="$CIARGS $1" ;;
94 shift; FILTERHEAD
=$1 ;;
105 echo "$PROGRAM_NAME version $PROGRAM_VERSION"
108 echo >&2 "$PROGRAM_NAME: error: unknown option $1"
122 # git repository check
123 cd "$GIT_WORK_TREE" &&
124 { test -d .git || git rev-parse
--git-dir >/dev
/null
; } ||
126 echo >&2 "$PROGRAM_NAME: error: not in a git repository ($(pwd))"
129 test -z "$(git status --porcelain)" &&
130 git update-index
-q --refresh ||
132 echo >&2 "$PROGRAM_NAME: error: unstaged files or dirty index"
141 # put the files in the archive dir
145 cp -R "$arch" "$TMPDIR"
147 aunpack
-X "$TMPDIR" "$arch"
150 archname
=$
( printf "%s\n" "$arch" | LC_ALL
=C
sed \
151 -e 's;^\([^/]*/\)*\([^/]\{1,\}\)/\{0,1\}$;\2;' \
152 -e 's;\.\([Zz]\|lzo\|bz\|gz\|bz2\|xz\|lzma\|7z\|lz\|rz\|lrz\)$;;' \
153 -e 's;\.\(tar\|t[Zz]\|tzo\|tbz\|tgz\|tbz2\|txz\|t7z\|tlz\|lha\|lzh\|7z\|alz\|arj\|zip\|rar\|jar\|war\|ace\|cab\|a\|cpio\|shar\|rpm\|deb\)$;;' \
154 -e 's;\.\(orig\)$;;' )
155 # remove (almost) everything from the repository dir
159 [ -e "$file" ] ||
continue
160 [ x
"$file" = x
"." ] ||
[ x
"$file" = x
".." ] ||
[ x
"$file" = x
".git" ] && continue
161 eval "$FILTER" ||
{ git
rm -r "$file" ||
rm -R "$file"; }
163 # move the content of the temp dir to the repository and stage it
164 for file in "$archdir"/* "$archdir"/.
*
166 [ -e "$file" ] ||
continue
167 file=${file#$archdir/}
168 [ x
"$file" = x
"." ] ||
[ x
"$file" = x
".." ] && continue
169 name
=$
(eval "$RENAME")
172 [ ! -e .
/"$name" ] ||
rm -R .
/"$name"
173 # remove conflicting file (previously kept or renamed to the same name)
174 mv "$archdir"/"$file" .
/"$name"
175 git add
$ADDARGS .
/"$name"
177 rm -R "$archdir"/"$file"
180 # commit, filter and clean up
182 git commit
-m "$(eval "$TITLE")${BODY:+$NL$BODY}" \
183 ${DATE:+--date "$(eval "$DATE")"} $CIARGS
184 if [ -n "$FILTERHEAD" ]
187 git filter-branch
$FILTERHEAD -- HEAD^..HEAD
188 rm -R "$(git rev-parse --git-dir)"/refs
/original
/
193 tagmessage
=$
(eval "$TAG" | LC_ALL
=C
tail -n +2)
194 tagname
=$
(eval "$TAG" | LC_ALL
=C
head -1)
195 git tag
${tagmessage:+-m "$tagmessage"} "$tagname"