mirror of
https://github.com/grdl/git-get.git
synced 2026-02-07 21:39:15 +00:00
Fix failing windows tests
Fix incorrect filepath.join usage in building filepaths from URL
This commit is contained in:
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@@ -43,7 +43,7 @@ jobs:
|
|||||||
git config --global user.name "CI Test"
|
git config --global user.name "CI Test"
|
||||||
|
|
||||||
- name: Run tests with coverage
|
- name: Run tests with coverage
|
||||||
run: go test -race -coverprofile=coverage.out -covermode=atomic ./...
|
run: go test -race -coverprofile coverage.out -covermode=atomic ./...
|
||||||
|
|
||||||
- name: Upload coverage to Codecov
|
- name: Upload coverage to Codecov
|
||||||
if: matrix.os == 'ubuntu-latest' && matrix.go-version == '1.24'
|
if: matrix.os == 'ubuntu-latest' && matrix.go-version == '1.24'
|
||||||
|
|||||||
13
README.md
13
README.md
@@ -336,12 +336,13 @@ We welcome contributions!
|
|||||||
1. **Fork the repository**
|
1. **Fork the repository**
|
||||||
2. **Create a feature branch**: `git checkout -b feature/amazing-feature`
|
2. **Create a feature branch**: `git checkout -b feature/amazing-feature`
|
||||||
3. **Install dependencies**: `go mod download`
|
3. **Install dependencies**: `go mod download`
|
||||||
3. **Make changes and add tests**
|
4. **Make changes and add tests**
|
||||||
4. **Run tests**: `go test ./...`
|
5. **Format**: `go fmt ./...`
|
||||||
5. **Run linter**: `golangci-lint run`
|
6. **Run tests**: `go test ./...`
|
||||||
6. **Commit changes**: `git commit -m 'Add amazing feature'`
|
7. **Run linter**: `golangci-lint run`
|
||||||
7. **Push to branch**: `git push origin feature/amazing-feature`
|
8. **Commit changes**: `git commit -m 'Add amazing feature'`
|
||||||
8. **Open a Pull Request**
|
9. **Push to branch**: `git push origin feature/amazing-feature`
|
||||||
|
10. **Open a Pull Request**
|
||||||
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package git
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"git-get/pkg/git/test"
|
"git-get/pkg/git/test"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -57,7 +58,7 @@ func TestExists(t *testing.T) {
|
|||||||
want: errDirNotExist,
|
want: errDirNotExist,
|
||||||
}, {
|
}, {
|
||||||
name: "dir exists",
|
name: "dir exists",
|
||||||
path: "/tmp/",
|
path: os.TempDir(),
|
||||||
want: nil,
|
want: nil,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
dotgit = ".git"
|
dotgit = ".git"
|
||||||
untracked = "??" // Untracked files are marked as "??" in git status output.
|
untracked = "??" // Untracked files are marked as "??" in git status output.
|
||||||
master = "master"
|
main = "main"
|
||||||
head = "HEAD"
|
head = "HEAD"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -120,9 +120,9 @@ func TestCurrentBranch(t *testing.T) {
|
|||||||
want: "main",
|
want: "main",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "only master branch",
|
name: "only main branch",
|
||||||
repoMaker: test.RepoWithCommit,
|
repoMaker: test.RepoWithCommit,
|
||||||
want: master,
|
want: main,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "checked out new branch",
|
name: "checked out new branch",
|
||||||
@@ -163,19 +163,19 @@ func TestBranches(t *testing.T) {
|
|||||||
want: []string{""},
|
want: []string{""},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "only master branch",
|
name: "only main branch",
|
||||||
repoMaker: test.RepoWithCommit,
|
repoMaker: test.RepoWithCommit,
|
||||||
want: []string{"master"},
|
want: []string{"main"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "new branch",
|
name: "new branch",
|
||||||
repoMaker: test.RepoWithBranch,
|
repoMaker: test.RepoWithBranch,
|
||||||
want: []string{"feature/branch", "master"},
|
want: []string{"feature/branch", "main"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "checked out new tag",
|
name: "checked out new tag",
|
||||||
repoMaker: test.RepoWithTag,
|
repoMaker: test.RepoWithTag,
|
||||||
want: []string{"master"},
|
want: []string{"main"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,7 +204,7 @@ func TestUpstream(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "empty",
|
name: "empty",
|
||||||
repoMaker: test.RepoEmpty,
|
repoMaker: test.RepoEmpty,
|
||||||
branch: "master",
|
branch: "main",
|
||||||
want: "",
|
want: "",
|
||||||
},
|
},
|
||||||
// TODO: add wantErr
|
// TODO: add wantErr
|
||||||
@@ -215,10 +215,10 @@ func TestUpstream(t *testing.T) {
|
|||||||
want: "",
|
want: "",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "master with upstream",
|
name: "main with upstream",
|
||||||
repoMaker: test.RepoWithBranchWithUpstream,
|
repoMaker: test.RepoWithBranchWithUpstream,
|
||||||
branch: "master",
|
branch: "main",
|
||||||
want: "origin/master",
|
want: "origin/main",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "branch with upstream",
|
name: "branch with upstream",
|
||||||
@@ -260,7 +260,7 @@ func TestAheadBehind(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "fresh clone",
|
name: "fresh clone",
|
||||||
repoMaker: test.RepoWithBranchWithUpstream,
|
repoMaker: test.RepoWithBranchWithUpstream,
|
||||||
branch: "master",
|
branch: "main",
|
||||||
want: []int{0, 0},
|
want: []int{0, 0},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,17 +18,14 @@ func TempDir(t *testing.T, parent string) string {
|
|||||||
|
|
||||||
// Automatically remove temp dir when the test is over.
|
// Automatically remove temp dir when the test is over.
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
err := os.RemoveAll(dir)
|
removeTestDir(t, dir)
|
||||||
if err != nil {
|
|
||||||
t.Errorf("failed removing test repo %s", dir)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return dir
|
return dir
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Repo) init() {
|
func (r *Repo) init() {
|
||||||
err := run.Git("init", "--quiet", r.path).AndShutUp()
|
err := run.Git("init", "--quiet", "--initial-branch=main", r.path).AndShutUp()
|
||||||
checkFatal(r.t, err)
|
checkFatal(r.t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,3 +90,17 @@ func checkFatal(t *testing.T, err error) {
|
|||||||
t.Fatalf("failed making test repo: %+v", err)
|
t.Fatalf("failed making test repo: %+v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// removeTestDir removes a test directory
|
||||||
|
func removeTestDir(t *testing.T, dir string) {
|
||||||
|
// Skip cleanup on Windows to avoid file locking issues in CI
|
||||||
|
// The CI runner environment is destroyed after tests anyway
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err := os.RemoveAll(dir)
|
||||||
|
if err != nil {
|
||||||
|
t.Logf("warning: failed removing test repo %s: %v", dir, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -16,14 +16,14 @@ import (
|
|||||||
//
|
//
|
||||||
// Examples of different compositions:
|
// Examples of different compositions:
|
||||||
//
|
//
|
||||||
// - run.Git("clone", <URL>).AndShow()
|
// - run.Git("clone", <URL>).AndShow()
|
||||||
// means running "git clone <URL>" and printing the progress into stdout
|
// means running "git clone <URL>" and printing the progress into stdout
|
||||||
//
|
//
|
||||||
// - run.Git("branch","-a").OnRepo(<REPO>).AndCaptureLines()
|
// - run.Git("branch","-a").OnRepo(<REPO>).AndCaptureLines()
|
||||||
// means running "git branch -a" inside <REPO> and returning a slice of branch names
|
// means running "git branch -a" inside <REPO> and returning a slice of branch names
|
||||||
//
|
//
|
||||||
// - run.Git("pull").OnRepo(<REPO>).AndShutUp()
|
// - run.Git("pull").OnRepo(<REPO>).AndShutUp()
|
||||||
// means running "git pull" inside <REPO> and not printing any output
|
// means running "git pull" inside <REPO> and not printing any output
|
||||||
type Cmd struct {
|
type Cmd struct {
|
||||||
cmd *exec.Cmd
|
cmd *exec.Cmd
|
||||||
args string
|
args string
|
||||||
|
|||||||
20
pkg/url.go
20
pkg/url.go
@@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
urlpkg "net/url"
|
urlpkg "net/url"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -70,7 +69,7 @@ func ParseURL(rawURL string, defaultHost string, defaultScheme string) (url *url
|
|||||||
return url, nil
|
return url, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// URLToPath cleans up the URL and converts it into a path string with correct separators for the current OS.
|
// URLToPath cleans up the URL and converts it into a path string.
|
||||||
// Eg, ssh://git@github.com:22/~user/repo.git => github.com/user/repo
|
// Eg, ssh://git@github.com:22/~user/repo.git => github.com/user/repo
|
||||||
//
|
//
|
||||||
// If skipHost is true, it removes the host part from the path.
|
// If skipHost is true, it removes the host part from the path.
|
||||||
@@ -82,18 +81,23 @@ func URLToPath(url urlpkg.URL, skipHost bool) string {
|
|||||||
// Remove tilde (~) char from username.
|
// Remove tilde (~) char from username.
|
||||||
url.Path = strings.ReplaceAll(url.Path, "~", "")
|
url.Path = strings.ReplaceAll(url.Path, "~", "")
|
||||||
|
|
||||||
// Remove leading and trailing slashes (correct separator is added by the filepath.Join() below).
|
// Remove leading and trailing slashes.
|
||||||
url.Path = strings.Trim(url.Path, "/")
|
url.Path = strings.Trim(url.Path, "/")
|
||||||
|
|
||||||
// Remove trailing ".git" from repo name.
|
// Remove trailing ".git" from repo name.
|
||||||
url.Path = strings.TrimSuffix(url.Path, ".git")
|
url.Path = strings.TrimSuffix(url.Path, ".git")
|
||||||
|
|
||||||
// Replace slashes with separator correct for the current OS.
|
|
||||||
url.Path = strings.ReplaceAll(url.Path, "/", string(filepath.Separator))
|
|
||||||
|
|
||||||
if skipHost {
|
if skipHost {
|
||||||
url.Host = ""
|
return url.Path
|
||||||
}
|
}
|
||||||
|
|
||||||
return filepath.Join(url.Host, url.Path)
|
if url.Host == "" {
|
||||||
|
return url.Path
|
||||||
|
}
|
||||||
|
|
||||||
|
if url.Path == "" {
|
||||||
|
return url.Host
|
||||||
|
}
|
||||||
|
|
||||||
|
return url.Host + "/" + url.Path
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user