Skip to content

Commit

Permalink
Merge pull request #40 from codecrafters-io/support-gitignore
Browse files Browse the repository at this point in the history
CC-1511: Fix CLI + Visual Studio issue
  • Loading branch information
andy1li authored Nov 23, 2024
2 parents 3a4ccd1 + fab322a commit b905341
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 10 deletions.
15 changes: 11 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
name: Test Install Script
name: Test

on:
pull_request:
push:
branches: [main]

jobs:
test:
run-tests:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.23"
- run: make test

install-cli:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: dash -x install.sh
- run: codecrafters --version
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ require (
github.com/mitchellh/go-wordwrap v1.0.1
github.com/otiai10/copy v1.7.0
github.com/rs/zerolog v1.28.0
github.com/stretchr/testify v1.5.1
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06
github.com/stretchr/testify v1.6.1
)

require (
Expand All @@ -26,5 +27,5 @@ require (
golang.org/x/net v0.0.0-20221002022538-bcab6841153b // indirect
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 7 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,12 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY=
github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDjyw0ULyrTYWeN0UNCCkmCWfjPnIA2W6oviI=
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down Expand Up @@ -162,3 +165,6 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
10 changes: 7 additions & 3 deletions internal/commands/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"
Expand Down Expand Up @@ -122,12 +121,17 @@ func TestCommand(ctx context.Context, shouldTestPrevious bool) (err error) {
}

func copyRepositoryDirToTempDir(repoDir string) (string, error) {
tmpDir, err := ioutil.TempDir("", "codecrafters")
tmpDir, err := os.MkdirTemp("", "codecrafters")

if err != nil {
return "", fmt.Errorf("create temp dir: %w", err)
}

err = cp.Copy(repoDir, tmpDir)
gitIgnore := utils.NewGitIgnore(repoDir)

err = cp.Copy(repoDir, tmpDir, cp.Options{
Skip: gitIgnore.SkipFile,
})
if err != nil {
return "", fmt.Errorf("copy files: %w", err)
}
Expand Down
64 changes: 64 additions & 0 deletions internal/utils/git_ignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Package utils
package utils

import (
"os"
"os/exec"
"path/filepath"
"strings"

ignore "github.com/sabhiram/go-gitignore"
)

type GitIgnore struct {
baseDir string
localGitIgnore *ignore.GitIgnore
globalGitIgnore *ignore.GitIgnore
gitInfoExclude *ignore.GitIgnore
}

func NewGitIgnore(baseDir string) GitIgnore {
return GitIgnore{
baseDir: baseDir,
localGitIgnore: compileIgnorer(filepath.Join(baseDir, ".gitignore")),
globalGitIgnore: compileIgnorer(getGlobalGitIgnorePath()),
gitInfoExclude: compileIgnorer(filepath.Join(baseDir, ".git", "info", "exclude")),
}
}

func (i GitIgnore) SkipFile(path string) (bool, error) {
for _, ignorer := range []*ignore.GitIgnore{i.localGitIgnore, i.globalGitIgnore, i.gitInfoExclude} {
if ignorer != nil && ignorer.MatchesPath(path) {
return true, nil
}
}

return false, nil
}

func compileIgnorer(path string) *ignore.GitIgnore {
ignorer, err := ignore.CompileIgnoreFile(path)
if err != nil {
return nil
}

return ignorer
}

func getGlobalGitIgnorePath() string {
output, err := exec.Command("git", "config", "--get", "core.excludesfile").Output()
if err != nil {
return ""
}

path := strings.TrimSpace(string(output))
if strings.HasPrefix(path, "~") {
homeDir, err := os.UserHomeDir()
if err != nil {
return ""
}
path = filepath.Join(homeDir, path[2:])
}

return path
}
119 changes: 119 additions & 0 deletions internal/utils/git_ignore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package utils

import (
"os"
"os/exec"
"path/filepath"
"testing"

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

func TestGitIgnore(t *testing.T) {
t.Run("without gitignore files", func(t *testing.T) {
gitIgnore := NewGitIgnore(t.TempDir())
assertFileNotSkipped(t, &gitIgnore, "some/random/file.txt")
})

t.Run("with local gitignore", func(t *testing.T) {
tmpRepoDir := t.TempDir()
writeFile(t, filepath.Join(tmpRepoDir, ".gitignore"), "ignore/this/file.txt")

gitIgnore := NewGitIgnore(tmpRepoDir)
assertFileSkipped(t, &gitIgnore, "ignore/this/file.txt")
assertFileNotSkipped(t, &gitIgnore, "some/other/file.txt")
})

t.Run("with global gitignore", func(t *testing.T) {
backup := setupGlobalGitIgnore(t, "ignore/this/file.txt")
defer func() {
if backup.originalPath == "" {
unsetGlobalGitIgnoreConfig(t)
}
backup.Restore(t)
}()

gitIgnore := NewGitIgnore(t.TempDir())
assertFileSkipped(t, &gitIgnore, "ignore/this/file.txt")
assertFileNotSkipped(t, &gitIgnore, "some/other/file.txt")
})

t.Run("with git info exclude", func(t *testing.T) {
tmpRepoDir := createEmptyRepository(t)
backup := setupGitInfoExclude(t, tmpRepoDir, "ignore/this/file.txt")
defer backup.Restore(t)

gitIgnore := NewGitIgnore(tmpRepoDir)
assertFileSkipped(t, &gitIgnore, "ignore/this/file.txt")
assertFileNotSkipped(t, &gitIgnore, "some/other/file.txt")
})
}

func assertFileSkipped(t *testing.T, gitIgnore *GitIgnore, path string) {
skip, err := gitIgnore.SkipFile(path)
assert.NoError(t, err)
assert.True(t, skip)
}

func assertFileNotSkipped(t *testing.T, gitIgnore *GitIgnore, path string) {
skip, err := gitIgnore.SkipFile(path)
assert.NoError(t, err)
assert.False(t, skip)
}

type FileBackup struct {
originalPath string
backupPath string
}

func (b *FileBackup) Restore(t *testing.T) {
if b.originalPath != "" {
moveFile(t, b.backupPath, b.originalPath)
}
}

func setupGlobalGitIgnore(t *testing.T, content string) *FileBackup {
globalGitIgnorePath := getGlobalGitIgnorePath()
backupPath := filepath.Join(t.TempDir(), ".gitignore_global")

if globalGitIgnorePath == "" {
writeFile(t, backupPath, content)
setGlobalGitIgnoreConfig(t, backupPath)
return &FileBackup{originalPath: "", backupPath: backupPath}
}

moveFile(t, globalGitIgnorePath, backupPath)
writeFile(t, globalGitIgnorePath, content)
return &FileBackup{originalPath: globalGitIgnorePath, backupPath: backupPath}
}

func setupGitInfoExclude(t *testing.T, baseDir string, content string) *FileBackup {
gitInfoExcludePath := filepath.Join(baseDir, ".git", "info", "exclude")
_, err := os.Stat(gitInfoExcludePath)
assert.NoError(t, err)

backupPath := filepath.Join(t.TempDir(), ".git_info_exclude_backup")
moveFile(t, gitInfoExcludePath, backupPath)
writeFile(t, gitInfoExcludePath, content)
return &FileBackup{originalPath: gitInfoExcludePath, backupPath: backupPath}
}

func setGlobalGitIgnoreConfig(t *testing.T, path string) {
_, err := exec.Command("git", "config", "--global", "core.excludesfile", path).Output()
assert.NoError(t, err)
}

func unsetGlobalGitIgnoreConfig(t *testing.T) {
_, err := exec.Command("git", "config", "--global", "--unset", "core.excludesfile").Output()
assert.NoError(t, err)
}

func moveFile(t *testing.T, srcPath string, dstPath string) {
err := os.Rename(srcPath, dstPath)
assert.NoError(t, err)
}

func writeFile(t *testing.T, path string, content string) {
err := os.WriteFile(path, []byte(content), 0644)
assert.NoError(t, err)
}

0 comments on commit b905341

Please sign in to comment.