diff --git a/.github/dependabot.yaml b/.github/dependabot.yaml index 403f428e4..7f9fff67a 100644 --- a/.github/dependabot.yaml +++ b/.github/dependabot.yaml @@ -13,6 +13,10 @@ updates: interval: "daily" commit-message: prefix: "build" + groups: + golang.org: + patterns: + - "golang.org/*" - package-ecosystem: "gomod" directory: "/cli/go-git" diff --git a/.github/workflows/cifuzz.yml b/.github/workflows/cifuzz.yml index a93087634..f3b67df1d 100644 --- a/.github/workflows/cifuzz.yml +++ b/.github/workflows/cifuzz.yml @@ -28,7 +28,7 @@ jobs: path: ./out/artifacts - name: Upload Sarif if: always() && steps.build.outcome == 'success' - uses: github/codeql-action/upload-sarif@v3 + uses: github/codeql-action/upload-sarif@v3.28.1 with: # Path to SARIF file relative to the root of the repository sarif_file: cifuzz-sarif/results.sarif diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 920fc3e58..52bd384f5 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -28,7 +28,7 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@03e7845b7bfcd5e7fb63d1ae8c61b0e791134fab # v2.22.11 + uses: github/codeql-action/init@1f86f55dce064513e2111b0d4fc7cfb11444c092 # v2.22.11 with: languages: ${{ matrix.language }} # xref: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs @@ -39,6 +39,6 @@ jobs: run: go build ./... - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@03e7845b7bfcd5e7fb63d1ae8c61b0e791134fab # v2.22.11 + uses: github/codeql-action/analyze@1f86f55dce064513e2111b0d4fc7cfb11444c092 # v2.22.11 with: category: "/language:${{matrix.language}}" diff --git a/.github/workflows/git.yml b/.github/workflows/git.yml index 6e0ebb651..7744999e7 100644 --- a/.github/workflows/git.yml +++ b/.github/workflows/git.yml @@ -22,7 +22,7 @@ jobs: - name: Install Go uses: actions/setup-go@v5 with: - go-version: 1.21.x + go-version: 1.23.x - name: Install build dependencies run: sudo apt-get update && sudo apt-get install gettext libcurl4-openssl-dev diff --git a/.github/workflows/pr-validation.yml b/.github/workflows/pr-validation.yml index d7b115095..34b609cb4 100644 --- a/.github/workflows/pr-validation.yml +++ b/.github/workflows/pr-validation.yml @@ -19,7 +19,7 @@ jobs: - name: Check Package Prefix uses: gsactions/commit-message-checker@v2 with: - pattern: '^(\*|git|plumbing|utils|config|_examples|internal|storage|cli|build): .+' + pattern: '^(\*|docs|git|plumbing|utils|config|_examples|internal|storage|cli|build): .+' error: | Commit message(s) does not align with contribution acceptance criteria. diff --git a/.github/workflows/stale-issues-bot.yaml b/.github/workflows/stale-issues-bot.yaml index 11b86ae27..fe40db367 100644 --- a/.github/workflows/stale-issues-bot.yaml +++ b/.github/workflows/stale-issues-bot.yaml @@ -15,7 +15,7 @@ jobs: with: ascending: true operations-per-run: 30 - days-before-stale: 90 + days-before-stale: 180 days-before-close: 30 stale-issue-label: stale stale-pr-label: stale diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f94d3e738..3b3c10bac 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,7 +8,7 @@ jobs: strategy: fail-fast: false matrix: - go-version: [1.19.x, 1.20.x, 1.21.x] + go-version: [1.21.x, 1.22.x, 1.23.x] platform: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.platform }} @@ -34,3 +34,6 @@ jobs: - name: Test run: make test-coverage + + - name: Test Examples + run: go test -timeout 30s -v -run '^TestExamples$' github.com/go-git/go-git/v5/_examples --examples diff --git a/COMPATIBILITY.md b/COMPATIBILITY.md index ff0c22c89..ba1fb90ac 100644 --- a/COMPATIBILITY.md +++ b/COMPATIBILITY.md @@ -11,7 +11,7 @@ compatibility status with go-git. | `init` | `--bare` | ✅ | | | | `init` | `--template`
`--separate-git-dir`
`--shared` | ❌ | | | | `clone` | | ✅ | | - [PlainClone](_examples/clone/main.go) | -| `clone` | Authentication:
- none
- access token
- username + password
- ssh | ✅ | | - [clone ssh](_examples/clone/auth/ssh/main.go)
- [clone access token](_examples/clone/auth/basic/access_token/main.go)
- [clone user + password](_examples/clone/auth/basic/username_password/main.go) | +| `clone` | Authentication:
- none
- access token
- username + password
- ssh | ✅ | | - [clone ssh (private_key)](_examples/clone/auth/ssh/private_key/main.go)
- [clone ssh (ssh_agent)](_examples/clone/auth/ssh/ssh_agent/main.go)
- [clone access token](_examples/clone/auth/basic/access_token/main.go)
- [clone user + password](_examples/clone/auth/basic/username_password/main.go) | | `clone` | `--progress`
`--single-branch`
`--depth`
`--origin`
`--recurse-submodules`
`--shared` | ✅ | | - [recurse submodules](_examples/clone/main.go)
- [progress](_examples/progress/main.go) | ## Basic snapshotting @@ -34,6 +34,7 @@ compatibility status with go-git. | `merge` | | ⚠️ (partial) | Fast-forward only | | | `mergetool` | | ❌ | | | | `stash` | | ❌ | | | +| `sparse-checkout` | | ✅ | | - [sparse-checkout](_examples/sparse-checkout/main.go) | | `tag` | | ✅ | | - [tag](_examples/tag/main.go)
- [tag create and push](_examples/tag-create-push/main.go) | ## Sharing and updating projects diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fce25328a..a5b01823b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -31,6 +31,13 @@ In order for a PR to be accepted it needs to pass a list of requirements: - If the PR is a new feature, it has to come with a suite of unit tests, that tests the new functionality. - In any case, all the PRs have to pass the personal evaluation of at least one of the maintainers of go-git. +### Branches + +The `master` branch is currently used for maintaining the `v5` major release only. The accepted changes would +be dependency bumps, bug fixes and small changes that aren't needed for `v6`. New development should target the +`v6-exp` branch, and if agreed with at least one go-git maintainer, it can be back ported to `v5` by creating +a new PR that targets `master`. + ### Format of the commit message Every commit message should describe what was changed, under which context and, if applicable, the GitHub issue it relates to: diff --git a/_examples/common_test.go b/_examples/common_test.go index 5e3f75381..75affcca8 100644 --- a/_examples/common_test.go +++ b/_examples/common_test.go @@ -30,9 +30,11 @@ var args = map[string][]string{ "progress": {defaultURL, tempFolder()}, "pull": {createRepositoryWithRemote(tempFolder(), defaultURL)}, "push": {setEmptyRemote(cloneRepository(defaultURL, tempFolder()))}, + "restore": {cloneRepository(defaultURL, tempFolder())}, "revision": {cloneRepository(defaultURL, tempFolder()), "master~2^"}, "sha256": {tempFolder()}, "showcase": {defaultURL, tempFolder()}, + "sparse-checkout": {defaultURL, "vendor", tempFolder()}, "tag": {cloneRepository(defaultURL, tempFolder())}, } diff --git a/_examples/restore/main.go b/_examples/restore/main.go new file mode 100644 index 000000000..8016b06f0 --- /dev/null +++ b/_examples/restore/main.go @@ -0,0 +1,103 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + "time" + + "github.com/go-git/go-git/v5" + . "github.com/go-git/go-git/v5/_examples" + "github.com/go-git/go-git/v5/plumbing/object" +) + +func prepareRepo(w *git.Worktree, directory string) { + // We need a known state of files inside the worktree for testing revert a modify and delete + Info("echo \"hello world! Modify\" > for-modify") + err := ioutil.WriteFile(filepath.Join(directory, "for-modify"), []byte("hello world! Modify"), 0644) + CheckIfError(err) + Info("git add for-modify") + _, err = w.Add("for-modify") + CheckIfError(err) + + Info("echo \"hello world! Delete\" > for-delete") + err = ioutil.WriteFile(filepath.Join(directory, "for-delete"), []byte("hello world! Delete"), 0644) + CheckIfError(err) + Info("git add for-delete") + _, err = w.Add("for-delete") + CheckIfError(err) + + Info("git commit -m \"example go-git commit\"") + _, err = w.Commit("example go-git commit", &git.CommitOptions{ + Author: &object.Signature{ + Name: "John Doe", + Email: "john@doe.org", + When: time.Now(), + }, + }) + CheckIfError(err) +} + +// An example of how to restore AKA unstage files +func main() { + CheckArgs("") + directory := os.Args[1] + + // Opens an already existing repository. + r, err := git.PlainOpen(directory) + CheckIfError(err) + + w, err := r.Worktree() + CheckIfError(err) + + prepareRepo(w, directory) + + // Perform the operation and stage them + Info("echo \"hello world! Modify 2\" > for-modify") + err = ioutil.WriteFile(filepath.Join(directory, "for-modify"), []byte("hello world! Modify 2"), 0644) + CheckIfError(err) + Info("git add for-modify") + _, err = w.Add("for-modify") + CheckIfError(err) + + Info("echo \"hello world! Add\" > for-add") + err = ioutil.WriteFile(filepath.Join(directory, "for-add"), []byte("hello world! Add"), 0644) + CheckIfError(err) + Info("git add for-add") + _, err = w.Add("for-add") + CheckIfError(err) + + Info("rm for-delete") + err = os.Remove(filepath.Join(directory, "for-delete")) + CheckIfError(err) + Info("git add for-delete") + _, err = w.Add("for-delete") + CheckIfError(err) + + // We can verify the current status of the worktree using the method Status. + Info("git status --porcelain") + status, err := w.Status() + CheckIfError(err) + fmt.Println(status) + + // Unstage a single file and see the status + Info("git restore --staged for-modify") + err = w.Restore(&git.RestoreOptions{Staged: true, Files: []string{"for-modify"}}) + CheckIfError(err) + + Info("git status --porcelain") + status, err = w.Status() + CheckIfError(err) + fmt.Println(status) + + // Unstage the other 2 files and see the status + Info("git restore --staged for-add for-delete") + err = w.Restore(&git.RestoreOptions{Staged: true, Files: []string{"for-add", "for-delete"}}) + CheckIfError(err) + + Info("git status --porcelain") + status, err = w.Status() + CheckIfError(err) + fmt.Println(status) +} diff --git a/_examples/sparse-checkout/main.go b/_examples/sparse-checkout/main.go new file mode 100644 index 000000000..1664ea897 --- /dev/null +++ b/_examples/sparse-checkout/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "os" + + "github.com/go-git/go-git/v5" + . "github.com/go-git/go-git/v5/_examples" +) + +func main() { + CheckArgs("", "", "") + url := os.Args[1] + path := os.Args[2] + directory := os.Args[3] + + Info("git clone %s %s", url, directory) + + r, err := git.PlainClone(directory, false, &git.CloneOptions{ + URL: url, + NoCheckout: true, + }) + CheckIfError(err) + + w, err := r.Worktree() + CheckIfError(err) + + err = w.Checkout(&git.CheckoutOptions{ + SparseCheckoutDirectories: []string{path}, + }) + CheckIfError(err) +} diff --git a/blame.go b/blame.go index 2a877dcdf..e3cb39aec 100644 --- a/blame.go +++ b/blame.go @@ -97,13 +97,10 @@ func Blame(c *object.Commit, path string) (*BlameResult, error) { if err != nil { return nil, err } - if finished == true { + if finished { break } } - if err != nil { - return nil, err - } b.lineToCommit = make([]*object.Commit, finalLength) for i := range needsMap { @@ -309,8 +306,8 @@ func (b *blame) addBlames(curItems []*queueItem) (bool, error) { for h := range hunks { hLines := countLines(hunks[h].Text) for hl := 0; hl < hLines; hl++ { - switch { - case hunks[h].Type == diffmatchpatch.DiffEqual: + switch hunks[h].Type { + case diffmatchpatch.DiffEqual: prevl++ curl++ if curl == curItem.NeedsMap[need].Cur { @@ -322,7 +319,7 @@ func (b *blame) addBlames(curItems []*queueItem) (bool, error) { break out } } - case hunks[h].Type == diffmatchpatch.DiffInsert: + case diffmatchpatch.DiffInsert: curl++ if curl == curItem.NeedsMap[need].Cur { // the line we want is added, it may have been added here (or by another parent), skip it for now @@ -331,7 +328,7 @@ func (b *blame) addBlames(curItems []*queueItem) (bool, error) { break out } } - case hunks[h].Type == diffmatchpatch.DiffDelete: + case diffmatchpatch.DiffDelete: prevl += hLines continue out default: diff --git a/cli/go-git/go.mod b/cli/go-git/go.mod index 33f5f2447..24f092bbd 100644 --- a/cli/go-git/go.mod +++ b/cli/go-git/go.mod @@ -1,16 +1,16 @@ module github.com/go-git/go-git/cli/go-git -go 1.19 +go 1.20 require ( - github.com/go-git/go-git/v5 v5.11.0 - github.com/jessevdk/go-flags v1.5.0 + github.com/go-git/go-git/v5 v5.12.0 + github.com/jessevdk/go-flags v1.6.1 ) require ( dario.cat/mergo v1.0.0 // indirect github.com/Microsoft/go-winio v0.6.1 // indirect - github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect + github.com/ProtonMail/go-crypto v1.0.0 // indirect github.com/cloudflare/circl v1.3.7 // indirect github.com/cyphar/filepath-securejoin v0.2.4 // indirect github.com/emirpasic/gods v1.18.1 // indirect @@ -20,13 +20,13 @@ require ( github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect github.com/kevinburke/ssh_config v1.2.0 // indirect github.com/pjbgf/sha1cd v0.3.0 // indirect - github.com/sergi/go-diff v1.1.0 // indirect - github.com/skeema/knownhosts v1.2.1 // indirect + github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect + github.com/skeema/knownhosts v1.2.2 // indirect github.com/xanzy/ssh-agent v0.3.3 // indirect - golang.org/x/crypto v0.17.0 // indirect + golang.org/x/crypto v0.31.0 // indirect golang.org/x/mod v0.12.0 // indirect - golang.org/x/net v0.19.0 // indirect - golang.org/x/sys v0.15.0 // indirect + golang.org/x/net v0.23.0 // indirect + golang.org/x/sys v0.28.0 // indirect golang.org/x/tools v0.13.0 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect ) diff --git a/cli/go-git/go.sum b/cli/go-git/go.sum index 42324f59a..a0604c4f9 100644 --- a/cli/go-git/go.sum +++ b/cli/go-git/go.sum @@ -3,8 +3,8 @@ dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY= github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow= github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= -github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 h1:kkhsdkhsCvIsutKu5zLMgWtgh9YxGCNAw8Ad8hjwfYg= -github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0= +github.com/ProtonMail/go-crypto v1.0.0 h1:LRuvITjQWX+WIfr930YHG2HNfjR1uOfyf5vE0kC2U78= +github.com/ProtonMail/go-crypto v1.0.0/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= @@ -19,21 +19,21 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcejNsXKSkQ6lcIaNec2nyfOdlTBR2lU= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= -github.com/gliderlabs/ssh v0.3.5 h1:OcaySEmAQJgyYcArR+gGGTHCyE7nvhEMTlYY+Dp8CpY= +github.com/gliderlabs/ssh v0.3.7 h1:iV3Bqi942d9huXnzEF2Mt+CY9gLu8DNM4Obd+8bODRE= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic= github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU= github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow= github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4= -github.com/go-git/go-git/v5 v5.11.0 h1:XIZc1p+8YzypNr34itUfSvYJcv+eYdTnTvOZ2vD3cA4= -github.com/go-git/go-git/v5 v5.11.0/go.mod h1:6GFcX2P3NM7FPBfpePbpLd21XxsgdAt+lKqXmCUiUCY= +github.com/go-git/go-git/v5 v5.12.0 h1:7Md+ndsjrzZxbddRDZjF14qK+NN56sy6wkqaVrjZtys= +github.com/go-git/go-git/v5 v5.12.0/go.mod h1:FTM9VKtnI2m65hNI/TenDDDnUf2Q9FHnXYjuz9i5OEY= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= -github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc= -github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= +github.com/jessevdk/go-flags v1.6.1 h1:Cvu5U8UGrLay1rZfv/zP7iLpSHGUZ/Ou68T0iX1bBK4= +github.com/jessevdk/go-flags v1.6.1/go.mod h1:Mk8T1hIAWpOiJiHa9rJASDK2UGWji0EuPGBnNLMooyc= github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= @@ -49,15 +49,15 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= -github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= -github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= +github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8= +github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= -github.com/skeema/knownhosts v1.2.1 h1:SHWdIUa82uGZz+F+47k8SY4QhhI291cXCpopT1lK2AQ= -github.com/skeema/knownhosts v1.2.1/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= +github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L3A= +github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM= github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= @@ -66,8 +66,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= -golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= -golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= +golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= @@ -79,8 +79,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= -golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= -golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= +golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -89,7 +89,6 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -99,14 +98,14 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= +golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= -golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4= +golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -114,7 +113,7 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= @@ -128,6 +127,6 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/common_test.go b/common_test.go index ff4d6b813..d1d368a7f 100644 --- a/common_test.go +++ b/common_test.go @@ -136,21 +136,6 @@ func (s *BaseSuite) GetLocalRepositoryURL(f *fixtures.Fixture) string { return f.DotGit().Root() } -func (s *BaseSuite) TemporalDir() (path string, clean func()) { - fs := osfs.New(os.TempDir()) - relPath, err := util.TempDir(fs, "", "") - if err != nil { - panic(err) - } - - path = fs.Join(fs.Root(), relPath) - clean = func() { - _ = util.RemoveAll(fs, relPath) - } - - return -} - func (s *BaseSuite) TemporalHomeDir() (path string, clean func()) { home, err := os.UserHomeDir() if err != nil { @@ -171,8 +156,8 @@ func (s *BaseSuite) TemporalHomeDir() (path string, clean func()) { return } -func (s *BaseSuite) TemporalFilesystem() (fs billy.Filesystem, clean func()) { - fs = osfs.New(os.TempDir()) +func (s *BaseSuite) TemporalFilesystem(c *C) (fs billy.Filesystem) { + fs = osfs.New(c.MkDir()) path, err := util.TempDir(fs, "", "") if err != nil { panic(err) @@ -183,10 +168,6 @@ func (s *BaseSuite) TemporalFilesystem() (fs billy.Filesystem, clean func()) { panic(err) } - clean = func() { - _ = util.RemoveAll(fs, path) - } - return } diff --git a/config/config.go b/config/config.go index 6d41c15dc..33f6e37d2 100644 --- a/config/config.go +++ b/config/config.go @@ -252,6 +252,7 @@ const ( extensionsSection = "extensions" fetchKey = "fetch" urlKey = "url" + pushurlKey = "pushurl" bareKey = "bare" worktreeKey = "worktree" commentCharKey = "commentChar" @@ -633,6 +634,7 @@ func (c *RemoteConfig) unmarshal(s *format.Subsection) error { c.Name = c.raw.Name c.URLs = append([]string(nil), c.raw.Options.GetAll(urlKey)...) + c.URLs = append(c.URLs, c.raw.Options.GetAll(pushurlKey)...) c.Fetch = fetch c.Mirror = c.raw.Options.Get(mirrorKey) == "true" diff --git a/config/config_test.go b/config/config_test.go index 7e9483f6f..f947850eb 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -367,7 +367,31 @@ func (s *ConfigSuite) TestRemoveUrlOptions(c *C) { buf, err = cfg.Marshal() c.Assert(err, IsNil) if strings.Contains(string(buf), "url") { - c.Fatal("conifg should not contain any url sections") + c.Fatal("config should not contain any url sections") } c.Assert(err, IsNil) } + +func (s *ConfigSuite) TestUnmarshalRemotes(c *C) { + input := []byte(`[core] + bare = true + worktree = foo + custom = ignored +[user] + name = John Doe + email = john@example.com +[remote "origin"] + url = https://git.sr.ht/~mcepl/go-git + pushurl = git@git.sr.ht:~mcepl/go-git.git + fetch = +refs/heads/*:refs/remotes/origin/* + mirror = true +`) + + cfg := NewConfig() + err := cfg.Unmarshal(input) + c.Assert(err, IsNil) + + c.Assert(cfg.Remotes["origin"].URLs[0], Equals, "https://git.sr.ht/~mcepl/go-git") + c.Assert(cfg.Remotes["origin"].URLs[1], Equals, "git@git.sr.ht:~mcepl/go-git.git") +} + diff --git a/example_test.go b/example_test.go index 27ea4a2d5..7b6adc5a6 100644 --- a/example_test.go +++ b/example_test.go @@ -137,6 +137,21 @@ func ExampleRepository_References() { } +func ExampleRepository_Branches() { + r, _ := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{ + URL: "https://github.com/git-fixtures/basic.git", + }) + + branches, _ := r.Branches() + branches.ForEach(func(branch *plumbing.Reference) error { + fmt.Println(branch.Hash().String(), branch.Name()) + return nil + }) + + // Example Output: + // 6ecf0ef2c2dffb796033e5a02219af86ec6584e5 refs/heads/master +} + func ExampleRepository_CreateRemote() { r, _ := git.Init(memory.NewStorage(), nil) diff --git a/go.mod b/go.mod index 3270b9279..85f9160f3 100644 --- a/go.mod +++ b/go.mod @@ -1,31 +1,31 @@ module github.com/go-git/go-git/v5 // go-git supports the last 3 stable Go versions. -go 1.19 +go 1.21 require ( dario.cat/mergo v1.0.0 - github.com/ProtonMail/go-crypto v1.0.0 + github.com/ProtonMail/go-crypto v1.1.5 github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 - github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a + github.com/elazarl/goproxy v1.4.0 github.com/emirpasic/gods v1.18.1 - github.com/gliderlabs/ssh v0.3.7 + github.com/gliderlabs/ssh v0.3.8 github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 - github.com/go-git/go-billy/v5 v5.5.0 + github.com/go-git/go-billy/v5 v5.6.2 github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da github.com/google/go-cmp v0.6.0 github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 github.com/kevinburke/ssh_config v1.2.0 - github.com/pjbgf/sha1cd v0.3.0 + github.com/pjbgf/sha1cd v0.3.2 github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 - github.com/skeema/knownhosts v1.2.2 - github.com/stretchr/testify v1.9.0 + github.com/skeema/knownhosts v1.3.0 + github.com/stretchr/testify v1.10.0 github.com/xanzy/ssh-agent v0.3.3 - golang.org/x/crypto v0.21.0 - golang.org/x/net v0.22.0 - golang.org/x/sys v0.18.0 - golang.org/x/text v0.14.0 + golang.org/x/crypto v0.32.0 + golang.org/x/net v0.34.0 + golang.org/x/sys v0.29.0 + golang.org/x/text v0.21.0 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c ) @@ -33,14 +33,15 @@ require ( github.com/Microsoft/go-winio v0.6.1 // indirect github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect github.com/cloudflare/circl v1.3.7 // indirect - github.com/cyphar/filepath-securejoin v0.2.4 // indirect + github.com/cyphar/filepath-securejoin v0.3.6 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/kr/pretty v0.3.1 // indirect github.com/kr/text v0.2.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/rogpeppe/go-internal v1.11.0 // indirect - golang.org/x/mod v0.12.0 // indirect - golang.org/x/tools v0.13.0 // indirect + github.com/rogpeppe/go-internal v1.12.0 // indirect + golang.org/x/mod v0.17.0 // indirect + golang.org/x/sync v0.10.0 // indirect + golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 0a2b9d442..120bcf381 100644 --- a/go.sum +++ b/go.sum @@ -3,34 +3,30 @@ dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY= github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow= github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= -github.com/ProtonMail/go-crypto v1.0.0 h1:LRuvITjQWX+WIfr930YHG2HNfjR1uOfyf5vE0kC2U78= -github.com/ProtonMail/go-crypto v1.0.0/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0= +github.com/ProtonMail/go-crypto v1.1.5 h1:eoAQfK2dwL+tFSFpr7TbOaPNUbPiJj4fLYwwGE1FQO4= +github.com/ProtonMail/go-crypto v1.1.5/go.mod h1:rA3QumHc/FZ8pAHreoekgiAbzpNsfQAosU5td4SnOrE= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= -github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= -github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU= github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg= -github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= +github.com/cyphar/filepath-securejoin v0.3.6 h1:4d9N5ykBnSp5Xn2JkhocYDkOpURL/18CYMpo6xB9uWM= +github.com/cyphar/filepath-securejoin v0.3.6/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGLDGQL7h7bg04C/+u9jI= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcejNsXKSkQ6lcIaNec2nyfOdlTBR2lU= -github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= -github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2 h1:dWB6v3RcOy03t/bUadywsbyrQwCqZeNIEX6M1OtSZOM= -github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8= +github.com/elazarl/goproxy v1.4.0 h1:4GyuSbFa+s26+3rmYNSuUVsx+HgPrV1bk1jXI0l9wjM= +github.com/elazarl/goproxy v1.4.0/go.mod h1:X/5W/t+gzDyLfHW4DrMdpjqYjpXsURlBt9lpBDxZZZQ= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= -github.com/gliderlabs/ssh v0.3.7 h1:iV3Bqi942d9huXnzEF2Mt+CY9gLu8DNM4Obd+8bODRE= -github.com/gliderlabs/ssh v0.3.7/go.mod h1:zpHEXBstFnQYtGnB8k8kQLol82umzn/2/snG7alWVD8= +github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c= +github.com/gliderlabs/ssh v0.3.8/go.mod h1:xYoytBv1sV0aL3CavoDuJIQNURXkkfPA/wxQ1pL1fAU= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic= -github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU= -github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow= +github.com/go-git/go-billy/v5 v5.6.2 h1:6Q86EsPXMa7c3YZ3aLAQsMA0VlWmy43r6FHqa/UNbRM= +github.com/go-git/go-billy/v5 v5.6.2/go.mod h1:rcFC2rAsp/erv7CMz9GczHcuD0D32fWzH+MJAU+jaUU= github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4= github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= @@ -49,92 +45,59 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI= -github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4= -github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI= +github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k= +github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY= +github.com/pjbgf/sha1cd v0.3.2 h1:a9wb0bp1oC2TGwStyn0Umc/IGKQnEgF0vVaZ8QF8eo4= +github.com/pjbgf/sha1cd v0.3.2/go.mod h1:zQWigSxVmsHEZow5qaLtPYxpcKMMQpa09ixqBxuCS6A= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= -github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= -github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= +github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8= github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= -github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L3A= -github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= +github.com/skeema/knownhosts v1.3.0 h1:AM+y0rI04VksttfwjkSTNQorvGqmwATnvnAHpSgc0LY= +github.com/skeema/knownhosts v1.3.0/go.mod h1:sPINvnADmT/qYH1kfv+ePMmOBTH6Tbl7b5LvTDjFK7M= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM= github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= -golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= -golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= -golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= -golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc= +golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc= +golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8= +golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY= +golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= +golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= -golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= -golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= -golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0= +golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k= +golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= +golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= -golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= +golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= -golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= -golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= -golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/term v0.28.0 h1:/Ts8HFuMR2E6IP/jlo7QVLZHggjKQbhu/7H0LJFr3Gg= +golang.org/x/term v0.28.0/go.mod h1:Sw/lC2IAUZ92udQNf3WodGtn4k/XoLyZoh8v/8uiwek= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= +golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= -golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg= +golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= diff --git a/internal/revision/parser_test.go b/internal/revision/parser_test.go index 1eb386100..0435348de 100644 --- a/internal/revision/parser_test.go +++ b/internal/revision/parser_test.go @@ -400,6 +400,14 @@ func (s *ParserSuite) TestParseRefWithInvalidName(c *C) { } func FuzzParser(f *testing.F) { + f.Add("@{2016-12-16T21:42:47Z}") + f.Add("@~3") + f.Add("v0.99.8^{}") + f.Add("master:./README") + f.Add("HEAD^{/fix nasty bug}") + f.Add("HEAD^{/[A-") + f.Add(":/fix nasty bug") + f.Add(":/[A-") f.Fuzz(func(t *testing.T, input string) { parser := NewParser(bytes.NewBufferString(input)) diff --git a/internal/revision/scanner.go b/internal/revision/scanner.go index c46c21b79..2444f33ec 100644 --- a/internal/revision/scanner.go +++ b/internal/revision/scanner.go @@ -43,6 +43,11 @@ func tokenizeExpression(ch rune, tokenType token, check runeCategoryValidator, r return tokenType, string(data), nil } +// maxRevisionLength holds the maximum length that will be parsed for a +// revision. Git itself doesn't enforce a max length, but rather leans on +// the OS to enforce it via its ARG_MAX. +const maxRevisionLength = 128 * 1024 // 128kb + var zeroRune = rune(0) // scanner represents a lexical scanner. @@ -52,7 +57,7 @@ type scanner struct { // newScanner returns a new instance of scanner. func newScanner(r io.Reader) *scanner { - return &scanner{r: bufio.NewReader(r)} + return &scanner{r: bufio.NewReader(io.LimitReader(r, maxRevisionLength))} } // Scan extracts tokens and their strings counterpart diff --git a/internal/test/checkers.go b/internal/test/checkers.go new file mode 100644 index 000000000..257d93d8c --- /dev/null +++ b/internal/test/checkers.go @@ -0,0 +1,43 @@ +package test + +import ( + "errors" + "fmt" + + check "gopkg.in/check.v1" +) + +// This check.Checker implementation exists because there's no implementation +// in the library that compares errors using `errors.Is`. If / when the check +// library fixes https://github.com/go-check/check/issues/139, this code can +// likely be removed and replaced with the library implementation. +// +// Added in Go 1.13 [https://go.dev/blog/go1.13-errors] `errors.Is` is the +// best mechanism to use to compare errors that might be wrapped in other +// errors. +type errorIsChecker struct { + *check.CheckerInfo +} + +var ErrorIs check.Checker = errorIsChecker{ + &check.CheckerInfo{ + Name: "ErrorIs", + Params: []string{"obtained", "expected"}, + }, +} + +func (e errorIsChecker) Check(params []interface{}, names []string) (bool, string) { + obtained, ok := params[0].(error) + if !ok { + return false, "obtained is not an error" + } + expected, ok := params[1].(error) + if !ok { + return false, "expected is not an error" + } + + if !errors.Is(obtained, expected) { + return false, fmt.Sprintf("obtained: %+v expected: %+v", obtained, expected) + } + return true, "" +} diff --git a/options.go b/options.go index d7776dad5..3cd0f952c 100644 --- a/options.go +++ b/options.go @@ -416,6 +416,9 @@ type ResetOptions struct { // the index (resetting it to the tree of Commit) and the working tree // depending on Mode. If empty MixedReset is used. Mode ResetMode + // Files, if not empty will constrain the reseting the index to only files + // specified in this list. + Files []string } // Validate validates the fields and sets the default values. @@ -790,3 +793,26 @@ type PlainInitOptions struct { // Validate validates the fields and sets the default values. func (o *PlainInitOptions) Validate() error { return nil } + +var ( + ErrNoRestorePaths = errors.New("you must specify path(s) to restore") +) + +// RestoreOptions describes how a restore should be performed. +type RestoreOptions struct { + // Marks to restore the content in the index + Staged bool + // Marks to restore the content of the working tree + Worktree bool + // List of file paths that will be restored + Files []string +} + +// Validate validates the fields and sets the default values. +func (o *RestoreOptions) Validate() error { + if len(o.Files) == 0 { + return ErrNoRestorePaths + } + + return nil +} diff --git a/options_test.go b/options_test.go index 677c31719..36970ee7d 100644 --- a/options_test.go +++ b/options_test.go @@ -97,7 +97,7 @@ func (s *OptionsSuite) TestCreateTagOptionsLoadGlobal(c *C) { } func (s *OptionsSuite) writeGlobalConfig(c *C, cfg *config.Config) func() { - fs, clean := s.TemporalFilesystem() + fs := s.TemporalFilesystem(c) tmp, err := util.TempDir(fs, "", "test-options") c.Assert(err, IsNil) @@ -115,7 +115,6 @@ func (s *OptionsSuite) writeGlobalConfig(c *C, cfg *config.Config) func() { c.Assert(err, IsNil) return func() { - clean() os.Setenv("XDG_CONFIG_HOME", "") } diff --git a/plumbing/format/gitattributes/dir.go b/plumbing/format/gitattributes/dir.go index d3b0dbefd..42381965c 100644 --- a/plumbing/format/gitattributes/dir.go +++ b/plumbing/format/gitattributes/dir.go @@ -2,6 +2,8 @@ package gitattributes import ( "os" + "path/filepath" + "strings" "github.com/go-git/go-billy/v5" @@ -59,7 +61,14 @@ func walkDirectory(fs billy.Filesystem, root []string) (attributes []MatchAttrib continue } - path := append(root, fi.Name()) + p := fi.Name() + + // Handles the case whereby just the volume name ("C:") is appended, + // to root. Change it to "C:\", which is better handled by fs.Join(). + if filepath.VolumeName(p) != "" && !strings.HasSuffix(p, string(filepath.Separator)) { + p = p + string(filepath.Separator) + } + path := append(root, p) dirAttributes, err := ReadAttributesFile(fs, path, gitattributesFile, false) if err != nil { diff --git a/plumbing/format/gitignore/dir.go b/plumbing/format/gitignore/dir.go index aca5d0dbd..92df5a3de 100644 --- a/plumbing/format/gitignore/dir.go +++ b/plumbing/format/gitignore/dir.go @@ -64,6 +64,10 @@ func ReadPatterns(fs billy.Filesystem, path []string) (ps []Pattern, err error) for _, fi := range fis { if fi.IsDir() && fi.Name() != gitDir { + if NewMatcher(ps).Match(append(path, fi.Name()), true) { + continue + } + var subps []Pattern subps, err = ReadPatterns(fs, append(path, fi.Name())) if err != nil { diff --git a/plumbing/format/gitignore/dir_test.go b/plumbing/format/gitignore/dir_test.go index 465c5713a..ba8ad806e 100644 --- a/plumbing/format/gitignore/dir_test.go +++ b/plumbing/format/gitignore/dir_test.go @@ -44,6 +44,8 @@ func (s *MatcherSuite) SetUpTest(c *C) { c.Assert(err, IsNil) _, err = f.Write([]byte("ignore.crlf\r\n")) c.Assert(err, IsNil) + _, err = f.Write([]byte("ignore_dir\n")) + c.Assert(err, IsNil) err = f.Close() c.Assert(err, IsNil) @@ -56,6 +58,17 @@ func (s *MatcherSuite) SetUpTest(c *C) { err = f.Close() c.Assert(err, IsNil) + err = fs.MkdirAll("ignore_dir", os.ModePerm) + c.Assert(err, IsNil) + f, err = fs.Create("ignore_dir/.gitignore") + c.Assert(err, IsNil) + _, err = f.Write([]byte("!file\n")) + c.Assert(err, IsNil) + _, err = fs.Create("ignore_dir/file") + c.Assert(err, IsNil) + err = f.Close() + c.Assert(err, IsNil) + err = fs.MkdirAll("another", os.ModePerm) c.Assert(err, IsNil) err = fs.MkdirAll("exclude.crlf", os.ModePerm) @@ -267,12 +280,13 @@ func (s *MatcherSuite) SetUpTest(c *C) { func (s *MatcherSuite) TestDir_ReadPatterns(c *C) { checkPatterns := func(ps []Pattern) { - c.Assert(ps, HasLen, 6) + c.Assert(ps, HasLen, 7) m := NewMatcher(ps) c.Assert(m.Match([]string{"exclude.crlf"}, true), Equals, true) c.Assert(m.Match([]string{"ignore.crlf"}, true), Equals, true) c.Assert(m.Match([]string{"vendor", "gopkg.in"}, true), Equals, true) + c.Assert(m.Match([]string{"ignore_dir", "file"}, false), Equals, true) c.Assert(m.Match([]string{"vendor", "github.com"}, true), Equals, false) c.Assert(m.Match([]string{"multiple", "sub", "ignores", "first", "ignore_dir"}, true), Equals, true) c.Assert(m.Match([]string{"multiple", "sub", "ignores", "second", "ignore_dir"}, true), Equals, true) diff --git a/plumbing/format/index/decoder.go b/plumbing/format/index/decoder.go index 6778cf74e..fc25d3702 100644 --- a/plumbing/format/index/decoder.go +++ b/plumbing/format/index/decoder.go @@ -24,8 +24,8 @@ var ( // ErrInvalidChecksum is returned by Decode if the SHA1 hash mismatch with // the read content ErrInvalidChecksum = errors.New("invalid checksum") - - errUnknownExtension = errors.New("unknown extension") + // ErrUnknownExtension is returned when an index extension is encountered that is considered mandatory + ErrUnknownExtension = errors.New("unknown extension") ) const ( @@ -39,6 +39,7 @@ const ( // A Decoder reads and decodes index files from an input stream. type Decoder struct { + buf *bufio.Reader r io.Reader hash hash.Hash lastEntry *Entry @@ -49,8 +50,10 @@ type Decoder struct { // NewDecoder returns a new decoder that reads from r. func NewDecoder(r io.Reader) *Decoder { h := hash.New(hash.CryptoType) + buf := bufio.NewReader(r) return &Decoder{ - r: io.TeeReader(r, h), + buf: buf, + r: io.TeeReader(buf, h), hash: h, extReader: bufio.NewReader(nil), } @@ -210,71 +213,75 @@ func (d *Decoder) readExtensions(idx *Index) error { // count that they are not supported by jgit or libgit var expected []byte + var peeked []byte var err error - var header [4]byte + // we should always be able to peek for 4 bytes (header) + 4 bytes (extlen) + final hash + // if this fails, we know that we're at the end of the index + peekLen := 4 + 4 + d.hash.Size() + for { expected = d.hash.Sum(nil) - - var n int - if n, err = io.ReadFull(d.r, header[:]); err != nil { - if n == 0 { - err = io.EOF - } - + peeked, err = d.buf.Peek(peekLen) + if len(peeked) < peekLen { + // there can't be an extension at this point, so let's bail out break } + if err != nil { + return err + } - err = d.readExtension(idx, header[:]) + err = d.readExtension(idx) if err != nil { - break + return err } } - if err != errUnknownExtension { + return d.readChecksum(expected) +} + +func (d *Decoder) readExtension(idx *Index) error { + var header [4]byte + + if _, err := io.ReadFull(d.r, header[:]); err != nil { return err } - return d.readChecksum(expected, header) -} + r, err := d.getExtensionReader() + if err != nil { + return err + } -func (d *Decoder) readExtension(idx *Index, header []byte) error { switch { - case bytes.Equal(header, treeExtSignature): - r, err := d.getExtensionReader() - if err != nil { - return err - } - + case bytes.Equal(header[:], treeExtSignature): idx.Cache = &Tree{} d := &treeExtensionDecoder{r} if err := d.Decode(idx.Cache); err != nil { return err } - case bytes.Equal(header, resolveUndoExtSignature): - r, err := d.getExtensionReader() - if err != nil { - return err - } - + case bytes.Equal(header[:], resolveUndoExtSignature): idx.ResolveUndo = &ResolveUndo{} d := &resolveUndoDecoder{r} if err := d.Decode(idx.ResolveUndo); err != nil { return err } - case bytes.Equal(header, endOfIndexEntryExtSignature): - r, err := d.getExtensionReader() - if err != nil { - return err - } - + case bytes.Equal(header[:], endOfIndexEntryExtSignature): idx.EndOfIndexEntry = &EndOfIndexEntry{} d := &endOfIndexEntryDecoder{r} if err := d.Decode(idx.EndOfIndexEntry); err != nil { return err } default: - return errUnknownExtension + // See https://git-scm.com/docs/index-format, which says: + // If the first byte is 'A'..'Z' the extension is optional and can be ignored. + if header[0] < 'A' || header[0] > 'Z' { + return ErrUnknownExtension + } + + d := &unknownExtensionDecoder{r} + if err := d.Decode(); err != nil { + return err + } } return nil @@ -290,11 +297,10 @@ func (d *Decoder) getExtensionReader() (*bufio.Reader, error) { return d.extReader, nil } -func (d *Decoder) readChecksum(expected []byte, alreadyRead [4]byte) error { +func (d *Decoder) readChecksum(expected []byte) error { var h plumbing.Hash - copy(h[:4], alreadyRead[:]) - if _, err := io.ReadFull(d.r, h[4:]); err != nil { + if _, err := io.ReadFull(d.r, h[:]); err != nil { return err } @@ -476,3 +482,22 @@ func (d *endOfIndexEntryDecoder) Decode(e *EndOfIndexEntry) error { _, err = io.ReadFull(d.r, e.Hash[:]) return err } + +type unknownExtensionDecoder struct { + r *bufio.Reader +} + +func (d *unknownExtensionDecoder) Decode() error { + var buf [1024]byte + + for { + _, err := d.r.Read(buf[:]) + if err == io.EOF { + break + } + if err != nil { + return err + } + } + return nil +} diff --git a/plumbing/format/index/decoder_test.go b/plumbing/format/index/decoder_test.go index 39ab3361f..4adddda09 100644 --- a/plumbing/format/index/decoder_test.go +++ b/plumbing/format/index/decoder_test.go @@ -1,6 +1,11 @@ package index import ( + "bytes" + "crypto" + "github.com/go-git/go-git/v5/plumbing/hash" + "github.com/go-git/go-git/v5/utils/binary" + "io" "testing" "github.com/go-git/go-git/v5/plumbing" @@ -218,3 +223,100 @@ func (s *IndexSuite) TestDecodeEndOfIndexEntry(c *C) { c.Assert(idx.EndOfIndexEntry.Offset, Equals, uint32(716)) c.Assert(idx.EndOfIndexEntry.Hash.String(), Equals, "922e89d9ffd7cefce93a211615b2053c0f42bd78") } + +func (s *IndexSuite) readSimpleIndex(c *C) *Index { + f, err := fixtures.Basic().One().DotGit().Open("index") + c.Assert(err, IsNil) + defer func() { c.Assert(f.Close(), IsNil) }() + + idx := &Index{} + d := NewDecoder(f) + err = d.Decode(idx) + c.Assert(err, IsNil) + + return idx +} + +func (s *IndexSuite) buildIndexWithExtension(c *C, signature string, data string) []byte { + idx := s.readSimpleIndex(c) + + buf := bytes.NewBuffer(nil) + e := NewEncoder(buf) + + err := e.encode(idx, false) + c.Assert(err, IsNil) + err = e.encodeRawExtension(signature, []byte(data)) + c.Assert(err, IsNil) + + err = e.encodeFooter() + c.Assert(err, IsNil) + + return buf.Bytes() +} + +func (s *IndexSuite) TestDecodeUnknownOptionalExt(c *C) { + f := bytes.NewReader(s.buildIndexWithExtension(c, "TEST", "testdata")) + + idx := &Index{} + d := NewDecoder(f) + err := d.Decode(idx) + c.Assert(err, IsNil) +} + +func (s *IndexSuite) TestDecodeUnknownMandatoryExt(c *C) { + f := bytes.NewReader(s.buildIndexWithExtension(c, "test", "testdata")) + + idx := &Index{} + d := NewDecoder(f) + err := d.Decode(idx) + c.Assert(err, ErrorMatches, ErrUnknownExtension.Error()) +} + +func (s *IndexSuite) TestDecodeTruncatedExt(c *C) { + idx := s.readSimpleIndex(c) + + buf := bytes.NewBuffer(nil) + e := NewEncoder(buf) + + err := e.encode(idx, false) + c.Assert(err, IsNil) + + _, err = e.w.Write([]byte("TEST")) + c.Assert(err, IsNil) + + err = binary.WriteUint32(e.w, uint32(100)) + c.Assert(err, IsNil) + + _, err = e.w.Write([]byte("truncated")) + c.Assert(err, IsNil) + + err = e.encodeFooter() + c.Assert(err, IsNil) + + idx = &Index{} + d := NewDecoder(buf) + err = d.Decode(idx) + c.Assert(err, ErrorMatches, io.EOF.Error()) +} + +func (s *IndexSuite) TestDecodeInvalidHash(c *C) { + idx := s.readSimpleIndex(c) + + buf := bytes.NewBuffer(nil) + e := NewEncoder(buf) + + err := e.encode(idx, false) + c.Assert(err, IsNil) + + err = e.encodeRawExtension("TEST", []byte("testdata")) + c.Assert(err, IsNil) + + h := hash.New(crypto.SHA1) + err = binary.Write(e.w, h.Sum(nil)) + c.Assert(err, IsNil) + + idx = &Index{} + d := NewDecoder(buf) + err = d.Decode(idx) + c.Assert(err, ErrorMatches, ErrInvalidChecksum.Error()) +} diff --git a/plumbing/format/index/encoder.go b/plumbing/format/index/encoder.go index fa2d81445..c232e0323 100644 --- a/plumbing/format/index/encoder.go +++ b/plumbing/format/index/encoder.go @@ -3,8 +3,11 @@ package index import ( "bytes" "errors" + "fmt" "io" + "path" "sort" + "strings" "time" "github.com/go-git/go-git/v5/plumbing/hash" @@ -13,7 +16,7 @@ import ( var ( // EncodeVersionSupported is the range of supported index versions - EncodeVersionSupported uint32 = 3 + EncodeVersionSupported uint32 = 4 // ErrInvalidTimestamp is returned by Encode if a Index with a Entry with // negative timestamp values @@ -22,20 +25,25 @@ var ( // An Encoder writes an Index to an output stream. type Encoder struct { - w io.Writer - hash hash.Hash + w io.Writer + hash hash.Hash + lastEntry *Entry } // NewEncoder returns a new encoder that writes to w. func NewEncoder(w io.Writer) *Encoder { h := hash.New(hash.CryptoType) mw := io.MultiWriter(w, h) - return &Encoder{mw, h} + return &Encoder{mw, h, nil} } // Encode writes the Index to the stream of the encoder. func (e *Encoder) Encode(idx *Index) error { - // TODO: support v4 + return e.encode(idx, true) +} + +func (e *Encoder) encode(idx *Index, footer bool) error { + // TODO: support extensions if idx.Version > EncodeVersionSupported { return ErrUnsupportedVersion @@ -49,7 +57,10 @@ func (e *Encoder) Encode(idx *Index) error { return err } - return e.encodeFooter() + if footer { + return e.encodeFooter() + } + return nil } func (e *Encoder) encodeHeader(idx *Index) error { @@ -64,7 +75,7 @@ func (e *Encoder) encodeEntries(idx *Index) error { sort.Sort(byName(idx.Entries)) for _, entry := range idx.Entries { - if err := e.encodeEntry(entry); err != nil { + if err := e.encodeEntry(idx, entry); err != nil { return err } entryLength := entryHeaderLength @@ -73,7 +84,7 @@ func (e *Encoder) encodeEntries(idx *Index) error { } wrote := entryLength + len(entry.Name) - if err := e.padEntry(wrote); err != nil { + if err := e.padEntry(idx, wrote); err != nil { return err } } @@ -81,7 +92,7 @@ func (e *Encoder) encodeEntries(idx *Index) error { return nil } -func (e *Encoder) encodeEntry(entry *Entry) error { +func (e *Encoder) encodeEntry(idx *Index, entry *Entry) error { sec, nsec, err := e.timeToUint32(&entry.CreatedAt) if err != nil { return err @@ -132,9 +143,68 @@ func (e *Encoder) encodeEntry(entry *Entry) error { return err } + switch idx.Version { + case 2, 3: + err = e.encodeEntryName(entry) + case 4: + err = e.encodeEntryNameV4(entry) + default: + err = ErrUnsupportedVersion + } + + return err +} + +func (e *Encoder) encodeEntryName(entry *Entry) error { return binary.Write(e.w, []byte(entry.Name)) } +func (e *Encoder) encodeEntryNameV4(entry *Entry) error { + name := entry.Name + l := 0 + if e.lastEntry != nil { + dir := path.Dir(e.lastEntry.Name) + "/" + if strings.HasPrefix(entry.Name, dir) { + l = len(e.lastEntry.Name) - len(dir) + name = strings.TrimPrefix(entry.Name, dir) + } else { + l = len(e.lastEntry.Name) + } + } + + e.lastEntry = entry + + err := binary.WriteVariableWidthInt(e.w, int64(l)) + if err != nil { + return err + } + + return binary.Write(e.w, []byte(name+string('\x00'))) +} + +func (e *Encoder) encodeRawExtension(signature string, data []byte) error { + if len(signature) != 4 { + return fmt.Errorf("invalid signature length") + } + + _, err := e.w.Write([]byte(signature)) + if err != nil { + return err + } + + err = binary.WriteUint32(e.w, uint32(len(data))) + if err != nil { + return err + } + + _, err = e.w.Write(data) + if err != nil { + return err + } + + return nil +} + func (e *Encoder) timeToUint32(t *time.Time) (uint32, uint32, error) { if t.IsZero() { return 0, 0, nil @@ -147,7 +217,11 @@ func (e *Encoder) timeToUint32(t *time.Time) (uint32, uint32, error) { return uint32(t.Unix()), uint32(t.Nanosecond()), nil } -func (e *Encoder) padEntry(wrote int) error { +func (e *Encoder) padEntry(idx *Index, wrote int) error { + if idx.Version == 4 { + return nil + } + padLen := 8 - wrote%8 _, err := e.w.Write(bytes.Repeat([]byte{'\x00'}, padLen)) diff --git a/plumbing/format/index/encoder_test.go b/plumbing/format/index/encoder_test.go index 25c24f14f..35052ff2f 100644 --- a/plumbing/format/index/encoder_test.go +++ b/plumbing/format/index/encoder_test.go @@ -56,8 +56,64 @@ func (s *IndexSuite) TestEncode(c *C) { } +func (s *IndexSuite) TestEncodeV4(c *C) { + idx := &Index{ + Version: 4, + Entries: []*Entry{{ + CreatedAt: time.Now(), + ModifiedAt: time.Now(), + Dev: 4242, + Inode: 424242, + UID: 84, + GID: 8484, + Size: 42, + Stage: TheirMode, + Hash: plumbing.NewHash("e25b29c8946e0e192fae2edc1dabf7be71e8ecf3"), + Name: "foo", + }, { + CreatedAt: time.Now(), + ModifiedAt: time.Now(), + Name: "bar", + Size: 82, + }, { + CreatedAt: time.Now(), + ModifiedAt: time.Now(), + Name: strings.Repeat(" ", 20), + Size: 82, + }, { + CreatedAt: time.Now(), + ModifiedAt: time.Now(), + Name: "baz/bar", + Size: 82, + }, { + CreatedAt: time.Now(), + ModifiedAt: time.Now(), + Name: "baz/bar/bar", + Size: 82, + }}, + } + + buf := bytes.NewBuffer(nil) + e := NewEncoder(buf) + err := e.Encode(idx) + c.Assert(err, IsNil) + + output := &Index{} + d := NewDecoder(buf) + err = d.Decode(output) + c.Assert(err, IsNil) + + c.Assert(cmp.Equal(idx, output), Equals, true) + + c.Assert(output.Entries[0].Name, Equals, strings.Repeat(" ", 20)) + c.Assert(output.Entries[1].Name, Equals, "bar") + c.Assert(output.Entries[2].Name, Equals, "baz/bar") + c.Assert(output.Entries[3].Name, Equals, "baz/bar/bar") + c.Assert(output.Entries[4].Name, Equals, "foo") +} + func (s *IndexSuite) TestEncodeUnsupportedVersion(c *C) { - idx := &Index{Version: 4} + idx := &Index{Version: 5} buf := bytes.NewBuffer(nil) e := NewEncoder(buf) diff --git a/plumbing/format/packfile/delta_index.go b/plumbing/format/packfile/delta_index.go index 07a61120e..a60ec0b24 100644 --- a/plumbing/format/packfile/delta_index.go +++ b/plumbing/format/packfile/delta_index.go @@ -32,19 +32,17 @@ func (idx *deltaIndex) findMatch(src, tgt []byte, tgtOffset int) (srcOffset, l i return 0, -1 } - if len(tgt) >= tgtOffset+s && len(src) >= blksz { - h := hashBlock(tgt, tgtOffset) - tIdx := h & idx.mask - eIdx := idx.table[tIdx] - if eIdx != 0 { - srcOffset = idx.entries[eIdx] - } else { - return - } - - l = matchLength(src, tgt, tgtOffset, srcOffset) + h := hashBlock(tgt, tgtOffset) + tIdx := h & idx.mask + eIdx := idx.table[tIdx] + if eIdx == 0 { + return } + srcOffset = idx.entries[eIdx] + + l = matchLength(src, tgt, tgtOffset, srcOffset) + return } diff --git a/plumbing/format/packfile/delta_test.go b/plumbing/format/packfile/delta_test.go index 9417e558a..848a77300 100644 --- a/plumbing/format/packfile/delta_test.go +++ b/plumbing/format/packfile/delta_test.go @@ -179,12 +179,12 @@ func (s *DeltaSuite) TestMaxCopySizeDeltaReader(c *C) { } func FuzzPatchDelta(f *testing.F) { + f.Add([]byte("some value"), []byte("\n\f\fsomenewvalue")) + f.Add([]byte("some value"), []byte("\n\x0e\x0evalue")) + f.Add([]byte("some value"), []byte("\n\x0e\x0eva")) + f.Add([]byte("some value"), []byte("\n\x80\x80\x80\x80\x80\x802\x7fvalue")) - f.Fuzz(func(t *testing.T, input []byte) { - - input_0 := input[:len(input)/2] - input_1 := input[len(input)/2:] - - PatchDelta(input_0, input_1) + f.Fuzz(func(t *testing.T, input1, input2 []byte) { + PatchDelta(input1, input2) }) } diff --git a/plumbing/format/packfile/parser_test.go b/plumbing/format/packfile/parser_test.go index b8d080f68..41d990363 100644 --- a/plumbing/format/packfile/parser_test.go +++ b/plumbing/format/packfile/parser_test.go @@ -82,7 +82,7 @@ func (s *ParserSuite) TestParserHashes(c *C) { } func (s *ParserSuite) TestThinPack(c *C) { - fs := osfs.New(os.TempDir()) + fs := osfs.New(c.MkDir()) path, err := util.TempDir(fs, "", "") c.Assert(err, IsNil) diff --git a/plumbing/format/packfile/patch_delta.go b/plumbing/format/packfile/patch_delta.go index 960769c7c..a9c6b9b56 100644 --- a/plumbing/format/packfile/patch_delta.go +++ b/plumbing/format/packfile/patch_delta.go @@ -26,6 +26,13 @@ var ( const ( payload = 0x7f // 0111 1111 continuation = 0x80 // 1000 0000 + + // maxPatchPreemptionSize defines what is the max size of bytes to be + // premptively made available for a patch operation. + maxPatchPreemptionSize uint = 65536 + + // minDeltaSize defines the smallest size for a delta. + minDeltaSize = 4 ) type offset struct { @@ -86,9 +93,13 @@ func ApplyDelta(target, base plumbing.EncodedObject, delta []byte) (err error) { } // PatchDelta returns the result of applying the modification deltas in delta to src. -// An error will be returned if delta is corrupted (ErrDeltaLen) or an action command +// An error will be returned if delta is corrupted (ErrInvalidDelta) or an action command // is not copy from source or copy from delta (ErrDeltaCmd). func PatchDelta(src, delta []byte) ([]byte, error) { + if len(src) == 0 || len(delta) < minDeltaSize { + return nil, ErrInvalidDelta + } + b := &bytes.Buffer{} if err := patchDelta(b, src, delta); err != nil { return nil, err @@ -239,7 +250,9 @@ func patchDelta(dst *bytes.Buffer, src, delta []byte) error { remainingTargetSz := targetSz var cmd byte - dst.Grow(int(targetSz)) + + growSz := min(targetSz, maxPatchPreemptionSize) + dst.Grow(int(growSz)) for { if len(delta) == 0 { return ErrInvalidDelta @@ -403,6 +416,10 @@ func patchDeltaWriter(dst io.Writer, base io.ReaderAt, delta io.Reader, // This must be called twice on the delta data buffer, first to get the // expected source buffer size, and again to get the target buffer size. func decodeLEB128(input []byte) (uint, []byte) { + if len(input) == 0 { + return 0, input + } + var num, sz uint var b byte for { diff --git a/plumbing/format/packfile/patch_delta_test.go b/plumbing/format/packfile/patch_delta_test.go new file mode 100644 index 000000000..0a4d99f21 --- /dev/null +++ b/plumbing/format/packfile/patch_delta_test.go @@ -0,0 +1,72 @@ +package packfile + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestDecodeLEB128(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + input []byte + want uint + wantRest []byte + }{ + { + name: "single byte, small number", + input: []byte{0x01, 0xFF}, + want: 1, + wantRest: []byte{0xFF}, + }, + { + name: "single byte, max value without continuation", + input: []byte{0x7F, 0xFF}, + want: 127, + wantRest: []byte{0xFF}, + }, + { + name: "two bytes", + input: []byte{0x80, 0x01, 0xFF}, + want: 128, + wantRest: []byte{0xFF}, + }, + { + name: "two bytes, larger number", + input: []byte{0xFF, 0x01, 0xFF}, + want: 255, + wantRest: []byte{0xFF}, + }, + { + name: "three bytes", + input: []byte{0x80, 0x80, 0x01, 0xFF}, + want: 16384, + wantRest: []byte{0xFF}, + }, + { + name: "empty remaining bytes", + input: []byte{0x01}, + want: 1, + wantRest: []byte{}, + }, + { + name: "empty input", + input: []byte{}, + want: 0, + wantRest: []byte{}, + }, + } + + for _, tc := range tests { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + gotNum, gotRest := decodeLEB128(tc.input) + assert.Equal(t, tc.want, gotNum, "decoded number mismatch") + assert.Equal(t, tc.wantRest, gotRest, "remaining bytes mismatch") + }) + } +} diff --git a/plumbing/format/pktline/scanner.go b/plumbing/format/pktline/scanner.go index fbb137de0..706d984ee 100644 --- a/plumbing/format/pktline/scanner.go +++ b/plumbing/format/pktline/scanner.go @@ -140,6 +140,8 @@ func asciiHexToByte(b byte) (byte, error) { return b - '0', nil case b >= 'a' && b <= 'f': return b - 'a' + 10, nil + case b >= 'A' && b <= 'F': + return b - 'A' + 10, nil default: return 0, ErrInvalidPktLen } diff --git a/plumbing/format/pktline/scanner_test.go b/plumbing/format/pktline/scanner_test.go index 60b622407..ea9327780 100644 --- a/plumbing/format/pktline/scanner_test.go +++ b/plumbing/format/pktline/scanner_test.go @@ -6,8 +6,10 @@ import ( "fmt" "io" "strings" + "testing" "github.com/go-git/go-git/v5/plumbing/format/pktline" + "github.com/stretchr/testify/assert" . "gopkg.in/check.v1" ) @@ -21,6 +23,7 @@ func (s *SuiteScanner) TestInvalid(c *C) { "0001", "0002", "0003", "0004", "0001asdfsadf", "0004foo", "fff5", "ffff", + "FFF5", "FFFF", "gorka", "0", "003", " 5a", "5 a", "5 \n", @@ -48,6 +51,24 @@ func (s *SuiteScanner) TestDecodeOversizePktLines(c *C) { } } +func TestValidPktSizes(t *testing.T) { + for _, test := range [...]string{ + "01fe" + strings.Repeat("a", 0x01fe-4), + "01FE" + strings.Repeat("a", 0x01fe-4), + "00b5" + strings.Repeat("a", 0x00b5-4), + "00B5" + strings.Repeat("a", 0x00b5-4), + } { + r := strings.NewReader(test) + sc := pktline.NewScanner(r) + hasPayload := sc.Scan() + obtained := sc.Bytes() + + assert.True(t, hasPayload) + assert.NoError(t, sc.Err()) + assert.Equal(t, []byte(test[4:]), obtained) + } +} + func (s *SuiteScanner) TestEmptyReader(c *C) { r := strings.NewReader("") sc := pktline.NewScanner(r) diff --git a/plumbing/object/signature.go b/plumbing/object/signature.go index 91cf371f0..f9c3d306b 100644 --- a/plumbing/object/signature.go +++ b/plumbing/object/signature.go @@ -19,6 +19,7 @@ var ( // a PKCS#7 (S/MIME) signature. x509SignatureFormat = signatureFormat{ []byte("-----BEGIN CERTIFICATE-----"), + []byte("-----BEGIN SIGNED MESSAGE-----"), } // sshSignatureFormat is the format of an SSH signature. diff --git a/plumbing/object/signature_test.go b/plumbing/object/signature_test.go index 3b20cded4..c4dabf07e 100644 --- a/plumbing/object/signature_test.go +++ b/plumbing/object/signature_test.go @@ -33,7 +33,7 @@ MKEQruIQWJb+8HVXwssA4= want: signatureTypeSSH, }, { - name: "known signature format (X509)", + name: "known signature format (X509) CERTIFICATE", b: []byte(`-----BEGIN CERTIFICATE----- MIIDZjCCAk6gAwIBAgIJALZ9Z3Z9Z3Z9MA0GCSqGSIb3DQEBCwUAMIGIMQswCQYD VQQGEwJTRTEOMAwGA1UECAwFVGV4YXMxDjAMBgNVBAcMBVRleGFzMQ4wDAYDVQQK @@ -45,6 +45,19 @@ ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDQZ9Z3Z9Z3Z9Z3Z9Z3Z9Z3 -----END CERTIFICATE-----`), want: signatureTypeX509, }, + { + name: "known signature format (x509) SIGNED MESSAGE", + b: []byte(`-----BEGIN SIGNED MESSAGE----- +MIIDZjCCAk6gAwIBAgIJALZ9Z3Z9Z3Z9MA0GCSqGSIb3DQEBCwUAMIGIMQswCQYD +VQQGEwJTRTEOMAwGA1UECAwFVGV4YXMxDjAMBgNVBAcMBVRleGFzMQ4wDAYDVQQK +DAVUZXhhczEOMAwGA1UECwwFVGV4YXMxGDAWBgNVBAMMD1RleGFzIENlcnRpZmlj +YXRlMB4XDTE3MDUyNjE3MjY0MloXDTI3MDUyNDE3MjY0MlowgYgxCzAJBgNVBAYT +AlNFMQ4wDAYDVQQIDAVUZXhhczEOMAwGA1UEBwwFVGV4YXMxDjAMBgNVBAoMBVRl +eGFzMQ4wDAYDVQQLDAVUZXhhczEYMBYGA1UEAwwPVGV4YXMgQ2VydGlmaWNhdGUw +ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDQZ9Z3Z9Z3Z9Z3Z9Z3Z9Z3 +-----END SIGNED MESSAGE-----`), + want: signatureTypeX509, + }, { name: "unknown signature format", b: []byte(`-----BEGIN ARBITRARY SIGNATURE----- @@ -180,6 +193,9 @@ signed tag`), } func FuzzParseSignedBytes(f *testing.F) { + f.Add([]byte(openPGPSignatureFormat[0])) + f.Add([]byte(x509SignatureFormat[0])) + f.Add([]byte(sshSignatureFormat[0])) f.Fuzz(func(t *testing.T, input []byte) { parseSignedBytes(input) diff --git a/plumbing/object/tree.go b/plumbing/object/tree.go index 0fd0e5139..2e1b78915 100644 --- a/plumbing/object/tree.go +++ b/plumbing/object/tree.go @@ -295,6 +295,7 @@ func (s TreeEntrySorter) Swap(i, j int) { } // Encode transforms a Tree into a plumbing.EncodedObject. +// The tree entries must be sorted by name. func (t *Tree) Encode(o plumbing.EncodedObject) (err error) { o.SetType(plumbing.TreeObject) w, err := o.Writer() diff --git a/plumbing/protocol/packp/filter.go b/plumbing/protocol/packp/filter.go new file mode 100644 index 000000000..145fc711c --- /dev/null +++ b/plumbing/protocol/packp/filter.go @@ -0,0 +1,76 @@ +package packp + +import ( + "errors" + "fmt" + "github.com/go-git/go-git/v5/plumbing" + "net/url" + "strings" +) + +var ErrUnsupportedObjectFilterType = errors.New("unsupported object filter type") + +// Filter values enable the partial clone capability which causes +// the server to omit objects that match the filter. +// +// See [Git's documentation] for more details. +// +// [Git's documentation]: https://github.com/git/git/blob/e02ecfcc534e2021aae29077a958dd11c3897e4c/Documentation/rev-list-options.txt#L948 +type Filter string + +type BlobLimitPrefix string + +const ( + BlobLimitPrefixNone BlobLimitPrefix = "" + BlobLimitPrefixKibi BlobLimitPrefix = "k" + BlobLimitPrefixMebi BlobLimitPrefix = "m" + BlobLimitPrefixGibi BlobLimitPrefix = "g" +) + +// FilterBlobNone omits all blobs. +func FilterBlobNone() Filter { + return "blob:none" +} + +// FilterBlobLimit omits blobs of size at least n bytes (when prefix is +// BlobLimitPrefixNone), n kibibytes (when prefix is BlobLimitPrefixKibi), +// n mebibytes (when prefix is BlobLimitPrefixMebi) or n gibibytes (when +// prefix is BlobLimitPrefixGibi). n can be zero, in which case all blobs +// will be omitted. +func FilterBlobLimit(n uint64, prefix BlobLimitPrefix) Filter { + return Filter(fmt.Sprintf("blob:limit=%d%s", n, prefix)) +} + +// FilterTreeDepth omits all blobs and trees whose depth from the root tree +// is larger or equal to depth. +func FilterTreeDepth(depth uint64) Filter { + return Filter(fmt.Sprintf("tree:%d", depth)) +} + +// FilterObjectType omits all objects which are not of the requested type t. +// Supported types are TagObject, CommitObject, TreeObject and BlobObject. +func FilterObjectType(t plumbing.ObjectType) (Filter, error) { + switch t { + case plumbing.TagObject: + fallthrough + case plumbing.CommitObject: + fallthrough + case plumbing.TreeObject: + fallthrough + case plumbing.BlobObject: + return Filter(fmt.Sprintf("object:type=%s", t.String())), nil + default: + return "", fmt.Errorf("%w: %s", ErrUnsupportedObjectFilterType, t.String()) + } +} + +// FilterCombine combines multiple Filter values together. +func FilterCombine(filters ...Filter) Filter { + var escapedFilters []string + + for _, filter := range filters { + escapedFilters = append(escapedFilters, url.QueryEscape(string(filter))) + } + + return Filter(fmt.Sprintf("combine:%s", strings.Join(escapedFilters, "+"))) +} diff --git a/plumbing/protocol/packp/filter_test.go b/plumbing/protocol/packp/filter_test.go new file mode 100644 index 000000000..266670fae --- /dev/null +++ b/plumbing/protocol/packp/filter_test.go @@ -0,0 +1,58 @@ +package packp + +import ( + "github.com/go-git/go-git/v5/plumbing" + "github.com/stretchr/testify/require" + "testing" +) + +func TestFilterBlobNone(t *testing.T) { + require.EqualValues(t, "blob:none", FilterBlobNone()) +} + +func TestFilterBlobLimit(t *testing.T) { + require.EqualValues(t, "blob:limit=0", FilterBlobLimit(0, BlobLimitPrefixNone)) + require.EqualValues(t, "blob:limit=1000", FilterBlobLimit(1000, BlobLimitPrefixNone)) + require.EqualValues(t, "blob:limit=4k", FilterBlobLimit(4, BlobLimitPrefixKibi)) + require.EqualValues(t, "blob:limit=4m", FilterBlobLimit(4, BlobLimitPrefixMebi)) + require.EqualValues(t, "blob:limit=4g", FilterBlobLimit(4, BlobLimitPrefixGibi)) +} + +func TestFilterTreeDepth(t *testing.T) { + require.EqualValues(t, "tree:0", FilterTreeDepth(0)) + require.EqualValues(t, "tree:1", FilterTreeDepth(1)) + require.EqualValues(t, "tree:2", FilterTreeDepth(2)) +} + +func TestFilterObjectType(t *testing.T) { + filter, err := FilterObjectType(plumbing.TagObject) + require.NoError(t, err) + require.EqualValues(t, "object:type=tag", filter) + + filter, err = FilterObjectType(plumbing.CommitObject) + require.NoError(t, err) + require.EqualValues(t, "object:type=commit", filter) + + filter, err = FilterObjectType(plumbing.TreeObject) + require.NoError(t, err) + require.EqualValues(t, "object:type=tree", filter) + + filter, err = FilterObjectType(plumbing.BlobObject) + require.NoError(t, err) + require.EqualValues(t, "object:type=blob", filter) + + _, err = FilterObjectType(plumbing.InvalidObject) + require.Error(t, err) + + _, err = FilterObjectType(plumbing.OFSDeltaObject) + require.Error(t, err) +} + +func TestFilterCombine(t *testing.T) { + require.EqualValues(t, "combine:tree%3A2+blob%3Anone", + FilterCombine( + FilterTreeDepth(2), + FilterBlobNone(), + ), + ) +} diff --git a/plumbing/protocol/packp/sideband/demux.go b/plumbing/protocol/packp/sideband/demux.go index 0116f962e..01d95a3ab 100644 --- a/plumbing/protocol/packp/sideband/demux.go +++ b/plumbing/protocol/packp/sideband/demux.go @@ -114,7 +114,7 @@ func (d *Demuxer) nextPackData() ([]byte, error) { size := len(content) if size == 0 { - return nil, nil + return nil, io.EOF } else if size > d.max { return nil, ErrMaxPackedExceeded } diff --git a/plumbing/protocol/packp/sideband/demux_test.go b/plumbing/protocol/packp/sideband/demux_test.go index 8f233538c..1ba3ad9a1 100644 --- a/plumbing/protocol/packp/sideband/demux_test.go +++ b/plumbing/protocol/packp/sideband/demux_test.go @@ -105,8 +105,34 @@ func (s *SidebandSuite) TestDecodeWithProgress(c *C) { c.Assert(progress, DeepEquals, []byte{'F', 'O', 'O', '\n'}) } -func (s *SidebandSuite) TestDecodeWithUnknownChannel(c *C) { +func (s *SidebandSuite) TestDecodeFlushEOF(c *C) { + expected := []byte("abcdefghijklmnopqrstuvwxyz") + + input := bytes.NewBuffer(nil) + e := pktline.NewEncoder(input) + e.Encode(PackData.WithPayload(expected[0:8])) + e.Encode(ProgressMessage.WithPayload([]byte{'F', 'O', 'O', '\n'})) + e.Encode(PackData.WithPayload(expected[8:16])) + e.Encode(PackData.WithPayload(expected[16:26])) + e.Flush() + e.Encode(PackData.WithPayload([]byte("bar\n"))) + + output := bytes.NewBuffer(nil) + content := bytes.NewBuffer(nil) + d := NewDemuxer(Sideband64k, input) + d.Progress = output + + n, err := content.ReadFrom(d) + c.Assert(err, IsNil) + c.Assert(n, Equals, int64(26)) + c.Assert(content.Bytes(), DeepEquals, expected) + progress, err := io.ReadAll(output) + c.Assert(err, IsNil) + c.Assert(progress, DeepEquals, []byte{'F', 'O', 'O', '\n'}) +} + +func (s *SidebandSuite) TestDecodeWithUnknownChannel(c *C) { buf := bytes.NewBuffer(nil) e := pktline.NewEncoder(buf) e.Encode([]byte{'4', 'F', 'O', 'O', '\n'}) @@ -150,5 +176,4 @@ func (s *SidebandSuite) TestDecodeErrMaxPacked(c *C) { n, err := io.ReadFull(d, content) c.Assert(err, Equals, ErrMaxPackedExceeded) c.Assert(n, Equals, 0) - } diff --git a/plumbing/protocol/packp/srvresp.go b/plumbing/protocol/packp/srvresp.go index a9ddb538b..d760ad660 100644 --- a/plumbing/protocol/packp/srvresp.go +++ b/plumbing/protocol/packp/srvresp.go @@ -120,6 +120,9 @@ func (r *ServerResponse) decodeACKLine(line []byte) error { } sp := bytes.Index(line, []byte(" ")) + if sp+41 > len(line) { + return fmt.Errorf("malformed ACK %q", line) + } h := plumbing.NewHash(string(line[sp+1 : sp+41])) r.ACKs = append(r.ACKs, h) return nil diff --git a/plumbing/protocol/packp/ulreq.go b/plumbing/protocol/packp/ulreq.go index 344f8c7e3..ef4e08a10 100644 --- a/plumbing/protocol/packp/ulreq.go +++ b/plumbing/protocol/packp/ulreq.go @@ -17,6 +17,7 @@ type UploadRequest struct { Wants []plumbing.Hash Shallows []plumbing.Hash Depth Depth + Filter Filter } // Depth values stores the desired depth of the requested packfile: see diff --git a/plumbing/protocol/packp/ulreq_encode.go b/plumbing/protocol/packp/ulreq_encode.go index c451e2316..8b19c0f67 100644 --- a/plumbing/protocol/packp/ulreq_encode.go +++ b/plumbing/protocol/packp/ulreq_encode.go @@ -132,6 +132,17 @@ func (e *ulReqEncoder) encodeDepth() stateFn { return nil } + return e.encodeFilter +} + +func (e *ulReqEncoder) encodeFilter() stateFn { + if filter := e.data.Filter; filter != "" { + if err := e.pe.Encodef("filter %s\n", filter); err != nil { + e.err = fmt.Errorf("encoding filter %s: %s", filter, err) + return nil + } + } + return e.encodeFlush } diff --git a/plumbing/protocol/packp/ulreq_encode_test.go b/plumbing/protocol/packp/ulreq_encode_test.go index ba6df1a6a..247de2767 100644 --- a/plumbing/protocol/packp/ulreq_encode_test.go +++ b/plumbing/protocol/packp/ulreq_encode_test.go @@ -273,6 +273,20 @@ func (s *UlReqEncodeSuite) TestDepthReference(c *C) { testUlReqEncode(c, ur, expected) } +func (s *UlReqEncodeSuite) TestFilter(c *C) { + ur := NewUploadRequest() + ur.Wants = append(ur.Wants, plumbing.NewHash("1111111111111111111111111111111111111111")) + ur.Filter = FilterTreeDepth(0) + + expected := []string{ + "want 1111111111111111111111111111111111111111\n", + "filter tree:0\n", + pktline.FlushString, + } + + testUlReqEncode(c, ur, expected) +} + func (s *UlReqEncodeSuite) TestAll(c *C) { ur := NewUploadRequest() ur.Wants = append(ur.Wants, diff --git a/plumbing/protocol/packp/uppackresp_test.go b/plumbing/protocol/packp/uppackresp_test.go index ec56507e2..5daaeba14 100644 --- a/plumbing/protocol/packp/uppackresp_test.go +++ b/plumbing/protocol/packp/uppackresp_test.go @@ -131,6 +131,8 @@ func (s *UploadPackResponseSuite) TestEncodeMultiACK(c *C) { } func FuzzDecoder(f *testing.F) { + f.Add([]byte("0045ACK 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f81\n")) + f.Add([]byte("003aACK5dc01c595e6c6ec9ccda4f6f69c131c0dd945f82 \n0008NAK\n0")) f.Fuzz(func(t *testing.T, input []byte) { req := NewUploadPackRequest() diff --git a/plumbing/reference.go b/plumbing/reference.go index ddba93029..4daa34164 100644 --- a/plumbing/reference.go +++ b/plumbing/reference.go @@ -188,7 +188,7 @@ func (r ReferenceName) Validate() error { isBranch := r.IsBranch() isTag := r.IsTag() - for _, part := range parts { + for i, part := range parts { // rule 6 if len(part) == 0 { return ErrInvalidReferenceName @@ -205,7 +205,7 @@ func (r ReferenceName) Validate() error { return ErrInvalidReferenceName } - if (isBranch || isTag) && strings.HasPrefix(part, "-") { // branches & tags can't start with - + if (isBranch || isTag) && strings.HasPrefix(part, "-") && (i == 2) { // branches & tags can't start with - return ErrInvalidReferenceName } } diff --git a/plumbing/reference_test.go b/plumbing/reference_test.go index ce570752f..cd715f34d 100644 --- a/plumbing/reference_test.go +++ b/plumbing/reference_test.go @@ -115,6 +115,8 @@ func (s *ReferenceSuite) TestValidReferenceNames(c *C) { "refs/pulls/1/abc.123", "refs/pulls", "refs/-", // should this be allowed? + "refs/ab/-testing", + "refs/123-testing", } for _, v := range valid { c.Assert(v.Validate(), IsNil) diff --git a/plumbing/serverinfo/serverinfo_test.go b/plumbing/serverinfo/serverinfo_test.go index 0a52ea2f9..251746b6d 100644 --- a/plumbing/serverinfo/serverinfo_test.go +++ b/plumbing/serverinfo/serverinfo_test.go @@ -179,6 +179,7 @@ func (s *ServerInfoSuite) TestUpdateServerInfoBasicChange(c *C) { c.Assert(err, IsNil) err = UpdateServerInfo(st, fs) + c.Assert(err, IsNil) assertInfoRefs(c, st, fs) assertObjectPacks(c, st, fs) diff --git a/plumbing/transport/common.go b/plumbing/transport/common.go index b05437fbf..fae1aa98c 100644 --- a/plumbing/transport/common.go +++ b/plumbing/transport/common.go @@ -19,6 +19,7 @@ import ( "fmt" "io" "net/url" + "path/filepath" "strconv" "strings" @@ -295,7 +296,11 @@ func parseFile(endpoint string) (*Endpoint, bool) { return nil, false } - path := endpoint + path, err := filepath.Abs(endpoint) + if err != nil { + return nil, false + } + return &Endpoint{ Protocol: "file", Path: path, diff --git a/plumbing/transport/common_test.go b/plumbing/transport/common_test.go index 3efc555e7..b571db3f2 100644 --- a/plumbing/transport/common_test.go +++ b/plumbing/transport/common_test.go @@ -3,6 +3,9 @@ package transport import ( "fmt" "net/url" + "os" + "path/filepath" + "runtime" "testing" "github.com/go-git/go-git/v5/plumbing/protocol/packp/capability" @@ -120,6 +123,14 @@ func (s *SuiteCommon) TestNewEndpointSCPLikeWithPort(c *C) { } func (s *SuiteCommon) TestNewEndpointFileAbs(c *C) { + var err error + abs := "/foo.git" + + if runtime.GOOS == "windows" { + abs, err = filepath.Abs(abs) + c.Assert(err, IsNil) + } + e, err := NewEndpoint("/foo.git") c.Assert(err, IsNil) c.Assert(e.Protocol, Equals, "file") @@ -127,11 +138,14 @@ func (s *SuiteCommon) TestNewEndpointFileAbs(c *C) { c.Assert(e.Password, Equals, "") c.Assert(e.Host, Equals, "") c.Assert(e.Port, Equals, 0) - c.Assert(e.Path, Equals, "/foo.git") - c.Assert(e.String(), Equals, "file:///foo.git") + c.Assert(e.Path, Equals, abs) + c.Assert(e.String(), Equals, "file://"+abs) } func (s *SuiteCommon) TestNewEndpointFileRel(c *C) { + abs, err := filepath.Abs("foo.git") + c.Assert(err, IsNil) + e, err := NewEndpoint("foo.git") c.Assert(err, IsNil) c.Assert(e.Protocol, Equals, "file") @@ -139,11 +153,20 @@ func (s *SuiteCommon) TestNewEndpointFileRel(c *C) { c.Assert(e.Password, Equals, "") c.Assert(e.Host, Equals, "") c.Assert(e.Port, Equals, 0) - c.Assert(e.Path, Equals, "foo.git") - c.Assert(e.String(), Equals, "file://foo.git") + c.Assert(e.Path, Equals, abs) + c.Assert(e.String(), Equals, "file://"+abs) } func (s *SuiteCommon) TestNewEndpointFileWindows(c *C) { + abs := "C:\\foo.git" + + if runtime.GOOS != "windows" { + cwd, err := os.Getwd() + c.Assert(err, IsNil) + + abs = filepath.Join(cwd, "C:\\foo.git") + } + e, err := NewEndpoint("C:\\foo.git") c.Assert(err, IsNil) c.Assert(e.Protocol, Equals, "file") @@ -151,8 +174,8 @@ func (s *SuiteCommon) TestNewEndpointFileWindows(c *C) { c.Assert(e.Password, Equals, "") c.Assert(e.Host, Equals, "") c.Assert(e.Port, Equals, 0) - c.Assert(e.Path, Equals, "C:\\foo.git") - c.Assert(e.String(), Equals, "file://C:\\foo.git") + c.Assert(e.Path, Equals, abs) + c.Assert(e.String(), Equals, "file://"+abs) } func (s *SuiteCommon) TestNewEndpointFileURL(c *C) { @@ -212,6 +235,11 @@ func (s *SuiteCommon) TestNewEndpointIPv6(c *C) { } func FuzzNewEndpoint(f *testing.F) { + f.Add("http://127.0.0.1:8080/foo.git") + f.Add("http://[::1]:8080/foo.git") + f.Add("file:///foo.git") + f.Add("ssh://git@github.com/user/repository.git") + f.Add("git@github.com:user/repository.git") f.Fuzz(func(t *testing.T, input string) { NewEndpoint(input) diff --git a/plumbing/transport/file/client.go b/plumbing/transport/file/client.go index 38714e2ad..d921d0a5a 100644 --- a/plumbing/transport/file/client.go +++ b/plumbing/transport/file/client.go @@ -7,6 +7,7 @@ import ( "io" "os" "path/filepath" + "runtime" "strings" "github.com/go-git/go-git/v5/plumbing/transport" @@ -95,7 +96,23 @@ func (r *runner) Command(cmd string, ep *transport.Endpoint, auth transport.Auth } } - return &command{cmd: execabs.Command(cmd, ep.Path)}, nil + return &command{cmd: execabs.Command(cmd, adjustPathForWindows(ep.Path))}, nil +} + +func isDriveLetter(c byte) bool { + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') +} + +// On Windows, the path that results from a file: URL has a leading slash. This +// has to be removed if there's a drive letter +func adjustPathForWindows(p string) string { + if runtime.GOOS != "windows" { + return p + } + if len(p) >= 3 && p[0] == '/' && isDriveLetter(p[1]) && p[2] == ':' { + return p[1:] + } + return p } type command struct { diff --git a/plumbing/transport/file/common_test.go b/plumbing/transport/file/common_test.go index a217e9716..cf44eb177 100644 --- a/plumbing/transport/file/common_test.go +++ b/plumbing/transport/file/common_test.go @@ -24,7 +24,7 @@ func (s *CommonSuite) SetUpSuite(c *C) { } var err error - s.tmpDir, err = os.MkdirTemp("", "") + s.tmpDir, err = os.MkdirTemp(c.MkDir(), "") c.Assert(err, IsNil) s.ReceivePackBin = filepath.Join(s.tmpDir, "git-receive-pack") s.UploadPackBin = filepath.Join(s.tmpDir, "git-upload-pack") @@ -38,5 +38,4 @@ func (s *CommonSuite) SetUpSuite(c *C) { func (s *CommonSuite) TearDownSuite(c *C) { defer s.Suite.TearDownSuite(c) - c.Assert(os.RemoveAll(s.tmpDir), IsNil) } diff --git a/plumbing/transport/git/common_test.go b/plumbing/transport/git/common_test.go index 3cab93314..7216d5c35 100644 --- a/plumbing/transport/git/common_test.go +++ b/plumbing/transport/git/common_test.go @@ -42,7 +42,7 @@ func (s *BaseSuite) SetUpTest(c *C) { s.port, err = freePort() c.Assert(err, IsNil) - s.base, err = os.MkdirTemp(os.TempDir(), fmt.Sprintf("go-git-protocol-%d", s.port)) + s.base, err = os.MkdirTemp(c.MkDir(), fmt.Sprintf("go-git-protocol-%d", s.port)) c.Assert(err, IsNil) } @@ -95,11 +95,6 @@ func (s *BaseSuite) TearDownTest(c *C) { _ = s.daemon.Process.Signal(os.Kill) _ = s.daemon.Wait() } - - if s.base != "" { - err := os.RemoveAll(s.base) - c.Assert(err, IsNil) - } } func freePort() (int, error) { diff --git a/plumbing/transport/http/common.go b/plumbing/transport/http/common.go index 1c4ceee68..df01890b2 100644 --- a/plumbing/transport/http/common.go +++ b/plumbing/transport/http/common.go @@ -17,6 +17,7 @@ import ( "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/protocol/packp" + "github.com/go-git/go-git/v5/plumbing/protocol/packp/capability" "github.com/go-git/go-git/v5/plumbing/transport" "github.com/go-git/go-git/v5/utils/ioutil" "github.com/golang/groupcache/lru" @@ -24,7 +25,7 @@ import ( // it requires a bytes.Buffer, because we need to know the length func applyHeadersToRequest(req *http.Request, content *bytes.Buffer, host string, requestType string) { - req.Header.Add("User-Agent", "git/1.0") + req.Header.Add("User-Agent", capability.DefaultAgent()) req.Header.Add("Host", host) // host:port if content == nil { @@ -430,11 +431,11 @@ func NewErr(r *http.Response) error { switch r.StatusCode { case http.StatusUnauthorized: - return transport.ErrAuthenticationRequired + return fmt.Errorf("%w: %s", transport.ErrAuthenticationRequired, reason) case http.StatusForbidden: - return transport.ErrAuthorizationFailed + return fmt.Errorf("%w: %s", transport.ErrAuthorizationFailed, reason) case http.StatusNotFound: - return transport.ErrRepositoryNotFound + return fmt.Errorf("%w: %s", transport.ErrRepositoryNotFound, reason) } return plumbing.NewUnexpectedError(&Err{r, reason}) diff --git a/plumbing/transport/http/common_test.go b/plumbing/transport/http/common_test.go index c831ac3c8..822c860cf 100644 --- a/plumbing/transport/http/common_test.go +++ b/plumbing/transport/http/common_test.go @@ -76,15 +76,15 @@ func (s *ClientSuite) TestNewErrOK(c *C) { } func (s *ClientSuite) TestNewErrUnauthorized(c *C) { - s.testNewHTTPError(c, http.StatusUnauthorized, "authentication required") + s.testNewHTTPError(c, http.StatusUnauthorized, ".*authentication required.*") } func (s *ClientSuite) TestNewErrForbidden(c *C) { - s.testNewHTTPError(c, http.StatusForbidden, "authorization failed") + s.testNewHTTPError(c, http.StatusForbidden, ".*authorization failed.*") } func (s *ClientSuite) TestNewErrNotFound(c *C) { - s.testNewHTTPError(c, http.StatusNotFound, "repository not found") + s.testNewHTTPError(c, http.StatusNotFound, ".*repository not found.*") } func (s *ClientSuite) TestNewHTTPError40x(c *C) { @@ -240,7 +240,7 @@ func (s *BaseSuite) SetUpTest(c *C) { l, err := net.Listen("tcp", "localhost:0") c.Assert(err, IsNil) - base, err := os.MkdirTemp(os.TempDir(), fmt.Sprintf("go-git-http-%d", s.port)) + base, err := os.MkdirTemp(c.MkDir(), fmt.Sprintf("go-git-http-%d", s.port)) c.Assert(err, IsNil) s.port = l.Addr().(*net.TCPAddr).Port @@ -283,8 +283,3 @@ func (s *BaseSuite) newEndpoint(c *C, name string) *transport.Endpoint { return ep } - -func (s *BaseSuite) TearDownTest(c *C) { - err := os.RemoveAll(s.base) - c.Assert(err, IsNil) -} diff --git a/plumbing/transport/http/upload_pack_test.go b/plumbing/transport/http/upload_pack_test.go index abb7adf37..3a1610a3f 100644 --- a/plumbing/transport/http/upload_pack_test.go +++ b/plumbing/transport/http/upload_pack_test.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" + . "github.com/go-git/go-git/v5/internal/test" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/protocol/packp" "github.com/go-git/go-git/v5/plumbing/transport" @@ -37,7 +38,7 @@ func (s *UploadPackSuite) TestAdvertisedReferencesNotExists(c *C) { r, err := s.Client.NewUploadPackSession(s.NonExistentEndpoint, s.EmptyAuth) c.Assert(err, IsNil) info, err := r.AdvertisedReferences() - c.Assert(err, Equals, transport.ErrRepositoryNotFound) + c.Assert(err, ErrorIs, transport.ErrRepositoryNotFound) c.Assert(info, IsNil) } diff --git a/plumbing/transport/server/loader.go b/plumbing/transport/server/loader.go index e7e2b075e..f03a91c6d 100644 --- a/plumbing/transport/server/loader.go +++ b/plumbing/transport/server/loader.go @@ -40,8 +40,16 @@ func (l *fsLoader) Load(ep *transport.Endpoint) (storer.Storer, error) { return nil, err } - if _, err := fs.Stat("config"); err != nil { - return nil, transport.ErrRepositoryNotFound + var bare bool + if _, err := fs.Stat("config"); err == nil { + bare = true + } + + if !bare { + // do not use git.GitDirName due to import cycle + if _, err := fs.Stat(".git"); err != nil { + return nil, transport.ErrRepositoryNotFound + } } return filesystem.NewStorage(fs, cache.NewObjectLRUDefault()), nil diff --git a/plumbing/transport/server/loader_test.go b/plumbing/transport/server/loader_test.go index 88f040348..d478e6521 100644 --- a/plumbing/transport/server/loader_test.go +++ b/plumbing/transport/server/loader_test.go @@ -10,11 +10,22 @@ import ( . "gopkg.in/check.v1" ) +type loaderSuiteRepo struct { + bare bool + + path string +} + type LoaderSuite struct { - RepoPath string + Repos map[string]loaderSuiteRepo } -var _ = Suite(&LoaderSuite{}) +var _ = Suite(&LoaderSuite{ + Repos: map[string]loaderSuiteRepo{ + "repo": {path: "repo.git"}, + "bare": {path: "bare.git", bare: true}, + }, +}) func (s *LoaderSuite) SetUpSuite(c *C) { if err := exec.Command("git", "--version").Run(); err != nil { @@ -22,8 +33,17 @@ func (s *LoaderSuite) SetUpSuite(c *C) { } dir := c.MkDir() - s.RepoPath = filepath.Join(dir, "repo.git") - c.Assert(exec.Command("git", "init", "--bare", s.RepoPath).Run(), IsNil) + + for key, repo := range s.Repos { + repo.path = filepath.Join(dir, repo.path) + if repo.bare { + c.Assert(exec.Command("git", "init", "--bare", repo.path).Run(), IsNil) + } else { + c.Assert(exec.Command("git", "init", repo.path).Run(), IsNil) + } + s.Repos[key] = repo + } + } func (s *LoaderSuite) endpoint(c *C, url string) *transport.Endpoint { @@ -45,13 +65,13 @@ func (s *LoaderSuite) TestLoadNonExistentIgnoreHost(c *C) { } func (s *LoaderSuite) TestLoad(c *C) { - sto, err := DefaultLoader.Load(s.endpoint(c, s.RepoPath)) + sto, err := DefaultLoader.Load(s.endpoint(c, s.Repos["repo"].path)) c.Assert(err, IsNil) c.Assert(sto, NotNil) } -func (s *LoaderSuite) TestLoadIgnoreHost(c *C) { - sto, err := DefaultLoader.Load(s.endpoint(c, s.RepoPath)) +func (s *LoaderSuite) TestLoadBare(c *C) { + sto, err := DefaultLoader.Load(s.endpoint(c, s.Repos["bare"].path)) c.Assert(err, IsNil) c.Assert(sto, NotNil) } diff --git a/plumbing/transport/ssh/internal/test/proxy_test.go b/plumbing/transport/ssh/internal/test/proxy_test.go index 8e775f89a..b4da33870 100644 --- a/plumbing/transport/ssh/internal/test/proxy_test.go +++ b/plumbing/transport/ssh/internal/test/proxy_test.go @@ -58,7 +58,7 @@ func (s *ProxyEnvSuite) TestCommand(c *C) { }() s.port = sshListener.Addr().(*net.TCPAddr).Port - s.base, err = os.MkdirTemp(os.TempDir(), fmt.Sprintf("go-git-ssh-%d", s.port)) + s.base, err = os.MkdirTemp(c.MkDir(), fmt.Sprintf("go-git-ssh-%d", s.port)) c.Assert(err, IsNil) ggssh.DefaultAuthBuilder = func(user string) (ggssh.AuthMethod, error) { diff --git a/plumbing/transport/ssh/proxy_test.go b/plumbing/transport/ssh/proxy_test.go index 2ba98e823..48285c498 100644 --- a/plumbing/transport/ssh/proxy_test.go +++ b/plumbing/transport/ssh/proxy_test.go @@ -53,7 +53,7 @@ func (s *ProxySuite) TestCommand(c *C) { }() s.u.port = sshListener.Addr().(*net.TCPAddr).Port - s.u.base, err = os.MkdirTemp(os.TempDir(), fmt.Sprintf("go-git-ssh-%d", s.u.port)) + s.u.base, err = os.MkdirTemp(c.MkDir(), fmt.Sprintf("go-git-ssh-%d", s.u.port)) c.Assert(err, IsNil) DefaultAuthBuilder = func(user string) (AuthMethod, error) { diff --git a/plumbing/transport/ssh/upload_pack_test.go b/plumbing/transport/ssh/upload_pack_test.go index 67af566e6..48c0c9289 100644 --- a/plumbing/transport/ssh/upload_pack_test.go +++ b/plumbing/transport/ssh/upload_pack_test.go @@ -42,7 +42,7 @@ func (s *UploadPackSuite) SetUpSuite(c *C) { c.Assert(err, IsNil) s.port = l.Addr().(*net.TCPAddr).Port - s.base, err = os.MkdirTemp(os.TempDir(), fmt.Sprintf("go-git-ssh-%d", s.port)) + s.base, err = os.MkdirTemp(c.MkDir(), fmt.Sprintf("go-git-ssh-%d", s.port)) c.Assert(err, IsNil) DefaultAuthBuilder = func(user string) (AuthMethod, error) { diff --git a/plumbing/transport/test/receive_pack.go b/plumbing/transport/test/receive_pack.go index 9414fbaff..d4d2b1070 100644 --- a/plumbing/transport/test/receive_pack.go +++ b/plumbing/transport/test/receive_pack.go @@ -9,6 +9,7 @@ import ( "os" "path/filepath" + . "github.com/go-git/go-git/v5/internal/test" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/format/packfile" "github.com/go-git/go-git/v5/plumbing/protocol/packp" @@ -42,7 +43,7 @@ func (s *ReceivePackSuite) TestAdvertisedReferencesNotExists(c *C) { r, err := s.Client.NewReceivePackSession(s.NonExistentEndpoint, s.EmptyAuth) c.Assert(err, IsNil) ar, err := r.AdvertisedReferences() - c.Assert(err, Equals, transport.ErrRepositoryNotFound) + c.Assert(err, ErrorIs, transport.ErrRepositoryNotFound) c.Assert(ar, IsNil) c.Assert(r.Close(), IsNil) @@ -54,7 +55,7 @@ func (s *ReceivePackSuite) TestAdvertisedReferencesNotExists(c *C) { } writer, err := r.ReceivePack(context.Background(), req) - c.Assert(err, Equals, transport.ErrRepositoryNotFound) + c.Assert(err, ErrorIs, transport.ErrRepositoryNotFound) c.Assert(writer, IsNil) c.Assert(r.Close(), IsNil) } diff --git a/remote.go b/remote.go index 7cc0db9b7..e2c734e75 100644 --- a/remote.go +++ b/remote.go @@ -9,6 +9,7 @@ import ( "time" "github.com/go-git/go-billy/v5/osfs" + "github.com/go-git/go-git/v5/config" "github.com/go-git/go-git/v5/internal/url" "github.com/go-git/go-git/v5/plumbing" @@ -82,7 +83,7 @@ func (r *Remote) String() string { var fetch, push string if len(r.c.URLs) > 0 { fetch = r.c.URLs[0] - push = r.c.URLs[0] + push = r.c.URLs[len(r.c.URLs)-1] } return fmt.Sprintf("%s\t%s (fetch)\n%[1]s\t%[3]s (push)", r.c.Name, fetch, push) @@ -109,8 +110,8 @@ func (r *Remote) PushContext(ctx context.Context, o *PushOptions) (err error) { return fmt.Errorf("remote names don't match: %s != %s", o.RemoteName, r.c.Name) } - if o.RemoteURL == "" { - o.RemoteURL = r.c.URLs[0] + if o.RemoteURL == "" && len(r.c.URLs) > 0 { + o.RemoteURL = r.c.URLs[len(r.c.URLs)-1] } s, err := newSendPackSession(o.RemoteURL, o.Auth, o.InsecureSkipTLS, o.CABundle, o.ProxyOptions) @@ -491,7 +492,18 @@ func (r *Remote) fetch(ctx context.Context, o *FetchOptions) (sto storer.Referen } if !updated && !updatedPrune { - return remoteRefs, NoErrAlreadyUpToDate + // No references updated, but may have fetched new objects, check if we now have any of our wants + for _, hash := range req.Wants { + exists, _ := objectExists(r.s, hash) + if exists { + updated = true + break + } + } + + if !updated { + return remoteRefs, NoErrAlreadyUpToDate + } } return remoteRefs, nil @@ -878,17 +890,12 @@ func getHavesFromRef( return nil } - // No need to load the commit if we know the remote already - // has this hash. - if remoteRefs[h] { - haves[h] = true - return nil - } - commit, err := object.GetCommit(s, h) if err != nil { - // Ignore the error if this isn't a commit. - haves[ref.Hash()] = true + if !errors.Is(err, plumbing.ErrObjectNotFound) { + // Ignore the error if this isn't a commit. + haves[ref.Hash()] = true + } return nil } diff --git a/remote_test.go b/remote_test.go index 1ffedd4a9..b6261cae9 100644 --- a/remote_test.go +++ b/remote_test.go @@ -14,6 +14,9 @@ import ( "time" "github.com/go-git/go-billy/v5/memfs" + "github.com/go-git/go-billy/v5/osfs" + "github.com/go-git/go-billy/v5/util" + "github.com/go-git/go-git/v5/config" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/cache" @@ -346,6 +349,37 @@ func (s *RemoteSuite) testFetch(c *C, r *Remote, o *FetchOptions, expected []*pl } } +func (s *RemoteSuite) TestFetchOfMissingObjects(c *C) { + tmp := c.MkDir() + + // clone to a local temp folder + _, err := PlainClone(tmp, true, &CloneOptions{ + URL: fixtures.Basic().One().DotGit().Root(), + }) + c.Assert(err, IsNil) + + // Delete the pack files + fsTmp := osfs.New(tmp) + err = util.RemoveAll(fsTmp, "objects/pack") + c.Assert(err, IsNil) + + // Reopen the repo from the filesystem (with missing objects) + r, err := Open(filesystem.NewStorage(fsTmp, cache.NewObjectLRUDefault()), nil) + c.Assert(err, IsNil) + + // Confirm we are missing a commit + _, err = r.CommitObject(plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")) + c.Assert(err, Equals, plumbing.ErrObjectNotFound) + + // Refetch to get all the missing objects + err = r.Fetch(&FetchOptions{}) + c.Assert(err, IsNil) + + // Confirm we now have the commit + _, err = r.CommitObject(plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")) + c.Assert(err, IsNil) +} + func (s *RemoteSuite) TestFetchWithProgress(c *C) { url := s.GetBasicLocalRepositoryURL() sto := memory.NewStorage() @@ -376,8 +410,7 @@ func (m *mockPackfileWriter) PackfileWriter() (io.WriteCloser, error) { } func (s *RemoteSuite) TestFetchWithPackfileWriter(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) fss := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) mock := &mockPackfileWriter{Storer: fss} @@ -509,8 +542,7 @@ func (s *RemoteSuite) TestFetchFastForwardMem(c *C) { } func (s *RemoteSuite) TestFetchFastForwardFS(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) fss := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) @@ -531,8 +563,7 @@ func (s *RemoteSuite) TestString(c *C) { } func (s *RemoteSuite) TestPushToEmptyRepository(c *C) { - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() server, err := PlainInit(url, true) c.Assert(err, IsNil) @@ -570,8 +601,7 @@ func (s *RemoteSuite) TestPushToEmptyRepository(c *C) { } func (s *RemoteSuite) TestPushContext(c *C) { - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() _, err := PlainInit(url, true) c.Assert(err, IsNil) @@ -613,8 +643,7 @@ func eventually(c *C, condition func() bool) { } func (s *RemoteSuite) TestPushContextCanceled(c *C) { - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() _, err := PlainInit(url, true) c.Assert(err, IsNil) @@ -643,8 +672,7 @@ func (s *RemoteSuite) TestPushContextCanceled(c *C) { } func (s *RemoteSuite) TestPushTags(c *C) { - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() server, err := PlainInit(url, true) c.Assert(err, IsNil) @@ -672,8 +700,7 @@ func (s *RemoteSuite) TestPushTags(c *C) { } func (s *RemoteSuite) TestPushFollowTags(c *C) { - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() server, err := PlainInit(url, true) c.Assert(err, IsNil) @@ -750,8 +777,7 @@ func (s *RemoteSuite) TestPushDeleteReference(c *C) { fs := fixtures.Basic().One().DotGit() sto := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() r, err := PlainClone(url, true, &CloneOptions{ URL: fs.Root(), @@ -777,8 +803,7 @@ func (s *RemoteSuite) TestForcePushDeleteReference(c *C) { fs := fixtures.Basic().One().DotGit() sto := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() r, err := PlainClone(url, true, &CloneOptions{ URL: fs.Root(), @@ -805,8 +830,7 @@ func (s *RemoteSuite) TestPushRejectNonFastForward(c *C) { fs := fixtures.Basic().One().DotGit() server := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() r, err := PlainClone(url, true, &CloneOptions{ URL: fs.Root(), @@ -1017,16 +1041,14 @@ func (s *RemoteSuite) TestPushForceWithLease_failure(c *C) { func (s *RemoteSuite) TestPushPrune(c *C) { fs := fixtures.Basic().One().DotGit() - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() server, err := PlainClone(url, true, &CloneOptions{ URL: fs.Root(), }) c.Assert(err, IsNil) - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainClone(dir, true, &CloneOptions{ URL: url, @@ -1080,16 +1102,14 @@ func (s *RemoteSuite) TestPushPrune(c *C) { func (s *RemoteSuite) TestPushNewReference(c *C) { fs := fixtures.Basic().One().DotGit() - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() server, err := PlainClone(url, true, &CloneOptions{ URL: fs.Root(), }) c.Assert(err, IsNil) - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainClone(dir, true, &CloneOptions{ URL: url, @@ -1119,16 +1139,14 @@ func (s *RemoteSuite) TestPushNewReference(c *C) { func (s *RemoteSuite) TestPushNewReferenceAndDeleteInBatch(c *C) { fs := fixtures.Basic().One().DotGit() - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() server, err := PlainClone(url, true, &CloneOptions{ URL: fs.Root(), }) c.Assert(err, IsNil) - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainClone(dir, true, &CloneOptions{ URL: url, @@ -1220,17 +1238,20 @@ func (s *RemoteSuite) TestGetHaves(c *C) { sto := filesystem.NewStorage(f.DotGit(), cache.NewObjectLRUDefault()) var localRefs = []*plumbing.Reference{ + // Exists plumbing.NewReferenceFromStrings( "foo", - "f7b877701fbf855b44c0a9e86f3fdce2c298b07f", + "b029517f6300c2da0f4b651b8642506cd6aaf45d", ), + // Exists plumbing.NewReferenceFromStrings( "bar", - "fe6cb94756faa81e5ed9240f9191b833db5f40ae", + "b8e471f58bcbca63b07bda20e428190409c2db47", ), + // Doesn't Exist plumbing.NewReferenceFromStrings( "qux", - "f7b877701fbf855b44c0a9e86f3fdce2c298b07f", + "0000000", ), } @@ -1368,8 +1389,7 @@ func (s *RemoteSuite) TestUpdateShallows(c *C) { } func (s *RemoteSuite) TestUseRefDeltas(c *C) { - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() _, err := PlainInit(url, true) c.Assert(err, IsNil) @@ -1447,16 +1467,14 @@ func (s *RemoteSuite) TestPushRequireRemoteRefs(c *C) { func (s *RemoteSuite) TestFetchPrune(c *C) { fs := fixtures.Basic().One().DotGit() - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() _, err := PlainClone(url, true, &CloneOptions{ URL: fs.Root(), }) c.Assert(err, IsNil) - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainClone(dir, true, &CloneOptions{ URL: url, @@ -1474,8 +1492,7 @@ func (s *RemoteSuite) TestFetchPrune(c *C) { }}) c.Assert(err, IsNil) - dirSave, clean := s.TemporalDir() - defer clean() + dirSave := c.MkDir() rSave, err := PlainClone(dirSave, true, &CloneOptions{ URL: url, @@ -1489,6 +1506,7 @@ func (s *RemoteSuite) TestFetchPrune(c *C) { err = remote.Push(&PushOptions{RefSpecs: []config.RefSpec{ ":refs/heads/branch", }}) + c.Assert(err, IsNil) AssertReferences(c, rSave, map[string]string{ "refs/remotes/origin/branch": ref.Hash().String(), @@ -1504,16 +1522,14 @@ func (s *RemoteSuite) TestFetchPrune(c *C) { func (s *RemoteSuite) TestFetchPruneTags(c *C) { fs := fixtures.Basic().One().DotGit() - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() _, err := PlainClone(url, true, &CloneOptions{ URL: fs.Root(), }) c.Assert(err, IsNil) - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainClone(dir, true, &CloneOptions{ URL: url, @@ -1531,8 +1547,7 @@ func (s *RemoteSuite) TestFetchPruneTags(c *C) { }}) c.Assert(err, IsNil) - dirSave, clean := s.TemporalDir() - defer clean() + dirSave := c.MkDir() rSave, err := PlainClone(dirSave, true, &CloneOptions{ URL: url, @@ -1546,6 +1561,7 @@ func (s *RemoteSuite) TestFetchPruneTags(c *C) { err = remote.Push(&PushOptions{RefSpecs: []config.RefSpec{ ":refs/tags/v1", }}) + c.Assert(err, IsNil) AssertReferences(c, rSave, map[string]string{ "refs/tags/v1": ref.Hash().String(), @@ -1559,12 +1575,12 @@ func (s *RemoteSuite) TestFetchPruneTags(c *C) { } func (s *RemoteSuite) TestCanPushShasToReference(c *C) { - d, err := os.MkdirTemp("", "TestCanPushShasToReference") + d := c.MkDir() + d, err := os.MkdirTemp(d, "TestCanPushShasToReference") c.Assert(err, IsNil) if err != nil { return } - defer os.RemoveAll(d) // remote currently forces a plain path for path based remotes inside the PushContext function. // This makes it impossible, in the current state to use memfs. @@ -1609,8 +1625,7 @@ func (s *RemoteSuite) TestCanPushShasToReference(c *C) { } func (s *RemoteSuite) TestFetchAfterShallowClone(c *C) { - tempDir, clean := s.TemporalDir() - defer clean() + tempDir := c.MkDir() remoteUrl := filepath.Join(tempDir, "remote") repoDir := filepath.Join(tempDir, "repo") diff --git a/repository.go b/repository.go index a57c7141f..200098e7a 100644 --- a/repository.go +++ b/repository.go @@ -956,7 +956,7 @@ func (r *Repository) clone(ctx context.Context, o *CloneOptions) error { } if o.RecurseSubmodules != NoRecurseSubmodules { - if err := w.updateSubmodules(&SubmoduleUpdateOptions{ + if err := w.updateSubmodules(ctx, &SubmoduleUpdateOptions{ RecurseSubmodules: o.RecurseSubmodules, Depth: func() int { if o.ShallowSubmodules { @@ -1037,7 +1037,7 @@ func (r *Repository) setIsBare(isBare bool) error { return r.Storer.SetConfig(cfg) } -func (r *Repository) updateRemoteConfigIfNeeded(o *CloneOptions, c *config.RemoteConfig, head *plumbing.Reference) error { +func (r *Repository) updateRemoteConfigIfNeeded(o *CloneOptions, c *config.RemoteConfig, _ *plumbing.Reference) error { if !o.SingleBranch { return nil } diff --git a/repository_test.go b/repository_test.go index b211f8cee..749c48fb7 100644 --- a/repository_test.go +++ b/repository_test.go @@ -103,17 +103,17 @@ func createCommit(c *C, r *Repository) plumbing.Hash { } h, err := wt.Commit("test commit message", &CommitOptions{ - All: true, - Author: &author, - Committer: &author, + All: true, + Author: &author, + Committer: &author, + AllowEmptyCommits: true, }) c.Assert(err, IsNil) return h } func (s *RepositorySuite) TestInitNonStandardDotGit(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() fs := osfs.New(dir) dot, _ := fs.Chroot("storage") @@ -138,8 +138,7 @@ func (s *RepositorySuite) TestInitNonStandardDotGit(c *C) { } func (s *RepositorySuite) TestInitStandardDotGit(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() fs := osfs.New(dir) dot, _ := fs.Chroot(".git") @@ -300,7 +299,8 @@ func (s *RepositorySuite) TestCloneWithTags(c *C) { func (s *RepositorySuite) TestCloneSparse(c *C) { fs := memfs.New() r, err := Clone(memory.NewStorage(), fs, &CloneOptions{ - URL: s.GetBasicLocalRepositoryURL(), + URL: s.GetBasicLocalRepositoryURL(), + NoCheckout: true, }) c.Assert(err, IsNil) @@ -637,8 +637,7 @@ func (s *RepositorySuite) TestDeleteBranch(c *C) { } func (s *RepositorySuite) TestPlainInit(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainInit(dir, true) c.Assert(err, IsNil) @@ -650,8 +649,7 @@ func (s *RepositorySuite) TestPlainInit(c *C) { } func (s *RepositorySuite) TestPlainInitWithOptions(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainInitWithOptions(dir, &PlainInitOptions{ InitOptions: InitOptions{ @@ -674,8 +672,7 @@ func (s *RepositorySuite) TestPlainInitWithOptions(c *C) { } func (s *RepositorySuite) TestPlainInitAlreadyExists(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainInit(dir, true) c.Assert(err, IsNil) @@ -687,8 +684,7 @@ func (s *RepositorySuite) TestPlainInitAlreadyExists(c *C) { } func (s *RepositorySuite) TestPlainOpen(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainInit(dir, false) c.Assert(err, IsNil) @@ -723,8 +719,7 @@ func (s *RepositorySuite) TestPlainOpenTildePath(c *C) { } func (s *RepositorySuite) TestPlainOpenBare(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainInit(dir, true) c.Assert(err, IsNil) @@ -736,8 +731,7 @@ func (s *RepositorySuite) TestPlainOpenBare(c *C) { } func (s *RepositorySuite) TestPlainOpenNotBare(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainInit(dir, false) c.Assert(err, IsNil) @@ -749,8 +743,7 @@ func (s *RepositorySuite) TestPlainOpenNotBare(c *C) { } func (s *RepositorySuite) testPlainOpenGitFile(c *C, f func(string, string) string) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) dir, err := util.TempDir(fs, "", "plain-open") c.Assert(err, IsNil) @@ -803,8 +796,7 @@ func (s *RepositorySuite) TestPlainOpenBareRelativeGitDirFileNoEOL(c *C) { } func (s *RepositorySuite) TestPlainOpenBareRelativeGitDirFileTrailingGarbage(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) dir, err := util.TempDir(fs, "", "") c.Assert(err, IsNil) @@ -828,8 +820,7 @@ func (s *RepositorySuite) TestPlainOpenBareRelativeGitDirFileTrailingGarbage(c * } func (s *RepositorySuite) TestPlainOpenBareRelativeGitDirFileBadPrefix(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) dir, err := util.TempDir(fs, "", "") c.Assert(err, IsNil) @@ -859,8 +850,7 @@ func (s *RepositorySuite) TestPlainOpenNotExists(c *C) { } func (s *RepositorySuite) TestPlainOpenDetectDotGit(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) dir, err := util.TempDir(fs, "", "") c.Assert(err, IsNil) @@ -894,8 +884,7 @@ func (s *RepositorySuite) TestPlainOpenDetectDotGit(c *C) { } func (s *RepositorySuite) TestPlainOpenNotExistsDetectDotGit(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() opt := &PlainOpenOptions{DetectDotGit: true} r, err := PlainOpenWithOptions(dir, opt) @@ -904,8 +893,7 @@ func (s *RepositorySuite) TestPlainOpenNotExistsDetectDotGit(c *C) { } func (s *RepositorySuite) TestPlainClone(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainClone(dir, false, &CloneOptions{ URL: s.GetBasicLocalRepositoryURL(), @@ -923,8 +911,7 @@ func (s *RepositorySuite) TestPlainClone(c *C) { } func (s *RepositorySuite) TestPlainCloneBareAndShared(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() remote := s.GetBasicLocalRepositoryURL() @@ -951,8 +938,7 @@ func (s *RepositorySuite) TestPlainCloneBareAndShared(c *C) { } func (s *RepositorySuite) TestPlainCloneShared(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() remote := s.GetBasicLocalRepositoryURL() @@ -979,8 +965,7 @@ func (s *RepositorySuite) TestPlainCloneShared(c *C) { } func (s *RepositorySuite) TestPlainCloneSharedHttpShouldReturnError(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() remote := "http://somerepo" @@ -992,8 +977,7 @@ func (s *RepositorySuite) TestPlainCloneSharedHttpShouldReturnError(c *C) { } func (s *RepositorySuite) TestPlainCloneSharedHttpsShouldReturnError(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() remote := "https://somerepo" @@ -1005,8 +989,7 @@ func (s *RepositorySuite) TestPlainCloneSharedHttpsShouldReturnError(c *C) { } func (s *RepositorySuite) TestPlainCloneSharedSSHShouldReturnError(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() remote := "ssh://somerepo" @@ -1018,8 +1001,7 @@ func (s *RepositorySuite) TestPlainCloneSharedSSHShouldReturnError(c *C) { } func (s *RepositorySuite) TestPlainCloneWithRemoteName(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainClone(dir, false, &CloneOptions{ URL: s.GetBasicLocalRepositoryURL(), @@ -1034,8 +1016,7 @@ func (s *RepositorySuite) TestPlainCloneWithRemoteName(c *C) { } func (s *RepositorySuite) TestPlainCloneOverExistingGitDirectory(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainInit(dir, false) c.Assert(r, NotNil) @@ -1052,8 +1033,7 @@ func (s *RepositorySuite) TestPlainCloneContextCancel(c *C) { ctx, cancel := context.WithCancel(context.Background()) cancel() - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainCloneContext(ctx, dir, false, &CloneOptions{ URL: s.GetBasicLocalRepositoryURL(), @@ -1067,8 +1047,7 @@ func (s *RepositorySuite) TestPlainCloneContextNonExistentWithExistentDir(c *C) ctx, cancel := context.WithCancel(context.Background()) defer cancel() - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) dir, err := util.TempDir(fs, "", "") c.Assert(err, IsNil) @@ -1091,8 +1070,7 @@ func (s *RepositorySuite) TestPlainCloneContextNonExistentWithNonExistentDir(c * ctx, cancel := context.WithCancel(context.Background()) defer cancel() - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) tmpDir, err := util.TempDir(fs, "", "") c.Assert(err, IsNil) @@ -1113,8 +1091,7 @@ func (s *RepositorySuite) TestPlainCloneContextNonExistentWithNotDir(c *C) { ctx, cancel := context.WithCancel(context.Background()) cancel() - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) tmpDir, err := util.TempDir(fs, "", "") c.Assert(err, IsNil) @@ -1140,8 +1117,7 @@ func (s *RepositorySuite) TestPlainCloneContextNonExistentWithNotEmptyDir(c *C) ctx, cancel := context.WithCancel(context.Background()) defer cancel() - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) tmpDir, err := util.TempDir(fs, "", "") c.Assert(err, IsNil) @@ -1169,8 +1145,7 @@ func (s *RepositorySuite) TestPlainCloneContextNonExistingOverExistingGitDirecto ctx, cancel := context.WithCancel(context.Background()) defer cancel() - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainInit(dir, false) c.Assert(r, NotNil) @@ -1188,8 +1163,7 @@ func (s *RepositorySuite) TestPlainCloneWithRecurseSubmodules(c *C) { c.Skip("skipping test in short mode.") } - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() path := fixtures.ByTag("submodule").One().Worktree().Root() r, err := PlainClone(dir, false, &CloneOptions{ @@ -1211,8 +1185,7 @@ func (s *RepositorySuite) TestPlainCloneWithShallowSubmodules(c *C) { c.Skip("skipping test in short mode.") } - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() path := fixtures.ByTag("submodule").One().Worktree().Root() mainRepo, err := PlainClone(dir, false, &CloneOptions{ @@ -1244,8 +1217,7 @@ func (s *RepositorySuite) TestPlainCloneWithShallowSubmodules(c *C) { } func (s *RepositorySuite) TestPlainCloneNoCheckout(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() path := fixtures.ByTag("submodule").One().Worktree().Root() r, err := PlainClone(dir, false, &CloneOptions{ @@ -1640,8 +1612,7 @@ func (s *RepositorySuite) TestCloneDetachedHEADAnnotatedTag(c *C) { } func (s *RepositorySuite) TestPush(c *C) { - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() server, err := PlainInit(url, true) c.Assert(err, IsNil) @@ -1669,8 +1640,7 @@ func (s *RepositorySuite) TestPush(c *C) { } func (s *RepositorySuite) TestPushContext(c *C) { - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() _, err := PlainInit(url, true) c.Assert(err, IsNil) @@ -1703,8 +1673,7 @@ func installPreReceiveHook(c *C, fs billy.Filesystem, path, m string) { } func (s *RepositorySuite) TestPushWithProgress(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) path, err := util.TempDir(fs, "", "") c.Assert(err, IsNil) @@ -1739,8 +1708,7 @@ func (s *RepositorySuite) TestPushWithProgress(c *C) { } func (s *RepositorySuite) TestPushDepth(c *C) { - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() server, err := PlainClone(url, true, &CloneOptions{ URL: fixtures.Basic().One().DotGit().Root(), @@ -2831,8 +2799,7 @@ func (s *RepositorySuite) TestDeleteTagAnnotated(c *C) { fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(), ) - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) fss := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) @@ -2879,8 +2846,7 @@ func (s *RepositorySuite) TestDeleteTagAnnotatedUnpacked(c *C) { fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(), ) - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) fss := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) diff --git a/repository_windows_test.go b/repository_windows_test.go index bec0acdd8..87fcd5cbb 100644 --- a/repository_windows_test.go +++ b/repository_windows_test.go @@ -1,9 +1,46 @@ package git -import "fmt" +import ( + "fmt" + "strings" + + "github.com/go-git/go-billy/v5/util" + "github.com/go-git/go-git/v5/storage/memory" + . "gopkg.in/check.v1" +) // preReceiveHook returns the bytes of a pre-receive hook script // that prints m before exiting successfully func preReceiveHook(m string) []byte { return []byte(fmt.Sprintf("#!C:/Program\\ Files/Git/usr/bin/sh.exe\nprintf '%s'\n", m)) } + +func (s *RepositorySuite) TestCloneFileUrlWindows(c *C) { + dir := c.MkDir() + + r, err := PlainInit(dir, false) + c.Assert(err, IsNil) + + err = util.WriteFile(r.wt, "foo", nil, 0755) + c.Assert(err, IsNil) + + w, err := r.Worktree() + c.Assert(err, IsNil) + + _, err = w.Add("foo") + c.Assert(err, IsNil) + + _, err = w.Commit("foo", &CommitOptions{ + Author: defaultSignature(), + Committer: defaultSignature(), + }) + c.Assert(err, IsNil) + + url := "file:///" + strings.ReplaceAll(dir, "\\", "/") + c.Assert(url, Matches, "file:///[A-Za-z]:/.*") + _, err = Clone(memory.NewStorage(), nil, &CloneOptions{ + URL: url, + }) + + c.Assert(err, IsNil) +} diff --git a/status.go b/status.go index 7f18e0227..d14f7e657 100644 --- a/status.go +++ b/status.go @@ -4,6 +4,9 @@ import ( "bytes" "fmt" "path/filepath" + + mindex "github.com/go-git/go-git/v5/utils/merkletrie/index" + "github.com/go-git/go-git/v5/utils/merkletrie/noder" ) // Status represents the current status of a Worktree. @@ -77,3 +80,69 @@ const ( Copied StatusCode = 'C' UpdatedButUnmerged StatusCode = 'U' ) + +// StatusStrategy defines the different types of strategies when processing +// the worktree status. +type StatusStrategy int + +const ( + // TODO: (V6) Review the default status strategy. + // TODO: (V6) Review the type used to represent Status, to enable lazy + // processing of statuses going direct to the backing filesystem. + defaultStatusStrategy = Empty + + // Empty starts its status map from empty. Missing entries for a given + // path means that the file is untracked. This causes a known issue (#119) + // whereby unmodified files can be incorrectly reported as untracked. + // + // This can be used when returning the changed state within a modified Worktree. + // For example, to check whether the current worktree is clean. + Empty StatusStrategy = 0 + // Preload goes through all existing nodes from the index and add them to the + // status map as unmodified. This is currently the most reliable strategy + // although it comes at a performance cost in large repositories. + // + // This method is recommended when fetching the status of unmodified files. + // For example, to confirm the status of a specific file that is either + // untracked or unmodified. + Preload StatusStrategy = 1 +) + +func (s StatusStrategy) new(w *Worktree) (Status, error) { + switch s { + case Preload: + return preloadStatus(w) + case Empty: + return make(Status), nil + } + return nil, fmt.Errorf("%w: %+v", ErrUnsupportedStatusStrategy, s) +} + +func preloadStatus(w *Worktree) (Status, error) { + idx, err := w.r.Storer.Index() + if err != nil { + return nil, err + } + + idxRoot := mindex.NewRootNode(idx) + nodes := []noder.Noder{idxRoot} + + status := make(Status) + for len(nodes) > 0 { + var node noder.Noder + node, nodes = nodes[0], nodes[1:] + if node.IsDir() { + children, err := node.Children() + if err != nil { + return nil, err + } + nodes = append(nodes, children...) + continue + } + fs := status.File(node.Name()) + fs.Worktree = Unmodified + fs.Staging = Unmodified + } + + return status, nil +} diff --git a/storage/filesystem/dotgit/dotgit.go b/storage/filesystem/dotgit/dotgit.go index 31c469481..72c9ccfc1 100644 --- a/storage/filesystem/dotgit/dotgit.go +++ b/storage/filesystem/dotgit/dotgit.go @@ -72,6 +72,9 @@ var ( // ErrIsDir is returned when a reference file is attempting to be read, // but the path specified is a directory. ErrIsDir = errors.New("reference path is a directory") + // ErrEmptyRefFile is returned when a reference file is attempted to be read, + // but the file is empty + ErrEmptyRefFile = errors.New("ref file is empty") ) // Options holds configuration for the storage. @@ -249,7 +252,7 @@ func (d *DotGit) objectPacks() ([]plumbing.Hash, error) { continue } - h := plumbing.NewHash(n[5 : len(n)-5]) //pack-(hash).pack + h := plumbing.NewHash(n[5 : len(n)-5]) // pack-(hash).pack if h.IsZero() { // Ignore files with badly-formatted names. continue @@ -661,18 +664,33 @@ func (d *DotGit) readReferenceFrom(rd io.Reader, name string) (ref *plumbing.Ref return nil, err } + if len(b) == 0 { + return nil, ErrEmptyRefFile + } + line := strings.TrimSpace(string(b)) return plumbing.NewReferenceFromStrings(name, line), nil } +// checkReferenceAndTruncate reads the reference from the given file, or the `pack-refs` file if +// the file was empty. Then it checks that the old reference matches the stored reference and +// truncates the file. func (d *DotGit) checkReferenceAndTruncate(f billy.File, old *plumbing.Reference) error { if old == nil { return nil } + ref, err := d.readReferenceFrom(f, old.Name().String()) + if errors.Is(err, ErrEmptyRefFile) { + // This may happen if the reference is being read from a newly created file. + // In that case, try getting the reference from the packed refs file. + ref, err = d.packedRef(old.Name()) + } + if err != nil { return err } + if ref.Hash() != old.Hash() { return storage.ErrReferenceHasChanged } @@ -701,16 +719,16 @@ func (d *DotGit) SetRef(r, old *plumbing.Reference) error { // Symbolic references are resolved and included in the output. func (d *DotGit) Refs() ([]*plumbing.Reference, error) { var refs []*plumbing.Reference - var seen = make(map[plumbing.ReferenceName]bool) - if err := d.addRefsFromRefDir(&refs, seen); err != nil { + seen := make(map[plumbing.ReferenceName]bool) + if err := d.addRefFromHEAD(&refs); err != nil { return nil, err } - if err := d.addRefsFromPackedRefs(&refs, seen); err != nil { + if err := d.addRefsFromRefDir(&refs, seen); err != nil { return nil, err } - if err := d.addRefFromHEAD(&refs); err != nil { + if err := d.addRefsFromPackedRefs(&refs, seen); err != nil { return nil, err } @@ -815,7 +833,8 @@ func (d *DotGit) addRefsFromPackedRefsFile(refs *[]*plumbing.Reference, f billy. } func (d *DotGit) openAndLockPackedRefs(doCreate bool) ( - pr billy.File, err error) { + pr billy.File, err error, +) { var f billy.File defer func() { if err != nil && f != nil { @@ -1020,7 +1039,7 @@ func (d *DotGit) readReferenceFile(path, name string) (ref *plumbing.Reference, func (d *DotGit) CountLooseRefs() (int, error) { var refs []*plumbing.Reference - var seen = make(map[plumbing.ReferenceName]bool) + seen := make(map[plumbing.ReferenceName]bool) if err := d.addRefsFromRefDir(&refs, seen); err != nil { return 0, err } diff --git a/storage/filesystem/dotgit/dotgit_test.go b/storage/filesystem/dotgit/dotgit_test.go index 2cbdb0c9f..076846a3d 100644 --- a/storage/filesystem/dotgit/dotgit_test.go +++ b/storage/filesystem/dotgit/dotgit_test.go @@ -16,6 +16,7 @@ import ( "github.com/go-git/go-billy/v5/util" fixtures "github.com/go-git/go-git-fixtures/v4" "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/storage" "github.com/stretchr/testify/assert" . "gopkg.in/check.v1" ) @@ -28,8 +29,8 @@ type SuiteDotGit struct { var _ = Suite(&SuiteDotGit{}) -func (s *SuiteDotGit) TemporalFilesystem() (fs billy.Filesystem, clean func()) { - fs = osfs.New(os.TempDir()) +func (s *SuiteDotGit) TemporalFilesystem(c *C) (fs billy.Filesystem) { + fs = osfs.New(c.MkDir()) path, err := util.TempDir(fs, "", "") if err != nil { panic(err) @@ -40,14 +41,11 @@ func (s *SuiteDotGit) TemporalFilesystem() (fs billy.Filesystem, clean func()) { panic(err) } - return fs, func() { - util.RemoveAll(fs, path) - } + return fs } func (s *SuiteDotGit) TestInitialize(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) dir := New(fs) @@ -68,8 +66,7 @@ func (s *SuiteDotGit) TestInitialize(c *C) { } func (s *SuiteDotGit) TestSetRefs(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) dir := New(fs) @@ -77,14 +74,22 @@ func (s *SuiteDotGit) TestSetRefs(c *C) { } func (s *SuiteDotGit) TestSetRefsNorwfs(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) dir := New(&norwfs{fs}) testSetRefs(c, dir) } +func (s *SuiteDotGit) TestRefsHeadFirst(c *C) { + fs := fixtures.Basic().ByTag(".git").One().DotGit() + dir := New(fs) + refs, err := dir.Refs() + c.Assert(err, IsNil) + c.Assert(len(refs), Not(Equals), 0) + c.Assert(refs[0].Name().String(), Equals, "HEAD") +} + func testSetRefs(c *C, dir *DotGit) { firstFoo := plumbing.NewReferenceFromStrings( "refs/heads/foo", @@ -175,7 +180,6 @@ func (s *SuiteDotGit) TestRefsFromPackedRefs(c *C) { ref := findReference(refs, "refs/remotes/origin/branch") c.Assert(ref, NotNil) c.Assert(ref.Hash().String(), Equals, "e8d3ffab552895c19b9fcf7aa264d277cde33881") - } func (s *SuiteDotGit) TestRefsFromReferenceFile(c *C) { @@ -189,7 +193,6 @@ func (s *SuiteDotGit) TestRefsFromReferenceFile(c *C) { c.Assert(ref, NotNil) c.Assert(ref.Type(), Equals, plumbing.SymbolicReference) c.Assert(string(ref.Target()), Equals, "refs/remotes/origin/master") - } func BenchmarkRefMultipleTimes(b *testing.B) { @@ -361,8 +364,7 @@ func (s *SuiteDotGit) TestConfig(c *C) { } func (s *SuiteDotGit) TestConfigWriteAndConfig(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) dir := New(fs) @@ -391,8 +393,7 @@ func (s *SuiteDotGit) TestIndex(c *C) { } func (s *SuiteDotGit) TestIndexWriteAndIndex(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) dir := New(fs) @@ -421,8 +422,7 @@ func (s *SuiteDotGit) TestShallow(c *C) { } func (s *SuiteDotGit) TestShallowWriteAndShallow(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) dir := New(fs) @@ -538,7 +538,6 @@ func (s *SuiteDotGit) TestObjectPackWithKeepDescriptors(c *C) { err = dir.Close() c.Assert(err, NotNil) - } func (s *SuiteDotGit) TestObjectPackIdx(c *C) { @@ -566,8 +565,7 @@ func (s *SuiteDotGit) TestObjectPackNotFound(c *C) { } func (s *SuiteDotGit) TestNewObject(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) dir := New(fs) w, err := dir.NewObject() @@ -605,7 +603,7 @@ func (s *SuiteDotGit) TestObjectsExclusive(c *C) { testObjectsWithPrefix(c, fs, dir) } -func testObjects(c *C, fs billy.Filesystem, dir *DotGit) { +func testObjects(c *C, _ billy.Filesystem, dir *DotGit) { hashes, err := dir.Objects() c.Assert(err, IsNil) c.Assert(hashes, HasLen, 187) @@ -614,7 +612,7 @@ func testObjects(c *C, fs billy.Filesystem, dir *DotGit) { c.Assert(hashes[2].String(), Equals, "03db8e1fbe133a480f2867aac478fd866686d69e") } -func testObjectsWithPrefix(c *C, fs billy.Filesystem, dir *DotGit) { +func testObjectsWithPrefix(c *C, _ billy.Filesystem, dir *DotGit) { prefix, _ := hex.DecodeString("01d5") hashes, err := dir.ObjectsWithPrefix(prefix) c.Assert(err, IsNil) @@ -629,8 +627,7 @@ func testObjectsWithPrefix(c *C, fs billy.Filesystem, dir *DotGit) { } func (s *SuiteDotGit) TestObjectsNoFolder(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) dir := New(fs) hash, err := dir.Objects() @@ -649,7 +646,7 @@ func (s *SuiteDotGit) TestObject(c *C) { file.Name(), fs.Join("objects", "03", "db8e1fbe133a480f2867aac478fd866686d69e")), Equals, true, ) - incomingHash := "9d25e0f9bde9f82882b49fe29117b9411cb157b7" //made up hash + incomingHash := "9d25e0f9bde9f82882b49fe29117b9411cb157b7" // made up hash incomingDirPath := fs.Join("objects", "tmp_objdir-incoming-123456") incomingFilePath := fs.Join(incomingDirPath, incomingHash[0:2], incomingHash[2:40]) fs.MkdirAll(incomingDirPath, os.FileMode(0755)) @@ -670,7 +667,7 @@ func (s *SuiteDotGit) TestPreGit235Object(c *C) { file.Name(), fs.Join("objects", "03", "db8e1fbe133a480f2867aac478fd866686d69e")), Equals, true, ) - incomingHash := "9d25e0f9bde9f82882b49fe29117b9411cb157b7" //made up hash + incomingHash := "9d25e0f9bde9f82882b49fe29117b9411cb157b7" // made up hash incomingDirPath := fs.Join("objects", "incoming-123456") incomingFilePath := fs.Join(incomingDirPath, incomingHash[0:2], incomingHash[2:40]) fs.MkdirAll(incomingDirPath, os.FileMode(0755)) @@ -687,7 +684,7 @@ func (s *SuiteDotGit) TestObjectStat(c *C) { hash := plumbing.NewHash("03db8e1fbe133a480f2867aac478fd866686d69e") _, err := dir.ObjectStat(hash) c.Assert(err, IsNil) - incomingHash := "9d25e0f9bde9f82882b49fe29117b9411cb157b7" //made up hash + incomingHash := "9d25e0f9bde9f82882b49fe29117b9411cb157b7" // made up hash incomingDirPath := fs.Join("objects", "tmp_objdir-incoming-123456") incomingFilePath := fs.Join(incomingDirPath, incomingHash[0:2], incomingHash[2:40]) fs.MkdirAll(incomingDirPath, os.FileMode(0755)) @@ -705,7 +702,7 @@ func (s *SuiteDotGit) TestObjectDelete(c *C) { err := dir.ObjectDelete(hash) c.Assert(err, IsNil) - incomingHash := "9d25e0f9bde9f82882b49fe29117b9411cb157b7" //made up hash + incomingHash := "9d25e0f9bde9f82882b49fe29117b9411cb157b7" // made up hash incomingDirPath := fs.Join("objects", "tmp_objdir-incoming-123456") incomingSubDirPath := fs.Join(incomingDirPath, incomingHash[0:2]) incomingFilePath := fs.Join(incomingSubDirPath, incomingHash[2:40]) @@ -743,8 +740,7 @@ func (s *SuiteDotGit) TestSubmodules(c *C) { } func (s *SuiteDotGit) TestPackRefs(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) dir := New(fs) @@ -1008,8 +1004,7 @@ func (f *notExistsFS) ReadDir(path string) ([]os.FileInfo, error) { } func (s *SuiteDotGit) TestDeletedRefs(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) dir := New(¬ExistsFS{ Filesystem: fs, @@ -1040,3 +1035,62 @@ func (s *SuiteDotGit) TestDeletedRefs(c *C) { c.Assert(refs, HasLen, 1) c.Assert(refs[0].Name(), Equals, plumbing.ReferenceName("refs/heads/foo")) } + +// Checks that setting a reference that has been packed and checking its old value is successful +func (s *SuiteDotGit) TestSetPackedRef(c *C) { + fs := s.TemporalFilesystem(c) + + dir := New(fs) + + err := dir.SetRef(plumbing.NewReferenceFromStrings( + "refs/heads/foo", + "e8d3ffab552895c19b9fcf7aa264d277cde33881", + ), nil) + c.Assert(err, IsNil) + + refs, err := dir.Refs() + c.Assert(err, IsNil) + c.Assert(refs, HasLen, 1) + looseCount, err := dir.CountLooseRefs() + c.Assert(err, IsNil) + c.Assert(looseCount, Equals, 1) + + err = dir.PackRefs() + c.Assert(err, IsNil) + + // Make sure the refs are still there, but no longer loose. + refs, err = dir.Refs() + c.Assert(err, IsNil) + c.Assert(refs, HasLen, 1) + looseCount, err = dir.CountLooseRefs() + c.Assert(err, IsNil) + c.Assert(looseCount, Equals, 0) + + ref, err := dir.Ref("refs/heads/foo") + c.Assert(err, IsNil) + c.Assert(ref, NotNil) + c.Assert(ref.Hash().String(), Equals, "e8d3ffab552895c19b9fcf7aa264d277cde33881") + + // Attempt to update the reference using an invalid old reference value + err = dir.SetRef(plumbing.NewReferenceFromStrings( + "refs/heads/foo", + "b8d3ffab552895c19b9fcf7aa264d277cde33881", + ), plumbing.NewReferenceFromStrings( + "refs/heads/foo", + "e8d3ffab552895c19b9fcf7aa264d277cde33882", + )) + c.Assert(err, Equals, storage.ErrReferenceHasChanged) + + // Now update the reference and it should pass + err = dir.SetRef(plumbing.NewReferenceFromStrings( + "refs/heads/foo", + "b8d3ffab552895c19b9fcf7aa264d277cde33881", + ), plumbing.NewReferenceFromStrings( + "refs/heads/foo", + "e8d3ffab552895c19b9fcf7aa264d277cde33881", + )) + c.Assert(err, IsNil) + looseCount, err = dir.CountLooseRefs() + c.Assert(err, IsNil) + c.Assert(looseCount, Equals, 1) +} diff --git a/storage/filesystem/dotgit/repository_filesystem_test.go b/storage/filesystem/dotgit/repository_filesystem_test.go index 022bde75f..c87856470 100644 --- a/storage/filesystem/dotgit/repository_filesystem_test.go +++ b/storage/filesystem/dotgit/repository_filesystem_test.go @@ -7,8 +7,7 @@ import ( ) func (s *SuiteDotGit) TestRepositoryFilesystem(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) err := fs.MkdirAll("dotGit", 0777) c.Assert(err, IsNil) diff --git a/storage/filesystem/dotgit/writers_test.go b/storage/filesystem/dotgit/writers_test.go index a2517ccb1..f0f01b377 100644 --- a/storage/filesystem/dotgit/writers_test.go +++ b/storage/filesystem/dotgit/writers_test.go @@ -19,8 +19,7 @@ import ( func (s *SuiteDotGit) TestNewObjectPack(c *C) { f := fixtures.Basic().One() - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) dot := New(fs) @@ -59,8 +58,7 @@ func (s *SuiteDotGit) TestNewObjectPack(c *C) { } func (s *SuiteDotGit) TestNewObjectPackUnused(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) dot := New(fs) @@ -126,8 +124,7 @@ func (s *SuiteDotGit) TestSyncedReader(c *C) { } func (s *SuiteDotGit) TestPackWriterUnusedNotify(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) w, err := newPackWrite(fs) c.Assert(err, IsNil) diff --git a/storage/filesystem/index.go b/storage/filesystem/index.go index a19176f83..a86ef3e2e 100644 --- a/storage/filesystem/index.go +++ b/storage/filesystem/index.go @@ -48,7 +48,7 @@ func (s *IndexStorage) Index() (i *index.Index, err error) { defer ioutil.CheckClose(f, &err) - d := index.NewDecoder(bufio.NewReader(f)) + d := index.NewDecoder(f) err = d.Decode(idx) return idx, err } diff --git a/storage/filesystem/object.go b/storage/filesystem/object.go index e812fe934..91b4aceae 100644 --- a/storage/filesystem/object.go +++ b/storage/filesystem/object.go @@ -431,13 +431,13 @@ func (s *ObjectStorage) getFromUnpacked(h plumbing.Hash) (obj plumbing.EncodedOb defer ioutil.CheckClose(w, &err) - s.objectCache.Put(obj) - bufp := copyBufferPool.Get().(*[]byte) buf := *bufp _, err = io.CopyBuffer(w, r, buf) copyBufferPool.Put(bufp) + s.objectCache.Put(obj) + return obj, err } diff --git a/storage/filesystem/object_test.go b/storage/filesystem/object_test.go index 251077a61..4f98458c4 100644 --- a/storage/filesystem/object_test.go +++ b/storage/filesystem/object_test.go @@ -547,3 +547,64 @@ func BenchmarkGetObjectFromPackfile(b *testing.B) { }) } } + +func (s *FsSuite) TestGetFromUnpackedCachesObjects(c *C) { + fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit() + objectCache := cache.NewObjectLRUDefault() + objectStorage := NewObjectStorage(dotgit.New(fs), objectCache) + hash := plumbing.NewHash("f3dfe29d268303fc6e1bbce268605fc99573406e") + + // Assert the cache is empty initially + _, ok := objectCache.Get(hash) + c.Assert(ok, Equals, false) + + // Load the object + obj, err := objectStorage.EncodedObject(plumbing.AnyObject, hash) + c.Assert(err, IsNil) + c.Assert(obj.Hash(), Equals, hash) + + // The object should've been cached during the load + cachedObj, ok := objectCache.Get(hash) + c.Assert(ok, Equals, true) + c.Assert(cachedObj, DeepEquals, obj) + + // Assert that both objects can be read and that they both produce the same bytes + + objReader, err := obj.Reader() + c.Assert(err, IsNil) + objBytes, err := io.ReadAll(objReader) + c.Assert(err, IsNil) + c.Assert(len(objBytes), Not(Equals), 0) + err = objReader.Close() + c.Assert(err, IsNil) + + cachedObjReader, err := cachedObj.Reader() + c.Assert(err, IsNil) + cachedObjBytes, err := io.ReadAll(cachedObjReader) + c.Assert(len(cachedObjBytes), Not(Equals), 0) + c.Assert(err, IsNil) + err = cachedObjReader.Close() + c.Assert(err, IsNil) + + c.Assert(cachedObjBytes, DeepEquals, objBytes) +} + +func (s *FsSuite) TestGetFromUnpackedDoesNotCacheLargeObjects(c *C) { + fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit() + objectCache := cache.NewObjectLRUDefault() + objectStorage := NewObjectStorageWithOptions(dotgit.New(fs), objectCache, Options{LargeObjectThreshold: 1}) + hash := plumbing.NewHash("f3dfe29d268303fc6e1bbce268605fc99573406e") + + // Assert the cache is empty initially + _, ok := objectCache.Get(hash) + c.Assert(ok, Equals, false) + + // Load the object + obj, err := objectStorage.EncodedObject(plumbing.AnyObject, hash) + c.Assert(err, IsNil) + c.Assert(obj.Hash(), Equals, hash) + + // The object should not have been cached during the load + _, ok = objectCache.Get(hash) + c.Assert(ok, Equals, false) +} diff --git a/submodule.go b/submodule.go index 84f020dc7..afabb6aca 100644 --- a/submodule.go +++ b/submodule.go @@ -214,10 +214,10 @@ func (s *Submodule) update(ctx context.Context, o *SubmoduleUpdateOptions, force return err } - return s.doRecursiveUpdate(r, o) + return s.doRecursiveUpdate(ctx, r, o) } -func (s *Submodule) doRecursiveUpdate(r *Repository, o *SubmoduleUpdateOptions) error { +func (s *Submodule) doRecursiveUpdate(ctx context.Context, r *Repository, o *SubmoduleUpdateOptions) error { if o.RecurseSubmodules == NoRecurseSubmodules { return nil } @@ -236,7 +236,7 @@ func (s *Submodule) doRecursiveUpdate(r *Repository, o *SubmoduleUpdateOptions) *new = *o new.RecurseSubmodules-- - return l.Update(new) + return l.UpdateContext(ctx, new) } func (s *Submodule) fetchAndCheckout( diff --git a/submodule_test.go b/submodule_test.go index 0e88391f4..8264193c4 100644 --- a/submodule_test.go +++ b/submodule_test.go @@ -17,7 +17,6 @@ import ( type SubmoduleSuite struct { BaseSuite Worktree *Worktree - clean func() } var _ = Suite(&SubmoduleSuite{}) @@ -25,8 +24,7 @@ var _ = Suite(&SubmoduleSuite{}) func (s *SubmoduleSuite) SetUpTest(c *C) { path := fixtures.ByTag("submodule").One().Worktree().Root() - var dir string - dir, s.clean = s.TemporalDir() + dir := c.MkDir() r, err := PlainClone(filepath.Join(dir, "worktree"), false, &CloneOptions{ URL: path, @@ -39,10 +37,6 @@ func (s *SubmoduleSuite) SetUpTest(c *C) { c.Assert(err, IsNil) } -func (s *SubmoduleSuite) TearDownTest(_ *C) { - s.clean() -} - func (s *SubmoduleSuite) TestInit(c *C) { sm, err := s.Worktree.Submodule("basic") c.Assert(err, IsNil) diff --git a/utils/merkletrie/change.go b/utils/merkletrie/change.go index cc6dc8907..450feb4ba 100644 --- a/utils/merkletrie/change.go +++ b/utils/merkletrie/change.go @@ -1,12 +1,17 @@ package merkletrie import ( + "errors" "fmt" "io" "github.com/go-git/go-git/v5/utils/merkletrie/noder" ) +var ( + ErrEmptyFileName = errors.New("empty filename in tree entry") +) + // Action values represent the kind of things a Change can represent: // insertion, deletions or modifications of files. type Action int @@ -121,6 +126,10 @@ func (l *Changes) AddRecursiveDelete(root noder.Path) error { type noderToChangeFn func(noder.Path) Change // NewInsert or NewDelete func (l *Changes) addRecursive(root noder.Path, ctor noderToChangeFn) error { + if root.String() == "" { + return ErrEmptyFileName + } + if !root.IsDir() { l.Add(ctor(root)) return nil diff --git a/utils/merkletrie/change_test.go b/utils/merkletrie/change_test.go index f73eb861d..cd28bfe52 100644 --- a/utils/merkletrie/change_test.go +++ b/utils/merkletrie/change_test.go @@ -28,6 +28,17 @@ func (s *ChangeSuite) TestUnsupportedAction(c *C) { c.Assert(a.String, PanicMatches, "unsupported action.*") } +func (s ChangeSuite) TestEmptyChanges(c *C) { + ret := merkletrie.NewChanges() + p := noder.Path{} + + err := ret.AddRecursiveInsert(p) + c.Assert(err, Equals, merkletrie.ErrEmptyFileName) + + err = ret.AddRecursiveDelete(p) + c.Assert(err, Equals, merkletrie.ErrEmptyFileName) +} + func (s ChangeSuite) TestNewInsert(c *C) { tree, err := fsnoder.New("(a(b(z<>)))") c.Assert(err, IsNil) diff --git a/utils/merkletrie/difftree.go b/utils/merkletrie/difftree.go index 8090942dd..4ef2d9907 100644 --- a/utils/merkletrie/difftree.go +++ b/utils/merkletrie/difftree.go @@ -11,7 +11,7 @@ package merkletrie // corresponding changes and move the iterators further over both // trees. // -// The table bellow show all the possible comparison results, along +// The table below shows all the possible comparison results, along // with what changes should we produce and how to advance the // iterators. // diff --git a/utils/merkletrie/filesystem/node_test.go b/utils/merkletrie/filesystem/node_test.go index b76abc412..d3b40be71 100644 --- a/utils/merkletrie/filesystem/node_test.go +++ b/utils/merkletrie/filesystem/node_test.go @@ -205,8 +205,7 @@ func (s *NoderSuite) TestSocket(c *C) { c.Skip("socket files do not exist on windows") } - td, err := os.MkdirTemp("", "socket-test") - defer os.RemoveAll(td) + td, err := os.MkdirTemp(c.MkDir(), "socket-test") c.Assert(err, IsNil) sock, err := net.ListenUnix("unix", &net.UnixAddr{Name: fmt.Sprintf("%s/socket", td), Net: "unix"}) diff --git a/utils/sync/bufio.go b/utils/sync/bufio.go index 5009ea804..42f60f7ea 100644 --- a/utils/sync/bufio.go +++ b/utils/sync/bufio.go @@ -13,7 +13,7 @@ var bufioReader = sync.Pool{ } // GetBufioReader returns a *bufio.Reader that is managed by a sync.Pool. -// Returns a bufio.Reader that is resetted with reader and ready for use. +// Returns a bufio.Reader that is reset with reader and ready for use. // // After use, the *bufio.Reader should be put back into the sync.Pool // by calling PutBufioReader. diff --git a/utils/sync/bytes.go b/utils/sync/bytes.go index dd06fc0bc..c67b97837 100644 --- a/utils/sync/bytes.go +++ b/utils/sync/bytes.go @@ -35,7 +35,7 @@ func PutByteSlice(buf *[]byte) { } // GetBytesBuffer returns a *bytes.Buffer that is managed by a sync.Pool. -// Returns a buffer that is resetted and ready for use. +// Returns a buffer that is reset and ready for use. // // After use, the *bytes.Buffer should be put back into the sync.Pool // by calling PutBytesBuffer. diff --git a/utils/sync/zlib.go b/utils/sync/zlib.go index c61388595..edf674d85 100644 --- a/utils/sync/zlib.go +++ b/utils/sync/zlib.go @@ -35,7 +35,7 @@ type ZLibReader struct { } // GetZlibReader returns a ZLibReader that is managed by a sync.Pool. -// Returns a ZLibReader that is resetted using a dictionary that is +// Returns a ZLibReader that is reset using a dictionary that is // also managed by a sync.Pool. // // After use, the ZLibReader should be put back into the sync.Pool @@ -58,7 +58,7 @@ func PutZlibReader(z ZLibReader) { } // GetZlibWriter returns a *zlib.Writer that is managed by a sync.Pool. -// Returns a writer that is resetted with w and ready for use. +// Returns a writer that is reset with w and ready for use. // // After use, the *zlib.Writer should be put back into the sync.Pool // by calling PutZlibWriter. diff --git a/worktree.go b/worktree.go index ab11d42db..dded08e99 100644 --- a/worktree.go +++ b/worktree.go @@ -25,11 +25,12 @@ import ( ) var ( - ErrWorktreeNotClean = errors.New("worktree is not clean") - ErrSubmoduleNotFound = errors.New("submodule not found") - ErrUnstagedChanges = errors.New("worktree contains unstaged changes") - ErrGitModulesSymlink = errors.New(gitmodulesFile + " is a symlink") - ErrNonFastForwardUpdate = errors.New("non-fast-forward update") + ErrWorktreeNotClean = errors.New("worktree is not clean") + ErrSubmoduleNotFound = errors.New("submodule not found") + ErrUnstagedChanges = errors.New("worktree contains unstaged changes") + ErrGitModulesSymlink = errors.New(gitmodulesFile + " is a symlink") + ErrNonFastForwardUpdate = errors.New("non-fast-forward update") + ErrRestoreWorktreeOnlyNotSupported = errors.New("worktree only is not supported") ) // Worktree represents a git worktree. @@ -139,7 +140,7 @@ func (w *Worktree) PullContext(ctx context.Context, o *PullOptions) error { } if o.RecurseSubmodules != NoRecurseSubmodules { - return w.updateSubmodules(&SubmoduleUpdateOptions{ + return w.updateSubmodules(ctx, &SubmoduleUpdateOptions{ RecurseSubmodules: o.RecurseSubmodules, Auth: o.Auth, }) @@ -148,13 +149,13 @@ func (w *Worktree) PullContext(ctx context.Context, o *PullOptions) error { return nil } -func (w *Worktree) updateSubmodules(o *SubmoduleUpdateOptions) error { +func (w *Worktree) updateSubmodules(ctx context.Context, o *SubmoduleUpdateOptions) error { s, err := w.Submodules() if err != nil { return err } o.Init = true - return s.Update(o) + return s.UpdateContext(ctx, o) } // Checkout switch branches or restore working tree files. @@ -307,13 +308,13 @@ func (w *Worktree) ResetSparsely(opts *ResetOptions, dirs []string) error { } if opts.Mode == MixedReset || opts.Mode == MergeReset || opts.Mode == HardReset { - if err := w.resetIndex(t, dirs); err != nil { + if err := w.resetIndex(t, dirs, opts.Files); err != nil { return err } } if opts.Mode == MergeReset || opts.Mode == HardReset { - if err := w.resetWorktree(t); err != nil { + if err := w.resetWorktree(t, opts.Files); err != nil { return err } } @@ -321,20 +322,52 @@ func (w *Worktree) ResetSparsely(opts *ResetOptions, dirs []string) error { return nil } +// Restore restores specified files in the working tree or stage with contents from +// a restore source. If a path is tracked but does not exist in the restore, +// source, it will be removed to match the source. +// +// If Staged and Worktree are true, then the restore source will be the index. +// If only Staged is true, then the restore source will be HEAD. +// If only Worktree is true or neither Staged nor Worktree are true, will +// result in ErrRestoreWorktreeOnlyNotSupported because restoring the working +// tree while leaving the stage untouched is not currently supported. +// +// Restore with no files specified will return ErrNoRestorePaths. +func (w *Worktree) Restore(o *RestoreOptions) error { + if err := o.Validate(); err != nil { + return err + } + + if o.Staged { + opts := &ResetOptions{ + Files: o.Files, + } + + if o.Worktree { + // If we are doing both Worktree and Staging then it is a hard reset + opts.Mode = HardReset + } else { + // If we are doing just staging then it is a mixed reset + opts.Mode = MixedReset + } + + return w.Reset(opts) + } + + return ErrRestoreWorktreeOnlyNotSupported +} + // Reset the worktree to a specified state. func (w *Worktree) Reset(opts *ResetOptions) error { return w.ResetSparsely(opts, nil) } -func (w *Worktree) resetIndex(t *object.Tree, dirs []string) error { +func (w *Worktree) resetIndex(t *object.Tree, dirs []string, files []string) error { idx, err := w.r.Storer.Index() - if len(dirs) > 0 { - idx.SkipUnless(dirs) - } - if err != nil { return err } + b := newIndexBuilder(idx) changes, err := w.diffTreeWithStaging(t, true) @@ -362,6 +395,13 @@ func (w *Worktree) resetIndex(t *object.Tree, dirs []string) error { name = ch.From.String() } + if len(files) > 0 { + contains := inFiles(files, name) + if !contains { + continue + } + } + b.Remove(name) if e == nil { continue @@ -376,10 +416,26 @@ func (w *Worktree) resetIndex(t *object.Tree, dirs []string) error { } b.Write(idx) + + if len(dirs) > 0 { + idx.SkipUnless(dirs) + } + return w.r.Storer.SetIndex(idx) } -func (w *Worktree) resetWorktree(t *object.Tree) error { +func inFiles(files []string, v string) bool { + v = filepath.Clean(v) + for _, s := range files { + if filepath.Clean(s) == v { + return true + } + } + + return false +} + +func (w *Worktree) resetWorktree(t *object.Tree, files []string) error { changes, err := w.diffStagingWithWorktree(true, false) if err != nil { return err @@ -395,6 +451,25 @@ func (w *Worktree) resetWorktree(t *object.Tree) error { if err := w.validChange(ch); err != nil { return err } + + if len(files) > 0 { + file := "" + if ch.From != nil { + file = ch.From.String() + } else if ch.To != nil { + file = ch.To.String() + } + + if file == "" { + continue + } + + contains := inFiles(files, file) + if !contains { + continue + } + } + if err := w.checkoutChange(ch, t, b); err != nil { return err } @@ -642,7 +717,7 @@ func (w *Worktree) checkoutChangeRegularFile(name string, return err } - return w.addIndexFromFile(name, e.Hash, idx) + return w.addIndexFromFile(name, e.Hash, f.Mode, idx) } return nil @@ -725,18 +800,13 @@ func (w *Worktree) addIndexFromTreeEntry(name string, f *object.TreeEntry, idx * return nil } -func (w *Worktree) addIndexFromFile(name string, h plumbing.Hash, idx *indexBuilder) error { +func (w *Worktree) addIndexFromFile(name string, h plumbing.Hash, mode filemode.FileMode, idx *indexBuilder) error { idx.Remove(name) fi, err := w.Filesystem.Lstat(name) if err != nil { return err } - mode, err := filemode.NewFromOSFileMode(fi.Mode()) - if err != nil { - return err - } - e := &index.Entry{ Hash: h, Name: name, @@ -1058,7 +1128,7 @@ func rmFileAndDirsIfEmpty(fs billy.Filesystem, name string) error { dir := filepath.Dir(name) for { removed, err := removeDirIfEmpty(fs, dir) - if err != nil { + if err != nil && !os.IsNotExist(err) { return err } diff --git a/worktree_commit.go b/worktree_commit.go index f62054bcb..9b1988ae6 100644 --- a/worktree_commit.go +++ b/worktree_commit.go @@ -5,6 +5,7 @@ import ( "errors" "io" "path" + "regexp" "sort" "strings" @@ -23,6 +24,10 @@ var ( // ErrEmptyCommit occurs when a commit is attempted using a clean // working tree, with no changes to be committed. ErrEmptyCommit = errors.New("cannot create empty commit: clean working tree") + + // characters to be removed from user name and/or email before using them to build a commit object + // See https://git-scm.com/docs/git-commit#_commit_information + invalidCharactersRe = regexp.MustCompile(`[<>\n]`) ) // Commit stores the current contents of the index in a new commit along with @@ -38,8 +43,6 @@ func (w *Worktree) Commit(msg string, opts *CommitOptions) (plumbing.Hash, error } } - var treeHash plumbing.Hash - if opts.Amend { head, err := w.r.Head() if err != nil { @@ -61,16 +64,34 @@ func (w *Worktree) Commit(msg string, opts *CommitOptions) (plumbing.Hash, error return plumbing.ZeroHash, err } + // First handle the case of the first commit in the repository being empty. + if len(opts.Parents) == 0 && len(idx.Entries) == 0 && !opts.AllowEmptyCommits { + return plumbing.ZeroHash, ErrEmptyCommit + } + h := &buildTreeHelper{ fs: w.Filesystem, s: w.r.Storer, } - treeHash, err = h.BuildTree(idx, opts) + treeHash, err := h.BuildTree(idx, opts) if err != nil { return plumbing.ZeroHash, err } + previousTree := plumbing.ZeroHash + if len(opts.Parents) > 0 { + parentCommit, err := w.r.CommitObject(opts.Parents[0]) + if err != nil { + return plumbing.ZeroHash, err + } + previousTree = parentCommit.TreeHash + } + + if treeHash == previousTree && !opts.AllowEmptyCommits { + return plumbing.ZeroHash, ErrEmptyCommit + } + commit, err := w.buildCommitObject(msg, opts, treeHash) if err != nil { return plumbing.ZeroHash, err @@ -121,8 +142,8 @@ func (w *Worktree) updateHEAD(commit plumbing.Hash) error { func (w *Worktree) buildCommitObject(msg string, opts *CommitOptions, tree plumbing.Hash) (plumbing.Hash, error) { commit := &object.Commit{ - Author: *opts.Author, - Committer: *opts.Committer, + Author: w.sanitize(*opts.Author), + Committer: w.sanitize(*opts.Committer), Message: msg, TreeHash: tree, ParentHashes: opts.Parents, @@ -148,6 +169,14 @@ func (w *Worktree) buildCommitObject(msg string, opts *CommitOptions, tree plumb return w.r.Storer.SetEncodedObject(obj) } +func (w *Worktree) sanitize(signature object.Signature) object.Signature { + return object.Signature{ + Name: invalidCharactersRe.ReplaceAllString(signature.Name, ""), + Email: invalidCharactersRe.ReplaceAllString(signature.Email, ""), + When: signature.When, + } +} + type gpgSigner struct { key *openpgp.Entity cfg *packet.Config @@ -175,10 +204,6 @@ type buildTreeHelper struct { // BuildTree builds the tree objects and push its to the storer, the hash // of the root tree is returned. func (h *buildTreeHelper) BuildTree(idx *index.Index, opts *CommitOptions) (plumbing.Hash, error) { - if len(idx.Entries) == 0 && (opts == nil || !opts.AllowEmptyCommits) { - return plumbing.ZeroHash, ErrEmptyCommit - } - const rootNode = "" h.trees = map[string]*object.Tree{rootNode: {}} h.entries = map[string]*object.TreeEntry{} diff --git a/worktree_commit_test.go b/worktree_commit_test.go index fee8b1548..5b2b356c9 100644 --- a/worktree_commit_test.go +++ b/worktree_commit_test.go @@ -10,6 +10,7 @@ import ( "strings" "time" + fixtures "github.com/go-git/go-git-fixtures/v4" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/cache" "github.com/go-git/go-git/v5/plumbing/object" @@ -89,6 +90,56 @@ func (s *WorktreeSuite) TestNothingToCommit(c *C) { c.Assert(err, IsNil) } +func (s *WorktreeSuite) TestNothingToCommitNonEmptyRepo(c *C) { + fs := memfs.New() + r, err := Init(memory.NewStorage(), fs) + c.Assert(err, IsNil) + + w, err := r.Worktree() + c.Assert(err, IsNil) + + err = util.WriteFile(fs, "foo", []byte("foo"), 0644) + c.Assert(err, IsNil) + + w.Add("foo") + _, err = w.Commit("previous commit\n", &CommitOptions{Author: defaultSignature()}) + c.Assert(err, IsNil) + + hash, err := w.Commit("failed empty commit\n", &CommitOptions{Author: defaultSignature()}) + c.Assert(hash, Equals, plumbing.ZeroHash) + c.Assert(err, Equals, ErrEmptyCommit) + + _, err = w.Commit("enable empty commits\n", &CommitOptions{Author: defaultSignature(), AllowEmptyCommits: true}) + c.Assert(err, IsNil) +} + +func (s *WorktreeSuite) TestRemoveAndCommitToMakeEmptyRepo(c *C) { + fs := memfs.New() + r, err := Init(memory.NewStorage(), fs) + c.Assert(err, IsNil) + + w, err := r.Worktree() + c.Assert(err, IsNil) + + err = util.WriteFile(fs, "foo", []byte("foo"), 0644) + c.Assert(err, IsNil) + + _, err = w.Add("foo") + c.Assert(err, IsNil) + + _, err = w.Commit("Add in Repo\n", &CommitOptions{Author: defaultSignature()}) + c.Assert(err, IsNil) + + err = fs.Remove("foo") + c.Assert(err, IsNil) + + _, err = w.Add("foo") + c.Assert(err, IsNil) + + _, err = w.Commit("Remove foo\n", &CommitOptions{Author: defaultSignature()}) + c.Assert(err, IsNil) +} + func (s *WorktreeSuite) TestCommitParent(c *C) { expected := plumbing.NewHash("ef3ca05477530b37f48564be33ddd48063fc7a22") @@ -101,7 +152,8 @@ func (s *WorktreeSuite) TestCommitParent(c *C) { err := w.Checkout(&CheckoutOptions{}) c.Assert(err, IsNil) - util.WriteFile(fs, "foo", []byte("foo"), 0644) + err = util.WriteFile(fs, "foo", []byte("foo"), 0644) + c.Assert(err, IsNil) _, err = w.Add("foo") c.Assert(err, IsNil) @@ -113,7 +165,42 @@ func (s *WorktreeSuite) TestCommitParent(c *C) { assertStorageStatus(c, s.Repository, 13, 11, 10, expected) } -func (s *WorktreeSuite) TestCommitAmend(c *C) { +func (s *WorktreeSuite) TestCommitAmendWithoutChanges(c *C) { + fs := memfs.New() + w := &Worktree{ + r: s.Repository, + Filesystem: fs, + } + + err := w.Checkout(&CheckoutOptions{}) + c.Assert(err, IsNil) + + err = util.WriteFile(fs, "foo", []byte("foo"), 0644) + c.Assert(err, IsNil) + + _, err = w.Add("foo") + c.Assert(err, IsNil) + + prevHash, err := w.Commit("foo\n", &CommitOptions{Author: defaultSignature()}) + c.Assert(err, IsNil) + + amendedHash, err := w.Commit("foo\n", &CommitOptions{Author: defaultSignature(), Amend: true}) + c.Assert(err, IsNil) + + headRef, err := w.r.Head() + c.Assert(err, IsNil) + + c.Assert(amendedHash, Equals, headRef.Hash()) + c.Assert(amendedHash, Equals, prevHash) + + commit, err := w.r.CommitObject(headRef.Hash()) + c.Assert(err, IsNil) + c.Assert(commit.Message, Equals, "foo\n") + + assertStorageStatus(c, s.Repository, 13, 11, 10, amendedHash) +} + +func (s *WorktreeSuite) TestCommitAmendWithChanges(c *C) { fs := memfs.New() w := &Worktree{ r: s.Repository, @@ -164,6 +251,34 @@ func (s *WorktreeSuite) TestCommitAmend(c *C) { assertStorageStatus(c, s.Repository, 14, 12, 11, amendedHash) } +func (s *WorktreeSuite) TestCommitAmendNothingToCommit(c *C) { + fs := memfs.New() + w := &Worktree{ + r: s.Repository, + Filesystem: fs, + } + + err := w.Checkout(&CheckoutOptions{}) + c.Assert(err, IsNil) + + err = util.WriteFile(fs, "foo", []byte("foo"), 0644) + c.Assert(err, IsNil) + + _, err = w.Add("foo") + c.Assert(err, IsNil) + + prevHash, err := w.Commit("foo\n", &CommitOptions{Author: defaultSignature()}) + c.Assert(err, IsNil) + + _, err = w.Commit("bar\n", &CommitOptions{Author: defaultSignature(), AllowEmptyCommits: true}) + c.Assert(err, IsNil) + + amendedHash, err := w.Commit("foo\n", &CommitOptions{Author: defaultSignature(), Amend: true}) + c.Log(prevHash, amendedHash) + c.Assert(err, Equals, ErrEmptyCommit) + c.Assert(amendedHash, Equals, plumbing.ZeroHash) +} + func (s *WorktreeSuite) TestAddAndCommitWithSkipStatus(c *C) { expected := plumbing.NewHash("375a3808ffde7f129cdd3c8c252fd0fe37cfd13b") @@ -233,7 +348,9 @@ func (s *WorktreeSuite) TestAddAndCommitWithSkipStatusPathNotModified(c *C) { }) c.Assert(hash, Equals, expected) c.Assert(err, IsNil) + commit1, err := w.r.CommitObject(hash) + c.Assert(err, IsNil) status, err = w.Status() c.Assert(err, IsNil) @@ -256,11 +373,14 @@ func (s *WorktreeSuite) TestAddAndCommitWithSkipStatusPathNotModified(c *C) { c.Assert(foo.Worktree, Equals, Untracked) hash, err = w.Commit("commit with no changes\n", &CommitOptions{ - Author: defaultSignature(), + Author: defaultSignature(), + AllowEmptyCommits: true, }) c.Assert(hash, Equals, expected2) c.Assert(err, IsNil) + commit2, err := w.r.CommitObject(hash) + c.Assert(err, IsNil) status, err = w.Status() c.Assert(err, IsNil) @@ -395,8 +515,7 @@ func (s *WorktreeSuite) TestCommitSignBadKey(c *C) { } func (s *WorktreeSuite) TestCommitTreeSort(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) st := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) _, err := Init(st, nil) @@ -443,8 +562,7 @@ func (s *WorktreeSuite) TestCommitTreeSort(c *C) { // https://github.com/go-git/go-git/pull/224 func (s *WorktreeSuite) TestJustStoreObjectsNotAlreadyStored(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) fsDotgit, err := fs.Chroot(".git") // real fs to get modified timestamps c.Assert(err, IsNil) @@ -501,6 +619,44 @@ func (s *WorktreeSuite) TestJustStoreObjectsNotAlreadyStored(c *C) { c.Assert(infoLicenseSecond.ModTime(), Equals, infoLicense.ModTime()) // object of LICENSE should have the same timestamp because no additional write operation was performed } +func (s *WorktreeSuite) TestCommitInvalidCharactersInAuthorInfos(c *C) { + f := fixtures.Basic().One() + s.Repository = s.NewRepositoryWithEmptyWorktree(f) + + expected := plumbing.NewHash("e8eecef2524c3a37cf0f0996603162f81e0373f1") + + fs := memfs.New() + storage := memory.NewStorage() + + r, err := Init(storage, fs) + c.Assert(err, IsNil) + + w, err := r.Worktree() + c.Assert(err, IsNil) + + util.WriteFile(fs, "foo", []byte("foo"), 0644) + + _, err = w.Add("foo") + c.Assert(err, IsNil) + + hash, err := w.Commit("foo\n", &CommitOptions{Author: invalidSignature()}) + c.Assert(hash, Equals, expected) + c.Assert(err, IsNil) + + assertStorageStatus(c, r, 1, 1, 1, expected) + + // Check HEAD commit contains author informations with '<', '>' and '\n' stripped + lr, err := r.Log(&LogOptions{}) + c.Assert(err, IsNil) + + commit, err := lr.Next() + c.Assert(err, IsNil) + + c.Assert(commit.Author.Name, Equals, "foo bad") + c.Assert(commit.Author.Email, Equals, "badfoo@foo.foo") + +} + func assertStorageStatus( c *C, r *Repository, treesCount, blobCount, commitCount int, head plumbing.Hash, @@ -540,6 +696,15 @@ func defaultSignature() *object.Signature { } } +func invalidSignature() *object.Signature { + when, _ := time.Parse(object.DateFormat, "Thu May 04 00:03:43 2017 +0200") + return &object.Signature{ + Name: "foo \n", + Email: "\nfoo@foo.foo", + When: when, + } +} + func commitSignKey(c *C, decrypt bool) *openpgp.Entity { s := strings.NewReader(armoredKeyRing) es, err := openpgp.ReadArmoredKeyRing(s) diff --git a/worktree_linux.go b/worktree_linux.go index 6fcace2f9..f6b85fe3d 100644 --- a/worktree_linux.go +++ b/worktree_linux.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package git @@ -21,6 +22,6 @@ func init() { } } -func isSymlinkWindowsNonAdmin(err error) bool { +func isSymlinkWindowsNonAdmin(_ error) bool { return false } diff --git a/worktree_status.go b/worktree_status.go index dd9b2439c..7870d138d 100644 --- a/worktree_status.go +++ b/worktree_status.go @@ -29,10 +29,23 @@ var ( // ErrGlobNoMatches in an AddGlob if the glob pattern does not match any // files in the worktree. ErrGlobNoMatches = errors.New("glob pattern did not match any files") + // ErrUnsupportedStatusStrategy occurs when an invalid StatusStrategy is used + // when processing the Worktree status. + ErrUnsupportedStatusStrategy = errors.New("unsupported status strategy") ) // Status returns the working tree status. func (w *Worktree) Status() (Status, error) { + return w.StatusWithOptions(StatusOptions{Strategy: defaultStatusStrategy}) +} + +// StatusOptions defines the options for Worktree.StatusWithOptions(). +type StatusOptions struct { + Strategy StatusStrategy +} + +// StatusWithOptions returns the working tree status. +func (w *Worktree) StatusWithOptions(o StatusOptions) (Status, error) { var hash plumbing.Hash ref, err := w.r.Head() @@ -44,11 +57,14 @@ func (w *Worktree) Status() (Status, error) { hash = ref.Hash() } - return w.status(hash) + return w.status(o.Strategy, hash) } -func (w *Worktree) status(commit plumbing.Hash) (Status, error) { - s := make(Status) +func (w *Worktree) status(ss StatusStrategy, commit plumbing.Hash) (Status, error) { + s, err := ss.new(w) + if err != nil { + return nil, err + } left, err := w.diffCommitWithStaging(commit, false) if err != nil { @@ -354,6 +370,8 @@ func (w *Worktree) doAdd(path string, ignorePattern []gitignore.Pattern, skipSta } } + path = filepath.Clean(path) + if err != nil || !fi.IsDir() { added, h, err = w.doAddFile(idx, s, path, ignorePattern) } else { @@ -488,7 +506,7 @@ func (w *Worktree) copyFileToStorage(path string) (hash plumbing.Hash, err error return w.r.Storer.SetEncodedObject(obj) } -func (w *Worktree) fillEncodedObjectFromFile(dst io.Writer, path string, fi os.FileInfo) (err error) { +func (w *Worktree) fillEncodedObjectFromFile(dst io.Writer, path string, _ os.FileInfo) (err error) { src, err := w.Filesystem.Open(path) if err != nil { return err @@ -503,7 +521,7 @@ func (w *Worktree) fillEncodedObjectFromFile(dst io.Writer, path string, fi os.F return err } -func (w *Worktree) fillEncodedObjectFromSymlink(dst io.Writer, path string, fi os.FileInfo) error { +func (w *Worktree) fillEncodedObjectFromSymlink(dst io.Writer, path string, _ os.FileInfo) error { target, err := w.Filesystem.Readlink(path) if err != nil { return err @@ -543,9 +561,11 @@ func (w *Worktree) doUpdateFileToIndex(e *index.Entry, filename string, h plumbi return err } - if e.Mode.IsRegular() { - e.Size = uint32(info.Size()) - } + // The entry size must always reflect the current state, otherwise + // it will cause go-git's Worktree.Status() to divert from "git status". + // The size of a symlink is the length of the path to the target. + // The size of Regular and Executable files is the size of the files. + e.Size = uint32(info.Size()) fillSystemInfo(e, info.Sys()) return nil diff --git a/worktree_status_test.go b/worktree_status_test.go new file mode 100644 index 000000000..629ebd5bf --- /dev/null +++ b/worktree_status_test.go @@ -0,0 +1,89 @@ +package git + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "github.com/go-git/go-billy/v5/osfs" + "github.com/go-git/go-git/v5/plumbing/cache" + "github.com/go-git/go-git/v5/storage/filesystem" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// For additional context: #1159. +func TestIndexEntrySizeUpdatedForNonRegularFiles(t *testing.T) { + w := osfs.New(t.TempDir(), osfs.WithBoundOS()) + dot, err := w.Chroot(GitDirName) + require.NoError(t, err) + + s := filesystem.NewStorage(dot, cache.NewObjectLRUDefault()) + r, err := Init(s, w) + require.NoError(t, err) + require.NotNil(t, r) + + wt, err := r.Worktree() + require.NoError(t, err) + require.NotNil(t, wt) + + file := "LICENSE" + f, err := w.OpenFile(file, os.O_CREATE|os.O_WRONLY, 0o666) + require.NoError(t, err) + require.NotNil(t, f) + + content := []byte(strings.Repeat("a\n", 1000)) + _, err = f.Write(content) + require.NoError(t, err) + err = f.Close() + require.NoError(t, err) + + _, err = wt.Add(file) + require.NoError(t, err) + + _, err = wt.Commit("add file", &CommitOptions{}) + require.NoError(t, err) + + st, err := wt.StatusWithOptions(StatusOptions{Strategy: Preload}) + require.NoError(t, err) + assert.Equal(t, + &FileStatus{Worktree: Unmodified, Staging: Unmodified}, + st.File(file)) + + // Make the file not regular. The same would apply to a transition + // from regular file to symlink. + err = os.Chmod(filepath.Join(w.Root(), file), 0o777) + require.NoError(t, err) + + f, err = w.OpenFile(file, os.O_APPEND|os.O_RDWR, 0o777) + require.NoError(t, err) + require.NotNil(t, f) + + _, err = f.Write([]byte("\n\n")) + require.NoError(t, err) + err = f.Close() + require.NoError(t, err) + + _, err = wt.Add(file) + assert.NoError(t, err) + + // go-git's Status diverges from "git status", so this check does not + // fail, even when the issue is present. As at this point "git status" + // reports the unstaged file was modified while "git diff" would return + // empty, as the files are the same but the index has the incorrect file + // size. + st, err = wt.StatusWithOptions(StatusOptions{Strategy: Preload}) + assert.NoError(t, err) + assert.Equal(t, + &FileStatus{Worktree: Unmodified, Staging: Modified}, + st.File(file)) + + idx, err := wt.r.Storer.Index() + assert.NoError(t, err) + require.NotNil(t, idx) + require.Len(t, idx.Entries, 1) + + // Check whether the index was updated with the two new line breaks. + assert.Equal(t, uint32(len(content)+2), idx.Entries[0].Size) +} diff --git a/worktree_test.go b/worktree_test.go index 668c30a70..a3dbcfeb3 100644 --- a/worktree_test.go +++ b/worktree_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "errors" - "fmt" "io" "os" "path/filepath" @@ -26,6 +25,7 @@ import ( "github.com/go-git/go-git/v5/storage/memory" "github.com/stretchr/testify/assert" + "github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5/memfs" "github.com/go-git/go-billy/v5/osfs" "github.com/go-git/go-billy/v5/util" @@ -33,9 +33,11 @@ import ( . "gopkg.in/check.v1" ) -var ( - defaultTestCommitOptions = &CommitOptions{Author: &object.Signature{Name: "testuser", Email: "testemail"}} -) +func defaultTestCommitOptions() *CommitOptions { + return &CommitOptions{ + Author: &object.Signature{Name: "testuser", Email: "testemail"}, + } +} type WorktreeSuite struct { BaseSuite @@ -68,8 +70,7 @@ func (s *WorktreeSuite) TestPullCheckout(c *C) { } func (s *WorktreeSuite) TestPullFastForward(c *C) { - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() @@ -78,8 +79,7 @@ func (s *WorktreeSuite) TestPullFastForward(c *C) { }) c.Assert(err, IsNil) - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainClone(dir, false, &CloneOptions{ URL: url, @@ -88,8 +88,9 @@ func (s *WorktreeSuite) TestPullFastForward(c *C) { w, err := server.Worktree() c.Assert(err, IsNil) - err = os.WriteFile(filepath.Join(path, "foo"), []byte("foo"), 0755) + err = os.WriteFile(filepath.Join(url, "foo"), []byte("foo"), 0755) c.Assert(err, IsNil) + w.Add("foo") hash, err := w.Commit("foo", &CommitOptions{Author: defaultSignature()}) c.Assert(err, IsNil) @@ -105,8 +106,7 @@ func (s *WorktreeSuite) TestPullFastForward(c *C) { } func (s *WorktreeSuite) TestPullNonFastForward(c *C) { - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() @@ -115,8 +115,7 @@ func (s *WorktreeSuite) TestPullNonFastForward(c *C) { }) c.Assert(err, IsNil) - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainClone(dir, false, &CloneOptions{ URL: url, @@ -125,15 +124,17 @@ func (s *WorktreeSuite) TestPullNonFastForward(c *C) { w, err := server.Worktree() c.Assert(err, IsNil) - err = os.WriteFile(filepath.Join(path, "foo"), []byte("foo"), 0755) + err = os.WriteFile(filepath.Join(url, "foo"), []byte("foo"), 0755) c.Assert(err, IsNil) + w.Add("foo") _, err = w.Commit("foo", &CommitOptions{Author: defaultSignature()}) c.Assert(err, IsNil) w, err = r.Worktree() c.Assert(err, IsNil) - err = os.WriteFile(filepath.Join(path, "bar"), []byte("bar"), 0755) + err = os.WriteFile(filepath.Join(dir, "bar"), []byte("bar"), 0755) c.Assert(err, IsNil) + w.Add("bar") _, err = w.Commit("bar", &CommitOptions{Author: defaultSignature()}) c.Assert(err, IsNil) @@ -225,8 +226,7 @@ func (s *WorktreeSuite) TestPullProgressWithRecursion(c *C) { path := fixtures.ByTag("submodule").One().Worktree().Root() - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, _ := PlainInit(dir, false) r.CreateRemote(&config.RemoteConfig{ @@ -286,7 +286,8 @@ func (s *RepositorySuite) TestPullAdd(c *C) { func (s *WorktreeSuite) TestPullAlreadyUptodate(c *C) { path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() - r, err := Clone(memory.NewStorage(), memfs.New(), &CloneOptions{ + fs := memfs.New() + r, err := Clone(memory.NewStorage(), fs, &CloneOptions{ URL: filepath.Join(path, ".git"), }) @@ -294,8 +295,9 @@ func (s *WorktreeSuite) TestPullAlreadyUptodate(c *C) { w, err := r.Worktree() c.Assert(err, IsNil) - err = os.WriteFile(filepath.Join(path, "bar"), []byte("bar"), 0755) + err = util.WriteFile(fs, "bar", []byte("bar"), 0755) c.Assert(err, IsNil) + w.Add("bar") _, err = w.Commit("bar", &CommitOptions{Author: defaultSignature()}) c.Assert(err, IsNil) @@ -318,8 +320,7 @@ func (s *WorktreeSuite) TestPullDepth(c *C) { } func (s *WorktreeSuite) TestPullAfterShallowClone(c *C) { - tempDir, clean := s.TemporalDir() - defer clean() + tempDir := c.MkDir() remoteURL := filepath.Join(tempDir, "remote") repoDir := filepath.Join(tempDir, "repo") @@ -447,8 +448,7 @@ func (s *WorktreeSuite) TestCheckoutSymlink(c *C) { c.Skip("git doesn't support symlinks by default in windows") } - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainInit(dir, false) c.Assert(err, IsNil) @@ -478,7 +478,8 @@ func (s *WorktreeSuite) TestCheckoutSymlink(c *C) { func (s *WorktreeSuite) TestCheckoutSparse(c *C) { fs := memfs.New() r, err := Clone(memory.NewStorage(), fs, &CloneOptions{ - URL: s.GetBasicLocalRepositoryURL(), + URL: s.GetBasicLocalRepositoryURL(), + NoCheckout: true, }) c.Assert(err, IsNil) @@ -511,8 +512,7 @@ func (s *WorktreeSuite) TestFilenameNormalization(c *C) { c.Skip("windows paths may contain non utf-8 sequences") } - url, clean := s.TemporalDir() - defer clean() + url := c.MkDir() path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() @@ -704,8 +704,7 @@ func (s *WorktreeSuite) TestCheckoutIndexMem(c *C) { } func (s *WorktreeSuite) TestCheckoutIndexOS(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) w := &Worktree{ r: s.Repository, @@ -1002,14 +1001,14 @@ func (s *WorktreeSuite) TestStatusCheckedInBeforeIgnored(c *C) { _, err = w.Add("fileToIgnore") c.Assert(err, IsNil) - _, err = w.Commit("Added file that will be ignored later", defaultTestCommitOptions) + _, err = w.Commit("Added file that will be ignored later", defaultTestCommitOptions()) c.Assert(err, IsNil) err = util.WriteFile(fs, ".gitignore", []byte("fileToIgnore\nsecondIgnoredFile"), 0755) c.Assert(err, IsNil) _, err = w.Add(".gitignore") c.Assert(err, IsNil) - _, err = w.Commit("Added .gitignore", defaultTestCommitOptions) + _, err = w.Commit("Added .gitignore", defaultTestCommitOptions()) c.Assert(err, IsNil) status, err := w.Status() c.Assert(err, IsNil) @@ -1052,6 +1051,33 @@ func (s *WorktreeSuite) TestStatusEmptyDirty(c *C) { c.Assert(status, HasLen, 1) } +func (s *WorktreeSuite) TestStatusUnmodified(c *C) { + fs := memfs.New() + w := &Worktree{ + r: s.Repository, + Filesystem: fs, + } + + err := w.Checkout(&CheckoutOptions{Force: true}) + c.Assert(err, IsNil) + + status, err := w.StatusWithOptions(StatusOptions{Strategy: Preload}) + c.Assert(err, IsNil) + c.Assert(status.IsClean(), Equals, true) + c.Assert(status.IsUntracked("LICENSE"), Equals, false) + + c.Assert(status.File("LICENSE").Staging, Equals, Unmodified) + c.Assert(status.File("LICENSE").Worktree, Equals, Unmodified) + + status, err = w.StatusWithOptions(StatusOptions{Strategy: Empty}) + c.Assert(err, IsNil) + c.Assert(status.IsClean(), Equals, true) + c.Assert(status.IsUntracked("LICENSE"), Equals, false) + + c.Assert(status.File("LICENSE").Staging, Equals, Untracked) + c.Assert(status.File("LICENSE").Worktree, Equals, Untracked) +} + func (s *WorktreeSuite) TestReset(c *C) { fs := memfs.New() w := &Worktree{ @@ -1215,6 +1241,44 @@ func (s *WorktreeSuite) TestResetHard(c *C) { c.Assert(branch.Hash(), Equals, commit) } +func (s *WorktreeSuite) TestResetHardSubFolders(c *C) { + fs := memfs.New() + w := &Worktree{ + r: s.Repository, + Filesystem: fs, + } + + err := w.Checkout(&CheckoutOptions{}) + c.Assert(err, IsNil) + + err = fs.MkdirAll("dir", os.ModePerm) + c.Assert(err, IsNil) + tf, err := fs.Create("./dir/testfile.txt") + c.Assert(err, IsNil) + _, err = tf.Write([]byte("testfile content")) + c.Assert(err, IsNil) + err = tf.Close() + c.Assert(err, IsNil) + _, err = w.Add("dir/testfile.txt") + c.Assert(err, IsNil) + _, err = w.Commit("testcommit", &CommitOptions{Author: &object.Signature{Name: "name", Email: "email"}}) + c.Assert(err, IsNil) + + err = fs.Remove("dir/testfile.txt") + c.Assert(err, IsNil) + + status, err := w.Status() + c.Assert(err, IsNil) + c.Assert(status.IsClean(), Equals, false) + + err = w.Reset(&ResetOptions{Files: []string{"dir/testfile.txt"}, Mode: HardReset}) + c.Assert(err, IsNil) + + status, err = w.Status() + c.Assert(err, IsNil) + c.Assert(status.IsClean(), Equals, true) +} + func (s *WorktreeSuite) TestResetHardWithGitIgnore(c *C) { fs := memfs.New() w := &Worktree{ @@ -1241,6 +1305,7 @@ func (s *WorktreeSuite) TestResetHardWithGitIgnore(c *C) { f, err := fs.Create(".gitignore") c.Assert(err, IsNil) _, err = f.Write([]byte("foo\n")) + c.Assert(err, IsNil) _, err = f.Write([]byte("newTestFile.txt\n")) c.Assert(err, IsNil) err = f.Close() @@ -1258,6 +1323,29 @@ func (s *WorktreeSuite) TestResetHardWithGitIgnore(c *C) { c.Assert(status.IsClean(), Equals, true) } +func (s *WorktreeSuite) TestResetSparsely(c *C) { + fs := memfs.New() + w := &Worktree{ + r: s.Repository, + Filesystem: fs, + } + + sparseResetDirs := []string{"php"} + + err := w.ResetSparsely(&ResetOptions{Mode: HardReset}, sparseResetDirs) + c.Assert(err, IsNil) + + files, err := fs.ReadDir("/") + c.Assert(err, IsNil) + c.Assert(files, HasLen, 1) + c.Assert(files[0].Name(), Equals, "php") + + files, err = fs.ReadDir("/php") + c.Assert(err, IsNil) + c.Assert(files, HasLen, 1) + c.Assert(files[0].Name(), Equals, "crappy.php") +} + func (s *WorktreeSuite) TestStatusAfterCheckout(c *C) { fs := memfs.New() w := &Worktree{ @@ -1275,8 +1363,7 @@ func (s *WorktreeSuite) TestStatusAfterCheckout(c *C) { } func (s *WorktreeSuite) TestStatusModified(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) w := &Worktree{ r: s.Repository, @@ -1367,8 +1454,7 @@ func (s *WorktreeSuite) TestStatusUntracked(c *C) { } func (s *WorktreeSuite) TestStatusDeleted(c *C) { - fs, clean := s.TemporalFilesystem() - defer clean() + fs := s.TemporalFilesystem(c) w := &Worktree{ r: s.Repository, @@ -1740,8 +1826,7 @@ func (s *WorktreeSuite) TestAddRemovedInDirectoryDot(c *C) { } func (s *WorktreeSuite) TestAddSymlink(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainInit(dir, false) c.Assert(err, IsNil) @@ -1922,6 +2007,66 @@ func (s *WorktreeSuite) TestAddGlob(c *C) { c.Assert(file.Worktree, Equals, Unmodified) } +func (s *WorktreeSuite) TestAddFilenameStartingWithDot(c *C) { + fs := memfs.New() + w := &Worktree{ + r: s.Repository, + Filesystem: fs, + } + + err := w.Checkout(&CheckoutOptions{Force: true}) + c.Assert(err, IsNil) + + idx, err := w.r.Storer.Index() + c.Assert(err, IsNil) + c.Assert(idx.Entries, HasLen, 9) + + err = util.WriteFile(w.Filesystem, "qux", []byte("QUX"), 0o755) + c.Assert(err, IsNil) + err = util.WriteFile(w.Filesystem, "baz", []byte("BAZ"), 0o755) + c.Assert(err, IsNil) + err = util.WriteFile(w.Filesystem, "foo/bar/baz", []byte("BAZ"), 0o755) + c.Assert(err, IsNil) + + _, err = w.Add("./qux") + c.Assert(err, IsNil) + + _, err = w.Add("./baz") + c.Assert(err, IsNil) + + _, err = w.Add("foo/bar/../bar/./baz") + c.Assert(err, IsNil) + + idx, err = w.r.Storer.Index() + c.Assert(err, IsNil) + c.Assert(idx.Entries, HasLen, 12) + + e, err := idx.Entry("qux") + c.Assert(err, IsNil) + c.Assert(e.Mode, Equals, filemode.Executable) + + e, err = idx.Entry("baz") + c.Assert(err, IsNil) + c.Assert(e.Mode, Equals, filemode.Executable) + + status, err := w.Status() + c.Assert(err, IsNil) + c.Assert(status, HasLen, 3) + + file := status.File("qux") + c.Assert(file.Staging, Equals, Added) + c.Assert(file.Worktree, Equals, Unmodified) + + file = status.File("baz") + c.Assert(file.Staging, Equals, Added) + c.Assert(file.Worktree, Equals, Unmodified) + + file = status.File("foo/bar/baz") + c.Assert(file.Staging, Equals, Added) + c.Assert(file.Worktree, Equals, Unmodified) + +} + func (s *WorktreeSuite) TestAddGlobErrorNoMatches(c *C) { r, _ := Init(memory.NewStorage(), memfs.New()) w, _ := r.Worktree() @@ -2056,7 +2201,7 @@ func (s *WorktreeSuite) TestAddSkipStatusWithIgnoredPath(c *C) { c.Assert(err, IsNil) _, err = w.Add(".gitignore") c.Assert(err, IsNil) - _, err = w.Commit("Added .gitignore", defaultTestCommitOptions) + _, err = w.Commit("Added .gitignore", defaultTestCommitOptions()) c.Assert(err, IsNil) err = util.WriteFile(fs, "fileToIgnore", []byte("file to ignore"), 0644) @@ -2621,8 +2766,7 @@ func (s *WorktreeSuite) TestGrep(c *C) { path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() server, err := PlainClone(dir, false, &CloneOptions{ URL: path, @@ -2705,8 +2849,7 @@ func (s *WorktreeSuite) TestGrepBare(c *C) { path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() r, err := PlainClone(dir, true, &CloneOptions{ URL: path, @@ -2754,8 +2897,7 @@ func (s *WorktreeSuite) TestGrepBare(c *C) { } func (s *WorktreeSuite) TestResetLingeringDirectories(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() commitOpts := &CommitOptions{Author: &object.Signature{ Name: "foo", @@ -2806,8 +2948,7 @@ func (s *WorktreeSuite) TestResetLingeringDirectories(c *C) { func (s *WorktreeSuite) TestAddAndCommit(c *C) { expectedFiles := 2 - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() repo, err := PlainInit(dir, false) c.Assert(err, IsNil) @@ -2849,8 +2990,7 @@ func (s *WorktreeSuite) TestAddAndCommit(c *C) { } func (s *WorktreeSuite) TestAddAndCommitEmpty(c *C) { - dir, clean := s.TemporalDir() - defer clean() + dir := c.MkDir() repo, err := PlainInit(dir, false) c.Assert(err, IsNil) @@ -2982,7 +3122,7 @@ func TestValidPath(t *testing.T) { } for _, tc := range tests { - t.Run(fmt.Sprintf("%s", tc.path), func(t *testing.T) { + t.Run(tc.path, func(t *testing.T) { err := validPath(tc.path) if tc.wantErr { assert.Error(t, err) @@ -3013,9 +3153,240 @@ func TestWindowsValidPath(t *testing.T) { } for _, tc := range tests { - t.Run(fmt.Sprintf("%s", tc.path), func(t *testing.T) { + t.Run(tc.path, func(t *testing.T) { got := windowsValidPath(tc.path) assert.Equal(t, tc.want, got) }) } } + +var statusCodeNames = map[StatusCode]string{ + Unmodified: "Unmodified", + Untracked: "Untracked", + Modified: "Modified", + Added: "Added", + Deleted: "Deleted", + Renamed: "Renamed", + Copied: "Copied", + UpdatedButUnmerged: "UpdatedButUnmerged", +} + +func setupForRestore(c *C, s *WorktreeSuite) (fs billy.Filesystem, w *Worktree, names []string) { + fs = memfs.New() + w = &Worktree{ + r: s.Repository, + Filesystem: fs, + } + + err := w.Checkout(&CheckoutOptions{}) + c.Assert(err, IsNil) + + names = []string{"foo", "CHANGELOG", "LICENSE", "binary.jpg"} + verifyStatus(c, "Checkout", w, names, []FileStatus{ + {Worktree: Untracked, Staging: Untracked}, + {Worktree: Untracked, Staging: Untracked}, + {Worktree: Untracked, Staging: Untracked}, + {Worktree: Untracked, Staging: Untracked}, + }) + + // Touch of bunch of files including create a new file and delete an exsiting file + for _, name := range names { + err = util.WriteFile(fs, name, []byte("Foo Bar"), 0755) + c.Assert(err, IsNil) + } + err = util.RemoveAll(fs, names[3]) + c.Assert(err, IsNil) + + // Confirm the status after doing the edits without staging anything + verifyStatus(c, "Edits", w, names, []FileStatus{ + {Worktree: Untracked, Staging: Untracked}, + {Worktree: Modified, Staging: Unmodified}, + {Worktree: Modified, Staging: Unmodified}, + {Worktree: Deleted, Staging: Unmodified}, + }) + + // Stage all files and verify the updated status + for _, name := range names { + _, err = w.Add(name) + c.Assert(err, IsNil) + } + verifyStatus(c, "Staged", w, names, []FileStatus{ + {Worktree: Unmodified, Staging: Added}, + {Worktree: Unmodified, Staging: Modified}, + {Worktree: Unmodified, Staging: Modified}, + {Worktree: Unmodified, Staging: Deleted}, + }) + + // Add secondary changes to a file to make sure we only restore the staged file + err = util.WriteFile(fs, names[1], []byte("Foo Bar:11"), 0755) + c.Assert(err, IsNil) + err = util.WriteFile(fs, names[2], []byte("Foo Bar:22"), 0755) + c.Assert(err, IsNil) + + verifyStatus(c, "Secondary Edits", w, names, []FileStatus{ + {Worktree: Unmodified, Staging: Added}, + {Worktree: Modified, Staging: Modified}, + {Worktree: Modified, Staging: Modified}, + {Worktree: Unmodified, Staging: Deleted}, + }) + + return +} + +func verifyStatus(c *C, marker string, w *Worktree, files []string, statuses []FileStatus) { + c.Assert(len(files), Equals, len(statuses)) + + status, err := w.Status() + c.Assert(err, IsNil) + + for i, file := range files { + current := status.File(file) + expected := statuses[i] + c.Assert(current.Worktree, Equals, expected.Worktree, Commentf("%s - [%d] : %s Worktree %s != %s", marker, i, file, statusCodeNames[current.Worktree], statusCodeNames[expected.Worktree])) + c.Assert(current.Staging, Equals, expected.Staging, Commentf("%s - [%d] : %s Staging %s != %s", marker, i, file, statusCodeNames[current.Staging], statusCodeNames[expected.Staging])) + } +} + +func (s *WorktreeSuite) TestRestoreStaged(c *C) { + fs, w, names := setupForRestore(c, s) + + // Attempt without files should throw an error like the git restore --staged + opts := RestoreOptions{Staged: true} + err := w.Restore(&opts) + c.Assert(err, Equals, ErrNoRestorePaths) + + // Restore Staged files in 2 groups and confirm status + opts.Files = []string{names[0], "./" + names[1]} + err = w.Restore(&opts) + c.Assert(err, IsNil) + verifyStatus(c, "Restored First", w, names, []FileStatus{ + {Worktree: Untracked, Staging: Untracked}, + {Worktree: Modified, Staging: Unmodified}, + {Worktree: Modified, Staging: Modified}, + {Worktree: Unmodified, Staging: Deleted}, + }) + + // Make sure the restore didn't overwrite our secondary changes + contents, err := util.ReadFile(fs, names[1]) + c.Assert(err, IsNil) + c.Assert(string(contents), Equals, "Foo Bar:11") + + opts.Files = []string{"./" + names[2], names[3]} + err = w.Restore(&opts) + c.Assert(err, IsNil) + verifyStatus(c, "Restored Second", w, names, []FileStatus{ + {Worktree: Untracked, Staging: Untracked}, + {Worktree: Modified, Staging: Unmodified}, + {Worktree: Modified, Staging: Unmodified}, + {Worktree: Deleted, Staging: Unmodified}, + }) + + // Make sure the restore didn't overwrite our secondary changes + contents, err = util.ReadFile(fs, names[2]) + c.Assert(err, IsNil) + c.Assert(string(contents), Equals, "Foo Bar:22") +} + +func (s *WorktreeSuite) TestRestoreWorktree(c *C) { + _, w, names := setupForRestore(c, s) + + // Attempt without files should throw an error like the git restore + opts := RestoreOptions{} + err := w.Restore(&opts) + c.Assert(err, Equals, ErrNoRestorePaths) + + opts.Files = []string{names[0], names[1]} + err = w.Restore(&opts) + c.Assert(err, Equals, ErrRestoreWorktreeOnlyNotSupported) +} + +func (s *WorktreeSuite) TestRestoreBoth(c *C) { + _, w, names := setupForRestore(c, s) + + // Attempt without files should throw an error like the git restore --staged --worktree + opts := RestoreOptions{Staged: true, Worktree: true} + err := w.Restore(&opts) + c.Assert(err, Equals, ErrNoRestorePaths) + + // Restore Staged files in 2 groups and confirm status + opts.Files = []string{names[0], names[1]} + err = w.Restore(&opts) + c.Assert(err, IsNil) + verifyStatus(c, "Restored First", w, names, []FileStatus{ + {Worktree: Untracked, Staging: Untracked}, + {Worktree: Untracked, Staging: Untracked}, + {Worktree: Modified, Staging: Modified}, + {Worktree: Unmodified, Staging: Deleted}, + }) + + opts.Files = []string{names[2], names[3]} + err = w.Restore(&opts) + c.Assert(err, IsNil) + verifyStatus(c, "Restored Second", w, names, []FileStatus{ + {Worktree: Untracked, Staging: Untracked}, + {Worktree: Untracked, Staging: Untracked}, + {Worktree: Untracked, Staging: Untracked}, + {Worktree: Untracked, Staging: Untracked}, + }) +} + +func TestFilePermissions(t *testing.T) { + + // Initialize an in memory repository + remoteUrl := t.TempDir() + + inMemoryFs := memfs.New() + remoteFs := osfs.New(remoteUrl) + remoteStorage := filesystem.NewStorage(remoteFs, cache.NewObjectLRUDefault()) + + remoteRepository, err := Init(remoteStorage, inMemoryFs) + assert.NoError(t, err) + + err = util.WriteFile(inMemoryFs, "fileWithExecuteBit", []byte("Initial data"), 0755) + assert.NoError(t, err) + + err = util.WriteFile(inMemoryFs, "regularFile", []byte("Initial data"), 0644) + assert.NoError(t, err) + + remoteWorktree, err := remoteRepository.Worktree() + assert.NoError(t, err) + + _, err = remoteWorktree.Add("fileWithExecuteBit") + assert.NoError(t, err) + + _, err = remoteWorktree.Add("regularFile") + assert.NoError(t, err) + + _, err = remoteWorktree.Commit("my commit", &CommitOptions{}) + assert.NoError(t, err) + + worktreePath := t.TempDir() + + localRepo, err := PlainClone(worktreePath, false, &CloneOptions{URL: remoteUrl}) + assert.NoError(t, err) + + localWorktree, err := localRepo.Worktree() + assert.NoError(t, err) + + idx, err := localWorktree.r.Storer.Index() + assert.NoError(t, err) + + expectedEntries := []index.Entry{ + { + Name: "fileWithExecuteBit", + Mode: filemode.Executable, + }, + { + Name: "regularFile", + Mode: filemode.Regular, + }, + } + + assert.Len(t, idx.Entries, len(expectedEntries)) + + for i, expectedEntry := range expectedEntries { + assert.Equal(t, expectedEntry.Name, idx.Entries[i].Name) + assert.Equal(t, expectedEntry.Mode, idx.Entries[i].Mode) + } + +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy