Skip to content

Commit 407f7f9

Browse files
committed
fix: remote does not exist error on git fetch when git remote is not set (#9123)
* fix: remote does not exist error on git fetch when git remote is not set * Adding a unit test
1 parent ba8ca7c commit 407f7f9

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

cli/cmd/deploy/deploy_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,28 @@ func TestManagedDeploy(t *testing.T) {
6363

6464
// verify changes are pushed to Github repo
6565
verifyGithubRepoContents(t, ghClient, resp.Project.GitRemote, changes)
66+
67+
// clone the project to an empty directory and remove the __rill_remote to simulate fresh deploys in CI/CD
68+
69+
cloneDir := t.TempDir()
70+
cmd := exec.CommandContext(t.Context(), "git", "clone", resp.Project.GitRemote, cloneDir)
71+
out, err := cmd.CombinedOutput()
72+
require.NoError(t, err, "git clone failed: %s", out)
73+
74+
// remove __rill_remote to simulate fresh deploys in CI/CD where the git history is not preserved
75+
err = os.Remove(filepath.Join(cloneDir, ".git", "refs", "heads", "main"))
76+
require.NoError(t, err)
77+
78+
// deploy again from the cloned directory with changes
79+
changes2 := map[string]string{
80+
"models/model.sql": `SELECT 2 AS two`,
81+
}
82+
putFiles(t, cloneDir, changes2)
83+
result = u1.Run(t, "deploy", "--interactive=false", "--org=github-test", "--project=rill-mgd-deploy", "--skip-deploy=true", "--path="+cloneDir)
84+
require.Equal(t, 0, result.ExitCode, result.Output)
85+
86+
// verify changes are pushed to Github repo
87+
verifyGithubRepoContents(t, ghClient, resp.Project.GitRemote, changes2)
6688
}
6789

6890
func TestManagedDeployWithPrimaryBranch(t *testing.T) {

cli/pkg/cmdutil/helper.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,14 @@ func (h *Helper) HandleRepoTransfer(path, remote string) error {
589589
//
590590
// If h.Interactive is true and there are remote commits, the user will be prompted to choose how to proceed.
591591
func (h *Helper) CommitAndSafePush(ctx context.Context, root string, config *gitutil.Config, commitMsg string, author *object.Signature, defaultPushChoice string) error {
592+
// Set remote if not set (go-git complains if we try to fetch without setting remote)
593+
err := gitutil.SetRemote(root, config)
594+
if err != nil {
595+
return fmt.Errorf("failed to set git remote: %w", err)
596+
}
597+
592598
// 1. Fetch latest from remote
593-
err := gitutil.GitFetch(ctx, root, config)
599+
err = gitutil.GitFetch(ctx, root, config)
594600
if err != nil {
595601
return fmt.Errorf("failed to fetch from remote: %w", err)
596602
}

0 commit comments

Comments
 (0)