Skip to content

Commit

Permalink
fix: Restore proper namespace separation of cached clones
Browse files Browse the repository at this point in the history
Commit d306437 pulled out the
name normalization for cache directories and left it up to the
tooling to work it out; turns out the tooling is preposterously bad
at it, and shouldn't be trusted.

Restore the needed functionality, and keep cached copies in
properly-namespaced locations under the hood.

Fixes: Issue #100
  • Loading branch information
nisimond authored and retr0h committed Dec 31, 2023
1 parent 1e574d0 commit e271544
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
7 changes: 5 additions & 2 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ import (
"github.com/retr0h/go-gilt/pkg/config"
)

// We'll use this to normalize Git URLs as "safe" filenames
var replacer = strings.NewReplacer("/", "-", ":", "-")

// New factory to create a new Repository instance.
func New(
appFs afero.Fs,
Expand Down Expand Up @@ -84,12 +87,12 @@ func (r *Repository) Clone(
c config.Repository,
cloneDir string,
) (string, error) {
targetDir := filepath.Join(cloneDir, replacer.Replace(c.Git))
r.logger.Info(
"cloning",
slog.String("repository", c.Git),
slog.String("dstDir", cloneDir),
slog.String("dstDir", targetDir),
)
targetDir := filepath.Join(cloneDir, filepath.Base(c.Git))

if _, err := r.appFs.Stat(targetDir); os.IsNotExist(err) {
if err := r.gitManager.Clone(c.Git, targetDir); err != nil {
Expand Down
6 changes: 4 additions & 2 deletions internal/repository/repository_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type RepositoryPublicTestSuite struct {
cloneDir string
dstDir string
gitURL string
cacheDir string
gitSHA string
gitTag string
logger *slog.Logger
Expand All @@ -74,6 +75,7 @@ func (suite *RepositoryPublicTestSuite) SetupTest() {
suite.cloneDir = "/cloneDir"
suite.dstDir = "/dstDir"
suite.gitURL = "https://example.com/user/repo.git"
suite.cacheDir = "https---example.com-user-repo.git"
suite.gitSHA = "abc123"
suite.gitTag = "v1.1"
suite.logger = slog.New(slog.NewTextHandler(os.Stdout, nil))
Expand All @@ -86,7 +88,7 @@ func (suite *RepositoryPublicTestSuite) TestCloneOk() {
Git: suite.gitURL,
Version: suite.gitSHA,
}
targetDir := filepath.Join(suite.cloneDir, filepath.Base(c.Git))
targetDir := filepath.Join(suite.cloneDir, suite.cacheDir)

gomock.InOrder(
suite.mockGit.EXPECT().Clone(suite.gitURL, targetDir).Return(nil),
Expand Down Expand Up @@ -120,7 +122,7 @@ func (suite *RepositoryPublicTestSuite) TestCloneDoesNotCloneWhenCloneDirExists(
Git: suite.gitURL,
Version: suite.gitSHA,
}
targetDir := filepath.Join(suite.cloneDir, filepath.Base(c.Git))
targetDir := filepath.Join(suite.cloneDir, suite.cacheDir)

_ = suite.appFs.MkdirAll(targetDir, 0o755)

Expand Down
2 changes: 1 addition & 1 deletion test/integration/test_cli.bats
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ GILT_PROGRAM="../../../main.go"
GILT_DIR=~/.gilt/clone

setup() {
GILT_CLONED_REPO=${GILT_DIR}/cache/ansible-etcd.git
GILT_CLONED_REPO=${GILT_DIR}/cache/https---github.com-retr0h-ansible-etcd.git

GILT_CLONED_REPO_1_DST_DIR=${GILT_TEST_BASE_TMP_DIR}/retr0h.ansible-etcd
GILT_CLONED_REPO_2_DST_DIR=${GILT_TEST_BASE_TMP_DIR}/retr0h.ansible-etcd-tag
Expand Down

0 comments on commit e271544

Please sign in to comment.