From 154308fd319aaabece908ed24e0c7eff65a11dc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olive=CC=81r=20Falvai?= Date: Wed, 13 Mar 2024 12:38:47 +0100 Subject: [PATCH 1/6] Clean dirty state before checkout --- e2e/bitrise.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/e2e/bitrise.yml b/e2e/bitrise.yml index cca78305..cd9d2dc1 100644 --- a/e2e/bitrise.yml +++ b/e2e/bitrise.yml @@ -15,6 +15,17 @@ app: - GIT_HTTP_PASSWORD_FOR_TEST: $GIT_HTTP_PASSWORD_FOR_TEST workflows: + test_dirty_state_before_checkout: + envs: + - BRANCH: conflict-test/a + - BRANCH_DEST: conflict-test/b + before_run: + - _setup + after_run: + - _run + - _check_outputs + - _teardown + test_generate_changelog_by_commit: envs: - COMMIT: a409478 @@ -616,6 +627,7 @@ workflows: - clone_into_dir: $WORKDIR - commit: $COMMIT - branch: $BRANCH + - branch_dest: $BRANCH_DEST - tag: $TAG - fetch_tags: $FETCH_TAGS - update_submodules: $UPDATE_SUBMODULES From 530d88540792a307c0f61ce01ab9da92ca054c5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olive=CC=81r=20Falvai?= Date: Wed, 13 Mar 2024 12:45:20 +0100 Subject: [PATCH 2/6] * --- e2e/bitrise.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/e2e/bitrise.yml b/e2e/bitrise.yml index cd9d2dc1..186a2e12 100644 --- a/e2e/bitrise.yml +++ b/e2e/bitrise.yml @@ -5,6 +5,7 @@ app: envs: - ORIGINAL_WORKDIR: $BITRISE_SOURCE_DIR - TEST_REPO_URL: https://github.com/bitrise-io/git-clone-test.git + - PR_REPO_URL: https://github.com/bitrise-io/git-clone-test.git - FETCH_TAGS: "no" - UPDATE_SUBMODULES: "yes" - MERGE_PR: "yes" @@ -634,6 +635,7 @@ workflows: - merge_pr: $MERGE_PR - clone_depth: $CLONE_DEPTH - sparse_directories: $SPARSE_DIRECTORIES + - pull_request_repository_url: PR_REPO_URL _teardown: steps: From 6976c7c0938d8c2a893ba565201c9ebc204a9a1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olive=CC=81r=20Falvai?= Date: Thu, 14 Mar 2024 13:04:14 +0100 Subject: [PATCH 3/6] * --- e2e/bitrise.yml | 39 ++++++++++++++++++++++++++++----------- gitclone/git.go | 10 ++++++++++ gitclone/gitclone.go | 19 +++++++++++++++++++ 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/e2e/bitrise.yml b/e2e/bitrise.yml index 186a2e12..2f5409cb 100644 --- a/e2e/bitrise.yml +++ b/e2e/bitrise.yml @@ -18,10 +18,27 @@ app: workflows: test_dirty_state_before_checkout: envs: - - BRANCH: conflict-test/a - - BRANCH_DEST: conflict-test/b + - BITRISEIO_PULL_REQUEST_MERGE_BRANCH: pull/5/merge + - BITRISEIO_PULL_REQUEST_HEAD_BRANCH: pull/5/head + - BITRISEIO_GIT_BRANCH_DEST: master + - COMMIT: 4301a9b8499ed3e87778dd053c55fd698d0a3b7e + - MERGE_PR: "yes" + - UPDATE_SUBMODULES: "no" before_run: - _setup + steps: + - script: + title: Make a mess + is_skippable: true + inputs: + - content: |- + #!/bin/env bash + set -ex + cd $WORKDIR + git clone $TEST_REPO_URL . + git checkout conflict-test/b + git merge origin/conflict-test/a || true + git status after_run: - _run - _check_outputs @@ -601,14 +618,6 @@ workflows: envman unset --key GIT_CLONE_COMMIT_AUTHOR_EMAIL envman unset --key GIT_CLONE_COMMIT_COMMITTER_NAME envman unset --key GIT_CLONE_COMMIT_COMMITTER_EMAIL - - _run: - steps: - - activate-ssh-key: - run_if: |- - {{ getenv "GIT_CLONE_SSH_PRIVATE_KEY" | ne "" }} - inputs: - - ssh_rsa_private_key: $GIT_CLONE_SSH_PRIVATE_KEY - script: inputs: - title: Create temporary dir @@ -621,6 +630,14 @@ workflows: envman add --key WORKDIR --value $WD envman add --key WORKDIR_ABSOLUTE --value $WD fi + + _run: + steps: + - activate-ssh-key: + run_if: |- + {{ getenv "GIT_CLONE_SSH_PRIVATE_KEY" | ne "" }} + inputs: + - ssh_rsa_private_key: $GIT_CLONE_SSH_PRIVATE_KEY - path::./: run_if: "true" inputs: @@ -635,7 +652,7 @@ workflows: - merge_pr: $MERGE_PR - clone_depth: $CLONE_DEPTH - sparse_directories: $SPARSE_DIRECTORIES - - pull_request_repository_url: PR_REPO_URL + - pull_request_repository_url: $PR_REPO_URL _teardown: steps: diff --git a/gitclone/git.go b/gitclone/git.go index 8f9765bc..cd2c833d 100644 --- a/gitclone/git.go +++ b/gitclone/git.go @@ -150,3 +150,13 @@ func handleCheckoutError(callback getAvailableBranches, tag string, err error, s shortMsg, ) } + +func isWorkingTreeClean(gitCmd git.Git) (bool, error) { + // Despite the flag name, `--porcelain` is the plumbing format to use in scripts: + // https://git-scm.com/docs/git-status#Documentation/git-status.txt---porcelainltversiongt + out, err := gitCmd.Status("--porcelain").RunAndReturnTrimmedOutput() + if err != nil { + return false, fmt.Errorf("git status check: %s", err) + } + return strings.TrimSpace(out) == "", nil +} diff --git a/gitclone/gitclone.go b/gitclone/gitclone.go index 12c7b057..9be6db2b 100644 --- a/gitclone/gitclone.go +++ b/gitclone/gitclone.go @@ -131,6 +131,25 @@ func (g GitCloner) CheckoutState(cfg Config) (CheckoutStateResult, error) { return CheckoutStateResult{}, err } + clean, err := isWorkingTreeClean(gitCmd) + if err != nil { + g.logger.Warnf("Failed to check if working tree is clean: %s", err) + } + if !clean { + g.logger.Println() + g.logger.Warnf("Working tree is dirty, cleaning before checkout:") + + err = runner.Run(gitCmd.Clean("-fd")) + if err != nil { + g.logger.Warnf("Failed to clean untracked files: %s", err) + } + + err = runner.Run(gitCmd.Reset("--hard", "HEAD")) + if err != nil { + g.logger.Warnf("Failed to reset repository: %s", err) + } + } + checkoutStrategy, isPR, err := g.checkoutState(gitCmd, cfg) if err != nil { return CheckoutStateResult{}, err From a5ed4667c113d3086d7256dd46811a26ad0cf7f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olive=CC=81r=20Falvai?= Date: Thu, 14 Mar 2024 13:10:26 +0100 Subject: [PATCH 4/6] * --- e2e/bitrise.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/e2e/bitrise.yml b/e2e/bitrise.yml index 2f5409cb..15734517 100644 --- a/e2e/bitrise.yml +++ b/e2e/bitrise.yml @@ -5,7 +5,6 @@ app: envs: - ORIGINAL_WORKDIR: $BITRISE_SOURCE_DIR - TEST_REPO_URL: https://github.com/bitrise-io/git-clone-test.git - - PR_REPO_URL: https://github.com/bitrise-io/git-clone-test.git - FETCH_TAGS: "no" - UPDATE_SUBMODULES: "yes" - MERGE_PR: "yes" @@ -645,14 +644,12 @@ workflows: - clone_into_dir: $WORKDIR - commit: $COMMIT - branch: $BRANCH - - branch_dest: $BRANCH_DEST - tag: $TAG - fetch_tags: $FETCH_TAGS - update_submodules: $UPDATE_SUBMODULES - merge_pr: $MERGE_PR - clone_depth: $CLONE_DEPTH - sparse_directories: $SPARSE_DIRECTORIES - - pull_request_repository_url: $PR_REPO_URL _teardown: steps: From cb80c67f494970ed31aa7b5c33ad90d8aeba32c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olive=CC=81r=20Falvai?= Date: Thu, 14 Mar 2024 13:20:08 +0100 Subject: [PATCH 5/6] Reorder tests --- e2e/bitrise.yml | 56 ++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/e2e/bitrise.yml b/e2e/bitrise.yml index 15734517..4cb67f7d 100644 --- a/e2e/bitrise.yml +++ b/e2e/bitrise.yml @@ -15,34 +15,6 @@ app: - GIT_HTTP_PASSWORD_FOR_TEST: $GIT_HTTP_PASSWORD_FOR_TEST workflows: - test_dirty_state_before_checkout: - envs: - - BITRISEIO_PULL_REQUEST_MERGE_BRANCH: pull/5/merge - - BITRISEIO_PULL_REQUEST_HEAD_BRANCH: pull/5/head - - BITRISEIO_GIT_BRANCH_DEST: master - - COMMIT: 4301a9b8499ed3e87778dd053c55fd698d0a3b7e - - MERGE_PR: "yes" - - UPDATE_SUBMODULES: "no" - before_run: - - _setup - steps: - - script: - title: Make a mess - is_skippable: true - inputs: - - content: |- - #!/bin/env bash - set -ex - cd $WORKDIR - git clone $TEST_REPO_URL . - git checkout conflict-test/b - git merge origin/conflict-test/a || true - git status - after_run: - - _run - - _check_outputs - - _teardown - test_generate_changelog_by_commit: envs: - COMMIT: a409478 @@ -501,6 +473,34 @@ workflows: - _run - _check_outputs - _teardown + + test_dirty_state_before_checkout: + envs: + - BITRISEIO_PULL_REQUEST_MERGE_BRANCH: pull/5/merge + - BITRISEIO_PULL_REQUEST_HEAD_BRANCH: pull/5/head + - BITRISEIO_GIT_BRANCH_DEST: master + - COMMIT: 4301a9b8499ed3e87778dd053c55fd698d0a3b7e + - MERGE_PR: "yes" + - UPDATE_SUBMODULES: "no" + before_run: + - _setup + steps: + - script: + title: Make a mess + is_skippable: true + inputs: + - content: |- + #!/bin/env bash + set -ex + cd $WORKDIR + git clone $TEST_REPO_URL . + git checkout conflict-test/b + git merge origin/conflict-test/a || true + git status + after_run: + - _run + - _check_outputs + - _teardown _check_changelog: steps: From 34ae77aef996f6b8e6164a8b436d983df1317139 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olive=CC=81r=20Falvai?= Date: Thu, 14 Mar 2024 13:23:23 +0100 Subject: [PATCH 6/6] Whitespace --- e2e/bitrise.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/bitrise.yml b/e2e/bitrise.yml index 4cb67f7d..30a07b64 100644 --- a/e2e/bitrise.yml +++ b/e2e/bitrise.yml @@ -473,7 +473,7 @@ workflows: - _run - _check_outputs - _teardown - + test_dirty_state_before_checkout: envs: - BITRISEIO_PULL_REQUEST_MERGE_BRANCH: pull/5/merge