From a23e7df0247b51c5daf5fa44897ced9cedf23b95 Mon Sep 17 00:00:00 2001
From: "Claus Schneider(Eficode)" <claus.schneider@eficode.com>
Date: Thu, 23 Nov 2023 17:08:42 +0100
Subject: [PATCH] Add pipeline to test against git latest, 2.36.2, 2.26.2

---
 .github/workflows/pr.yml | 83 ++++++++++++++++++++++++++++++++++++++++
 Dockerfile               | 14 +++++++
 README.md                |  9 +----
 _tests.sh                | 11 +++++-
 git-artifact             |  9 ++++-
 git-artifact-tests       |  1 +
 6 files changed, 116 insertions(+), 11 deletions(-)
 create mode 100644 .github/workflows/pr.yml
 create mode 100644 Dockerfile
 create mode 120000 git-artifact-tests

diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml
new file mode 100644
index 0000000..f55e518
--- /dev/null
+++ b/.github/workflows/pr.yml
@@ -0,0 +1,83 @@
+name: CI
+
+on:
+  # Triggers the workflow on push or pull request events but only for the "main" branch
+  push:
+    branches: [ "main" ]
+  pull_request:
+    branches: [ "main" ]
+
+  # Allows you to run this workflow manually from the Actions tab
+  workflow_dispatch:
+
+jobs:
+    test-git-native:
+        runs-on: ubuntu-latest
+
+        steps:
+        - name: Checkout code
+          uses: actions/checkout@v2
+
+        - name: Run tests git-native
+          run: | 
+            git config --global user.email "git-artifact@github.com"
+            git config --global user.name "Git Artifact"
+            git --version
+            verbose=true bash _tests.sh || {
+                exit_code=$?
+                find . -name run.log 
+                find . -name run.log | xargs -I % cat % 
+                exit $exit_code
+            }
+    test-git-alpine-latest:      
+        runs-on: ubuntu-latest
+        steps:
+        - name: Checkout code
+          uses: actions/checkout@v2
+        - name: Run tests
+          run: | 
+            set -x
+            docker build --build-arg ALPINE_GIT_DOCKER_VERSION=${git_version} -t git-artifact:latest .
+            docker run --rm -e debug=${debug:-false} -e verbose=true -v $(pwd):/git -v $HOME/.ssh:/root/.ssh git-artifact:latest --version
+            docker run --rm -e debug=${debug:-false} -e verbose=true -v $(pwd):/git -v $HOME/.ssh:/root/.ssh git-artifact:latest artifact-tests || {
+                exit_code=$?
+                find . -name run.log 
+                find . -name run.log | xargs -I % cat % 
+                exit $exit_code
+            }
+          
+    test-git-alpine-v2-36-2:      
+        runs-on: ubuntu-latest
+        steps:
+        - name: Checkout code
+          uses: actions/checkout@v2
+        - name: Run tests
+          run: | 
+            set -x  
+            git_version=v2.36.2
+                docker build --build-arg ALPINE_GIT_DOCKER_VERSION=${git_version} -t git-artifact:${git_version} .
+                docker run --rm -e debug=${debug:-false} -e verbose=true -v $(pwd):/git -v $HOME/.ssh:/root/.ssh git-artifact:${git_version} --version
+                docker run --rm -e debug=${debug:-false} -e verbose=true -v $(pwd):/git -v $HOME/.ssh:/root/.ssh git-artifact:${git_version} artifact-tests || {
+                    exit_code=$?
+                    find . -name run.log 
+                    find . -name run.log | xargs -I % cat % 
+                    exit $exit_code
+                }
+
+    test-git-alpine-v2-26-2:      
+        runs-on: ubuntu-latest
+        steps:
+        - name: Checkout code
+          uses: actions/checkout@v2
+        - name: Run tests
+          run: | 
+            set -x
+            git_version=v2.26.2
+            docker build --build-arg ALPINE_GIT_DOCKER_VERSION=${git_version} -t git-artifact:${git_version} .
+            docker run --rm -e debug=${debug:-false} -e verbose=true -v $(pwd):/git -v $HOME/.ssh:/root/.ssh git-artifact:${git_version} --version
+            docker run --rm -e debug=${debug:-false} -e verbose=true -v $(pwd):/git -v $HOME/.ssh:/root/.ssh git-artifact:${git_version} artifact-tests || {
+                exit_code=$?
+                find . -name run.log 
+                find . -name run.log | xargs -I % cat % 
+                exit $exit_code
+            }
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..2166b5b
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,14 @@
+ARG ALPINE_GIT_DOCKER_VERSION=dummy
+
+FROM alpine/git:${ALPINE_GIT_DOCKER_VERSION}
+
+RUN apk fix && \
+    apk --no-cache --update add bash
+
+RUN git config --global user.email "git@artifacts.com"
+RUN git config --global user.name "Git Artifacts"
+
+RUN git config --global --add safe.directory /git
+
+ENV PATH="/git:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
+
diff --git a/README.md b/README.md
index a46c18f..1ca3c88 100644
--- a/README.md
+++ b/README.md
@@ -47,14 +47,6 @@ The tool uses tags hence the producer need to tag push-rights. It is also benefi
 
 It can also run in branch mode. It can  maintain a `latest` branch which needs to be force pushed or delete + push rights. The concept is similar to docker concept of `<image>/latest`. It is only important if you want to use tracking branches without using `git-artifact`. It could be in context of `submodules` or `repo manifests`.
 
-### Integrations
-#### CI Systems
-CI system's git integrations can usually be configured to trigger on glob-pattern. You can also use a branch to track the "latest" which make it easier for some systems.
-
-#### Git submdules, Android repo tool
-Similar to the CI systems, the submodules and repo tool have features to track branches which makes it easy "just" get the latest. In releases, it is though recommended to use specific version/sha1, so it is reproducable. 
-Alternatively you can use `git-artifact` to find  the `latest` of a given a regular expression.
-
 ### Installation
 Download or clone this repo (`git-artifact`) and add make it available in the PATH. Given that `git-artifact` is in the PATH, then `git` can use it as an command like `git artifact`. It is now integrated with git and git is extended.
 
@@ -129,3 +121,4 @@ TODO: based on count..
 
 ### Promotions
 There are few ways to you can do promotions. 
+
diff --git a/_tests.sh b/_tests.sh
index 6bb2553..1f9e05d 100755
--- a/_tests.sh
+++ b/_tests.sh
@@ -6,13 +6,20 @@ set -euo pipefail
 [[ ${debug:-} == true ]] && set -x
 PATH=$(pwd):$PATH
 
+which git 
+git --version
+
 cd .test
 root_folder=$(pwd)
+echo "Cleaning $(pwd)"
+git clean -xffdq .
 
 local_tester_repo=.local
 remote_tester_repo=.remote
 clone_tester_repo=.clone
 
+
+
 function testcase_header() {
     [[ ${verbose:-} == true ]] || return 0
     echo
@@ -29,7 +36,7 @@ function eval_testcase() {
         git log --graph --all --oneline --decorate --format="%d %s" > "${root_folder}/${test}/git-test.log"
     fi
     cd "${root_folder}/${test}"
-    if diff -Z git-test.log git-reference.log ; then 
+    if diff -w git-test.log git-reference.log ; then 
         if [[ ${verbose:-} == true ]] ; then 
             cat git-test.log
             echo "INFO: Test $test : OK"
@@ -157,7 +164,7 @@ testcase_header
     cd ../$clone_tester_repo
     git artifact fetch-co-latest --regex 'v*.*'
 
-} > ${test}/run.log 2>&1 || { pwd && cat ../run.log; }
+} > ${test}/run.log 2>&1 || { echo "ERROR_CODE: $?";  pwd && cat ../run.log; }
 eval_testcase
 
 test="5.1"
diff --git a/git-artifact b/git-artifact
index 636f31e..5ef9e14 100755
--- a/git-artifact
+++ b/git-artifact
@@ -284,7 +284,14 @@ cmd_fetch-co() {
 find-latest() {
     local -n _latest_tag=${1}
     # https://stackoverflow.com/questions/10649814/get-last-git-tag-from-a-remote-repo-without-cloning
-    _latest_tag=$(git ls-remote --tags --refs --sort='-version:refname' origin ${arg_regex} | head --lines=1 | cut  -f2 | cut -d / -f3-)
+    _latest_tag=$(git ls-remote --tags --refs --sort='-version:refname' origin ${arg_regex} | head -n 1 | cut  -f2 | cut -d / -f3-) || {
+        local exit_code=$?
+        if [[ $exit_code -ne 141 ]]; then 
+            #https://unix.stackexchange.com/questions/580117/debugging-sporadic-141-shell-script-errors
+            echo "ERROR: Something unknown happend.."
+            exit 1
+        fi
+    } 
     if [ -z "${_latest_tag:-}" ]; then 
         echo "ERROR: No tag found using regex: ${arg_regex} "
         exit 1
diff --git a/git-artifact-tests b/git-artifact-tests
new file mode 120000
index 0000000..50b4bde
--- /dev/null
+++ b/git-artifact-tests
@@ -0,0 +1 @@
+_tests.sh
\ No newline at end of file