From 3f25514e1ad7a780e2aa87c4249e305146911267 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 9 Jan 2021 07:21:44 -0500 Subject: [PATCH] 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