git-import-tarball

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi folks,


I'm currently working on an automatic import mechanism for 
various upstreams into repositories of the oss-qm project [1]
and written a little shellscript which imports an existing
tarball as new commit into a git repo.

Maybe someone finds it useful ;-)
(see attachement)

cu
[1] https://sourceforge.net/p/oss-qm/home/
-- 
----------------------------------------------------------------------
 Enrico Weigelt, metux IT service -- http://www.metux.de/

 phone:  +49 36207 519931  email: weigelt@xxxxxxxx
 mobile: +49 151 27565287  icq:   210169427         skype: nekrad666
----------------------------------------------------------------------
 Embedded-Linux / Portierung / Opensource-QM / Verteilte Systeme
----------------------------------------------------------------------
#!/bin/bash

help() {
    echo "git-import-tarball <args>"
    echo ""
    echo "Imports an tarball into an existing repository, ontop of given parent commit"
    echo ""
    echo "    --repository=<repository>   Git repository"
    echo "    --tarball=<tarball>         Name of the tarball file"
    echo "    --parent=<parent-ref>       Parent commit to base on"
    echo "    --tag=<tag-name>            Name for the tag to create"
    echo "    --strip-components=<number> String <number> of pathname components before decompression"
    echo ""
    echo "Exit codes:   1    Generic error"
    echo "              2    Syntax error"
    echo ""
}

die() {
    echo "$0: $*"
    exit 1
}

diehelp() {
    echo "$0: $*"
    echo ""
    help
    exit 2
}

callgit() {
    if git --work-tree=${temp_workdir} --git-dir=${param_repository} "$@" ; then
	return 0
    else
	return 1
    fi
}

clean_workdir() {
    rm -Rf "${temp_workdir}"   || die "cannot remove workdir: \"${temp_workdir}\""
    mkdir -p "${temp_workdir}" || die "cannot create workdir: \"${temp_workdir}\""
    return 0
}

## command line parsing
while [ "$1" ]; do
    case "$1" in
	--repository=*)
	    param_repository="${1##--repository=}"
	;;
	--tarball=*)
	    param_tarball="${1##--tarball=}"
	;;
	--parent=*)
	    param_parent="${1##--parent=}"
	;;
	--root-commit)
	    param_root_commit=1
	;;
	--tag=*)
	    param_tag="${1##--tag=}"
	;;
	--strip-components=*)
	    param_strip_components="${1##--strip-components=}"
	    taropt_strip_components="--strip-components=${param_strip_components}"
	;;
	*)
	    echo "$0: unhandled parameter \"$1\""
	    exit 1
	;;
    esac
    shift
done

### parameter checks
[ "${param_root_commit}" ] || [ "$param_parent" ] || diehelp "missing --parent or --root-commit"
[ "${param_tag}" ]                                || diehelp "missing --tag"

### fixup repository path
case "${param_repository}" in
    "")
	diehelp "missing --repository"
    ;;
    ~*)
	param_repository="${HOME}/${param_repository##\~}/.git"
    ;;
    *)
	param_repository="${param_repository}/.git"
    ;;
esac

### fixup the tarball name
case "${param_tarball}" in
    ~*)
	param_tarball="${HOME}/${param_tarball##\~}"
    ;;
    *)
esac

### find out which uncompress command to use
case "${param_tarball}" in
    *.tar.bz2|*.tbz2)
	uncompress_cmd="tar -x -j    -f ${param_tarball} ${taropt_strip_components}"
    ;;
    *.tar.gz|*.tgz)
	uncompress_cmd="tar -x -z    -f ${param_tarball} ${taropt_strip_components}"
    ;;
    *.tar)
	uncompress_cmd="tar -x -z    -f  ${param_tarball} ${taropt_strip_components}"
    ;;
    *.tar.Z|*.tZ)
	uncompress_cmd="tar -x -Z    -f ${param_tarball} ${taropt_strip_components}"
    ;;
    *.tar.lzma)
	uncompress_cmd="tar -x -lzma -f ${param_tarball} ${taropt_strip_components}"
    ;;
    *.tar.xz)
	uncompress_cmd="tar -x -xz   -f ${param_tarball} ${taropt_strip_components}"
    ;;
    *.tar.lzip)
	uncompress_cmd="tar -x -lzip -f ${param_tarball} ${taropt_strip_components}"
    ;;
    *.tar.lzop)
	uncompress_cmd="tar -x -lzop -f ${param_tarball} ${taropt_strip_components}"
    ;;
    "")
	diehelp "missing --tarball option"
    ;;
    *)
	diehelp "unsupported tarball format: \"${param_tarball}\""
    ;;
esac
__tarball_basename=`basename "${parameter_tarball}"`


### render temporary branch name, temporary workdir, etc
temp_branch=`date "+__import_tarball_tmp-%Y-%m-%d-%H-%M-%S"`
temp_workdir=`mktemp`

### create temporary working dir
clean_workdir

### store the previous HEAD pointer
last_head=`callgit symbolic-ref HEAD`

### checkout the parent ref and cleanup again
## fixme: perhaps we should clean out old temporary branches ;-o
if [ "${param_root_commit}" ]; then
    callgit symbolic-ref HEAD "refs/heads/$temp_branch" || die "git-symbolic-ref failed"
else
    callgit checkout -f "${param_parent}" -b "$temp_branch" || die "git-checkout failed"
fi
clean_workdir

### now decompress the workdir
cd ${temp_workdir} || die "cannot chdir to ${temp_workdir}"
$uncompress_cmd || die "uncompress failed: \"$uncompress_cmd\""

### add everything in there to the index
callgit add -A     || die "git add -A failed"

### commit
callgit commit -m "tarball import: ${__tarball_basename}" || die "git-commit failed"

### tag the commit
callgit tag "${param_tag}" || die "git-tag failed"

### final cleanup
callgit symbolic-ref HEAD "$last_head" || die "git-symbolic-ref failed (2)"
callgit branch -D "$temp_branch" || die "git-branch -D for temporary branch failed"
clean_workdir

exit 0

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]