Skip to content

Commit 4bca19f

Browse files
committed
cleanup: remove all redundant top-level repo functions
This is a breaking change for v2 that removes all top-level functions that take repoPath as the first parameter. These functions have been consolidated into Repository methods. **Removed top-level functions (use Repository methods instead):** repo.go: - Push(repoPath, ...) → repo.Push(...) - Checkout(repoPath, ...) → repo.Checkout(...) - Reset(repoPath, ...) → repo.Reset(...) - Move(repoPath, ...) → repo.Move(...) - Add(repoPath, ...) → repo.Add(...) - CreateCommit(repoPath, ...) → repo.Commit(...) - ShowNameStatus(repoPath, ...) → repo.ShowNameStatus(...) - CountObjects(repoPath, ...) → repo.CountObjects(...) - Fsck(repoPath, ...) → repo.Fsck(...) repo_commit.go: - Log(repoPath, ...) → repo.Log(...) - DiffNameOnly(repoPath, ...) → repo.DiffNameOnly(...) repo_pull.go: - MergeBase(repoPath, ...) → repo.MergeBase(...) repo_reference.go: - ShowRefVerify(repoPath, ...) → repo.ShowRefVerify(...) - SymbolicRef(repoPath, ...) → repo.SymbolicRef(...) - DeleteBranch(repoPath, ...) → repo.DeleteBranch(...) - HasReference(repoPath, ...) → repo.HasReference(...) - HasBranch(repoPath, ...) → repo.HasBranch(...) - HasTag(repoPath, ...) → repo.HasTag(...) repo_remote.go: - RemoteAdd(repoPath, ...) → repo.RemoteAdd(...) - RemoteRemove(repoPath, ...) → repo.RemoteRemove(...) - Remotes(repoPath, ...) → repo.Remotes(...) - RemoteGetURL(repoPath, ...) → repo.RemoteGetURL(...) - RemoteSetURL(repoPath, ...) → repo.RemoteSetURL(...) - RemoteSetURLAdd(repoPath, ...) → repo.RemoteSetURLAdd(...) - RemoteSetURLDelete(repoPath, ...) → repo.RemoteSetURLDelete(...) repo_tag.go: - Tags(repoPath, ...) → repo.Tags(...) **Kept top-level functions (no repo required):** - Init(path, ...) - creates new repo - Clone(url, dst, ...) - clones repo - Open(repoPath) - opens repo **Stats:** 342 deletions, 80 insertions (-262 net lines) All tests updated and passing.
1 parent 20722b9 commit 4bca19f

8 files changed

Lines changed: 80 additions & 342 deletions

File tree

repo.go

Lines changed: 28 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -253,29 +253,18 @@ type PushOptions struct {
253253
CommandOptions
254254
}
255255

256-
// Push pushes local changes to given remote and branch for the repository in
257-
// given path.
258-
func Push(repoPath, remote, branch string, opts ...PushOptions) error {
256+
// Push pushes local changes to given remote and branch for the repository.
257+
func (r *Repository) Push(remote, branch string, opts ...PushOptions) error {
259258
var opt PushOptions
260259
if len(opts) > 0 {
261260
opt = opts[0]
262261
}
263262

264263
cmd := NewCommand("push").AddOptions(opt.CommandOptions).AddArgs("--end-of-options", remote, branch)
265-
_, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath)
264+
_, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path)
266265
return err
267266
}
268267

269-
// Deprecated: Use Push instead.
270-
func RepoPush(repoPath, remote, branch string, opts ...PushOptions) error {
271-
return Push(repoPath, remote, branch, opts...)
272-
}
273-
274-
// Push pushes local changes to given remote and branch for the repository.
275-
func (r *Repository) Push(remote, branch string, opts ...PushOptions) error {
276-
return Push(r.path, remote, branch, opts...)
277-
}
278-
279268
// CheckoutOptions contains optional arguments for checking out to a branch.
280269
//
281270
// Docs: https://git-scm.com/docs/git-checkout
@@ -291,8 +280,8 @@ type CheckoutOptions struct {
291280
CommandOptions
292281
}
293282

294-
// Checkout checks out to given branch for the repository in given path.
295-
func Checkout(repoPath, branch string, opts ...CheckoutOptions) error {
283+
// Checkout checks out to given branch for the repository.
284+
func (r *Repository) Checkout(branch string, opts ...CheckoutOptions) error {
296285
var opt CheckoutOptions
297286
if len(opts) > 0 {
298287
opt = opts[0]
@@ -307,20 +296,10 @@ func Checkout(repoPath, branch string, opts ...CheckoutOptions) error {
307296
cmd.AddArgs(opt.BaseBranch)
308297
}
309298

310-
_, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath)
299+
_, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path)
311300
return err
312301
}
313302

314-
// Deprecated: Use Checkout instead.
315-
func RepoCheckout(repoPath, branch string, opts ...CheckoutOptions) error {
316-
return Checkout(repoPath, branch, opts...)
317-
}
318-
319-
// Checkout checks out to given branch for the repository.
320-
func (r *Repository) Checkout(branch string, opts ...CheckoutOptions) error {
321-
return Checkout(r.path, branch, opts...)
322-
}
323-
324303
// ResetOptions contains optional arguments for resetting a branch.
325304
//
326305
// Docs: https://git-scm.com/docs/git-reset
@@ -336,8 +315,8 @@ type ResetOptions struct {
336315
CommandOptions
337316
}
338317

339-
// Reset resets working tree to given revision for the repository in given path.
340-
func Reset(repoPath, rev string, opts ...ResetOptions) error {
318+
// Reset resets working tree to given revision for the repository.
319+
func (r *Repository) Reset(rev string, opts ...ResetOptions) error {
341320
var opt ResetOptions
342321
if len(opts) > 0 {
343322
opt = opts[0]
@@ -348,20 +327,10 @@ func Reset(repoPath, rev string, opts ...ResetOptions) error {
348327
cmd.AddArgs("--hard")
349328
}
350329

351-
_, err := cmd.AddOptions(opt.CommandOptions).AddArgs("--end-of-options", rev).RunInDir(repoPath)
330+
_, err := cmd.AddOptions(opt.CommandOptions).AddArgs("--end-of-options", rev).RunInDir(r.path)
352331
return err
353332
}
354333

355-
// Deprecated: Use Reset instead.
356-
func RepoReset(repoPath, rev string, opts ...ResetOptions) error {
357-
return Reset(repoPath, rev, opts...)
358-
}
359-
360-
// Reset resets working tree to given revision for the repository.
361-
func (r *Repository) Reset(rev string, opts ...ResetOptions) error {
362-
return Reset(r.path, rev, opts...)
363-
}
364-
365334
// MoveOptions contains optional arguments for moving a file, a directory, or a
366335
// symlink.
367336
//
@@ -377,28 +346,17 @@ type MoveOptions struct {
377346
}
378347

379348
// Move moves a file, a directory, or a symlink file or directory from source to
380-
// destination for the repository in given path.
381-
func Move(repoPath, src, dst string, opts ...MoveOptions) error {
349+
// destination for the repository.
350+
func (r *Repository) Move(src, dst string, opts ...MoveOptions) error {
382351
var opt MoveOptions
383352
if len(opts) > 0 {
384353
opt = opts[0]
385354
}
386355

387-
_, err := NewCommand("mv").AddOptions(opt.CommandOptions).AddArgs("--end-of-options", src, dst).RunInDirWithTimeout(opt.Timeout, repoPath)
356+
_, err := NewCommand("mv").AddOptions(opt.CommandOptions).AddArgs("--end-of-options", src, dst).RunInDirWithTimeout(opt.Timeout, r.path)
388357
return err
389358
}
390359

391-
// Deprecated: Use Move instead.
392-
func RepoMove(repoPath, src, dst string, opts ...MoveOptions) error {
393-
return Move(repoPath, src, dst, opts...)
394-
}
395-
396-
// Move moves a file, a directory, or a symlink file or directory from source to
397-
// destination for the repository.
398-
func (r *Repository) Move(src, dst string, opts ...MoveOptions) error {
399-
return Move(r.path, src, dst, opts...)
400-
}
401-
402360
// AddOptions contains optional arguments for adding local changes.
403361
//
404362
// Docs: https://git-scm.com/docs/git-add
@@ -416,8 +374,8 @@ type AddOptions struct {
416374
CommandOptions
417375
}
418376

419-
// Add adds local changes to index for the repository in given path.
420-
func Add(repoPath string, opts ...AddOptions) error {
377+
// Add adds local changes to index for the repository.
378+
func (r *Repository) Add(opts ...AddOptions) error {
421379
var opt AddOptions
422380
if len(opts) > 0 {
423381
opt = opts[0]
@@ -431,20 +389,10 @@ func Add(repoPath string, opts ...AddOptions) error {
431389
cmd.AddArgs("--")
432390
cmd.AddArgs(opt.Pathspecs...)
433391
}
434-
_, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath)
392+
_, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path)
435393
return err
436394
}
437395

438-
// Deprecated: Use Add instead.
439-
func RepoAdd(repoPath string, opts ...AddOptions) error {
440-
return Add(repoPath, opts...)
441-
}
442-
443-
// Add adds local changes to index for the repository.
444-
func (r *Repository) Add(opts ...AddOptions) error {
445-
return Add(r.path, opts...)
446-
}
447-
448396
// CommitOptions contains optional arguments to commit changes.
449397
//
450398
// Docs: https://git-scm.com/docs/git-commit
@@ -460,9 +408,9 @@ type CommitOptions struct {
460408
CommandOptions
461409
}
462410

463-
// CreateCommit commits local changes with given author, committer and message
464-
// for the repository in given path.
465-
func CreateCommit(repoPath string, committer *Signature, message string, opts ...CommitOptions) error {
411+
// Commit commits local changes with given author, committer and message for the
412+
// repository.
413+
func (r *Repository) Commit(committer *Signature, message string, opts ...CommitOptions) error {
466414
var opt CommitOptions
467415
if len(opts) > 0 {
468416
opt = opts[0]
@@ -478,25 +426,14 @@ func CreateCommit(repoPath string, committer *Signature, message string, opts ..
478426
AddArgs("-m", message).
479427
AddOptions(opt.CommandOptions)
480428

481-
_, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath)
429+
_, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path)
482430
// No stderr but exit status 1 means nothing to commit.
483431
if err != nil && err.Error() == "exit status 1" {
484432
return nil
485433
}
486434
return err
487435
}
488436

489-
// Deprecated: Use CreateCommit instead.
490-
func RepoCommit(repoPath string, committer *Signature, message string, opts ...CommitOptions) error {
491-
return CreateCommit(repoPath, committer, message, opts...)
492-
}
493-
494-
// Commit commits local changes with given author, committer and message for the
495-
// repository.
496-
func (r *Repository) Commit(committer *Signature, message string, opts ...CommitOptions) error {
497-
return CreateCommit(r.path, committer, message, opts...)
498-
}
499-
500437
// NameStatus contains name status of a commit.
501438
type NameStatus struct {
502439
Added []string
@@ -517,9 +454,8 @@ type ShowNameStatusOptions struct {
517454
CommandOptions
518455
}
519456

520-
// ShowNameStatus returns name status of given revision of the repository in
521-
// given path.
522-
func ShowNameStatus(repoPath, rev string, opts ...ShowNameStatusOptions) (*NameStatus, error) {
457+
// ShowNameStatus returns name status of given revision of the repository.
458+
func (r *Repository) ShowNameStatus(rev string, opts ...ShowNameStatusOptions) (*NameStatus, error) {
523459
var opt ShowNameStatusOptions
524460
if len(opts) > 0 {
525461
opt = opts[0]
@@ -552,7 +488,7 @@ func ShowNameStatus(repoPath, rev string, opts ...ShowNameStatusOptions) (*NameS
552488
cmd := NewCommand("show", "--name-status", "--pretty=format:''").
553489
AddOptions(opt.CommandOptions).
554490
AddArgs("--end-of-options", rev)
555-
err := cmd.RunInDirPipelineWithTimeout(opt.Timeout, w, stderr, repoPath)
491+
err := cmd.RunInDirPipelineWithTimeout(opt.Timeout, w, stderr, r.path)
556492
_ = w.Close() // Close writer to exit parsing goroutine
557493
if err != nil {
558494
return nil, concatenateError(err, stderr.String())
@@ -562,16 +498,6 @@ func ShowNameStatus(repoPath, rev string, opts ...ShowNameStatusOptions) (*NameS
562498
return fileStatus, nil
563499
}
564500

565-
// Deprecated: Use ShowNameStatus instead.
566-
func RepoShowNameStatus(repoPath, rev string, opts ...ShowNameStatusOptions) (*NameStatus, error) {
567-
return ShowNameStatus(repoPath, rev, opts...)
568-
}
569-
570-
// ShowNameStatus returns name status of given revision of the repository.
571-
func (r *Repository) ShowNameStatus(rev string, opts ...ShowNameStatusOptions) (*NameStatus, error) {
572-
return ShowNameStatus(r.path, rev, opts...)
573-
}
574-
575501
// RevParseOptions contains optional arguments for parsing revision.
576502
//
577503
// Docs: https://git-scm.com/docs/git-rev-parse
@@ -631,16 +557,16 @@ type CountObjectsOptions struct {
631557
CommandOptions
632558
}
633559

634-
// CountObjects returns disk usage report of the repository in given path.
635-
func CountObjects(repoPath string, opts ...CountObjectsOptions) (*CountObject, error) {
560+
// CountObjects returns disk usage report of the repository.
561+
func (r *Repository) CountObjects(opts ...CountObjectsOptions) (*CountObject, error) {
636562
var opt CountObjectsOptions
637563
if len(opts) > 0 {
638564
opt = opts[0]
639565
}
640566

641567
stdout, err := NewCommand("count-objects", "-v").
642568
AddOptions(opt.CommandOptions).
643-
RunInDirWithTimeout(opt.Timeout, repoPath)
569+
RunInDirWithTimeout(opt.Timeout, r.path)
644570
if err != nil {
645571
return nil, err
646572
}
@@ -675,16 +601,6 @@ func CountObjects(repoPath string, opts ...CountObjectsOptions) (*CountObject, e
675601
return countObject, nil
676602
}
677603

678-
// Deprecated: Use CountObjects instead.
679-
func RepoCountObjects(repoPath string, opts ...CountObjectsOptions) (*CountObject, error) {
680-
return CountObjects(repoPath, opts...)
681-
}
682-
683-
// CountObjects returns disk usage report of the repository.
684-
func (r *Repository) CountObjects(opts ...CountObjectsOptions) (*CountObject, error) {
685-
return CountObjects(r.path, opts...)
686-
}
687-
688604
// FsckOptions contains optional arguments for verifying the objects.
689605
//
690606
// Docs: https://git-scm.com/docs/git-fsck
@@ -699,25 +615,14 @@ type FsckOptions struct {
699615
}
700616

701617
// Fsck verifies the connectivity and validity of the objects in the database
702-
// for the repository in given path.
703-
func Fsck(repoPath string, opts ...FsckOptions) error {
618+
// for the repository.
619+
func (r *Repository) Fsck(opts ...FsckOptions) error {
704620
var opt FsckOptions
705621
if len(opts) > 0 {
706622
opt = opts[0]
707623
}
708624

709625
cmd := NewCommand("fsck").AddOptions(opt.CommandOptions)
710-
_, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath)
626+
_, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path)
711627
return err
712628
}
713-
714-
// Deprecated: Use Fsck instead.
715-
func RepoFsck(repoPath string, opts ...FsckOptions) error {
716-
return Fsck(repoPath, opts...)
717-
}
718-
719-
// Fsck verifies the connectivity and validity of the objects in the database
720-
// for the repository.
721-
func (r *Repository) Fsck(opts ...FsckOptions) error {
722-
return Fsck(r.path, opts...)
723-
}

repo_commit.go

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package git
77
import (
88
"bytes"
99
"errors"
10-
"fmt"
1110
"strconv"
1211
"strings"
1312
"time"
@@ -194,23 +193,6 @@ func escapePath(path string) string {
194193
return path
195194
}
196195

197-
// Log returns a list of commits in the state of given revision of the
198-
// repository in given path. The returned list is in reverse chronological
199-
// order.
200-
func Log(repoPath, rev string, opts ...LogOptions) ([]*Commit, error) {
201-
r, err := Open(repoPath)
202-
if err != nil {
203-
return nil, fmt.Errorf("open: %v", err)
204-
}
205-
206-
return r.Log(rev, opts...)
207-
}
208-
209-
// Deprecated: Use Log instead.
210-
func RepoLog(repoPath, rev string, opts ...LogOptions) ([]*Commit, error) {
211-
return Log(repoPath, rev, opts...)
212-
}
213-
214196
// Log returns a list of commits in the state of given revision of the repository.
215197
// The returned list is in reverse chronological order.
216198
func (r *Repository) Log(rev string, opts ...LogOptions) ([]*Commit, error) {
@@ -395,9 +377,9 @@ type DiffNameOnlyOptions struct {
395377
CommandOptions
396378
}
397379

398-
// DiffNameOnly returns a list of changed files between base and head revisions
399-
// of the repository in given path.
400-
func DiffNameOnly(repoPath, base, head string, opts ...DiffNameOnlyOptions) ([]string, error) {
380+
// DiffNameOnly returns a list of changed files between base and head revisions of the
381+
// repository.
382+
func (r *Repository) DiffNameOnly(base, head string, opts ...DiffNameOnlyOptions) ([]string, error) {
401383
var opt DiffNameOnlyOptions
402384
if len(opts) > 0 {
403385
opt = opts[0]
@@ -417,7 +399,7 @@ func DiffNameOnly(repoPath, base, head string, opts ...DiffNameOnlyOptions) ([]s
417399
cmd.AddArgs(escapePath(opt.Path))
418400
}
419401

420-
stdout, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath)
402+
stdout, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path)
421403
if err != nil {
422404
return nil, err
423405
}
@@ -434,17 +416,6 @@ func DiffNameOnly(repoPath, base, head string, opts ...DiffNameOnlyOptions) ([]s
434416
return names, nil
435417
}
436418

437-
// Deprecated: Use DiffNameOnly instead.
438-
func RepoDiffNameOnly(repoPath, base, head string, opts ...DiffNameOnlyOptions) ([]string, error) {
439-
return DiffNameOnly(repoPath, base, head, opts...)
440-
}
441-
442-
// DiffNameOnly returns a list of changed files between base and head revisions of the
443-
// repository.
444-
func (r *Repository) DiffNameOnly(base, head string, opts ...DiffNameOnlyOptions) ([]string, error) {
445-
return DiffNameOnly(r.path, base, head, opts...)
446-
}
447-
448419
// RevListCountOptions contains optional arguments for counting commits.
449420
//
450421
// Docs: https://git-scm.com/docs/git-rev-list#Documentation/git-rev-list.txt---count

repo_commit_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,6 @@ func TestRepository_Log(t *testing.T) {
117117
}
118118

119119
assert.Equal(t, test.expCommitIDs, commitsToIDs(commits))
120-
121-
commits, err = Log(testrepo.path, test.rev, test.opt)
122-
if err != nil {
123-
t.Fatal(err)
124-
}
125-
126-
assert.Equal(t, test.expCommitIDs, commitsToIDs(commits))
127120
})
128121
}
129122
}

0 commit comments

Comments
 (0)