diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index a862f7df..42c99ae5 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -1,7 +1,7 @@ name: Go on: push: - branches: [ master ] + branches: [ master, v2 ] paths: - '**.go' - 'go.mod' @@ -29,7 +29,7 @@ jobs: - name: Install Go uses: actions/setup-go@v5 with: - go-version: 1.24.x + go-version: 1.26.x - name: Check Go module tidiness shell: bash run: | @@ -42,7 +42,7 @@ jobs: exit 1 fi - name: Run golangci-lint - uses: golangci/golangci-lint-action@v3 + uses: golangci/golangci-lint-action@v9 with: version: latest args: --timeout=30m @@ -51,7 +51,7 @@ jobs: name: Test strategy: matrix: - go-version: [ 1.24.x ] + go-version: [ 1.26.x ] platform: [ ubuntu-latest, macos-latest, windows-latest ] runs-on: ${{ matrix.platform }} steps: diff --git a/.golangci.yml b/.golangci.yml index 479ac812..f4e977cb 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,24 +1,41 @@ -linters-settings: - staticcheck: - checks: [ - "all", - "-SA1019" # There are valid use cases of strings.Title - ] - nakedret: - max-func-lines: 0 # Disallow any unnamed return statement - +version: "2" linters: enable: - - unused - - errcheck - - gosimple - - govet - - ineffassign - - staticcheck - - typecheck - nakedret - - gofmt - rowserrcheck - unconvert - - goimports - unparam + settings: + govet: + disable: + # printf: non-constant format string in call to fmt.Errorf (govet) + # showing up since golangci-lint version 1.60.1 + - printf + staticcheck: + checks: + - all + - "-QF1001" # I'm a math noob + - "-ST1016" # Some legit code uses this pattern + nakedret: + max-func-lines: 0 # Disallow any unnamed return statement + exclusions: + generated: lax + presets: + - comments + - common-false-positives + - legacy + - std-error-handling + paths: + - third_party$ + - builtin$ + - examples$ +formatters: + enable: + - gofmt + - goimports + exclusions: + generated: lax + paths: + - third_party$ + - builtin$ + - examples$ diff --git a/commit.go b/commit.go index e287df65..301be259 100644 --- a/commit.go +++ b/commit.go @@ -7,7 +7,6 @@ package git import ( "bytes" "io" - "io/ioutil" "net/http" "strings" "sync" @@ -154,7 +153,7 @@ func (c *Commit) isImageFile(blob *Blob, err error) (bool, error) { N: int64(buf.Cap()), } - err = blob.Pipeline(stdout, ioutil.Discard) + err = blob.Pipeline(stdout, io.Discard) if err != nil { return false, err } diff --git a/diff.go b/diff.go index 3dab3733..e70932e6 100644 --- a/diff.go +++ b/diff.go @@ -10,7 +10,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "strconv" "strings" ) @@ -482,7 +481,7 @@ func (p *diffParser) parse() (*Diff, error) { // Check if reached maximum number of files if p.maxFiles > 0 && len(diff.Files) >= p.maxFiles { diff.isIncomplete = true - _, _ = io.Copy(ioutil.Discard, p) + _, _ = io.Copy(io.Discard, p) break } diff --git a/go.mod b/go.mod index 83a77403..0753589a 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/gogs/git-module/v2 -go 1.24.0 +go 1.26.0 require github.com/stretchr/testify v1.11.1 diff --git a/hook.go b/hook.go index 566d5381..7083c1ad 100644 --- a/hook.go +++ b/hook.go @@ -5,7 +5,6 @@ package git import ( - "io/ioutil" "os" "path" "strings" @@ -235,11 +234,11 @@ func (h *Hook) Content() string { // memory copy of the content as well. func (h *Hook) Update(content string) error { h.content = strings.TrimSpace(content) - h.content = strings.Replace(h.content, "\r", "", -1) + h.content = strings.ReplaceAll(h.content, "\r", "") if err := os.MkdirAll(path.Dir(h.path), os.ModePerm); err != nil { return err - } else if err = ioutil.WriteFile(h.path, []byte(h.content), os.ModePerm); err != nil { + } else if err = os.WriteFile(h.path, []byte(h.content), os.ModePerm); err != nil { return err } diff --git a/hook_test.go b/hook_test.go index 0eda6b4b..92cd4866 100644 --- a/hook_test.go +++ b/hook_test.go @@ -5,7 +5,6 @@ package git import ( - "io/ioutil" "os" "testing" @@ -43,7 +42,7 @@ func TestHook_Update(t *testing.T) { t.Fatal(err) } - p, err := ioutil.ReadFile(path) + p, err := os.ReadFile(path) if err != nil { t.Fatal(err) } diff --git a/repo.go b/repo.go index dd327131..1eee7b2d 100644 --- a/repo.go +++ b/repo.go @@ -253,29 +253,18 @@ type PushOptions struct { CommandOptions } -// Push pushes local changes to given remote and branch for the repository in -// given path. -func Push(repoPath, remote, branch string, opts ...PushOptions) error { +// Push pushes local changes to given remote and branch for the repository. +func (r *Repository) Push(remote, branch string, opts ...PushOptions) error { var opt PushOptions if len(opts) > 0 { opt = opts[0] } cmd := NewCommand("push").AddOptions(opt.CommandOptions).AddArgs("--end-of-options", remote, branch) - _, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath) + _, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path) return err } -// Deprecated: Use Push instead. -func RepoPush(repoPath, remote, branch string, opts ...PushOptions) error { - return Push(repoPath, remote, branch, opts...) -} - -// Push pushes local changes to given remote and branch for the repository. -func (r *Repository) Push(remote, branch string, opts ...PushOptions) error { - return Push(r.path, remote, branch, opts...) -} - // CheckoutOptions contains optional arguments for checking out to a branch. // // Docs: https://git-scm.com/docs/git-checkout @@ -291,8 +280,8 @@ type CheckoutOptions struct { CommandOptions } -// Checkout checks out to given branch for the repository in given path. -func Checkout(repoPath, branch string, opts ...CheckoutOptions) error { +// Checkout checks out to given branch for the repository. +func (r *Repository) Checkout(branch string, opts ...CheckoutOptions) error { var opt CheckoutOptions if len(opts) > 0 { opt = opts[0] @@ -307,20 +296,10 @@ func Checkout(repoPath, branch string, opts ...CheckoutOptions) error { cmd.AddArgs(opt.BaseBranch) } - _, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath) + _, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path) return err } -// Deprecated: Use Checkout instead. -func RepoCheckout(repoPath, branch string, opts ...CheckoutOptions) error { - return Checkout(repoPath, branch, opts...) -} - -// Checkout checks out to given branch for the repository. -func (r *Repository) Checkout(branch string, opts ...CheckoutOptions) error { - return Checkout(r.path, branch, opts...) -} - // ResetOptions contains optional arguments for resetting a branch. // // Docs: https://git-scm.com/docs/git-reset @@ -336,8 +315,8 @@ type ResetOptions struct { CommandOptions } -// Reset resets working tree to given revision for the repository in given path. -func Reset(repoPath, rev string, opts ...ResetOptions) error { +// Reset resets working tree to given revision for the repository. +func (r *Repository) Reset(rev string, opts ...ResetOptions) error { var opt ResetOptions if len(opts) > 0 { opt = opts[0] @@ -348,20 +327,10 @@ func Reset(repoPath, rev string, opts ...ResetOptions) error { cmd.AddArgs("--hard") } - _, err := cmd.AddOptions(opt.CommandOptions).AddArgs("--end-of-options", rev).RunInDir(repoPath) + _, err := cmd.AddOptions(opt.CommandOptions).AddArgs("--end-of-options", rev).RunInDir(r.path) return err } -// Deprecated: Use Reset instead. -func RepoReset(repoPath, rev string, opts ...ResetOptions) error { - return Reset(repoPath, rev, opts...) -} - -// Reset resets working tree to given revision for the repository. -func (r *Repository) Reset(rev string, opts ...ResetOptions) error { - return Reset(r.path, rev, opts...) -} - // MoveOptions contains optional arguments for moving a file, a directory, or a // symlink. // @@ -377,28 +346,17 @@ type MoveOptions struct { } // Move moves a file, a directory, or a symlink file or directory from source to -// destination for the repository in given path. -func Move(repoPath, src, dst string, opts ...MoveOptions) error { +// destination for the repository. +func (r *Repository) Move(src, dst string, opts ...MoveOptions) error { var opt MoveOptions if len(opts) > 0 { opt = opts[0] } - _, err := NewCommand("mv").AddOptions(opt.CommandOptions).AddArgs("--end-of-options", src, dst).RunInDirWithTimeout(opt.Timeout, repoPath) + _, err := NewCommand("mv").AddOptions(opt.CommandOptions).AddArgs("--end-of-options", src, dst).RunInDirWithTimeout(opt.Timeout, r.path) return err } -// Deprecated: Use Move instead. -func RepoMove(repoPath, src, dst string, opts ...MoveOptions) error { - return Move(repoPath, src, dst, opts...) -} - -// Move moves a file, a directory, or a symlink file or directory from source to -// destination for the repository. -func (r *Repository) Move(src, dst string, opts ...MoveOptions) error { - return Move(r.path, src, dst, opts...) -} - // AddOptions contains optional arguments for adding local changes. // // Docs: https://git-scm.com/docs/git-add @@ -416,8 +374,8 @@ type AddOptions struct { CommandOptions } -// Add adds local changes to index for the repository in given path. -func Add(repoPath string, opts ...AddOptions) error { +// Add adds local changes to index for the repository. +func (r *Repository) Add(opts ...AddOptions) error { var opt AddOptions if len(opts) > 0 { opt = opts[0] @@ -431,20 +389,10 @@ func Add(repoPath string, opts ...AddOptions) error { cmd.AddArgs("--") cmd.AddArgs(opt.Pathspecs...) } - _, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath) + _, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path) return err } -// Deprecated: Use Add instead. -func RepoAdd(repoPath string, opts ...AddOptions) error { - return Add(repoPath, opts...) -} - -// Add adds local changes to index for the repository. -func (r *Repository) Add(opts ...AddOptions) error { - return Add(r.path, opts...) -} - // CommitOptions contains optional arguments to commit changes. // // Docs: https://git-scm.com/docs/git-commit @@ -460,9 +408,9 @@ type CommitOptions struct { CommandOptions } -// CreateCommit commits local changes with given author, committer and message -// for the repository in given path. -func CreateCommit(repoPath string, committer *Signature, message string, opts ...CommitOptions) error { +// Commit commits local changes with given author, committer and message for the +// repository. +func (r *Repository) Commit(committer *Signature, message string, opts ...CommitOptions) error { var opt CommitOptions if len(opts) > 0 { opt = opts[0] @@ -478,7 +426,7 @@ func CreateCommit(repoPath string, committer *Signature, message string, opts .. AddArgs("-m", message). AddOptions(opt.CommandOptions) - _, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath) + _, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path) // No stderr but exit status 1 means nothing to commit. if err != nil && err.Error() == "exit status 1" { return nil @@ -486,17 +434,6 @@ func CreateCommit(repoPath string, committer *Signature, message string, opts .. return err } -// Deprecated: Use CreateCommit instead. -func RepoCommit(repoPath string, committer *Signature, message string, opts ...CommitOptions) error { - return CreateCommit(repoPath, committer, message, opts...) -} - -// Commit commits local changes with given author, committer and message for the -// repository. -func (r *Repository) Commit(committer *Signature, message string, opts ...CommitOptions) error { - return CreateCommit(r.path, committer, message, opts...) -} - // NameStatus contains name status of a commit. type NameStatus struct { Added []string @@ -517,9 +454,8 @@ type ShowNameStatusOptions struct { CommandOptions } -// ShowNameStatus returns name status of given revision of the repository in -// given path. -func ShowNameStatus(repoPath, rev string, opts ...ShowNameStatusOptions) (*NameStatus, error) { +// ShowNameStatus returns name status of given revision of the repository. +func (r *Repository) ShowNameStatus(rev string, opts ...ShowNameStatusOptions) (*NameStatus, error) { var opt ShowNameStatusOptions if len(opts) > 0 { opt = opts[0] @@ -552,7 +488,7 @@ func ShowNameStatus(repoPath, rev string, opts ...ShowNameStatusOptions) (*NameS cmd := NewCommand("show", "--name-status", "--pretty=format:''"). AddOptions(opt.CommandOptions). AddArgs("--end-of-options", rev) - err := cmd.RunInDirPipelineWithTimeout(opt.Timeout, w, stderr, repoPath) + err := cmd.RunInDirPipelineWithTimeout(opt.Timeout, w, stderr, r.path) _ = w.Close() // Close writer to exit parsing goroutine if err != nil { return nil, concatenateError(err, stderr.String()) @@ -562,16 +498,6 @@ func ShowNameStatus(repoPath, rev string, opts ...ShowNameStatusOptions) (*NameS return fileStatus, nil } -// Deprecated: Use ShowNameStatus instead. -func RepoShowNameStatus(repoPath, rev string, opts ...ShowNameStatusOptions) (*NameStatus, error) { - return ShowNameStatus(repoPath, rev, opts...) -} - -// ShowNameStatus returns name status of given revision of the repository. -func (r *Repository) ShowNameStatus(rev string, opts ...ShowNameStatusOptions) (*NameStatus, error) { - return ShowNameStatus(r.path, rev, opts...) -} - // RevParseOptions contains optional arguments for parsing revision. // // Docs: https://git-scm.com/docs/git-rev-parse @@ -631,8 +557,8 @@ type CountObjectsOptions struct { CommandOptions } -// CountObjects returns disk usage report of the repository in given path. -func CountObjects(repoPath string, opts ...CountObjectsOptions) (*CountObject, error) { +// CountObjects returns disk usage report of the repository. +func (r *Repository) CountObjects(opts ...CountObjectsOptions) (*CountObject, error) { var opt CountObjectsOptions if len(opts) > 0 { opt = opts[0] @@ -640,7 +566,7 @@ func CountObjects(repoPath string, opts ...CountObjectsOptions) (*CountObject, e stdout, err := NewCommand("count-objects", "-v"). AddOptions(opt.CommandOptions). - RunInDirWithTimeout(opt.Timeout, repoPath) + RunInDirWithTimeout(opt.Timeout, r.path) if err != nil { return nil, err } @@ -675,16 +601,6 @@ func CountObjects(repoPath string, opts ...CountObjectsOptions) (*CountObject, e return countObject, nil } -// Deprecated: Use CountObjects instead. -func RepoCountObjects(repoPath string, opts ...CountObjectsOptions) (*CountObject, error) { - return CountObjects(repoPath, opts...) -} - -// CountObjects returns disk usage report of the repository. -func (r *Repository) CountObjects(opts ...CountObjectsOptions) (*CountObject, error) { - return CountObjects(r.path, opts...) -} - // FsckOptions contains optional arguments for verifying the objects. // // Docs: https://git-scm.com/docs/git-fsck @@ -699,25 +615,14 @@ type FsckOptions struct { } // Fsck verifies the connectivity and validity of the objects in the database -// for the repository in given path. -func Fsck(repoPath string, opts ...FsckOptions) error { +// for the repository. +func (r *Repository) Fsck(opts ...FsckOptions) error { var opt FsckOptions if len(opts) > 0 { opt = opts[0] } cmd := NewCommand("fsck").AddOptions(opt.CommandOptions) - _, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath) + _, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path) return err } - -// Deprecated: Use Fsck instead. -func RepoFsck(repoPath string, opts ...FsckOptions) error { - return Fsck(repoPath, opts...) -} - -// Fsck verifies the connectivity and validity of the objects in the database -// for the repository. -func (r *Repository) Fsck(opts ...FsckOptions) error { - return Fsck(r.path, opts...) -} diff --git a/repo_blame.go b/repo_blame.go index 9ac4caee..047fa950 100644 --- a/repo_blame.go +++ b/repo_blame.go @@ -21,9 +21,9 @@ type BlameOptions struct { CommandOptions } -// BlameFile returns blame results of the file with the given revision of the +// Blame returns blame results of the file with the given revision of the // repository. -func (r *Repository) BlameFile(rev, file string, opts ...BlameOptions) (*Blame, error) { +func (r *Repository) Blame(rev, file string, opts ...BlameOptions) (*Blame, error) { var opt BlameOptions if len(opts) > 0 { opt = opts[0] diff --git a/repo_blame_test.go b/repo_blame_test.go index ec9b45db..6073b78d 100644 --- a/repo_blame_test.go +++ b/repo_blame_test.go @@ -11,13 +11,13 @@ import ( "github.com/stretchr/testify/assert" ) -func TestRepository_BlameFile(t *testing.T) { +func TestRepository_Blame(t *testing.T) { t.Run("bad file", func(t *testing.T) { - _, err := testrepo.BlameFile("", "404.txt") + _, err := testrepo.Blame("", "404.txt") assert.Error(t, err) }) - blame, err := testrepo.BlameFile("cfc3b2993f74726356887a5ec093de50486dc617", "README.txt") + blame, err := testrepo.Blame("cfc3b2993f74726356887a5ec093de50486dc617", "README.txt") assert.Nil(t, err) // Assert representative commits diff --git a/repo_commit.go b/repo_commit.go index d4e8c1fd..a8ee370c 100644 --- a/repo_commit.go +++ b/repo_commit.go @@ -7,7 +7,6 @@ package git import ( "bytes" "errors" - "fmt" "strconv" "strings" "time" @@ -194,23 +193,6 @@ func escapePath(path string) string { return path } -// Log returns a list of commits in the state of given revision of the -// repository in given path. The returned list is in reverse chronological -// order. -func Log(repoPath, rev string, opts ...LogOptions) ([]*Commit, error) { - r, err := Open(repoPath) - if err != nil { - return nil, fmt.Errorf("open: %v", err) - } - - return r.Log(rev, opts...) -} - -// Deprecated: Use Log instead. -func RepoLog(repoPath, rev string, opts ...LogOptions) ([]*Commit, error) { - return Log(repoPath, rev, opts...) -} - // Log returns a list of commits in the state of given revision of the repository. // The returned list is in reverse chronological order. func (r *Repository) Log(rev string, opts ...LogOptions) ([]*Commit, error) { @@ -395,9 +377,9 @@ type DiffNameOnlyOptions struct { CommandOptions } -// DiffNameOnly returns a list of changed files between base and head revisions -// of the repository in given path. -func DiffNameOnly(repoPath, base, head string, opts ...DiffNameOnlyOptions) ([]string, error) { +// DiffNameOnly returns a list of changed files between base and head revisions of the +// repository. +func (r *Repository) DiffNameOnly(base, head string, opts ...DiffNameOnlyOptions) ([]string, error) { var opt DiffNameOnlyOptions if len(opts) > 0 { opt = opts[0] @@ -417,7 +399,7 @@ func DiffNameOnly(repoPath, base, head string, opts ...DiffNameOnlyOptions) ([]s cmd.AddArgs(escapePath(opt.Path)) } - stdout, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath) + stdout, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path) if err != nil { return nil, err } @@ -434,17 +416,6 @@ func DiffNameOnly(repoPath, base, head string, opts ...DiffNameOnlyOptions) ([]s return names, nil } -// Deprecated: Use DiffNameOnly instead. -func RepoDiffNameOnly(repoPath, base, head string, opts ...DiffNameOnlyOptions) ([]string, error) { - return DiffNameOnly(repoPath, base, head, opts...) -} - -// DiffNameOnly returns a list of changed files between base and head revisions of the -// repository. -func (r *Repository) DiffNameOnly(base, head string, opts ...DiffNameOnlyOptions) ([]string, error) { - return DiffNameOnly(r.path, base, head, opts...) -} - // RevListCountOptions contains optional arguments for counting commits. // // Docs: https://git-scm.com/docs/git-rev-list#Documentation/git-rev-list.txt---count diff --git a/repo_commit_test.go b/repo_commit_test.go index f8b6e5a4..809b6ffc 100644 --- a/repo_commit_test.go +++ b/repo_commit_test.go @@ -117,13 +117,6 @@ func TestRepository_Log(t *testing.T) { } assert.Equal(t, test.expCommitIDs, commitsToIDs(commits)) - - commits, err = Log(testrepo.path, test.rev, test.opt) - if err != nil { - t.Fatal(err) - } - - assert.Equal(t, test.expCommitIDs, commitsToIDs(commits)) }) } } diff --git a/repo_hook.go b/repo_hook.go index 8794876f..32fffc85 100644 --- a/repo_hook.go +++ b/repo_hook.go @@ -5,7 +5,6 @@ package git import ( - "io/ioutil" "os" "path/filepath" ) @@ -32,7 +31,7 @@ func (r *Repository) Hook(dir string, name HookName) (*Hook, error) { // 1. Check if there is an active hook. fpath := filepath.Join(r.path, dir, string(name)) if isFile(fpath) { - p, err := ioutil.ReadFile(fpath) + p, err := os.ReadFile(fpath) if err != nil { return nil, err } diff --git a/repo_pull.go b/repo_pull.go index 5d2cfb1e..2b42229e 100644 --- a/repo_pull.go +++ b/repo_pull.go @@ -23,8 +23,8 @@ type MergeBaseOptions struct { } // MergeBase returns merge base between base and head revisions of the -// repository in given path. -func MergeBase(repoPath, base, head string, opts ...MergeBaseOptions) (string, error) { +// repository. +func (r *Repository) MergeBase(base, head string, opts ...MergeBaseOptions) (string, error) { var opt MergeBaseOptions if len(opts) > 0 { opt = opts[0] @@ -36,7 +36,7 @@ func MergeBase(repoPath, base, head string, opts ...MergeBaseOptions) (string, e "--end-of-options", base, head, - ).RunInDirWithTimeout(opt.Timeout, repoPath) + ).RunInDirWithTimeout(opt.Timeout, r.path) if err != nil { if strings.Contains(err.Error(), "exit status 1") { return "", ErrNoMergeBase @@ -45,14 +45,3 @@ func MergeBase(repoPath, base, head string, opts ...MergeBaseOptions) (string, e } return strings.TrimSpace(string(stdout)), nil } - -// Deprecated: Use MergeBase instead. -func RepoMergeBase(repoPath, base, head string, opts ...MergeBaseOptions) (string, error) { - return MergeBase(repoPath, base, head, opts...) -} - -// MergeBase returns merge base between base and head revisions of the -// repository. -func (r *Repository) MergeBase(base, head string, opts ...MergeBaseOptions) (string, error) { - return MergeBase(r.path, base, head, opts...) -} diff --git a/repo_reference.go b/repo_reference.go index 43dfb3f9..1b90e7b8 100644 --- a/repo_reference.go +++ b/repo_reference.go @@ -48,16 +48,16 @@ type ShowRefVerifyOptions struct { var ErrReferenceNotExist = errors.New("reference does not exist") -// ShowRefVerify returns the commit ID of given reference if it exists in the -// repository in given path. -func ShowRefVerify(repoPath, ref string, opts ...ShowRefVerifyOptions) (string, error) { +// ShowRefVerify returns the commit ID of given reference (e.g. +// "refs/heads/master") if it exists in the repository. +func (r *Repository) ShowRefVerify(ref string, opts ...ShowRefVerifyOptions) (string, error) { var opt ShowRefVerifyOptions if len(opts) > 0 { opt = opts[0] } cmd := NewCommand("show-ref", "--verify", "--end-of-options", ref).AddOptions(opt.CommandOptions) - stdout, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath) + stdout, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path) if err != nil { if strings.Contains(err.Error(), "not a valid ref") { return "", ErrReferenceNotExist @@ -67,17 +67,6 @@ func ShowRefVerify(repoPath, ref string, opts ...ShowRefVerifyOptions) (string, return strings.Split(string(stdout), " ")[0], nil } -// Deprecated: Use ShowRefVerify instead. -func RepoShowRefVerify(repoPath, ref string, opts ...ShowRefVerifyOptions) (string, error) { - return ShowRefVerify(repoPath, ref, opts...) -} - -// ShowRefVerify returns the commit ID of given reference (e.g. -// "refs/heads/master") if it exists in the repository. -func (r *Repository) ShowRefVerify(ref string, opts ...ShowRefVerifyOptions) (string, error) { - return ShowRefVerify(r.path, ref, opts...) -} - // BranchCommitID returns the commit ID of given branch if it exists in the // repository. The branch must be given in short name e.g. "master". func (r *Repository) BranchCommitID(branch string, opts ...ShowRefVerifyOptions) (string, error) { @@ -90,47 +79,23 @@ func (r *Repository) TagCommitID(tag string, opts ...ShowRefVerifyOptions) (stri return r.ShowRefVerify(RefsTags+tag, opts...) } -// RepoHasReference returns true if given reference exists in the repository in -// given path. The reference must be given in full refspec, e.g. -// "refs/heads/master". -func RepoHasReference(repoPath, ref string, opts ...ShowRefVerifyOptions) bool { - _, err := ShowRefVerify(repoPath, ref, opts...) - return err == nil -} - -// RepoHasBranch returns true if given branch exists in the repository in given -// path. The branch must be given in short name e.g. "master". -func RepoHasBranch(repoPath, branch string, opts ...ShowRefVerifyOptions) bool { - return RepoHasReference(repoPath, RefsHeads+branch, opts...) -} - -// HasTag returns true if given tag exists in the repository in given path. The -// tag must be given in short name e.g. "v1.0.0". -func HasTag(repoPath, tag string, opts ...ShowRefVerifyOptions) bool { - return RepoHasReference(repoPath, RefsTags+tag, opts...) -} - -// Deprecated: Use HasTag instead. -func RepoHasTag(repoPath, tag string, opts ...ShowRefVerifyOptions) bool { - return HasTag(repoPath, tag, opts...) -} - // HasReference returns true if given reference exists in the repository. The // reference must be given in full refspec, e.g. "refs/heads/master". func (r *Repository) HasReference(ref string, opts ...ShowRefVerifyOptions) bool { - return RepoHasReference(r.path, ref, opts...) + _, err := r.ShowRefVerify(ref, opts...) + return err == nil } // HasBranch returns true if given branch exists in the repository. The branch // must be given in short name e.g. "master". func (r *Repository) HasBranch(branch string, opts ...ShowRefVerifyOptions) bool { - return RepoHasBranch(r.path, branch, opts...) + return r.HasReference(RefsHeads+branch, opts...) } // HasTag returns true if given tag exists in the repository. The tag must be // given in short name e.g. "v1.0.0". func (r *Repository) HasTag(tag string, opts ...ShowRefVerifyOptions) bool { - return HasTag(r.path, tag, opts...) + return r.HasReference(RefsTags+tag, opts...) } // SymbolicRefOptions contains optional arguments for get and set symbolic ref. @@ -150,9 +115,9 @@ type SymbolicRefOptions struct { } // SymbolicRef returns the reference name (e.g. "refs/heads/master") pointed by -// the symbolic ref in the repository in given path. It returns an empty string -// and nil error when doing set operation. -func SymbolicRef(repoPath string, opts ...SymbolicRefOptions) (string, error) { +// the symbolic ref. It returns an empty string and nil error when doing set +// operation. +func (r *Repository) SymbolicRef(opts ...SymbolicRefOptions) (string, error) { var opt SymbolicRefOptions if len(opts) > 0 { opt = opts[0] @@ -167,20 +132,13 @@ func SymbolicRef(repoPath string, opts ...SymbolicRefOptions) (string, error) { cmd.AddArgs(opt.Ref) } - stdout, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath) + stdout, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path) if err != nil { return "", err } return strings.TrimSpace(string(stdout)), nil } -// SymbolicRef returns the reference name (e.g. "refs/heads/master") pointed by -// the symbolic ref. It returns an empty string and nil error when doing set -// operation. -func (r *Repository) SymbolicRef(opts ...SymbolicRefOptions) (string, error) { - return SymbolicRef(r.path, opts...) -} - // ShowRefOptions contains optional arguments for listing references. // // Docs: https://git-scm.com/docs/git-show-ref @@ -268,8 +226,8 @@ type DeleteBranchOptions struct { CommandOptions } -// DeleteBranch deletes the branch from the repository in given path. -func DeleteBranch(repoPath, name string, opts ...DeleteBranchOptions) error { +// DeleteBranch deletes the branch from the repository. +func (r *Repository) DeleteBranch(name string, opts ...DeleteBranchOptions) error { var opt DeleteBranchOptions if len(opts) > 0 { opt = opts[0] @@ -281,16 +239,6 @@ func DeleteBranch(repoPath, name string, opts ...DeleteBranchOptions) error { } else { cmd.AddArgs("-d") } - _, err := cmd.AddArgs("--end-of-options", name).RunInDirWithTimeout(opt.Timeout, repoPath) + _, err := cmd.AddArgs("--end-of-options", name).RunInDirWithTimeout(opt.Timeout, r.path) return err } - -// Deprecated: Use DeleteBranch instead. -func RepoDeleteBranch(repoPath, name string, opts ...DeleteBranchOptions) error { - return DeleteBranch(repoPath, name, opts...) -} - -// DeleteBranch deletes the branch from the repository. -func (r *Repository) DeleteBranch(name string, opts ...DeleteBranchOptions) error { - return DeleteBranch(r.path, name, opts...) -} diff --git a/repo_remote.go b/repo_remote.go index 363b8998..9ff23c51 100644 --- a/repo_remote.go +++ b/repo_remote.go @@ -103,11 +103,8 @@ type RemoteAddOptions struct { CommandOptions } -// Deprecated: Use RemoteAddOptions instead. -type AddRemoteOptions = RemoteAddOptions - -// RemoteAdd adds a new remote to the repository in given path. -func RemoteAdd(repoPath, name, url string, opts ...RemoteAddOptions) error { +// RemoteAdd adds a new remote to the repository. +func (r *Repository) RemoteAdd(name, url string, opts ...RemoteAddOptions) error { var opt RemoteAddOptions if len(opts) > 0 { opt = opts[0] @@ -121,25 +118,10 @@ func RemoteAdd(repoPath, name, url string, opts ...RemoteAddOptions) error { cmd.AddArgs("--mirror=fetch") } - _, err := cmd.AddArgs("--end-of-options", name, url).RunInDirWithTimeout(opt.Timeout, repoPath) + _, err := cmd.AddArgs("--end-of-options", name, url).RunInDirWithTimeout(opt.Timeout, r.path) return err } -// Deprecated: Use RemoteAdd instead. -func RepoAddRemote(repoPath, name, url string, opts ...RemoteAddOptions) error { - return RemoteAdd(repoPath, name, url, opts...) -} - -// RemoteAdd adds a new remote to the repository. -func (r *Repository) RemoteAdd(name, url string, opts ...RemoteAddOptions) error { - return RemoteAdd(r.path, name, url, opts...) -} - -// Deprecated: Use RemoteAdd instead. -func (r *Repository) AddRemote(name, url string, opts ...RemoteAddOptions) error { - return RemoteAdd(r.path, name, url, opts...) -} - // RemoteRemoveOptions contains arguments for removing a remote from the // repository. // @@ -154,11 +136,8 @@ type RemoteRemoveOptions struct { CommandOptions } -// Deprecated: Use RemoteRemoveOptions instead. -type RemoveRemoteOptions = RemoteRemoveOptions - -// RemoteRemove removes a remote from the repository in given path. -func RemoteRemove(repoPath, name string, opts ...RemoteRemoveOptions) error { +// RemoteRemove removes a remote from the repository. +func (r *Repository) RemoteRemove(name string, opts ...RemoteRemoveOptions) error { var opt RemoteRemoveOptions if len(opts) > 0 { opt = opts[0] @@ -167,7 +146,7 @@ func RemoteRemove(repoPath, name string, opts ...RemoteRemoveOptions) error { _, err := NewCommand("remote", "remove"). AddOptions(opt.CommandOptions). AddArgs("--end-of-options", name). - RunInDirWithTimeout(opt.Timeout, repoPath) + RunInDirWithTimeout(opt.Timeout, r.path) if err != nil { // the error status may differ from git clients if strings.Contains(err.Error(), "error: No such remote") || @@ -179,21 +158,6 @@ func RemoteRemove(repoPath, name string, opts ...RemoteRemoveOptions) error { return nil } -// Deprecated: Use RemoteRemove instead. -func RepoRemoveRemote(repoPath, name string, opts ...RemoteRemoveOptions) error { - return RemoteRemove(repoPath, name, opts...) -} - -// RemoteRemove removes a remote from the repository. -func (r *Repository) RemoteRemove(name string, opts ...RemoteRemoveOptions) error { - return RemoteRemove(r.path, name, opts...) -} - -// Deprecated: Use RemoteRemove instead. -func (r *Repository) RemoveRemote(name string, opts ...RemoteRemoveOptions) error { - return RemoteRemove(r.path, name, opts...) -} - // RemotesOptions contains arguments for listing remotes of the repository. // / // Docs: https://git-scm.com/docs/git-remote#_commands @@ -207,8 +171,8 @@ type RemotesOptions struct { CommandOptions } -// Remotes lists remotes of the repository in given path. -func Remotes(repoPath string, opts ...RemotesOptions) ([]string, error) { +// Remotes lists remotes of the repository. +func (r *Repository) Remotes(opts ...RemotesOptions) ([]string, error) { var opt RemotesOptions if len(opts) > 0 { opt = opts[0] @@ -216,7 +180,7 @@ func Remotes(repoPath string, opts ...RemotesOptions) ([]string, error) { stdout, err := NewCommand("remote"). AddOptions(opt.CommandOptions). - RunInDirWithTimeout(opt.Timeout, repoPath) + RunInDirWithTimeout(opt.Timeout, r.path) if err != nil { return nil, err } @@ -224,11 +188,6 @@ func Remotes(repoPath string, opts ...RemotesOptions) ([]string, error) { return bytesToStrings(stdout), nil } -// Remotes lists remotes of the repository. -func (r *Repository) Remotes(opts ...RemotesOptions) ([]string, error) { - return Remotes(r.path, opts...) -} - // RemoteGetURLOptions contains arguments for retrieving URL(s) of a remote of // the repository. // @@ -248,8 +207,8 @@ type RemoteGetURLOptions struct { CommandOptions } -// RemoteGetURL retrieves URL(s) of a remote of the repository in given path. -func RemoteGetURL(repoPath, name string, opts ...RemoteGetURLOptions) ([]string, error) { +// RemoteGetURL retrieves URL(s) of a remote of the repository. +func (r *Repository) RemoteGetURL(name string, opts ...RemoteGetURLOptions) ([]string, error) { var opt RemoteGetURLOptions if len(opts) > 0 { opt = opts[0] @@ -263,18 +222,13 @@ func RemoteGetURL(repoPath, name string, opts ...RemoteGetURLOptions) ([]string, cmd.AddArgs("--all") } - stdout, err := cmd.AddArgs("--end-of-options", name).RunInDirWithTimeout(opt.Timeout, repoPath) + stdout, err := cmd.AddArgs("--end-of-options", name).RunInDirWithTimeout(opt.Timeout, r.path) if err != nil { return nil, err } return bytesToStrings(stdout), nil } -// RemoteGetURL retrieves URL(s) of a remote of the repository in given path. -func (r *Repository) RemoteGetURL(name string, opts ...RemoteGetURLOptions) ([]string, error) { - return RemoteGetURL(r.path, name, opts...) -} - // RemoteSetURLOptions contains arguments for setting an URL of a remote of the // repository. // @@ -293,9 +247,9 @@ type RemoteSetURLOptions struct { CommandOptions } -// RemoteSetURL sets first URL of the remote with given name of the repository -// in given path. -func RemoteSetURL(repoPath, name, newurl string, opts ...RemoteSetURLOptions) error { +// RemoteSetURL sets the first URL of the remote with given name of the +// repository. +func (r *Repository) RemoteSetURL(name, newurl string, opts ...RemoteSetURLOptions) error { var opt RemoteSetURLOptions if len(opts) > 0 { opt = opts[0] @@ -312,7 +266,7 @@ func RemoteSetURL(repoPath, name, newurl string, opts ...RemoteSetURLOptions) er cmd.AddArgs(opt.Regex) } - _, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath) + _, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path) if err != nil { if strings.Contains(err.Error(), "No such URL found") { return ErrURLNotExist @@ -324,12 +278,6 @@ func RemoteSetURL(repoPath, name, newurl string, opts ...RemoteSetURLOptions) er return nil } -// RemoteSetURL sets the first URL of the remote with given name of the -// repository. -func (r *Repository) RemoteSetURL(name, newurl string, opts ...RemoteSetURLOptions) error { - return RemoteSetURL(r.path, name, newurl, opts...) -} - // RemoteSetURLAddOptions contains arguments for appending an URL to a remote // of the repository. // @@ -347,8 +295,8 @@ type RemoteSetURLAddOptions struct { } // RemoteSetURLAdd appends an URL to the remote with given name of the -// repository in given path. Use RemoteSetURL to overwrite the URL(s) instead. -func RemoteSetURLAdd(repoPath, name, newurl string, opts ...RemoteSetURLAddOptions) error { +// repository. Use RemoteSetURL to overwrite the URL(s) instead. +func (r *Repository) RemoteSetURLAdd(name, newurl string, opts ...RemoteSetURLAddOptions) error { var opt RemoteSetURLAddOptions if len(opts) > 0 { opt = opts[0] @@ -363,19 +311,13 @@ func RemoteSetURLAdd(repoPath, name, newurl string, opts ...RemoteSetURLAddOptio cmd.AddArgs("--end-of-options", name, newurl) - _, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath) + _, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path) if err != nil && strings.Contains(err.Error(), "Will not delete all non-push URLs") { return ErrNotDeleteNonPushURLs } return err } -// RemoteSetURLAdd appends an URL to the remote with given name of the -// repository. Use RemoteSetURL to overwrite the URL(s) instead. -func (r *Repository) RemoteSetURLAdd(name, newurl string, opts ...RemoteSetURLAddOptions) error { - return RemoteSetURLAdd(r.path, name, newurl, opts...) -} - // RemoteSetURLDeleteOptions contains arguments for deleting an URL of a remote // of the repository. // @@ -392,9 +334,9 @@ type RemoteSetURLDeleteOptions struct { CommandOptions } -// RemoteSetURLDelete deletes the remote with given name of the repository in -// given path. -func RemoteSetURLDelete(repoPath, name, regex string, opts ...RemoteSetURLDeleteOptions) error { +// RemoteSetURLDelete deletes all URLs matching regex of the remote with given +// name of the repository. +func (r *Repository) RemoteSetURLDelete(name, regex string, opts ...RemoteSetURLDeleteOptions) error { var opt RemoteSetURLDeleteOptions if len(opts) > 0 { opt = opts[0] @@ -409,15 +351,9 @@ func RemoteSetURLDelete(repoPath, name, regex string, opts ...RemoteSetURLDelete cmd.AddArgs("--end-of-options", name, regex) - _, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath) + _, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path) if err != nil && strings.Contains(err.Error(), "Will not delete all non-push URLs") { return ErrNotDeleteNonPushURLs } return err } - -// RemoteSetURLDelete deletes all URLs matching regex of the remote with given -// name of the repository. -func (r *Repository) RemoteSetURLDelete(name, regex string, opts ...RemoteSetURLDeleteOptions) error { - return RemoteSetURLDelete(r.path, name, regex, opts...) -} diff --git a/repo_tag.go b/repo_tag.go index 931b6dc6..26142b27 100644 --- a/repo_tag.go +++ b/repo_tag.go @@ -159,8 +159,8 @@ type TagsOptions struct { CommandOptions } -// RepoTags returns a list of tags of the repository in given path. -func RepoTags(repoPath string, opts ...TagsOptions) ([]string, error) { +// Tags returns a list of tags of the repository. +func (r *Repository) Tags(opts ...TagsOptions) ([]string, error) { var opt TagsOptions if len(opts) > 0 { opt = opts[0] @@ -178,7 +178,7 @@ func RepoTags(repoPath string, opts ...TagsOptions) ([]string, error) { cmd.AddArgs(opt.Pattern) } - stdout, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath) + stdout, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path) if err != nil { return nil, err } @@ -189,11 +189,6 @@ func RepoTags(repoPath string, opts ...TagsOptions) ([]string, error) { return tags, nil } -// Tags returns a list of tags of the repository. -func (r *Repository) Tags(opts ...TagsOptions) ([]string, error) { - return RepoTags(r.path, opts...) -} - // CreateTagOptions contains optional arguments for creating a tag. // // Docs: https://git-scm.com/docs/git-tag diff --git a/repo_test.go b/repo_test.go index 7f4ef1c4..42ae99c9 100644 --- a/repo_test.go +++ b/repo_test.go @@ -5,7 +5,6 @@ package git import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -320,7 +319,7 @@ func TestRepository_Add(t *testing.T) { // Generate a file fpath := filepath.Join(r.Path(), "TESTFILE") - err = ioutil.WriteFile(fpath, []byte("something"), 0600) + err = os.WriteFile(fpath, []byte("something"), 0600) if err != nil { t.Fatal(err) } @@ -362,7 +361,7 @@ func TestRepository_Commit(t *testing.T) { t.Run("committer is also the author", func(t *testing.T) { // Generate a file and add to index fpath := filepath.Join(r.Path(), "COMMITTER_IS_AUTHOR") - err = ioutil.WriteFile(fpath, []byte("something"), 0600) + err = os.WriteFile(fpath, []byte("something"), 0600) if err != nil { t.Fatal(err) } @@ -394,7 +393,7 @@ func TestRepository_Commit(t *testing.T) { t.Run("committer is not the author", func(t *testing.T) { // Generate a file and add to index fpath := filepath.Join(r.Path(), "COMMITTER_IS_NOT_AUTHOR") - err = ioutil.WriteFile(fpath, []byte("something"), 0600) + err = os.WriteFile(fpath, []byte("something"), 0600) if err != nil { t.Fatal(err) } diff --git a/repo_tree_test.go b/repo_tree_test.go index 16beaa4a..51965f6c 100644 --- a/repo_tree_test.go +++ b/repo_tree_test.go @@ -69,13 +69,13 @@ func TestRepository_LsTree(t *testing.T) { err = os.WriteFile(filepath.Join(path, specialName), []byte("content"), 0o644) require.NoError(t, err) - err = Add(path, AddOptions{All: true}) + repo, err := Open(path) require.NoError(t, err) - err = CreateCommit(path, &Signature{Name: "test", Email: "test@test.com"}, "initial commit") + err = repo.Add(AddOptions{All: true}) require.NoError(t, err) - repo, err := Open(path) + err = repo.Commit(&Signature{Name: "test", Email: "test@test.com"}, "initial commit") require.NoError(t, err) commit, err := repo.CatFileCommit("HEAD")