Skip to content

Commit

Permalink
Clean dirty state before checkout (#220)
Browse files Browse the repository at this point in the history
* Clean dirty state before checkout

* *

* *

* *

* Reorder tests

* Whitespace
  • Loading branch information
ofalvai authored Mar 18, 2024
1 parent 51ad770 commit cacdd02
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
44 changes: 36 additions & 8 deletions e2e/bitrise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,34 @@ workflows:
- _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:
- generate-changelog:
Expand Down Expand Up @@ -589,14 +617,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
Expand All @@ -609,6 +629,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:
Expand Down
10 changes: 10 additions & 0 deletions gitclone/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
19 changes: 19 additions & 0 deletions gitclone/gitclone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cacdd02

Please sign in to comment.