Skip to content

Commit

Permalink
[tools] vault sync utility. Fixes MER#926
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Zalevskiy <denis.zalevskiy@jolla.com>
  • Loading branch information
Denis Zalevskiy committed Jul 20, 2015
1 parent 40578c7 commit 97ea6fe
Show file tree
Hide file tree
Showing 4 changed files with 402 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tools/git-vault-root
@@ -0,0 +1,27 @@
#!/bin/bash

src=`dirname $0`
src=`cd $src;pwd`
source $src/vault-misc $src

root=$(git rev-parse --show-toplevel)
rc=$?

if [ $rc -ne 0 ] || [ "x$root" == "x" ]; then
error 1 "Not a git `pwd`"
fi

if ! [ -d "$root" ]; then
error 2 "Wrong location, not a git repo? `pwd`"
fi

if [ -f $root/.vault ]; then
echo $root
else
if [ root == "`dirname /`" ]; then
error 3 "Reached /, vault is not found"
fi
root=`dirname $root`
res=$(cd $root && git vault-root) || exit 34
echo $res
fi
161 changes: 161 additions & 0 deletions tools/git-vault-sync
@@ -0,0 +1,161 @@
#!/bin/bash

src=$(dirname $0)
src=$(cd $src;pwd)
source $src/vault-misc $src

init_root_and_enter

full_uri=$1

if [ "x$full_uri" == "x" ]; then
full_uri=$(git config --get remote.origin.url)
fi

src_repo=
port=
user=
host=
src_type=

split_git_uri $full_uri

sync_tool=
cp_tool=

has_rsync=0
if which rsync 1>/dev/null; then has_rsync=1; fi

case "$src_type" in
ssh)
trace "SRC:user=$user,host=$host,port=$port,path=$src_repo"
has_scp=0
if which scp 1>/dev/null; then has_scp=1; fi
if [ $has_scp ]; then
if [ $has_rsync ]; then
sync_tool=rsync
else
sync_tool=scp
fi
cp_tool=scp
elif [ $has_rsync ]; then
sync_tool=rsync
cp_tool=rsync
else
error 51 "No rsync, no scp"
fi
;;
local)
trace "SRC:path=$src_repo"
if ! [ -d "$path" ]; then
error 3 "Local path doesn't exists: $path"
fi
if [ $has_rsync ]; then
sync_tool=rsync
else
sync_tool=cp
fi
cp_tool=cp
;;
*)
error 4 "Unsupported uri: $uri"
esac

trace "Tools: sync=$sync_tool, cp=$cp_tool"

if [ $tree_version -ge 3 ]; then
trace "Sync .git/info"
fi

case $sync_tool in
rsync)
if [ $src_type == "local" ]; then
sync_cmd="$sync_tool -avz"
src_prefix=$src_repo/
else
src_prefix=$user@$host:$src_repo/
if [ "x$port" == "x" ]; then
sync_cmd='$sync_tool -avz'
else
sync_cmd="$sync_tool -avz -e 'ssh -p $port'"
fi
fi
;;
scp)
src_prefix=$user@$host:$src_repo/
if [ "x$port" == "x" ]; then
sync_cmd='scp'
else
sync_cmd="scp -r -p -P $port"
fi

;;
cp)
src_prefix=$src_repo/
sync_cmd="cp -rLpuf"
;;
*)

esac

if [ "x$sync_cmd" == "x" ]; then
error 66 "No sync command defined"
fi

cp_cmd=

case $cp_tool in
rsync)
if [ $src_type == "local" ]; then
cp_cmd="$cp_tool -avz"
src_prefix=$src_repo/
else
src_prefix=$user@$host:$src_repo/
if [ "x$port" == "x" ]; then
cp_cmd='$sync_tool -avz'
else
cp_cmd="$sync_tool -avz -e 'ssh -p $port'"
fi
fi
;;
scp)
src_prefix=$user@$host:$src_repo/
if [ "x$port" == "x" ]; then
cp_cmd='scp'
else
cp_cmd="scp -r -p -P $port"
fi

;;
cp)
src_prefix=$src_repo/
cp_cmd="cp -rLpuf"
;;
*)
esac

if [ "x$cp_cmd" == "x" ]; then
error 66 "No cp command defined"
fi

cmd=$(get_vault_lock_cmd "$cmd $sync_cmd ${src_prefix}.git/blobs/. .git/blobs/")
trace "Sync blobs: $cmd"
eval "$cmd" || error 27 "Error executing '$cmd'"

if ! git config user.name 1>/dev/null; then
tmp_config=$(mktemp)
trap "rm $tmp_config" EXIT
cmd=$(get_vault_lock_cmd "$cp_cmd ${src_prefix}.git/config $tmp_config")
trace "Copy config through $tmp_config"
eval "$cmd" || error 27 "Error executing '$cmd'"
git config user.name $(git config --file $tmp_config user.name)
git config user.email $(git config --file $tmp_config user.email)
rm $tmp_config; trap "echo" EXIT
fi
git config status.showUntrackedFiles all
cmd=$(get_vault_lock_cmd "$cp_cmd ${src_prefix}.git/info $(repo_version_path ${src_prefix}) $(units_path ${src_prefix}) .git")
trace "Duplicate storage metadata: $cmd"
eval "$cmd" || error 27 "Error executing '$cmd'"
cmd=$(get_vault_lock_cmd "$sync_cmd $(units_path ${src_prefix}) $(units_path $root)")
trace "Sync units : $cmd"
eval "$cmd" || error 27 "Error executing '$cmd'"
51 changes: 51 additions & 0 deletions tools/test-vault-split-uri
@@ -0,0 +1,51 @@
#!/bin/bash
exec=./vault-clone-blobs

src=$(dirname $0)
src=$(cd $src;pwd)
source $src/vault-misc $src

function assert_split {
pattern=$1
src_repo=
port=
user=
host=
src_type=

split_git_uri $pattern

res=
case "$src_type" in
ssh)
res="SRC:user=$user,host=$host,port=$port,path=$src_repo"
;;
local)
res="SRC:path=$src_repo"
;;
*)
error 4 "Unsupported uri: $uri"
esac

if [ "$res" != "$2" ]; then
error "FOR '$pattern': $res != $2"
exit 1
fi
}

# ssh:// form
assert_split ssh://user0@host1:/home/dir2 "SRC:user=user0,host=host1,port=,path=/home/dir2"
assert_split ssh://user0@host1:1/home/dir2 "SRC:user=user0,host=host1,port=1,path=/home/dir2"
assert_split ssh://user0@host1:12/home/dir2 "SRC:user=user0,host=host1,port=12,path=/home/dir2"
assert_split ssh://user0@host1.org:/home/dir2 "SRC:user=user0,host=host1.org,port=,path=/home/dir2"
assert_split ssh://host1.org:/home/dir2 "SRC:user=$(whoami),host=host1.org,port=,path=/home/dir2"

# user@host form
assert_split user0@host1:/home/dir2 "SRC:user=user0,host=host1,port=,path=/home/dir2"
assert_split user0@host1:1/home/dir2 "SRC:user=user0,host=host1,port=1,path=/home/dir2"
assert_split user0@host1:12/home/dir2 "SRC:user=user0,host=host1,port=12,path=/home/dir2"
assert_split user0@host1.org:/home/dir2 "SRC:user=user0,host=host1.org,port=,path=/home/dir2"
assert_split host1.org:/home/dir2 "SRC:user=$(whoami),host=host1.org,port=,path=/home/dir2"
assert_split host1.org\\:/home/dir2 "SRC:path=host1.org\\:/home/dir2"


0 comments on commit 97ea6fe

Please sign in to comment.