From c80fcf7b8e2a11a64fd3e1ecc23b4a0ba8869c6e Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Tue, 5 Jan 2021 05:37:22 -0500 Subject: [PATCH 01/31] feat: add support for cloning/syncing from a remote repository This makes it a bit easier for external projects to implement deploy webhooks by exposing the ability to sync from a git repository/sha. --- docs/deployment/methods/git.md | 84 +++++++++++++++- plugins/git/help-functions | 4 + plugins/git/internal-functions | 152 +++++++++++++++++++++++++++++ plugins/git/subcommands/allow-host | 6 ++ plugins/git/subcommands/clone | 6 ++ plugins/git/subcommands/fetch | 6 ++ plugins/git/subcommands/public-key | 6 ++ 7 files changed, 261 insertions(+), 3 deletions(-) create mode 100755 plugins/git/subcommands/allow-host create mode 100755 plugins/git/subcommands/clone create mode 100755 plugins/git/subcommands/fetch create mode 100755 plugins/git/subcommands/public-key diff --git a/docs/deployment/methods/git.md b/docs/deployment/methods/git.md index 4bc071ef6..93fe545eb 100644 --- a/docs/deployment/methods/git.md +++ b/docs/deployment/methods/git.md @@ -3,9 +3,13 @@ > Subcommands new as of 0.12.0 ``` -git:initialize # Initialize a git repository for an app -git:report [] [] # Displays a git report for one or more apps -git:set () # Set or clear a git property for an app +git:allow-host # Adds a host to known_hosts +git:clone [--build] [] # Creates an app from remote git repo +git:fetch [--build] [] # Fetch the app at an optional repo object and build the codebase +git:initialize # Initialize a git repository for an app +git:public-key # Outputs the dokku public deploy key +git:report [] [] # Displays a git report for one or more apps +git:set () # Set or clear a git property for an app ``` Git-based deployment has been the traditional method of deploying applications in Dokku. As of v0.12.0, Dokku introduces a few ways to customize the experience of deploying via `git push`. A Git-based deployment currently supports building applications via: @@ -105,3 +109,77 @@ dokku git:set node-js-app keep-git-dir false # delete the .git directory during builds (default) dokku git:set node-js-app keep-git-dir "" ``` + +### Initializing an app repository from a remote repository + +> The application must exist before the repository can be initialized + +A Dokku app repository can be initialized from a remote git repository via the `git:clone` command. This command should only be used for an initial sync to a remote repository as it may wipe out any local Dokku history that isn't in sync with the remote. Any repository that can be cloned by the `dokku` user can be specified. + +```shell +dokku git:clone node-js-app https://github.com/heroku/node-js-getting-started.git +``` + +The `git:clone` command optionally takes an optional third parameter containing a git reference, which may be a branch, tag, or specific commit. + +```shell +# specify a branch +dokku git:clone node-js-app https://github.com/heroku/node-js-getting-started.git main + +# specify a tag +dokku git:clone node-js-app https://github.com/heroku/node-js-getting-started.git 1 + +# specify a commit +dokku git:clone node-js-app https://github.com/heroku/node-js-getting-started.git 97e6c72491c7531507bfc5413903e0e00e31e1b0 +``` + +By default, this command does not trigger an application build. To do so during a `git:clone`, specify the `--build` flag. + +```shell +dokku git:clone --build node-js-app https://github.com/heroku/node-js-getting-started.git +``` + +### Fetching repository updates from a remote repository + +> The application must exist before the repository can be initialized + +A Dokku app repository can be updated from a remote git repository via the `git:fetch` command. This command may fail if the specified remote repository cannot be synced from the current Dokku repository state. Any repository that can be cloned by the `dokku` user can be specified. + +```shell +dokku git:fetch node-js-app https://github.com/heroku/node-js-getting-started.git +``` + +The `git:fetch` command optionally takes an optional third parameter containing a git reference, which may be a branch, tag, or specific commit. + +```shell +# specify a branch +dokku git:fetch node-js-app https://github.com/heroku/node-js-getting-started.git main + +# specify a tag +dokku git:fetch node-js-app https://github.com/heroku/node-js-getting-started.git 1 + +# specify a commit +dokku git:fetch node-js-app https://github.com/heroku/node-js-getting-started.git 97e6c72491c7531507bfc5413903e0e00e31e1b0 +``` + +By default, this command does not trigger an application build. To do so during a `git:fetch`, specify the `--build` flag. + +```shell +dokku git:fetch --build node-js-app https://github.com/heroku/node-js-getting-started.git +``` + +### Allowing remote repository hosts + +By default, the Dokku host may not have access to a server containing the remote repository. This can be initialized via the `git:allow-host` command. + +```shell +dokku git:allow-host github.com +``` + +### Verifying the cloning public key + +In order to clone a remote repository, the remote server should have the Dokku host's public key configured. This plugin does not currently create this key, but if there is one available, it can be shown via the `git:public-key` command. + +```shell +dokku git:public-key +``` diff --git a/plugins/git/help-functions b/plugins/git/help-functions index f3f72bac0..60153af2f 100755 --- a/plugins/git/help-functions +++ b/plugins/git/help-functions @@ -27,7 +27,11 @@ help_desc fn-help-content() { declare desc="return help content" cat <, Adds a host to known_hosts + git:clone [--build] [], Creates an app from remote git repo + git:fetch [--build] [], Fetch the app at an optional repo object and build the codebase git:initialize , Initialize a git repository for an app + git:public-key, Outputs the dokku public deploy key git:report [] [], Displays a git report for one or more apps git:set (), Set or clear a git property for an app help_content diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index 525ad122e..8bd889c17 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -4,6 +4,102 @@ source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions" set -eo pipefail [[ $DOKKU_TRACE ]] && set -x +cmd-git-allow-host() { + declare desc="adds a host to known_hosts" + local cmd="git:allow-host" + [[ "$1" == "$cmd" ]] && shift 1 + declare HOST="$1" + [[ -z "$HOST" ]] && dokku_log_fail "Please supply a git host" + + ssh-keyscan -t rsa "$HOST" >> "$DOKKU_ROOT/.ssh/known_hosts" + dokku_log_info1 "$HOST added to known hosts" +} + +cmd-git-clone() { + declare desc="creates an app from remote git repo" + local cmd="git:clone" + [[ "$1" == "$cmd" ]] && shift 1 + declare APP="$1" GIT_REMOTE="$2" GIT_REF="$3" FLAG="$4" + + ARGS=() + for arg in "$@"; do + if [[ "$arg" == "--build" ]]; then + FLAG="--build" + continue + fi + + ARGS+=("$arg") + done + + APP="$1" + GIT_REMOTE="$2" + GIT_REF="$3" + + verify_app_name "$APP" + + if [[ -z "$GIT_REMOTE" ]]; then + dokku_log_fail "Missing GIT_REMOTE parameter" + return + fi + + fn-git-clone "$APP" "$GIT_REMOTE" "$GIT_REF" + if [[ "$FLAG" == "--build" ]]; then + if [[ -n "$GIT_REF" ]]; then + plugn trigger receive-app "$APP" "$GIT_REF" + else + plugn trigger receive-app "$APP" + fi + fi +} + +cmd-git-fetch() { + declare desc="fetch the app at an optional repo object and build the codebase" + local cmd="git:fetch" + [[ "$1" == "$cmd" ]] && shift 1 + declare APP="$1" GIT_REMOTE="$2" GIT_REF="$3" FLAG="$4" + + ARGS=() + for arg in "$@"; do + if [[ "$arg" == "--build" ]]; then + FLAG="--build" + continue + fi + + ARGS+=("$arg") + done + + APP="$1" + GIT_REMOTE="$2" + GIT_REF="$3" + + verify_app_name "$APP" + + if [[ -z "$GIT_REMOTE" ]]; then + dokku_log_fail "Missing GIT_REMOTE parameter" + return + fi + + fn-git-fetch "$APP" "$GIT_REMOTE" "$GIT_REF" + if [[ "$FLAG" == "--build" ]]; then + if [[ -n "$GIT_REF" ]]; then + plugn trigger receive-app "$APP" "$GIT_REF" + else + plugn trigger receive-app "$APP" + fi + fi +} + +cmd-git-public-key() { + declare desc="outputs the dokku public deploy key" + local cmd="git:public-key" + [[ "$1" == "$cmd" ]] && shift 1 + + if [[ ! -f "$DOKKU_ROOT/.ssh/id_rsa.pub" ]]; then + dokku_log_fail "There is no deploy key" + fi + cat "$DOKKU_ROOT/.ssh/id_rsa.pub" +} + cmd-git-report() { declare desc="displays a git report for one or more apps" declare cmd="git:report" @@ -71,6 +167,62 @@ cmd-git-report-single() { fi } +fn-git-clone() { + declare desc="creates an app from remote git repo" + declare APP="$1" GIT_REMOTE="$2" GIT_REF="$2" + local APP_CLONE_ROOT="$PLUGIN_DATA_ROOT/$APP" + local APP_ROOT="$DOKKU_ROOT/$APP" + [[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on" + + if [[ -d "$DOKKU_ROOT/$APP/refs" ]]; then + dokku_log_fail "The clone subcommand can only be executed for new applications" + fi + + if [[ -d "$APP_CLONE_ROOT" ]]; then + dokku_log_fail "Clone in progress" + fi + + if [[ -z "$GIT_REF" ]]; then + GIT_REF="$(fn-git-deploy-branch "$APP")" + fi + + dokku_log_info1_quiet "Creating $APP from $GIT_REMOTE#$GIT_REF" + trap 'rm -rf $APP_CLONE_ROOT > /dev/null' RETURN INT TERM EXIT + git clone --depth 1 -n -qq --branch "$GIT_REF" "$GIT_REMOTE" "$APP_CLONE_ROOT" + + if [[ "$GIT_REF" != "master" ]]; then + pushd "$APP" > /dev/null + git checkout -qq -b master + popd > /dev/null + fi + + rsync -a "$APP_CLONE_ROOT/.git/" "$APP_ROOT" + fn-git-create-hook "$APP" +} + +fn-git-fetch() { + declare APP="$1" GIT_REMOTE="$2" GIT_REF="$3" + local DOKKU_DEPLOY_BRANCH + + DOKKU_DEPLOY_BRANCH="$(fn-git-deploy-branch "$APP")" + if ! git check-ref-format --branch "$DOKKU_DEPLOY_BRANCH" >/dev/null 2>&1; then + echo $'\e[1G\e[K'"-----> WARNING: Invalid branch name '$DOKKU_DEPLOY_BRANCH' specified via DOKKU_DEPLOY_BRANCH." + echo $'\e[1G\e[K'"-----> For more details, please see the man page for 'git-check-ref-format.'" + return + fi + + pushd "$DOKKU_ROOT/$APP" > /dev/null + git remote rm remote > /dev/null 2>&1 || true + git remote add --mirror=fetch --no-tags remote "$GIT_REMOTE" + if [[ -z "$GIT_REF" ]]; then + git fetch remote "$DOKKU_DEPLOY_BRANCH" + else + git fetch remote + git update-ref refs/heads/master "$GIT_REF" + fi + popd > /dev/null +} + fn-git-last-updated-at() { declare desc="retrieve the deploy branch for a given application" declare APP="$1" diff --git a/plugins/git/subcommands/allow-host b/plugins/git/subcommands/allow-host new file mode 100755 index 000000000..e24ce7ffb --- /dev/null +++ b/plugins/git/subcommands/allow-host @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +source "$PLUGIN_AVAILABLE_PATH/git/internal-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +cmd-git-allow-host "$@" diff --git a/plugins/git/subcommands/clone b/plugins/git/subcommands/clone new file mode 100755 index 000000000..35a43602d --- /dev/null +++ b/plugins/git/subcommands/clone @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +source "$PLUGIN_AVAILABLE_PATH/git/internal-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +cmd-git-clone "$@" diff --git a/plugins/git/subcommands/fetch b/plugins/git/subcommands/fetch new file mode 100755 index 000000000..ae2ba387c --- /dev/null +++ b/plugins/git/subcommands/fetch @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +source "$PLUGIN_AVAILABLE_PATH/git/internal-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +cmd-git-fetch "$@" diff --git a/plugins/git/subcommands/public-key b/plugins/git/subcommands/public-key new file mode 100755 index 000000000..11c04bb5e --- /dev/null +++ b/plugins/git/subcommands/public-key @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +source "$PLUGIN_AVAILABLE_PATH/git/internal-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +cmd-git-public-key "$@" From a7ef06028eced4f34d9a3963321bec3b0e3e9226 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 7 Jan 2021 02:53:13 -0500 Subject: [PATCH 02/31] docs: add cavet to git:allow-host command --- docs/deployment/methods/git.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/deployment/methods/git.md b/docs/deployment/methods/git.md index 93fe545eb..cfc0d7441 100644 --- a/docs/deployment/methods/git.md +++ b/docs/deployment/methods/git.md @@ -176,6 +176,8 @@ By default, the Dokku host may not have access to a server containing the remote dokku git:allow-host github.com ``` +Note that this command is currently not idempotent and may add duplicate entries to the `~dokku/.ssh/known_hosts` file. + ### Verifying the cloning public key In order to clone a remote repository, the remote server should have the Dokku host's public key configured. This plugin does not currently create this key, but if there is one available, it can be shown via the `git:public-key` command. From 8c0380beece85008a56cdc50c8fc1646f250d849 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 7 Jan 2021 02:53:44 -0500 Subject: [PATCH 03/31] tests: add missing `run` statement --- tests/unit/git_1.bats | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/git_1.bats b/tests/unit/git_1.bats index 63bf3714b..cb49ea9a1 100644 --- a/tests/unit/git_1.bats +++ b/tests/unit/git_1.bats @@ -27,7 +27,7 @@ teardown() { } @test "(git) ensure GIT_REV env var is set" { - deploy_app + run deploy_app echo "output: $output" echo "status: $status" assert_success @@ -44,7 +44,7 @@ teardown() { echo "status: $status" assert_success - deploy_app + run deploy_app echo "output: $output" echo "status: $status" assert_success @@ -61,7 +61,7 @@ teardown() { echo "status: $status" assert_success - deploy_app + run deploy_app echo "output: $output" echo "status: $status" assert_success From 1a66102e3043c3e0f5540c0527fb96982c5eeaff Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 7 Jan 2021 02:54:10 -0500 Subject: [PATCH 04/31] feat: add tests for allow-host and public-key commands --- tests/unit/git_3.bats | 77 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 tests/unit/git_3.bats diff --git a/tests/unit/git_3.bats b/tests/unit/git_3.bats new file mode 100644 index 000000000..dd6a99b5f --- /dev/null +++ b/tests/unit/git_3.bats @@ -0,0 +1,77 @@ +#!/usr/bin/env bats + +load test_helper + +setup() { + global_setup + touch /home/dokku/.ssh/known_hosts + chown dokku:dokku /home/dokku/.ssh/known_hosts +} + +teardown() { + rm -f /home/dokku/.ssh/id_rsa.pub || true + destroy_app + global_teardown +} + +@test "(git) git:allow-host" { + run /bin/bash -c "dokku git:allow-host" + echo "output: $output" + echo "status: $status" + assert_failure + + run /bin/bash -c "cat /home/dokku/.ssh/known_hosts | wc -l" + echo "output: $output" + echo "status: $status" + assert_success + start_lines=$output + + run /bin/bash -c "dokku git:allow-host github.com" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "cat /home/dokku/.ssh/known_hosts | wc -l" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "test -f /home/dokku/.ssh/known_hosts" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "cat /home/dokku/.ssh/known_hosts | wc -l" + echo "output: $output" + echo "status: $status" + assert_success + assert_equal "$output" "$((start_lines + 1))" + + run /bin/bash -c "dokku git:allow-host github.com" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "cat /home/dokku/.ssh/known_hosts | wc -l" + echo "output: $output" + echo "status: $status" + assert_success + assert_equal "$output" "$((start_lines + 2))" +} + +@test "(git) git:public-key" { + run /bin/bash -c "dokku git:public-key" + echo "output: $output" + echo "status: $status" + assert_failure + + run /bin/bash -c "cp /root/.ssh/dokku_test_rsa.pub /home/dokku/.ssh/id_rsa.pub" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:public-key" + echo "output: $output" + echo "status: $status" + assert_success +} From 31332a5061de2572445d6974d1ca92f01dc85ebe Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 7 Jan 2021 05:04:11 -0500 Subject: [PATCH 05/31] fix: move fn-git-create-hook to internal-functions Its unused elsewhere. --- plugins/git/functions | 19 ------------------- plugins/git/internal-functions | 19 +++++++++++++++++++ plugins/git/post-create | 2 +- plugins/git/subcommands/initialize | 2 +- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/plugins/git/functions b/plugins/git/functions index 67222785e..8d669ad84 100755 --- a/plugins/git/functions +++ b/plugins/git/functions @@ -234,22 +234,3 @@ cmd-git-glob() { fi git-shell -c "$args" } - -fn-git-create-hook() { - declare APP="$1" - local APP_PATH="$DOKKU_ROOT/$APP" - local PRERECEIVE_HOOK="$APP_PATH/hooks/pre-receive" - - if [[ ! -d "$APP_PATH/refs" ]]; then - git init --bare "$APP_PATH" >/dev/null - fi - - cat >"$PRERECEIVE_HOOK" </dev/null + fi + + cat >"$PRERECEIVE_HOOK" < Date: Thu, 7 Jan 2021 05:04:34 -0500 Subject: [PATCH 06/31] chore: reorder properties --- plugins/git/post-app-clone-setup | 2 +- plugins/git/post-app-rename-setup | 2 +- plugins/git/post-delete | 2 +- plugins/git/receive-app | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/git/post-app-clone-setup b/plugins/git/post-app-clone-setup index b0d6a21c0..9faa302bb 100755 --- a/plugins/git/post-app-clone-setup +++ b/plugins/git/post-app-clone-setup @@ -1,7 +1,7 @@ #!/usr/bin/env bash +source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions" set -eo pipefail [[ $DOKKU_TRACE ]] && set -x -source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions" trigger-git-post-app-clone-setup() { declare desc="modifies git-hook" diff --git a/plugins/git/post-app-rename-setup b/plugins/git/post-app-rename-setup index 5ecec4cdb..937c39c76 100755 --- a/plugins/git/post-app-rename-setup +++ b/plugins/git/post-app-rename-setup @@ -1,7 +1,7 @@ #!/usr/bin/env bash +source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions" set -eo pipefail [[ $DOKKU_TRACE ]] && set -x -source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions" trigger-git-post-app-rename-setup() { declare desc="modifies git-hook" diff --git a/plugins/git/post-delete b/plugins/git/post-delete index 9caa12790..dad74439a 100755 --- a/plugins/git/post-delete +++ b/plugins/git/post-delete @@ -1,7 +1,7 @@ #!/usr/bin/env bash +source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions" set -eo pipefail [[ $DOKKU_TRACE ]] && set -x -source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions" trigger-git-post-delete() { declare desc="destroys the git properties for a given app" diff --git a/plugins/git/receive-app b/plugins/git/receive-app index 9a2f2de6e..1c09982b4 100755 --- a/plugins/git/receive-app +++ b/plugins/git/receive-app @@ -1,6 +1,6 @@ #!/usr/bin/env bash +source "$PLUGIN_AVAILABLE_PATH/git/functions" set -eo pipefail [[ $DOKKU_TRACE ]] && set -x -source "$PLUGIN_AVAILABLE_PATH/git/functions" git_receive_app "$@" From f87525d8cfea695e63750c172e62f491c9262029 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 7 Jan 2021 05:05:27 -0500 Subject: [PATCH 07/31] refactor: move temp directory to git data directory --- plugins/git/install | 3 +++ plugins/git/internal-functions | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/plugins/git/install b/plugins/git/install index bb13e8ef7..8e6cdbc1c 100755 --- a/plugins/git/install +++ b/plugins/git/install @@ -9,6 +9,9 @@ trigger-git-install() { declare desc="installs the git plugin" declare trigger="install" + mkdir -p "${DOKKU_LIB_ROOT}/data/git" + chown -R "${DOKKU_SYSTEM_USER}:${DOKKU_SYSTEM_GROUP}" "${DOKKU_LIB_ROOT}/data/git" + fn-plugin-property-setup "git" migrate_git_vars_0_12_0 "$@" } diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index ec1b141bf..adaf04402 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -189,7 +189,7 @@ EOF fn-git-clone() { declare desc="creates an app from remote git repo" declare APP="$1" GIT_REMOTE="$2" GIT_REF="$2" - local APP_CLONE_ROOT="$PLUGIN_DATA_ROOT/$APP" + local APP_CLONE_ROOT="$DOKKU_LIB_ROOT/data/git/$APP" local APP_ROOT="$DOKKU_ROOT/$APP" [[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on" From c9fc20359649888867bb649ab2bdbe111a6e7555 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 7 Jan 2021 05:05:47 -0500 Subject: [PATCH 08/31] fix: properly parse arguments --- plugins/git/internal-functions | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index adaf04402..d7c9297a4 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -19,7 +19,7 @@ cmd-git-clone() { declare desc="creates an app from remote git repo" local cmd="git:clone" [[ "$1" == "$cmd" ]] && shift 1 - declare APP="$1" GIT_REMOTE="$2" GIT_REF="$3" FLAG="$4" + declare APP GIT_REMOTE GIT_REF FLAG ARGS=() for arg in "$@"; do @@ -31,9 +31,9 @@ cmd-git-clone() { ARGS+=("$arg") done - APP="$1" - GIT_REMOTE="$2" - GIT_REF="$3" + APP="${ARGS[0]}" + GIT_REMOTE="${ARGS[1]}" + GIT_REF="${ARGS[2]}" verify_app_name "$APP" @@ -56,7 +56,7 @@ cmd-git-fetch() { declare desc="fetch the app at an optional repo object and build the codebase" local cmd="git:fetch" [[ "$1" == "$cmd" ]] && shift 1 - declare APP="$1" GIT_REMOTE="$2" GIT_REF="$3" FLAG="$4" + declare APP GIT_REMOTE GIT_REF FLAG ARGS=() for arg in "$@"; do @@ -68,9 +68,9 @@ cmd-git-fetch() { ARGS+=("$arg") done - APP="$1" - GIT_REMOTE="$2" - GIT_REF="$3" + APP="${ARGS[0]}" + GIT_REMOTE="${ARGS[1]}" + GIT_REF="${ARGS[2]}" verify_app_name "$APP" From 87a786fec5c9b8725b31c3235795e86907288783 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 7 Jan 2021 05:05:56 -0500 Subject: [PATCH 09/31] fix: get arguments correctly --- plugins/git/internal-functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index d7c9297a4..c3f4f6a6d 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -188,7 +188,7 @@ EOF fn-git-clone() { declare desc="creates an app from remote git repo" - declare APP="$1" GIT_REMOTE="$2" GIT_REF="$2" + declare APP="$1" GIT_REMOTE="$2" GIT_REF="$3" local APP_CLONE_ROOT="$DOKKU_LIB_ROOT/data/git/$APP" local APP_ROOT="$DOKKU_ROOT/$APP" [[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on" From 5e645e1489394ff05eb98c31ff9445ff7de007e4 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 7 Jan 2021 05:06:11 -0500 Subject: [PATCH 10/31] fix: use correct method to detect if a repo is empty --- plugins/git/internal-functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index c3f4f6a6d..d9156e7bc 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -193,7 +193,7 @@ fn-git-clone() { local APP_ROOT="$DOKKU_ROOT/$APP" [[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on" - if [[ -d "$DOKKU_ROOT/$APP/refs" ]]; then + if [[ "$(git -C "$APP_ROOT" count-objects)" != "0 objects, 0 kilobytes" ]]; then dokku_log_fail "The clone subcommand can only be executed for new applications" fi From 6dc55624bd87fc9d8fce0608b372f35d305dc644 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 7 Jan 2021 05:06:22 -0500 Subject: [PATCH 11/31] chore: change message --- plugins/git/internal-functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index d9156e7bc..35a236015 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -205,7 +205,7 @@ fn-git-clone() { GIT_REF="$(fn-git-deploy-branch "$APP")" fi - dokku_log_info1_quiet "Creating $APP from $GIT_REMOTE#$GIT_REF" + dokku_log_info1_quiet "Cloning $APP from $GIT_REMOTE#$GIT_REF" trap 'rm -rf $APP_CLONE_ROOT > /dev/null' RETURN INT TERM EXIT git clone --depth 1 -n -qq --branch "$GIT_REF" "$GIT_REMOTE" "$APP_CLONE_ROOT" From dbd48356f5a46aa61936afe89fb9267278651079 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 7 Jan 2021 05:07:35 -0500 Subject: [PATCH 12/31] refactor: properly handle commits and branches - if it is a commit, we must clone and checkout. `clone --branch` only works for tags and branches - set the branch as the deploy-branch if detected --- plugins/git/internal-functions | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index 35a236015..3188270c7 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -207,12 +207,25 @@ fn-git-clone() { dokku_log_info1_quiet "Cloning $APP from $GIT_REMOTE#$GIT_REF" trap 'rm -rf $APP_CLONE_ROOT > /dev/null' RETURN INT TERM EXIT - git clone --depth 1 -n -qq --branch "$GIT_REF" "$GIT_REMOTE" "$APP_CLONE_ROOT" - if [[ "$GIT_REF" != "master" ]]; then - pushd "$APP" > /dev/null - git checkout -qq -b master - popd > /dev/null + is_ref=true + git clone --depth 1 -n -qq --branch "$GIT_REF" "$GIT_REMOTE" "$APP_CLONE_ROOT" 2>/dev/null && is_ref=false + rm -rf "$APP_CLONE_ROOT" + + if [[ "$is_ref" == "true" ]]; then + git clone -n -qq "$GIT_REMOTE" "$APP_CLONE_ROOT" 2>/dev/null + git -C "$APP_CLONE_ROOT" checkout -qq "$GIT_REF" + else + git clone -n -qq --branch "$GIT_REF" "$GIT_REMOTE" "$APP_CLONE_ROOT" 2>/dev/null + if git -C "$APP_CLONE_ROOT" show-ref --verify "refs/heads/$GIT_REF" >/dev/null 2>&1; then + dokku_log_verbose "Detected branch, setting deploy-branch to $GIT_REF" + fn-plugin-property-write "git" "$APP" "deploy-branch" "$GIT_REF" + fi + fi + + DOKKU_DEPLOY_BRANCH="$(fn-git-deploy-branch "$APP")" + if [[ "$GIT_REF" != "$DOKKU_DEPLOY_BRANCH" ]]; then + git -C "$APP_CLONE_ROOT" checkout -qq -b "$DOKKU_DEPLOY_BRANCH" fi rsync -a "$APP_CLONE_ROOT/.git/" "$APP_ROOT" From 279a706f78e5fdd318f30da86d656b19f44e50d4 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 7 Jan 2021 05:17:09 -0500 Subject: [PATCH 13/31] feat: add tests for git:clone --- tests/unit/git_3.bats | 138 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) diff --git a/tests/unit/git_3.bats b/tests/unit/git_3.bats index dd6a99b5f..e2a47592b 100644 --- a/tests/unit/git_3.bats +++ b/tests/unit/git_3.bats @@ -59,6 +59,144 @@ teardown() { assert_equal "$output" "$((start_lines + 2))" } +@test "(git) git:clone" { + run /bin/bash -c "dokku git:clone" + echo "output: $output" + echo "status: $status" + assert_failure + + run /bin/bash -c "dokku git:clone $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_failure + + run create_app + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:clone $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_failure + + run destroy_app + echo "output: $output" + echo "status: $status" + assert_success + run create_app + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/heroku/node-js-getting-started.git" + echo "output: $output" + echo "status: $status" + assert_success + + run destroy_app + echo "output: $output" + echo "status: $status" + assert_success + run create_app + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/heroku/node-js-getting-started.git main" + echo "output: $output" + echo "status: $status" + assert_success + + run destroy_app + echo "output: $output" + echo "status: $status" + assert_success + run create_app + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/heroku/node-js-getting-started.git 1" + echo "output: $output" + echo "status: $status" + assert_success + + run destroy_app + echo "output: $output" + echo "status: $status" + assert_success + run create_app + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/heroku/node-js-getting-started.git 97e6c72491c7531507bfc5413903e0e00e31e1b0" + echo "output: $output" + echo "status: $status" + assert_success + + run destroy_app + echo "output: $output" + echo "status: $status" + assert_success + run create_app + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/heroku/node-js-getting-started.git" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "Application deployed" + + run destroy_app + echo "output: $output" + echo "status: $status" + assert_success + run create_app + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/heroku/node-js-getting-started.git main" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "Application deployed" + + run destroy_app + echo "output: $output" + echo "status: $status" + assert_success + run create_app + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/heroku/node-js-getting-started.git 1" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "Application deployed" + + run destroy_app + echo "output: $output" + echo "status: $status" + assert_success + run create_app + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/heroku/node-js-getting-started.git 97e6c72491c7531507bfc5413903e0e00e31e1b0" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "Application deployed" +} + @test "(git) git:public-key" { run /bin/bash -c "dokku git:public-key" echo "output: $output" From d7282376976d279d030462e27789d7e713bbd376 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Fri, 8 Jan 2021 03:26:37 -0500 Subject: [PATCH 14/31] chore: split tests --- tests/unit/git_3.bats | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/tests/unit/git_3.bats b/tests/unit/git_3.bats index e2a47592b..1c3504f01 100644 --- a/tests/unit/git_3.bats +++ b/tests/unit/git_3.bats @@ -59,7 +59,7 @@ teardown() { assert_equal "$output" "$((start_lines + 2))" } -@test "(git) git:clone" { +@test "(git) git:clone [errors]" { run /bin/bash -c "dokku git:clone" echo "output: $output" echo "status: $status" @@ -79,11 +79,9 @@ teardown() { echo "output: $output" echo "status: $status" assert_failure +} - run destroy_app - echo "output: $output" - echo "status: $status" - assert_success +@test "(git) git:clone [--no-build]" { run create_app echo "output: $output" echo "status: $status" @@ -135,16 +133,9 @@ teardown() { echo "output: $output" echo "status: $status" assert_success +} - run destroy_app - echo "output: $output" - echo "status: $status" - assert_success - run create_app - echo "output: $output" - echo "status: $status" - assert_success - +@test "(git) git:clone [--build]" { run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/heroku/node-js-getting-started.git" echo "output: $output" echo "status: $status" From 6bfbe0bb666ec1b1f4885beb12a7b5570d6b1d2c Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Fri, 8 Jan 2021 03:37:01 -0500 Subject: [PATCH 15/31] fix: create app for build test --- tests/unit/git_3.bats | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/unit/git_3.bats b/tests/unit/git_3.bats index 1c3504f01..7d512f3fb 100644 --- a/tests/unit/git_3.bats +++ b/tests/unit/git_3.bats @@ -136,6 +136,11 @@ teardown() { } @test "(git) git:clone [--build]" { + run create_app + echo "output: $output" + echo "status: $status" + assert_success + run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/heroku/node-js-getting-started.git" echo "output: $output" echo "status: $status" From e877a996be107959f2e065719a565f2a11c5cc01 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Fri, 8 Jan 2021 03:58:09 -0500 Subject: [PATCH 16/31] tests: split out tests and use a slimer smoke-test-app to speed up builds --- tests/unit/git_3.bats | 66 ++++++++++++------------------------------- 1 file changed, 18 insertions(+), 48 deletions(-) diff --git a/tests/unit/git_3.bats b/tests/unit/git_3.bats index 7d512f3fb..f67ee650d 100644 --- a/tests/unit/git_3.bats +++ b/tests/unit/git_3.bats @@ -4,6 +4,7 @@ load test_helper setup() { global_setup + create_app touch /home/dokku/.ssh/known_hosts chown dokku:dokku /home/dokku/.ssh/known_hosts } @@ -65,7 +66,7 @@ teardown() { echo "status: $status" assert_failure - run /bin/bash -c "dokku git:clone $TEST_APP" + run /bin/bash -c "dokku git:clone $TEST_APP-non-existent" echo "output: $output" echo "status: $status" assert_failure @@ -75,19 +76,14 @@ teardown() { echo "status: $status" assert_success - run /bin/bash -c "dokku git:clone $TEST_APP" + run /bin/bash -c "dokku git:clone $TEST_APP-non-existent" echo "output: $output" echo "status: $status" assert_failure } @test "(git) git:clone [--no-build]" { - run create_app - echo "output: $output" - echo "status: $status" - assert_success - - run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/heroku/node-js-getting-started.git" + run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git" echo "output: $output" echo "status: $status" assert_success @@ -101,7 +97,7 @@ teardown() { echo "status: $status" assert_success - run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/heroku/node-js-getting-started.git main" + run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git other-branch" echo "output: $output" echo "status: $status" assert_success @@ -115,7 +111,7 @@ teardown() { echo "status: $status" assert_success - run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/heroku/node-js-getting-started.git 1" + run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" echo "output: $output" echo "status: $status" assert_success @@ -129,64 +125,38 @@ teardown() { echo "status: $status" assert_success - run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/heroku/node-js-getting-started.git 97e6c72491c7531507bfc5413903e0e00e31e1b0" + run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 9cf71bba639c4f1671dfd42685338b762d3354f2" echo "output: $output" echo "status: $status" assert_success } -@test "(git) git:clone [--build]" { - run create_app - echo "output: $output" - echo "status: $status" - assert_success - - run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/heroku/node-js-getting-started.git" +@test "(git) git:clone [--build noarg]" { + run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/dokku/smoke-test-app.git" echo "output: $output" echo "status: $status" assert_success assert_output_contains "Application deployed" +} - run destroy_app - echo "output: $output" - echo "status: $status" - assert_success - run create_app - echo "output: $output" - echo "status: $status" - assert_success - - run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/heroku/node-js-getting-started.git main" +@test "(git) git:clone [--build branch]" { + run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/dokku/smoke-test-app.git other-branch" echo "output: $output" echo "status: $status" assert_success assert_output_contains "Application deployed" +} - run destroy_app - echo "output: $output" - echo "status: $status" - assert_success - run create_app - echo "output: $output" - echo "status: $status" - assert_success - - run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/heroku/node-js-getting-started.git 1" +@test "(git) git:clone [--build tag]" { + run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" echo "output: $output" echo "status: $status" assert_success assert_output_contains "Application deployed" +} - run destroy_app - echo "output: $output" - echo "status: $status" - assert_success - run create_app - echo "output: $output" - echo "status: $status" - assert_success - - run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/heroku/node-js-getting-started.git 97e6c72491c7531507bfc5413903e0e00e31e1b0" +@test "(git) git:clone [--build commit]" { + run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/dokku/smoke-test-app.git 9cf71bba639c4f1671dfd42685338b762d3354f2" echo "output: $output" echo "status: $status" assert_success From 89e92749a63ba12563e68d761d7c189209b69f8c Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Fri, 8 Jan 2021 04:04:17 -0500 Subject: [PATCH 17/31] tests: handle non-existent app --- tests/unit/git_3.bats | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/unit/git_3.bats b/tests/unit/git_3.bats index f67ee650d..08720a51a 100644 --- a/tests/unit/git_3.bats +++ b/tests/unit/git_3.bats @@ -71,7 +71,7 @@ teardown() { echo "status: $status" assert_failure - run create_app + run create_app "$TEST_APP-non-existent" echo "output: $output" echo "status: $status" assert_success @@ -80,6 +80,11 @@ teardown() { echo "output: $output" echo "status: $status" assert_failure + + run destroy_app 0 "$TEST_APP-non-existent" + echo "output: $output" + echo "status: $status" + assert_success } @test "(git) git:clone [--no-build]" { From 827dbbc740598aa4ee6be3b74f1dbba175b061f3 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Fri, 8 Jan 2021 10:28:23 -0500 Subject: [PATCH 18/31] fix: delete the deploy branch if its a ref and not tag --- plugins/git/internal-functions | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index 3188270c7..26f551327 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -225,6 +225,9 @@ fn-git-clone() { DOKKU_DEPLOY_BRANCH="$(fn-git-deploy-branch "$APP")" if [[ "$GIT_REF" != "$DOKKU_DEPLOY_BRANCH" ]]; then + if [[ "$is_ref" == "true" ]]; then + git -C "$APP_CLONE_ROOT" branch -qq -D "$DOKKU_DEPLOY_BRANCH" 2>/dev/null || true + fi git -C "$APP_CLONE_ROOT" checkout -qq -b "$DOKKU_DEPLOY_BRANCH" fi From 97750f8b28e78e95dc9cc7f7bd9ef374b3df8a12 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 9 Jan 2021 06:54:27 -0500 Subject: [PATCH 19/31] chore: minor updates to tests --- tests/unit/git_3.bats | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/unit/git_3.bats b/tests/unit/git_3.bats index 08720a51a..714990bed 100644 --- a/tests/unit/git_3.bats +++ b/tests/unit/git_3.bats @@ -87,7 +87,7 @@ teardown() { assert_success } -@test "(git) git:clone [--no-build]" { +@test "(git) git:clone [--no-build noarg]" { run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git" echo "output: $output" echo "status: $status" @@ -102,7 +102,7 @@ teardown() { echo "status: $status" assert_success - run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git other-branch" + run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git another-branch" echo "output: $output" echo "status: $status" assert_success @@ -130,7 +130,7 @@ teardown() { echo "status: $status" assert_success - run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 9cf71bba639c4f1671dfd42685338b762d3354f2" + run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 5c8a5e42bbd7fae98bd657fb17f41c6019b303f9" echo "output: $output" echo "status: $status" assert_success @@ -145,7 +145,7 @@ teardown() { } @test "(git) git:clone [--build branch]" { - run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/dokku/smoke-test-app.git other-branch" + run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/dokku/smoke-test-app.git another-branch" echo "output: $output" echo "status: $status" assert_success @@ -161,7 +161,7 @@ teardown() { } @test "(git) git:clone [--build commit]" { - run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/dokku/smoke-test-app.git 9cf71bba639c4f1671dfd42685338b762d3354f2" + run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/dokku/smoke-test-app.git 5c8a5e42bbd7fae98bd657fb17f41c6019b303f9" echo "output: $output" echo "status: $status" assert_success From f73c9275127b4165d7988c02cfe33adc4f78728d Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 9 Jan 2021 06:54:59 -0500 Subject: [PATCH 20/31] fix: do not allow fetch on fresh apps --- plugins/git/internal-functions | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index 26f551327..29b98563e 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -238,6 +238,11 @@ fn-git-clone() { fn-git-fetch() { declare APP="$1" GIT_REMOTE="$2" GIT_REF="$3" local DOKKU_DEPLOY_BRANCH + local APP_ROOT="$DOKKU_ROOT/$APP" + + if [[ "$(git -C "$APP_ROOT" count-objects)" == "0 objects, 0 kilobytes" ]]; then + dokku_log_fail "The fetch subcommand can only be executed for existing applications" + fi DOKKU_DEPLOY_BRANCH="$(fn-git-deploy-branch "$APP")" if ! git check-ref-format --branch "$DOKKU_DEPLOY_BRANCH" >/dev/null 2>&1; then From 97b33a9138faab50635f69d88cc216bcf6036b7f Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 9 Jan 2021 06:55:43 -0500 Subject: [PATCH 21/31] feat: fix: use correct branch for ref updating --- plugins/git/internal-functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index 29b98563e..d271fbf33 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -258,7 +258,7 @@ fn-git-fetch() { git fetch remote "$DOKKU_DEPLOY_BRANCH" else git fetch remote - git update-ref refs/heads/master "$GIT_REF" + git update-ref "refs/heads/$DOKKU_DEPLOY_BRANCH" "$GIT_REF" fi popd > /dev/null } From e3d0f693a73beb88bda5c74e99cee438dfafac03 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 9 Jan 2021 06:55:48 -0500 Subject: [PATCH 22/31] tests: add tests for fetch --- tests/unit/git_3.bats | 127 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/tests/unit/git_3.bats b/tests/unit/git_3.bats index 714990bed..e6cc96ae5 100644 --- a/tests/unit/git_3.bats +++ b/tests/unit/git_3.bats @@ -168,6 +168,133 @@ teardown() { assert_output_contains "Application deployed" } +@test "(git) git:fetch [errors]" { + run /bin/bash -c "dokku git:fetch" + echo "output: $output" + echo "status: $status" + assert_failure + + run /bin/bash -c "dokku git:fetch $TEST_APP-non-existent" + echo "output: $output" + echo "status: $status" + assert_failure + + run create_app "$TEST_APP-non-existent" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:fetch $TEST_APP-non-existent" + echo "output: $output" + echo "status: $status" + assert_failure + + run destroy_app 0 "$TEST_APP-non-existent" + echo "output: $output" + echo "status: $status" + assert_success +} + +@test "(git) git:fetch [--no-build noarg]" { + run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:fetch $TEST_APP https://github.com/dokku/smoke-test-app.git" + echo "output: $output" + echo "status: $status" + assert_success +} + +@test "(git) git:fetch [--no-build branch]" { + run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:fetch $TEST_APP https://github.com/dokku/smoke-test-app.git another-branch" + echo "output: $output" + echo "status: $status" + assert_success +} + +@test "(git) git:fetch [--no-build tag]" { + run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:fetch $TEST_APP https://github.com/dokku/smoke-test-app.git 2.0.0" + echo "output: $output" + echo "status: $status" + assert_success +} + +@test "(git) git:fetch [--no-build commit]" { + run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:fetch $TEST_APP https://github.com/dokku/smoke-test-app.git 5c8a5e42bbd7fae98bd657fb17f41c6019b303f9" + echo "output: $output" + echo "status: $status" + assert_success +} + +@test "(git) git:fetch [--build noarg]" { + run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:fetch --build $TEST_APP https://github.com/dokku/smoke-test-app.git" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "Application deployed" +} + +@test "(git) git:fetch [--build branch]" { + run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 2.0.0" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:fetch --build $TEST_APP https://github.com/dokku/smoke-test-app.git another-branch" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "Application deployed" +} + +@test "(git) git:fetch [--build tag]" { + run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:fetch --build $TEST_APP https://github.com/dokku/smoke-test-app.git 2.0.0" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "Application deployed" +} + +@test "(git) git:fetch [--build commit]" { + run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:fetch --build $TEST_APP https://github.com/dokku/smoke-test-app.git 5c8a5e42bbd7fae98bd657fb17f41c6019b303f9" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "Application deployed" +} + @test "(git) git:public-key" { run /bin/bash -c "dokku git:public-key" echo "output: $output" From 3f25514e1ad7a780e2aa87c4249e305146911267 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 9 Jan 2021 07:21:44 -0500 Subject: [PATCH 23/31] refactor: combine both clone and sync commands Using a single command will allow users to not have to worry about the workflow they use to setup or update a given app. --- docs/deployment/methods/git.md | 48 ++------ plugins/git/help-functions | 3 +- plugins/git/internal-functions | 47 ++------ plugins/git/subcommands/fetch | 6 - plugins/git/subcommands/{clone => sync} | 2 +- tests/unit/git_3.bats | 147 ++++++++++-------------- 6 files changed, 82 insertions(+), 171 deletions(-) delete mode 100755 plugins/git/subcommands/fetch rename plugins/git/subcommands/{clone => sync} (86%) diff --git a/docs/deployment/methods/git.md b/docs/deployment/methods/git.md index cfc0d7441..061c3c926 100644 --- a/docs/deployment/methods/git.md +++ b/docs/deployment/methods/git.md @@ -4,8 +4,7 @@ ``` git:allow-host # Adds a host to known_hosts -git:clone [--build] [] # Creates an app from remote git repo -git:fetch [--build] [] # Fetch the app at an optional repo object and build the codebase +git:sync [--build] [] # Clone or fetch an app from remote git repo git:initialize # Initialize a git repository for an app git:public-key # Outputs the dokku public deploy key git:report [] [] # Displays a git report for one or more apps @@ -114,58 +113,29 @@ dokku git:set node-js-app keep-git-dir "" > The application must exist before the repository can be initialized -A Dokku app repository can be initialized from a remote git repository via the `git:clone` command. This command should only be used for an initial sync to a remote repository as it may wipe out any local Dokku history that isn't in sync with the remote. Any repository that can be cloned by the `dokku` user can be specified. +A Dokku app repository can be initialized or updated from a remote git repository via the `git:sync` command. This command will either clone or fetch updates from a remote repository and has undefined behavior if the history cannot be fast-fowarded to the referenced repository reference. Any repository that can be cloned by the `dokku` user can be specified. ```shell -dokku git:clone node-js-app https://github.com/heroku/node-js-getting-started.git +dokku git:sync node-js-app https://github.com/heroku/node-js-getting-started.git ``` -The `git:clone` command optionally takes an optional third parameter containing a git reference, which may be a branch, tag, or specific commit. +The `git:sync` command optionally takes an optional third parameter containing a git reference, which may be a branch, tag, or specific commit. ```shell # specify a branch -dokku git:clone node-js-app https://github.com/heroku/node-js-getting-started.git main +dokku git:sync node-js-app https://github.com/heroku/node-js-getting-started.git main # specify a tag -dokku git:clone node-js-app https://github.com/heroku/node-js-getting-started.git 1 +dokku git:sync node-js-app https://github.com/heroku/node-js-getting-started.git 1 # specify a commit -dokku git:clone node-js-app https://github.com/heroku/node-js-getting-started.git 97e6c72491c7531507bfc5413903e0e00e31e1b0 +dokku git:sync node-js-app https://github.com/heroku/node-js-getting-started.git 97e6c72491c7531507bfc5413903e0e00e31e1b0 ``` -By default, this command does not trigger an application build. To do so during a `git:clone`, specify the `--build` flag. +By default, this command does not trigger an application build. To do so during a `git:sync`, specify the `--build` flag. ```shell -dokku git:clone --build node-js-app https://github.com/heroku/node-js-getting-started.git -``` - -### Fetching repository updates from a remote repository - -> The application must exist before the repository can be initialized - -A Dokku app repository can be updated from a remote git repository via the `git:fetch` command. This command may fail if the specified remote repository cannot be synced from the current Dokku repository state. Any repository that can be cloned by the `dokku` user can be specified. - -```shell -dokku git:fetch node-js-app https://github.com/heroku/node-js-getting-started.git -``` - -The `git:fetch` command optionally takes an optional third parameter containing a git reference, which may be a branch, tag, or specific commit. - -```shell -# specify a branch -dokku git:fetch node-js-app https://github.com/heroku/node-js-getting-started.git main - -# specify a tag -dokku git:fetch node-js-app https://github.com/heroku/node-js-getting-started.git 1 - -# specify a commit -dokku git:fetch node-js-app https://github.com/heroku/node-js-getting-started.git 97e6c72491c7531507bfc5413903e0e00e31e1b0 -``` - -By default, this command does not trigger an application build. To do so during a `git:fetch`, specify the `--build` flag. - -```shell -dokku git:fetch --build node-js-app https://github.com/heroku/node-js-getting-started.git +dokku git:sync --build node-js-app https://github.com/heroku/node-js-getting-started.git ``` ### Allowing remote repository hosts diff --git a/plugins/git/help-functions b/plugins/git/help-functions index 60153af2f..72d075bf3 100755 --- a/plugins/git/help-functions +++ b/plugins/git/help-functions @@ -28,8 +28,7 @@ fn-help-content() { declare desc="return help content" cat <, Adds a host to known_hosts - git:clone [--build] [], Creates an app from remote git repo - git:fetch [--build] [], Fetch the app at an optional repo object and build the codebase + git:sync [--build] [], Clone or fetch an app from remote git repo git:initialize , Initialize a git repository for an app git:public-key, Outputs the dokku public deploy key git:report [] [], Displays a git report for one or more apps diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index d271fbf33..5f85a3c1c 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -15,9 +15,9 @@ cmd-git-allow-host() { dokku_log_info1 "$HOST added to known hosts" } -cmd-git-clone() { - declare desc="creates an app from remote git repo" - local cmd="git:clone" +cmd-git-sync() { + declare desc="clone or fetch an app from remote git repo" + local cmd="git:sync" [[ "$1" == "$cmd" ]] && shift 1 declare APP GIT_REMOTE GIT_REF FLAG @@ -42,44 +42,13 @@ cmd-git-clone() { return fi - fn-git-clone "$APP" "$GIT_REMOTE" "$GIT_REF" - if [[ "$FLAG" == "--build" ]]; then - if [[ -n "$GIT_REF" ]]; then - plugn trigger receive-app "$APP" "$GIT_REF" - else - plugn trigger receive-app "$APP" - fi - fi -} - -cmd-git-fetch() { - declare desc="fetch the app at an optional repo object and build the codebase" - local cmd="git:fetch" - [[ "$1" == "$cmd" ]] && shift 1 - declare APP GIT_REMOTE GIT_REF FLAG - - ARGS=() - for arg in "$@"; do - if [[ "$arg" == "--build" ]]; then - FLAG="--build" - continue - fi - - ARGS+=("$arg") - done - - APP="${ARGS[0]}" - GIT_REMOTE="${ARGS[1]}" - GIT_REF="${ARGS[2]}" - - verify_app_name "$APP" - - if [[ -z "$GIT_REMOTE" ]]; then - dokku_log_fail "Missing GIT_REMOTE parameter" - return + local APP_ROOT="$DOKKU_ROOT/$APP" + if [[ "$(git -C "$APP_ROOT" count-objects)" == "0 objects, 0 kilobytes" ]]; then + fn-git-clone "$APP" "$GIT_REMOTE" "$GIT_REF" + else + fn-git-fetch "$APP" "$GIT_REMOTE" "$GIT_REF" fi - fn-git-fetch "$APP" "$GIT_REMOTE" "$GIT_REF" if [[ "$FLAG" == "--build" ]]; then if [[ -n "$GIT_REF" ]]; then plugn trigger receive-app "$APP" "$GIT_REF" diff --git a/plugins/git/subcommands/fetch b/plugins/git/subcommands/fetch deleted file mode 100755 index ae2ba387c..000000000 --- a/plugins/git/subcommands/fetch +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env bash -source "$PLUGIN_AVAILABLE_PATH/git/internal-functions" -set -eo pipefail -[[ $DOKKU_TRACE ]] && set -x - -cmd-git-fetch "$@" diff --git a/plugins/git/subcommands/clone b/plugins/git/subcommands/sync similarity index 86% rename from plugins/git/subcommands/clone rename to plugins/git/subcommands/sync index 35a43602d..3bc9c92c3 100755 --- a/plugins/git/subcommands/clone +++ b/plugins/git/subcommands/sync @@ -3,4 +3,4 @@ source "$PLUGIN_AVAILABLE_PATH/git/internal-functions" set -eo pipefail [[ $DOKKU_TRACE ]] && set -x -cmd-git-clone "$@" +cmd-git-sync "$@" diff --git a/tests/unit/git_3.bats b/tests/unit/git_3.bats index e6cc96ae5..7586b0b5a 100644 --- a/tests/unit/git_3.bats +++ b/tests/unit/git_3.bats @@ -60,13 +60,13 @@ teardown() { assert_equal "$output" "$((start_lines + 2))" } -@test "(git) git:clone [errors]" { - run /bin/bash -c "dokku git:clone" +@test "(git) git:sync new [errors]" { + run /bin/bash -c "dokku git:sync" echo "output: $output" echo "status: $status" assert_failure - run /bin/bash -c "dokku git:clone $TEST_APP-non-existent" + run /bin/bash -c "dokku git:sync $TEST_APP-non-existent" echo "output: $output" echo "status: $status" assert_failure @@ -76,7 +76,7 @@ teardown() { echo "status: $status" assert_success - run /bin/bash -c "dokku git:clone $TEST_APP-non-existent" + run /bin/bash -c "dokku git:sync $TEST_APP-non-existent" echo "output: $output" echo "status: $status" assert_failure @@ -87,94 +87,73 @@ teardown() { assert_success } -@test "(git) git:clone [--no-build noarg]" { - run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git" - echo "output: $output" - echo "status: $status" - assert_success - - run destroy_app - echo "output: $output" - echo "status: $status" - assert_success - run create_app - echo "output: $output" - echo "status: $status" - assert_success - - run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git another-branch" - echo "output: $output" - echo "status: $status" - assert_success - - run destroy_app - echo "output: $output" - echo "status: $status" - assert_success - run create_app - echo "output: $output" - echo "status: $status" - assert_success - - run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" - echo "output: $output" - echo "status: $status" - assert_success - - run destroy_app - echo "output: $output" - echo "status: $status" - assert_success - run create_app - echo "output: $output" - echo "status: $status" - assert_success - - run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 5c8a5e42bbd7fae98bd657fb17f41c6019b303f9" +@test "(git) git:sync new [--no-build noarg]" { + run /bin/bash -c "dokku git:sync $TEST_APP https://github.com/dokku/smoke-test-app.git" echo "output: $output" echo "status: $status" assert_success } -@test "(git) git:clone [--build noarg]" { - run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/dokku/smoke-test-app.git" +@test "(git) git:sync new [--no-build branch]" { + run /bin/bash -c "dokku git:sync $TEST_APP https://github.com/dokku/smoke-test-app.git another-branch" + echo "output: $output" + echo "status: $status" + assert_success +} + +@test "(git) git:sync new [--no-build tag]" { + run /bin/bash -c "dokku git:sync $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" + echo "output: $output" + echo "status: $status" + assert_success +} + +@test "(git) git:sync new [--no-build commit]" { + run /bin/bash -c "dokku git:sync $TEST_APP https://github.com/dokku/smoke-test-app.git 5c8a5e42bbd7fae98bd657fb17f41c6019b303f9" + echo "output: $output" + echo "status: $status" + assert_success +} + +@test "(git) git:sync new [--build noarg]" { + run /bin/bash -c "dokku git:sync --build $TEST_APP https://github.com/dokku/smoke-test-app.git" echo "output: $output" echo "status: $status" assert_success assert_output_contains "Application deployed" } -@test "(git) git:clone [--build branch]" { - run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/dokku/smoke-test-app.git another-branch" +@test "(git) git:sync new [--build branch]" { + run /bin/bash -c "dokku git:sync --build $TEST_APP https://github.com/dokku/smoke-test-app.git another-branch" echo "output: $output" echo "status: $status" assert_success assert_output_contains "Application deployed" } -@test "(git) git:clone [--build tag]" { - run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" +@test "(git) git:sync new [--build tag]" { + run /bin/bash -c "dokku git:sync --build $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" echo "output: $output" echo "status: $status" assert_success assert_output_contains "Application deployed" } -@test "(git) git:clone [--build commit]" { - run /bin/bash -c "dokku git:clone --build $TEST_APP https://github.com/dokku/smoke-test-app.git 5c8a5e42bbd7fae98bd657fb17f41c6019b303f9" +@test "(git) git:sync new [--build commit]" { + run /bin/bash -c "dokku git:sync --build $TEST_APP https://github.com/dokku/smoke-test-app.git 5c8a5e42bbd7fae98bd657fb17f41c6019b303f9" echo "output: $output" echo "status: $status" assert_success assert_output_contains "Application deployed" } -@test "(git) git:fetch [errors]" { - run /bin/bash -c "dokku git:fetch" +@test "(git) git:sync existing [errors]" { + run /bin/bash -c "dokku git:sync" echo "output: $output" echo "status: $status" assert_failure - run /bin/bash -c "dokku git:fetch $TEST_APP-non-existent" + run /bin/bash -c "dokku git:sync $TEST_APP-non-existent" echo "output: $output" echo "status: $status" assert_failure @@ -184,7 +163,7 @@ teardown() { echo "status: $status" assert_success - run /bin/bash -c "dokku git:fetch $TEST_APP-non-existent" + run /bin/bash -c "dokku git:sync $TEST_APP-non-existent" echo "output: $output" echo "status: $status" assert_failure @@ -195,100 +174,100 @@ teardown() { assert_success } -@test "(git) git:fetch [--no-build noarg]" { - run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" +@test "(git) git:sync existing [--no-build noarg]" { + run /bin/bash -c "dokku git:sync $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" echo "output: $output" echo "status: $status" assert_success - run /bin/bash -c "dokku git:fetch $TEST_APP https://github.com/dokku/smoke-test-app.git" + run /bin/bash -c "dokku git:sync $TEST_APP https://github.com/dokku/smoke-test-app.git" echo "output: $output" echo "status: $status" assert_success } -@test "(git) git:fetch [--no-build branch]" { - run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" +@test "(git) git:sync existing [--no-build branch]" { + run /bin/bash -c "dokku git:sync $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" echo "output: $output" echo "status: $status" assert_success - run /bin/bash -c "dokku git:fetch $TEST_APP https://github.com/dokku/smoke-test-app.git another-branch" + run /bin/bash -c "dokku git:sync $TEST_APP https://github.com/dokku/smoke-test-app.git another-branch" echo "output: $output" echo "status: $status" assert_success } -@test "(git) git:fetch [--no-build tag]" { - run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" +@test "(git) git:sync existing [--no-build tag]" { + run /bin/bash -c "dokku git:sync $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" echo "output: $output" echo "status: $status" assert_success - run /bin/bash -c "dokku git:fetch $TEST_APP https://github.com/dokku/smoke-test-app.git 2.0.0" + run /bin/bash -c "dokku git:sync $TEST_APP https://github.com/dokku/smoke-test-app.git 2.0.0" echo "output: $output" echo "status: $status" assert_success } -@test "(git) git:fetch [--no-build commit]" { - run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" +@test "(git) git:sync existing [--no-build commit]" { + run /bin/bash -c "dokku git:sync $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" echo "output: $output" echo "status: $status" assert_success - run /bin/bash -c "dokku git:fetch $TEST_APP https://github.com/dokku/smoke-test-app.git 5c8a5e42bbd7fae98bd657fb17f41c6019b303f9" + run /bin/bash -c "dokku git:sync $TEST_APP https://github.com/dokku/smoke-test-app.git 5c8a5e42bbd7fae98bd657fb17f41c6019b303f9" echo "output: $output" echo "status: $status" assert_success } -@test "(git) git:fetch [--build noarg]" { - run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" +@test "(git) git:sync existing [--build noarg]" { + run /bin/bash -c "dokku git:sync $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" echo "output: $output" echo "status: $status" assert_success - run /bin/bash -c "dokku git:fetch --build $TEST_APP https://github.com/dokku/smoke-test-app.git" + run /bin/bash -c "dokku git:sync --build $TEST_APP https://github.com/dokku/smoke-test-app.git" echo "output: $output" echo "status: $status" assert_success assert_output_contains "Application deployed" } -@test "(git) git:fetch [--build branch]" { - run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 2.0.0" +@test "(git) git:sync existing [--build branch]" { + run /bin/bash -c "dokku git:sync $TEST_APP https://github.com/dokku/smoke-test-app.git 2.0.0" echo "output: $output" echo "status: $status" assert_success - run /bin/bash -c "dokku git:fetch --build $TEST_APP https://github.com/dokku/smoke-test-app.git another-branch" + run /bin/bash -c "dokku git:sync --build $TEST_APP https://github.com/dokku/smoke-test-app.git another-branch" echo "output: $output" echo "status: $status" assert_success assert_output_contains "Application deployed" } -@test "(git) git:fetch [--build tag]" { - run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" +@test "(git) git:sync existing [--build tag]" { + run /bin/bash -c "dokku git:sync $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" echo "output: $output" echo "status: $status" assert_success - run /bin/bash -c "dokku git:fetch --build $TEST_APP https://github.com/dokku/smoke-test-app.git 2.0.0" + run /bin/bash -c "dokku git:sync --build $TEST_APP https://github.com/dokku/smoke-test-app.git 2.0.0" echo "output: $output" echo "status: $status" assert_success assert_output_contains "Application deployed" } -@test "(git) git:fetch [--build commit]" { - run /bin/bash -c "dokku git:clone $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" +@test "(git) git:sync existing [--build commit]" { + run /bin/bash -c "dokku git:sync $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" echo "output: $output" echo "status: $status" assert_success - run /bin/bash -c "dokku git:fetch --build $TEST_APP https://github.com/dokku/smoke-test-app.git 5c8a5e42bbd7fae98bd657fb17f41c6019b303f9" + run /bin/bash -c "dokku git:sync --build $TEST_APP https://github.com/dokku/smoke-test-app.git 5c8a5e42bbd7fae98bd657fb17f41c6019b303f9" echo "output: $output" echo "status: $status" assert_success From d5dbd456ea0def78b50aef389a76316e46b7e8c5 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 17 Jan 2021 17:07:10 -0500 Subject: [PATCH 24/31] chore: quiet down git init The command is louder when running on newer versions of git, and the output isn't useful to end users. --- plugins/git/internal-functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index 5f85a3c1c..bcdd6c551 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -142,7 +142,7 @@ fn-git-create-hook() { local PRERECEIVE_HOOK="$APP_PATH/hooks/pre-receive" if [[ ! -d "$APP_PATH/refs" ]]; then - git init --bare "$APP_PATH" >/dev/null + git init -q --bare "$APP_PATH" >/dev/null fi cat >"$PRERECEIVE_HOOK" < Date: Sun, 17 Jan 2021 17:19:14 -0500 Subject: [PATCH 25/31] fix: properly report last-updated --- plugins/git/internal-functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index bcdd6c551..73c3297ad 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -236,7 +236,7 @@ fn-git-last-updated-at() { declare desc="retrieve the deploy branch for a given application" declare APP="$1" local DOKKU_DEPLOY_BRANCH="$(fn-git-deploy-branch "$APP")" - local HEAD_FILE="/home/dokku/test/refs/heads/$DOKKU_DEPLOY_BRANCH" + local HEAD_FILE="/home/dokku/$APP/refs/heads/$DOKKU_DEPLOY_BRANCH" if [[ -f "$HEAD_FILE" ]]; then stat -c %Y "$HEAD_FILE" From 3af1342a9b99635d106e77d67e7df2faba2808f9 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 17 Jan 2021 17:29:21 -0500 Subject: [PATCH 26/31] fix: expand git-ref to the full commit sha If using a branch, git commands that interact with worktrees may fail for newer versions of git. --- plugins/git/internal-functions | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index 73c3297ad..09cca5873 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -51,6 +51,7 @@ cmd-git-sync() { if [[ "$FLAG" == "--build" ]]; then if [[ -n "$GIT_REF" ]]; then + GIT_REF="$(git -C "/home/dokku/$APP" rev-parse "$GIT_REF")" plugn trigger receive-app "$APP" "$GIT_REF" else plugn trigger receive-app "$APP" From 7bb130edb72dc453703613aae9676a5cc969154c Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 17 Jan 2021 19:13:30 -0500 Subject: [PATCH 27/31] refactor: use flag instead of environment variable --- plugins/git/internal-functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index 09cca5873..4c787a5fd 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -107,7 +107,7 @@ cmd-git-report-single() { "--git-global-deploy-branch: $(fn-plugin-property-get "git" "--global" "deploy-branch" "master")" "--git-keep-git-dir: $(fn-plugin-property-get "git" "$APP" "keep-git-dir" "false")" "--git-rev-env-var: $(fn-plugin-property-get "git" "$APP" "rev-env-var" "GIT_REV")" - "--git-sha: $(GIT_DIR="$APP_DIR" git rev-parse --short HEAD 2>/dev/null || false)" + "--git-sha: $(git -C "$APP_DIR" rev-parse --short HEAD 2>/dev/null || false)" "--git-last-updated-at: $(fn-git-last-updated-at "$APP")" ) From 2a4918c7cef49f2e306a4435509f636c902ce27a Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 17 Jan 2021 19:23:40 -0500 Subject: [PATCH 28/31] fix: run check-ref-format in the correct directory --- plugins/git/internal-functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index 4c787a5fd..bd5089834 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -215,7 +215,7 @@ fn-git-fetch() { fi DOKKU_DEPLOY_BRANCH="$(fn-git-deploy-branch "$APP")" - if ! git check-ref-format --branch "$DOKKU_DEPLOY_BRANCH" >/dev/null 2>&1; then + if ! git -C "$APP_ROOT" check-ref-format --branch "$DOKKU_DEPLOY_BRANCH"; then echo $'\e[1G\e[K'"-----> WARNING: Invalid branch name '$DOKKU_DEPLOY_BRANCH' specified via DOKKU_DEPLOY_BRANCH." echo $'\e[1G\e[K'"-----> For more details, please see the man page for 'git-check-ref-format.'" return From 03dc90b4fd30a8ff9877ffea368f277680968b72 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 17 Jan 2021 19:33:36 -0500 Subject: [PATCH 29/31] chore: re-add output redirection --- plugins/git/internal-functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index bd5089834..f3a00dad8 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -215,7 +215,7 @@ fn-git-fetch() { fi DOKKU_DEPLOY_BRANCH="$(fn-git-deploy-branch "$APP")" - if ! git -C "$APP_ROOT" check-ref-format --branch "$DOKKU_DEPLOY_BRANCH"; then + if ! git -C "$APP_ROOT" check-ref-format --branch "$DOKKU_DEPLOY_BRANCH" >/dev/null 2>&1; then echo $'\e[1G\e[K'"-----> WARNING: Invalid branch name '$DOKKU_DEPLOY_BRANCH' specified via DOKKU_DEPLOY_BRANCH." echo $'\e[1G\e[K'"-----> For more details, please see the man page for 'git-check-ref-format.'" return From d58fe853d6b4c01871c558145aa3677964f27421 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 17 Jan 2021 19:34:07 -0500 Subject: [PATCH 30/31] fix: allow updating head --- plugins/git/internal-functions | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index f3a00dad8..f19649061 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -225,9 +225,9 @@ fn-git-fetch() { git remote rm remote > /dev/null 2>&1 || true git remote add --mirror=fetch --no-tags remote "$GIT_REMOTE" if [[ -z "$GIT_REF" ]]; then - git fetch remote "$DOKKU_DEPLOY_BRANCH" + git fetch --update-head-ok remote "$DOKKU_DEPLOY_BRANCH" else - git fetch remote + git fetch --update-head-ok remote git update-ref "refs/heads/$DOKKU_DEPLOY_BRANCH" "$GIT_REF" fi popd > /dev/null From cca507da6a9046bba98027a2e05501870b8384b3 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 17 Jan 2021 19:40:33 -0500 Subject: [PATCH 31/31] refactor: avoid pushd/popd to enable commands to pass from root-enabled directories --- plugins/git/internal-functions | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index f19649061..95af4eeca 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -221,16 +221,14 @@ fn-git-fetch() { return fi - pushd "$DOKKU_ROOT/$APP" > /dev/null - git remote rm remote > /dev/null 2>&1 || true - git remote add --mirror=fetch --no-tags remote "$GIT_REMOTE" + git -C "$APP_ROOT" remote rm remote > /dev/null 2>&1 || true + git -C "$APP_ROOT" remote add --mirror=fetch --no-tags remote "$GIT_REMOTE" if [[ -z "$GIT_REF" ]]; then - git fetch --update-head-ok remote "$DOKKU_DEPLOY_BRANCH" + git -C "$APP_ROOT" fetch --update-head-ok remote "$DOKKU_DEPLOY_BRANCH" else - git fetch --update-head-ok remote - git update-ref "refs/heads/$DOKKU_DEPLOY_BRANCH" "$GIT_REF" + git -C "$APP_ROOT" fetch --update-head-ok remote + git -C "$APP_ROOT" update-ref "refs/heads/$DOKKU_DEPLOY_BRANCH" "$GIT_REF" fi - popd > /dev/null } fn-git-last-updated-at() {