Merge pull request 'askpass helper rewritten on every git call despite "(once)" comment (archie)' (#68) from feat/59-askpass-helper-rewritten-on-every-git-call-despite-once-comm into main
Reviewed-on: http://git.catlow.cloud/sam/archie-core/pulls/68
This commit is contained in:
commit
d4e4148b77
2 files changed
+60
-9
No files matched your search
@@ -30,6 +30,10 @@ type Manager struct {
|
||||
BaseURL string
|
||||
|
||||
cacheMu sync.Mutex
|
||||
|
||||
askpassOnce sync.Once
|
||||
askpassPath string
|
||||
askpassErr error
|
||||
}
|
||||
|
||||
// Dir is the worktree path for a task.
|
||||
@@ -40,15 +44,19 @@ func (m *Manager) Dir(owner, repo string, issue int) string {
|
||||
// askpass writes (once) a helper script that emits the token, so git
|
||||
// authenticates without the token appearing in argv or .git/config.
|
||||
func (m *Manager) askpass() (string, error) {
|
||||
path := filepath.Join(m.WorkDir, ".git-askpass")
|
||||
script := "#!/bin/sh\necho \"$ARCHIE_GIT_TOKEN\"\n"
|
||||
if err := os.MkdirAll(m.WorkDir, 0o755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(script), 0o700); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return path, nil
|
||||
m.askpassOnce.Do(func() {
|
||||
m.askpassPath = filepath.Join(m.WorkDir, ".git-askpass")
|
||||
script := "#!/bin/sh\necho \"$ARCHIE_GIT_TOKEN\"\n"
|
||||
if err := os.MkdirAll(m.WorkDir, 0o755); err != nil {
|
||||
m.askpassErr = err
|
||||
return
|
||||
}
|
||||
if err := os.WriteFile(m.askpassPath, []byte(script), 0o700); err != nil {
|
||||
m.askpassErr = err
|
||||
return
|
||||
}
|
||||
})
|
||||
return m.askpassPath, m.askpassErr
|
||||
}
|
||||
|
||||
func (m *Manager) git(ctx context.Context, dir string, args ...string) (string, error) {
|
||||
|
||||
@@ -201,6 +201,48 @@ func TestPreparePersistentReusesRepoCacheAndCleanupExpiresIt(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
func TestAskpassWrittenOnce(t *testing.T) {
|
||||
m := &Manager{WorkDir: t.TempDir()}
|
||||
|
||||
p1, err1 := m.askpass()
|
||||
if err1 != nil {
|
||||
t.Fatalf("first askpass: %v", err1)
|
||||
}
|
||||
if _, err := os.Stat(p1); err != nil {
|
||||
t.Fatalf("askpass file not created: %v", err)
|
||||
}
|
||||
fi1, err := os.Stat(p1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
p2, err2 := m.askpass()
|
||||
if err2 != nil {
|
||||
t.Fatalf("second askpass: %v", err2)
|
||||
}
|
||||
if p1 != p2 {
|
||||
t.Fatalf("paths differ: %q vs %q", p1, p2)
|
||||
}
|
||||
|
||||
// Mod time must be unchanged — file was not rewritten.
|
||||
fi2, err := os.Stat(p2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !fi1.ModTime().Equal(fi2.ModTime()) {
|
||||
t.Fatalf("askpass file was rewritten: mod time changed from %v to %v", fi1.ModTime(), fi2.ModTime())
|
||||
}
|
||||
|
||||
// Content must be correct.
|
||||
b, err := os.ReadFile(p1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := "#!/bin/sh\necho \"$ARCHIE_GIT_TOKEN\"\n"
|
||||
if string(b) != want {
|
||||
t.Fatalf("content = %q, want %q", string(b), want)
|
||||
=======
|
||||
func TestPrepareResumesAfterInterruptedClone(t *testing.T) {
|
||||
if _, err := exec.LookPath("git"); err != nil {
|
||||
t.Skip("git not installed")
|
||||
@@ -289,6 +331,7 @@ func TestPrepareResumesAfterInterruptedClone(t *testing.T) {
|
||||
}
|
||||
if _, err := os.Stat(dir3); !os.IsNotExist(err) {
|
||||
t.Fatal("cleanup left the worktree behind")
|
||||
>>>>>>> origin/main
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user