Skip to content

Commit

Permalink
git: prune any deleted refs before fetching (#9504)
Browse files Browse the repository at this point in the history
* git: prune any deleted refers before fetching

This commit modifies `nativeGitClient.Fetch()` to call `git remote prune origin` before fetching refs.

In some cases, an old branch may exist that conflicts with the name of a new branch. The old branch will have been deleted from `origin` but still exist locally in the `argocd-repo-server`.

Example: an old branch `feature/foo` conflicts with a new branch `feature/foo/bar`

In these cases, syncing an application results in the error:

```
rpc error: code = Internal desc = Failed to fetch default: `git fetch origin --tags --force` failed exit status 1: error: cannot lock ref 'refs/remotes/origin/feature/foo/bar': 'refs/remotes/origin/feature/foo' exists; cannot create 'refs/remotes/origin/feature/foo/bar' From https://github.com/org/repo ! [new branch] feature/foo/bar -> origin/feature/foo/bar (unable to update local ref) error: some local refs could not be updated; try running 'git remote prune origin' to remove any old, conflicting branches
```

Adding `git remote prune origin` before fetching, as recommended by the error message, should fix this issue.

The current workaround is to restart the `argocd-repo-server` which should flush the local repository folder. This works when Argo CD is installed using the Helm chart.

Signed-off-by: Kevin Snyder <kevin.snyder.codes@gmail.com>

* fix: added extra protection to syncing app with replace (#9187)

* fix: added extra protection to syncing app with replace

Signed-off-by: ciiay <yicai@redhat.com>

* Code clean up

Signed-off-by: ciiay <yicai@redhat.com>

* Updated logic for isAppOfAppsPattern

Signed-off-by: ciiay <yicai@redhat.com>

* Updated text strings as per comment

Signed-off-by: ciiay <yicai@redhat.com>

* Fixed lint issue

Signed-off-by: ciiay <yicai@redhat.com>
Signed-off-by: Kevin Snyder <kevin.snyder.codes@gmail.com>

* chore: Simplified GetRepoHTTPClient function (#9396)

* chore: Simplified GetRepoHTTPClient function

Signed-off-by: ls0f <lovedboy.tk@qq.com>

* simplified code and improve unit test coverage

Signed-off-by: ls0f <lovedboy.tk@qq.com>
Signed-off-by: Kevin Snyder <kevin.snyder.codes@gmail.com>

* Only prune if fetch error message indicates that it is worthwhile, add unit tests

Confirmed that `Test_nativeGitClient_Fetch_Prune` fails without the bug fix, succeeds with it.

Signed-off-by: Kevin Snyder <kevin.snyder.codes@gmail.com>

* fix: avoid k8s call before authorization for terminal endpoint (#9434)

* fix: avoid k8s API call before authorization in k8s endpoint

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>

* check for bad project

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>

* lint

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>

* more logging

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>

* handle 404, return 500 instead of 400 for other errors

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>

* use user input

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>

* refactor validation

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>

* fix tests

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>

* fixes, tests

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
Signed-off-by: Kevin Snyder <kevin.snyder.codes@gmail.com>

* Match against "try running 'git remote prune origin'"

Signed-off-by: Kevin Snyder <kevin.snyder.codes@gmail.com>

Co-authored-by: Yi Cai <yicai@redhat.com>
Co-authored-by: ls0f <lovedboy.tk@qq.com>
Co-authored-by: Michael Crenshaw <michael@crenshaw.dev>
  • Loading branch information
4 people committed May 27, 2022
1 parent 847255a commit 2a69038
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 4 deletions.
27 changes: 23 additions & 4 deletions util/git/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,16 @@ func (m *nativeGitClient) IsLFSEnabled() bool {
return m.enableLfs
}

func (m *nativeGitClient) fetch(revision string) error {
var err error
if revision != "" {
err = m.runCredentialedCmd("git", "fetch", "origin", revision, "--tags", "--force")
} else {
err = m.runCredentialedCmd("git", "fetch", "origin", "--tags", "--force")
}
return err
}

// Fetch fetches latest updates from origin
func (m *nativeGitClient) Fetch(revision string) error {
if m.OnFetch != nil {
Expand All @@ -318,11 +328,19 @@ func (m *nativeGitClient) Fetch(revision string) error {
}

var err error
if revision != "" {
err = m.runCredentialedCmd("git", "fetch", "origin", revision, "--tags", "--force")
} else {
err = m.runCredentialedCmd("git", "fetch", "origin", "--tags", "--force")

err = m.fetch(revision)
if err != nil {
errMsg := strings.ReplaceAll(err.Error(), "\n", "")
if strings.Contains(errMsg, "try running 'git remote prune origin'") {
// Prune any deleted refs, then try fetching again
if err := m.runCredentialedCmd("git", "remote", "prune", "origin"); err != nil {
return err
}
err = m.fetch(revision)
}
}

// When we have LFS support enabled, check for large files and fetch them too.
if err == nil && m.IsLFSEnabled() {
largeFiles, err := m.LsLargeFiles()
Expand All @@ -333,6 +351,7 @@ func (m *nativeGitClient) Fetch(revision string) error {
}
}
}

return err
}

Expand Down
94 changes: 94 additions & 0 deletions util/git/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package git

import (
"fmt"
"os"
"os/exec"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_nativeGitClient_Fetch(t *testing.T) {
tempDir, err := os.MkdirTemp("", "")
require.NoError(t, err)

cmd := exec.Command("git", "init")
cmd.Dir = tempDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
require.NoError(t, err)

cmd = exec.Command("git", "commit", "-m", "Initial commit", "--allow-empty")
cmd.Dir = tempDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
require.NoError(t, err)

client, err := NewClient(fmt.Sprintf("file://%s", tempDir), NopCreds{}, true, false, "")
require.NoError(t, err)

err = client.Init()
require.NoError(t, err)

err = client.Fetch("")
assert.NoError(t, err)
}

func Test_nativeGitClient_Fetch_Prune(t *testing.T) {
tempDir, err := os.MkdirTemp("", "")
require.NoError(t, err)

cmd := exec.Command("git", "init")
cmd.Dir = tempDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
require.NoError(t, err)

cmd = exec.Command("git", "commit", "-m", "Initial commit", "--allow-empty")
cmd.Dir = tempDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
require.NoError(t, err)

client, err := NewClient(fmt.Sprintf("file://%s", tempDir), NopCreds{}, true, false, "")
require.NoError(t, err)

err = client.Init()
require.NoError(t, err)

// Create branch
cmd = exec.Command("git", "branch", "test/foo")
cmd.Dir = tempDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
require.NoError(t, err)

err = client.Fetch("")
assert.NoError(t, err)

// Delete branch
cmd = exec.Command("git", "branch", "-d", "test/foo")
cmd.Dir = tempDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
require.NoError(t, err)

// Create branch that conflicts with previous branch name
cmd = exec.Command("git", "branch", "test/foo/bar")
cmd.Dir = tempDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
require.NoError(t, err)

err = client.Fetch("")
assert.NoError(t, err)
}

0 comments on commit 2a69038

Please sign in to comment.