Skip to content

Commit

Permalink
Add initialization commands
Browse files Browse the repository at this point in the history
  • Loading branch information
asherikov committed Aug 13, 2024
1 parent 74b3909 commit 9fce372
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 22 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ WSHANDLER?=./wshandler
test: shellcheck
@${MAKE} test_type TYPE=rosinstall
@${MAKE} test_type TYPE=repos
${WSHANDLER} -r tests/clone -p shallow clone git https://github.com/asherikov/sharf.git main

test_type:
@${MAKE} wrap_test TEST=test_update
Expand All @@ -12,6 +13,7 @@ test_type:
@${MAKE} wrap_test TEST=test_merge
@${MAKE} wrap_test TEST=test_set_version
@${MAKE} wrap_test TEST=test_branch
@${MAKE} wrap_test TEST=test_init

wrap_test:
@echo ""
Expand Down Expand Up @@ -59,6 +61,8 @@ test_remove:
${WSHANDLER} -t ${TYPE} --root tests/remove/ remove staticoma_commit
${WSHANDLER} -t ${TYPE} --root tests/remove/ remove_by_url "https://github.com/ros-gbp/catkin-release.git"

test_init:
${WSHANDLER} -t ${TYPE} --root tests/init_${TYPE} -p shallow init git https://github.com/asherikov/staticoma.git

test_set_version:
${WSHANDLER} -t ${TYPE} --root tests/update/ set_version_by_url https://github.com/asherikov/qpmad.git master
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ List commands:
remove <NAME>
remove_by_url <URL>
[-p|--policy {keep}|replace] merge <FILENAME>
List initialization commands:
[-p|--policy {default}|shallow|rebase] clone git <URL> [<BRANCH>]
[-p|--policy {default}|shallow] init [git <URL1> ...]
Repository commands:
[-j|--jobs <NUM_THREADS> {1}] [-s|-source {git}] foreach '<COMMAND>'
prune
Expand Down
100 changes: 78 additions & 22 deletions wshandler
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ help()
echo " remove_by_url <URL>"
echo " [-p|--policy {keep}|replace] merge <FILENAME>"

echo "List initialization commands:"
echo " [-p|--policy {default}|shallow|rebase] clone git <URL> [<BRANCH>]"
echo " [-p|--policy {default}|shallow] init [git <URL1> ...]"

echo "Repository commands:"
echo " [-j|--jobs <NUM_THREADS> {1}] [-s|-source {git}] foreach '<COMMAND>'"
echo " prune"
Expand Down Expand Up @@ -194,6 +198,44 @@ git_scrape()
done
}

git_clone()
{
GIT_REPO=$1
VERSION=$2
DIR=$3

CLONE_ARGS=(--recurse-submodules)
for POLICY in "${POLICIES[@]}";
do
if [ "${POLICY}" == "shallow" ]
then
CLONE_ARGS+=(--depth 1 --shallow-submodules)
fi
done

SOURCE_DESTINATION=("${GIT_REPO}")
if [ -n "${DIR}" ]
then
SOURCE_DESTINATION+=("${DIR}")
fi

if [ "${VERSION}" == '-' ] || [ -z "${VERSION}" ]
then
git clone "${CLONE_ARGS[@]}" "${SOURCE_DESTINATION[@]}"
else
if ! (git clone "${CLONE_ARGS[@]}" --branch "${VERSION}" "${SOURCE_DESTINATION[@]}")
then
# GIT_VERSION is a hash
# clone with history to be able to find it
git clone --recurse-submodules "${SOURCE_DESTINATION[@]}"
pushd "${DIR}"
git checkout "${VERSION}"
popd
fi
fi
}


dir_run()
{
echo ">>> Processing '$1'"
Expand All @@ -205,7 +247,6 @@ dir_run()
fi
}


dir_git_uncommitted()
{
git status --porcelain | grep . > /dev/null
Expand Down Expand Up @@ -287,26 +328,7 @@ dir_git_update()
else
rm -rf "${GIT_DIR:?}/*"

CLONE_ARGS=(--recurse-submodules)
for POLICY in "${POLICIES[@]}";
do
if [ "${POLICY}" == "shallow" ]
then
CLONE_ARGS+=(--depth 1 --shallow-submodules)
fi
done

if [ "${GIT_VERSION}" == '-' ]
then
git clone "${CLONE_ARGS[@]}" "${GIT_REPO}" ./
else
if ! (git clone "${CLONE_ARGS[@]}" --branch "${GIT_VERSION}" "${GIT_REPO}" ./)
then
# GIT_VERSION is a hash
git clone --recurse-submodules "${GIT_REPO}" ./
git checkout "${GIT_VERSION}"
fi
fi
git_clone "${GIT_REPO}" "${GIT_VERSION}" "./"
fi
}

Expand Down Expand Up @@ -359,7 +381,7 @@ check_workspace()
if [ ! -f "${WSH_REPOLIST}" ]
then
case $1 in
scrape|add)
scrape|add|init)
touch "${WSH_REPOLIST}";;
*)
echo "'${WSH_REPOLIST}' is not a repository list"
Expand Down Expand Up @@ -541,6 +563,36 @@ execute_commit()
git_foreach "((git status --porcelain | grep . > /dev/null) && git commit -a -m '${1}') || true"
}

execute_clone()
{
"$1_clone" "$2" "$3" "${WSH_WORKSPACE_ROOT}"

check_workspace update
execute_command update
}

execute_init()
{
mkdir -p "${WSH_WORKSPACE_ROOT}"

if [ "$1" == "git" ]
then
shift
pushd "${WSH_WORKSPACE_ROOT}"
while [[ $# -gt 0 ]]
do
git_clone "$1"
shift
done
popd

check_workspace init
"${WSHANDLER[@]}" -p add scrape
else
check_workspace init
fi
}


set_version()
{
Expand Down Expand Up @@ -628,6 +680,10 @@ do
WSH_SOURCE_TYPE=$2
shift; shift;;

clone|init)
"execute_$1" "${@:2}"
exit;;

status|update|clean|scrape)
check_workspace "$1"
execute_command "$1"
Expand Down

0 comments on commit 9fce372

Please sign in to comment.