Skip to content
Merged
13 changes: 13 additions & 0 deletions .test/10/git-reference.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
* (HEAD -> main, origin/main) Added submodule repo
| * (tag: v2.0) v2.0
|/
| * (tag: v1.0) v1.0
|/
* First commit of git artifact
[submodule "submodule-repo"]
path = submodule-repo
url = ../.remote
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean
68 changes: 45 additions & 23 deletions _tests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env bash
# shellcheck disable=SC2317
# shellcheck disable=SC2329


function usage() {
Expand All @@ -7,8 +9,9 @@ Usage: $(basename "$0") [options]

Options:
-h, --help Show this help message and exit
--debug Enable debug mode
-d, --debug Enable debug mode
--verbose Enable verbose output
-t|--testcase <#> Specify a single test case to run (e.g., 1, 2, 3.1, etc.)

Description:
This script runs integration tests for git-artifact.
Expand Down Expand Up @@ -89,7 +92,6 @@ remote_tester_repo=.remote
clone_tester_repo=.clone
global_exit_code=0

# shellcheck disable=SC2317
function testcase_header() {
[[ ! -d "${test}" ]] && {
echo "Creating test directory: ${test} and empty git-reference.log"
Expand All @@ -103,13 +105,11 @@ function testcase_header() {
echo "--------------------------------------------------------------------------------"
}

# shellcheck disable=SC2317
function generate_git_test_log() {
rm -rf .git/refs/remotes/origin/HEAD
git log --graph --all --oneline --format="%d %s" >> "${root_folder}/${test}/git-test.log"
}

# shellcheck disable=SC2317
function eval_testcase() {
# expect to be in repo to test against

Expand Down Expand Up @@ -141,7 +141,6 @@ function eval_testcase() {
echo
}

# shellcheck disable=SC2317
function generate_base_repo() {
rm -rf "${local_tester_repo:?}/" "${remote_tester_repo:?}/" "${clone_tester_repo:?}/"
git init --bare -b "${default_branch:-main}" $remote_tester_repo || {
Expand All @@ -167,7 +166,6 @@ echo " - <test>/ok.log(all good)"
echo " - <test>/nok.log(failed tests)"
echo

# shellcheck disable=SC2317
function 1 {
export test="1"
testcase_synopsis="base-repo default-branch; clone"
Expand All @@ -180,7 +178,6 @@ function 1 {
eval_testcase
}

# shellcheck disable=SC2317
function 1.1 {
export test="1.1"
testcase_synopsis="base-repo master-branch; clone"
Expand All @@ -195,7 +192,6 @@ function 1.1 {
eval_testcase
}

# shellcheck disable=SC2317
function 2 {
test="${FUNCNAME[0]}"
testcase_synopsis="base-repo ; clone; fetch-co : the repo has two tags and the latest is checked out"
Expand All @@ -211,7 +207,6 @@ function 2 {
eval_testcase
}

# shellcheck disable=SC2317
function 3 {
test="${FUNCNAME[0]}"
testcase_synopsis="base-repo ; clone - gives a repo without any artifacts"
Expand All @@ -225,7 +220,6 @@ function 3 {
eval_testcase
}

# shellcheck disable=SC2317
function 4 {
export test="4"
testcase_synopsis="base-repo ; clone; add-n-push with branch"
Expand All @@ -243,7 +237,6 @@ function 4 {
eval_testcase
}

# shellcheck disable=SC2317
function 5 {
test="${FUNCNAME[0]}"
testcase_synopsis="base-repo ; clone; fetch-co-latest pattern"
Expand Down Expand Up @@ -279,7 +272,6 @@ function 5 {
eval_testcase
}

# shellcheck disable=SC2317
function 5.1 {
test="${FUNCNAME[0]}"
testcase_synopsis="base-repo ; clone; find-latest pattern"
Expand All @@ -297,7 +289,6 @@ function 5.1 {
}


# shellcheck disable=SC2317
function 6 {
test="${FUNCNAME[0]}"
testcase_synopsis="base-repo ; clone; fetch-tags"
Expand All @@ -315,7 +306,6 @@ function 6 {
eval_testcase
}

# shellcheck disable=SC2317
function 7 {
test="${FUNCNAME[0]}"
testcase_synopsis="base-repo ; clone; list"
Expand All @@ -342,7 +332,6 @@ function 7 {
eval_testcase
}

# shellcheck disable=SC2317
function 8 {
test="${FUNCNAME[0]}"
testcase_synopsis="base-repo ; clone; summary"
Expand Down Expand Up @@ -377,7 +366,6 @@ function 8 {
eval_testcase
}

# shellcheck disable=SC2317
function 9 {
test="${FUNCNAME[0]}"

Expand All @@ -400,26 +388,58 @@ function 9 {
sleep 1
done

generate_git_test_log
git artifact prune --glob 'v*.*' --keep 5 --dryrun >> ${root_folder}/${test}/git-test.log
git artifact prune --glob 'v*.*' --keep 5
git fetch origin -pP
generate_git_test_log
git artifact prune --glob 'v*.*' --keep 5 --dryrun >> ${root_folder}/${test}/git-test.log
git artifact prune --glob 'v*.*' --keep 5
git fetch origin -pP

generate_git_test_log
generate_git_test_log

git artifact list --glob 'v*.*' >> ${root_folder}/${test}/git-test.log
git artifact list --glob 'v*.*' >> ${root_folder}/${test}/git-test.log

} > ${root_folder}/${test}/run.log 2>&1 || { pwd && cat ${root_folder}/${test}/run.log; }
eval_testcase
}

function 10 {
test="${FUNCNAME[0]}"

testcase_synopsis="base-repo ; clone; add-as-submodule"
testcase_header
{
cd $test
generate_base_repo
sleep 1
git artifact clone --url "$(pwd)/$remote_tester_repo" --path "$clone_tester_repo"

cd $local_tester_repo

git artifact add-as-submodule --url "../$remote_tester_repo" --path submodule-repo

git status

git commit -m "Added submodule repo"
git push origin HEAD:"${default_branch:-main}"
git fetch origin -apP

generate_git_test_log
cat .gitmodules >> ${root_folder}/${test}/git-test.log
git status >> ${root_folder}/${test}/git-test.log

} > ${root_folder}/${test}/run.log 2>&1 || { pwd && cat ${root_folder}/${test}/run.log; }
eval_testcase
}


if [[ ${arg_testcase:-} == "" ]]; then
# Dynamically list and call test functions
mapfile -t test_functions < <(declare -F | awk '{print $3}' | grep -E '^[0-9]+(\.[0-9]+)?$')

for fn in "${test_functions[@]}"; do
"$fn"
"$fn" || {
echo "Test case '$fn' failed. Check the logs in .test/$fn/run.log"
global_exit_code=1
}
done
else
# Run a specific test case if provided
Expand All @@ -430,6 +450,8 @@ else
exit 1
fi
fi


echo
echo "########################################"
echo "All tests completed. Checking results..."
Expand Down
102 changes: 75 additions & 27 deletions git-artifact
Original file line number Diff line number Diff line change
Expand Up @@ -69,49 +69,50 @@ function set_opts_spec() {

commands:

init inititialize a new repository
clone clone and existing repository from the remote
add-n-tag add and commit files to repo abd leave workspace on tag/untouched
push push a tag
add-n-push add and commit files to repo and then push them
fetch-co fetch tag from remote and checkout it out
reset reset workspace and branches
list list all tags in the repo given a glob pattern
summary print a summary of the tags in the repo
prune prune tags in the repo given a glob pattern and keep amount of tags
find-latest Find the latest tags from reqex and print it
fetch-co-latest Get latest tag form remote using grep reg-ex and reset hard to it
fetch-tags Fetch all tags that points to a sha1 or HEAD - Useful in relation to detached HEAD and submodules
init Inititialize a new repository
clone Clone and existing repository from the remote
add-n-tag Add and commit files to repo abd leave workspace on tag/untouched
push push a tag
add-n-push Add and commit files to repo and then push them
fetch-co Fetch tag from remote and checkout it out
reset Reset workspace and branches
list List all tags in the repo given a glob pattern
summary Print a summary of the tags in the repo
prune Prune tags in the repo given a glob pattern and keep amount of tags
find-latest Find the latest tags from reqex and print it
fetch-co-latest Get latest tag form remote using grep reg-ex and reset hard to it
fetch-tags Fetch all tags that points to a sha1 or HEAD - Useful in relation to detached HEAD and submodules
add-as-submodule Add a submodule in a clever way to the current consuming repository. Special operation to avoid big clones .
--
h show the help
q quiet
v,verbose verbose
d show debug messages
h show the help
q quiet
v,verbose verbose
d show debug messages

option for 'prune'
dryrun dry run - do not execute any commands ( optional for prune)
dryrun dry run - do not execute any commands ( optional for prune)

options for 'init' and 'clone'
u,url= the remote url
p,path= the optional path for init and clone
options for 'init', 'clone' and 'add-as-submodule'
u,url= the remote url
p,path= the optional path for init and clone

options for 'init', 'clone', 'add-n-tag', 'push', 'add-n-push', 'fetch-co'
b,branch= use branch for local changes and pushes
b,branch= use branch for local changes and pushes

options for 'add-n-tag', 'push', 'add-n-push', 'fetch-co'
t,tag= the tag to be created
t,tag= the tag to be created

options for 'fetch-co-latest', 'find-latest', 'list', 'prune'
g,glob= the glob pattern search remote for tags
g,glob= the glob pattern search remote for tags

options for 'fetch-tags'
s,sha1= The sha1 of which to get tags from from
s,sha1= The sha1 of which to get tags from from

options for 'prune'
k,keep= The amount of tags to keep - default is 20
k,keep= The amount of tags to keep - default is 20

options for 'summary'
delimiter= The delimiter to use for the summary - default is '/'
delimiter= The delimiter to use for the summary - default is '/'
"
}

Expand Down Expand Up @@ -393,6 +394,40 @@ cmd_summary() {
done
}

cmd_add-as-submodule() {
#
# This function is operating on the consuming repository and not the git artifact repo.
# It is adding a submodule to the current repository.
# It is done as a special operation as the submodule is by default cloned with tags and branch and that it can be huge.
#

if [[ -d ${arg_path} ]]; then
echo "ERROR: Directory '${arg_path}' already exists"
exit 1
fi

mkdir -p "./.git/modules/"
echo "Cloning repo into .git/modules/${arg_path} but working tree is: ${arg_path}"
git clone --single-branch --no-tags --separate-git-dir="./.git/modules/${arg_path}" "${arg_remoteurl}" "${arg_path}"

echo "Adding the repo as submodule"
git submodule add --force "${arg_remoteurl}" "${arg_path}"
echo

echo "#############################################################################"
echo "# HINT: You can use the ../ notation in submodule path to avoid the full url,"
echo "# if your remote server is the same as the submodule server "
echo "# .. to ensure portability of the repositories "
echo "#############################################################################"
git remote -v
echo
echo "${arg_path} : ${arg_remoteurl}"
echo
echo "You are now ready to commit ..."
git status

}
Comment thread
bicschneider marked this conversation as resolved.
Comment thread
coderabbitai[bot] marked this conversation as resolved.

read_remote_tags_to_map() {
local -n _tags=${1}

Expand Down Expand Up @@ -620,6 +655,19 @@ main () {
arg_sha1=$(git rev-parse HEAD)
fi
;;
add-as-submodule)
# shellcheck source=/dev/null
. git-sh-setup
require_work_tree
if test -z "${arg_remoteurl:-}" ; then
echo "ERROR: --url <url> required for $arg_command"
exit 1
fi
if test -z "${arg_path:-}" ; then
echo "ERROR: --arg_path <path> required for $arg_command"
exit 1
fi
;;
Comment thread
bicschneider marked this conversation as resolved.
Comment thread
bicschneider marked this conversation as resolved.
*) printf "Unknown command: %s\n\n" "$arg_command"
git artifact -h
exit 1
Expand Down