From 28b5ac14215d0c6b9c530fcfcdc1d045764492a5 Mon Sep 17 00:00:00 2001 From: J Date: Mon, 26 Aug 2024 11:56:35 -0400 Subject: [PATCH 1/4] feat: add ability to specify the git work directory - can specify via cli or with the library, work-tree and git directory values - created a repository object that stores work-tree and git directory values which are then used on any 'run' call to the git binary --- README.md | 1 + internal/.DS_Store | Bin 0 -> 6148 bytes internal/git/.DS_Store | Bin 0 -> 6148 bytes internal/git/git.go | 80 ++++++++++++--- internal/git/git_test.go | 208 +++++++++++++++++++++++++++++++++------ internal/svu/svu.go | 17 +++- internal/svu/svu_test.go | 42 ++++---- main.go | 7 +- pkg/svu/svu.go | 12 +++ 9 files changed, 296 insertions(+), 71 deletions(-) create mode 100644 internal/.DS_Store create mode 100644 internal/git/.DS_Store diff --git a/README.md b/README.md index ed60a75..2c6aae2 100644 --- a/README.md +++ b/README.md @@ -279,6 +279,7 @@ All flags have a matching option function to configure the previous commands bey - `svu.Next(svu.WithTagMode(svu.AllBranches))` or `svu.Next(svu.ForAllBranches())` - `svu.Next(svu.WithTagMode(svu.CurrentBranch))` or `svu.Next(svu.ForCurrentBranch())` - `svu.Next(svu.ForcePatchIncrement())` +- `svu.Current(svu.WithGitDirectory("/some/other/repo/.git"), svu.WithWorkTree("/some/other/repo"))` Or multiple options: diff --git a/internal/.DS_Store b/internal/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..ea2933185b107004f1f71760c05436949cae83de GIT binary patch literal 6148 zcmeHKPm9w)6rX9U+qwujD6F@D7Y_?|mntl-5Nq|UM)aUkn@#Bk(@bg7txyU%>%}jy zM-P4j?}8t}kKxG=;P1`6x+!%%ipYB~^Lz9DWZwKTWHL%A-JQjIN_k4DCK_R_h3+>( zerg+1W6L&B(=+l(l$r>m&(UNfS`7ap1N83dYNQfXMDyzVOOw24x4-FHy>a2<7G!(t zRd{UXVKFFXd3P{3chiMW@}_^*gt2FL(XL!G0V1!TxgDYF@p5^Ujn0n{=F+caB1h-Dc%7;|ktG z;nl?XQ5vB-3T0C|+6#}&n78Jq(=*T5^m@E?o8M1wJiK)G>%FQs{)2aNTzRVgfV9)9bdsfA!cdZKbh^i0~2801&;9Lgy zE83(pKtvc2284ks1N43H(Fg;^(xLryptDy1U;}0=&^=OsK4ZXGI)n$JT`JI}YQAD< zmkztnaRFoL(4{NQmk-S|YrdgqJ3F55+i;}=htdlJ!oWHMbvN|r{C{x%{eL}4M#6wF z@Lw^Y8@;gC#gh1J-B=u*wLaQwG!lv{9sWvzVvl0P(owvFW(9Vi6kxzuI)nuxKLVBp K>4bs5%D_+9D}y)y literal 0 HcmV?d00001 diff --git a/internal/git/.DS_Store b/internal/git/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b6aacf7a1f681d254b8d91d46079d8d8620344a7 GIT binary patch literal 6148 zcmeHKF;2r!47DLc1WU(`{{%=q0Me%lC+N@>l}asAt8^%0&Bir2027DcTs+$!lqf1= z2Y8nJU%u^^pS&TCMMUS%>zT+zL~6L9yxKB0%bSnvqC_g7+C6rg+2XO?A4Zj@1IDf7 zMmF*!$07gYy`S5+O|#myE5z!Td|Y1Le%=l9>tFMSZSUFi4OAKxpaN8Y3Q&Q6sQ`Mm zS$!SIl?qS+D)6I#eIF9sFc<6s{nLTPM*yJx#=GI#X9-}k05BKq0ug~}P=P_!GGb`Z z5l^Kq7wiIqF6t-aKILRB6N>ujh$k->&4FC002LT2a2eZ)_5XYLiTQs>;*JVXfj^~y zR?VWB Date: Mon, 26 Aug 2024 12:09:05 -0400 Subject: [PATCH 2/4] chore: remove macos files --- internal/.DS_Store | Bin 6148 -> 0 bytes internal/git/.DS_Store | Bin 6148 -> 0 bytes 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 internal/.DS_Store delete mode 100644 internal/git/.DS_Store diff --git a/internal/.DS_Store b/internal/.DS_Store deleted file mode 100644 index ea2933185b107004f1f71760c05436949cae83de..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKPm9w)6rX9U+qwujD6F@D7Y_?|mntl-5Nq|UM)aUkn@#Bk(@bg7txyU%>%}jy zM-P4j?}8t}kKxG=;P1`6x+!%%ipYB~^Lz9DWZwKTWHL%A-JQjIN_k4DCK_R_h3+>( zerg+1W6L&B(=+l(l$r>m&(UNfS`7ap1N83dYNQfXMDyzVOOw24x4-FHy>a2<7G!(t zRd{UXVKFFXd3P{3chiMW@}_^*gt2FL(XL!G0V1!TxgDYF@p5^Ujn0n{=F+caB1h-Dc%7;|ktG z;nl?XQ5vB-3T0C|+6#}&n78Jq(=*T5^m@E?o8M1wJiK)G>%FQs{)2aNTzRVgfV9)9bdsfA!cdZKbh^i0~2801&;9Lgy zE83(pKtvc2284ks1N43H(Fg;^(xLryptDy1U;}0=&^=OsK4ZXGI)n$JT`JI}YQAD< zmkztnaRFoL(4{NQmk-S|YrdgqJ3F55+i;}=htdlJ!oWHMbvN|r{C{x%{eL}4M#6wF z@Lw^Y8@;gC#gh1J-B=u*wLaQwG!lv{9sWvzVvl0P(owvFW(9Vi6kxzuI)nuxKLVBp K>4bs5%D_+9D}y)y diff --git a/internal/git/.DS_Store b/internal/git/.DS_Store deleted file mode 100644 index b6aacf7a1f681d254b8d91d46079d8d8620344a7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKF;2r!47DLc1WU(`{{%=q0Me%lC+N@>l}asAt8^%0&Bir2027DcTs+$!lqf1= z2Y8nJU%u^^pS&TCMMUS%>zT+zL~6L9yxKB0%bSnvqC_g7+C6rg+2XO?A4Zj@1IDf7 zMmF*!$07gYy`S5+O|#myE5z!Td|Y1Le%=l9>tFMSZSUFi4OAKxpaN8Y3Q&Q6sQ`Mm zS$!SIl?qS+D)6I#eIF9sFc<6s{nLTPM*yJx#=GI#X9-}k05BKq0ug~}P=P_!GGb`Z z5l^Kq7wiIqF6t-aKILRB6N>ujh$k->&4FC002LT2a2eZ)_5XYLiTQs>;*JVXfj^~y zR?VWB Date: Mon, 26 Aug 2024 16:17:10 -0400 Subject: [PATCH 3/4] refactor: @ccoVeille's comments --- README.md | 2 +- internal/git/git.go | 18 +++++++----------- internal/git/git_test.go | 6 ++++-- internal/svu/svu.go | 4 ++-- main.go | 4 ++-- pkg/svu/svu.go | 4 ++-- 6 files changed, 18 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 2c6aae2..c18faa1 100644 --- a/README.md +++ b/README.md @@ -279,7 +279,7 @@ All flags have a matching option function to configure the previous commands bey - `svu.Next(svu.WithTagMode(svu.AllBranches))` or `svu.Next(svu.ForAllBranches())` - `svu.Next(svu.WithTagMode(svu.CurrentBranch))` or `svu.Next(svu.ForCurrentBranch())` - `svu.Next(svu.ForcePatchIncrement())` -- `svu.Current(svu.WithGitDirectory("/some/other/repo/.git"), svu.WithWorkTree("/some/other/repo"))` +- `svu.Current(svu.WithGitDirectory("/some/other/repo/.git"), svu.WithGitWorkTree("/some/other/repo"))` Or multiple options: diff --git a/internal/git/git.go b/internal/git/git.go index 90f57b1..3d5bdcd 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -25,30 +25,26 @@ type Repository struct { // NewRepository creates a Repository. worktree and gitdirectory will default to 'pwd' and 'pwd/.git' func NewRepository(worktree, gitdirectory string) (*Repository, error) { - var wt string var err error if worktree == "" { - wt, err = os.Getwd() + worktree, err = os.Getwd() if err != nil { return nil, err } - } else { - wt = filepath.Clean(worktree) } - var gd string + worktree = filepath.Clean(worktree) if gitdirectory == "" { - gd, err = os.Getwd() + gitdirectory, err = os.Getwd() if err != nil { return nil, err } - gd = gd + string(os.PathSeparator) + ".git" - } else { - gd = filepath.Clean(gitdirectory) + gitdirectory = filepath.Join(gitdirectory, ".git") } + gitdirectory = filepath.Clean(gitdirectory) r := &Repository{ - WorkTree: wt, - GitDirectory: gd, + WorkTree: worktree, + GitDirectory: gitdirectory, } return r, nil } diff --git a/internal/git/git_test.go b/internal/git/git_test.go index 2a40e18..e530692 100644 --- a/internal/git/git_test.go +++ b/internal/git/git_test.go @@ -3,6 +3,7 @@ package git import ( "os" "path" + "path/filepath" "strings" "testing" "time" @@ -12,7 +13,7 @@ import ( func TestNewRepository(t *testing.T) { cwd := currentWorkingDirectory(t) - cgd := cwd + string(os.PathSeparator) + ".git" + cgd := filepath.Join(cwd, ".git") t.Run("defaults", func(t *testing.T) { is := is.New(t) @@ -165,7 +166,7 @@ func TestRepository_ChangelogWithDirectory(t *testing.T) { func TestRepository_run(t *testing.T) { // current directory: . , ./.git rootWT := currentWorkingDirectory(t) - rootGD := rootWT + string(os.PathSeparator) + ".git" + rootGD := filepath.Join(rootWT, ".git") t.Run("current directory", func(t *testing.T) { is := is.New(t) @@ -295,6 +296,7 @@ func tempdir(tb testing.TB, cdToTempDir bool) string { previous, err := os.Getwd() is.NoErr(err) dir := tb.TempDir() + tb.TempDir() if cdToTempDir { tb.Cleanup(func() { is.NoErr(os.Chdir(previous)) diff --git a/internal/svu/svu.go b/internal/svu/svu.go index 9b81b39..db4ba5b 100644 --- a/internal/svu/svu.go +++ b/internal/svu/svu.go @@ -37,12 +37,12 @@ type Options struct { TagMode string ForcePatchIncrement bool PreventMajorIncrementOnV0 bool - WorkTree string + GitWorkTree string GitDirectory string } func Version(opts Options) (string, error) { - r, err := git.NewRepository(opts.WorkTree, opts.GitDirectory) + r, err := git.NewRepository(opts.GitWorkTree, opts.GitDirectory) if err != nil { return "", fmt.Errorf("failed finding the repository: %w", err) } diff --git a/main.go b/main.go index d662a91..3cd519f 100644 --- a/main.go +++ b/main.go @@ -29,7 +29,7 @@ var ( tagMode = app.Flag("tag-mode", "determines if latest tag of the current or all branches will be used"). Default("current-branch"). Enum("current-branch", "all-branches") - workTree = app.Flag("work-tree", "sets the git work-tree directory where the git repository exists"). + gitWorkTree = app.Flag("git-work-tree", "sets the git work-tree directory where the git repository exists"). Default("").String() gitDirectory = app.Flag("git-directory", "sets the directory where the .git folder exists").Default("").String() forcePatchIncrement = nextCmd.Flag("force-patch-increment", "forces a patch version increment regardless of the commit message content"). @@ -56,7 +56,7 @@ func main() { Build: *build, Directory: *directory, TagMode: *tagMode, - WorkTree: *workTree, + GitWorkTree: *gitWorkTree, GitDirectory: *gitDirectory, ForcePatchIncrement: *forcePatchIncrement, PreventMajorIncrementOnV0: *preventMajorIncrementOnV0, diff --git a/pkg/svu/svu.go b/pkg/svu/svu.go index 8b0fdbb..3b40afe 100644 --- a/pkg/svu/svu.go +++ b/pkg/svu/svu.go @@ -102,9 +102,9 @@ func ForcePatchIncrement() option { return WithForcePatchIncrement(true) } -func WithWorkTree(directory string) option { +func WithGitWorkTree(directory string) option { return func(o *svu.Options) { - o.WorkTree = directory + o.GitWorkTree = directory } } From 57cc86637b88b46682719549790e14d32dd7a5c0 Mon Sep 17 00:00:00 2001 From: J Date: Mon, 26 Aug 2024 16:25:50 -0400 Subject: [PATCH 4/4] refactor: more renaming --- internal/git/git.go | 18 ++++++------ internal/git/git_test.go | 60 ++++++++++++++++++++-------------------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/internal/git/git.go b/internal/git/git.go index 3d5bdcd..db6812e 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -19,20 +19,20 @@ const ( // copied from goreleaser type Repository struct { - WorkTree string + GitWorkTree string GitDirectory string } -// NewRepository creates a Repository. worktree and gitdirectory will default to 'pwd' and 'pwd/.git' -func NewRepository(worktree, gitdirectory string) (*Repository, error) { +// NewRepository creates a Repository. gitworktree and gitdirectory will default to 'pwd' and 'pwd/.git' +func NewRepository(gitworktree, gitdirectory string) (*Repository, error) { var err error - if worktree == "" { - worktree, err = os.Getwd() + if gitworktree == "" { + gitworktree, err = os.Getwd() if err != nil { return nil, err } } - worktree = filepath.Clean(worktree) + gitworktree = filepath.Clean(gitworktree) if gitdirectory == "" { gitdirectory, err = os.Getwd() if err != nil { @@ -43,7 +43,7 @@ func NewRepository(worktree, gitdirectory string) (*Repository, error) { gitdirectory = filepath.Clean(gitdirectory) r := &Repository{ - WorkTree: worktree, + GitWorkTree: gitworktree, GitDirectory: gitdirectory, } return r, nil @@ -109,8 +109,8 @@ func (r *Repository) Changelog(tag string, dir string) (string, error) { func (r *Repository) run(args ...string) (string, error) { extraArgs := []string{"-c", "log.showSignature=false"} - if r.WorkTree != "" { - extraArgs = append(extraArgs, fmt.Sprintf("--work-tree=%s", r.WorkTree)) + if r.GitWorkTree != "" { + extraArgs = append(extraArgs, fmt.Sprintf("--work-tree=%s", r.GitWorkTree)) } if r.GitDirectory != "" { extraArgs = append(extraArgs, fmt.Sprintf("--git-dir=%s", r.GitDirectory)) diff --git a/internal/git/git_test.go b/internal/git/git_test.go index e530692..e9d84ef 100644 --- a/internal/git/git_test.go +++ b/internal/git/git_test.go @@ -19,16 +19,16 @@ func TestNewRepository(t *testing.T) { is := is.New(t) r, err := NewRepository("", "") is.NoErr(err) - is.Equal(r.WorkTree, cwd) + is.Equal(r.GitWorkTree, cwd) is.Equal(r.GitDirectory, cgd) }) - t.Run("only worktree set", func(t *testing.T) { - wt := "/idk/some/work/tree/location" + t.Run("only gitworktree set", func(t *testing.T) { + gwt := "/idk/some/git/work/tree/location" is := is.New(t) - r, err := NewRepository(wt, "") + r, err := NewRepository(gwt, "") is.NoErr(err) - is.Equal(r.WorkTree, wt) + is.Equal(r.GitWorkTree, gwt) is.Equal(r.GitDirectory, cgd) }) @@ -37,15 +37,15 @@ func TestNewRepository(t *testing.T) { is := is.New(t) r, err := NewRepository("", gd) is.NoErr(err) - is.Equal(r.WorkTree, cwd) + is.Equal(r.GitWorkTree, cwd) is.Equal(r.GitDirectory, gd) }) - t.Run("worktree and gitdir set", func(t *testing.T) { + t.Run("gitworktree and gitdir set", func(t *testing.T) { is := is.New(t) r, err := NewRepository(cwd, cgd) is.NoErr(err) - is.Equal(r.WorkTree, cwd) + is.Equal(r.GitWorkTree, cwd) is.Equal(r.GitDirectory, cgd) }) } @@ -165,22 +165,22 @@ func TestRepository_ChangelogWithDirectory(t *testing.T) { func TestRepository_run(t *testing.T) { // current directory: . , ./.git - rootWT := currentWorkingDirectory(t) - rootGD := filepath.Join(rootWT, ".git") + rootGWT := currentWorkingDirectory(t) + rootGD := filepath.Join(rootGWT, ".git") t.Run("current directory", func(t *testing.T) { is := is.New(t) rootRepository := Repository{ - WorkTree: rootWT, + GitWorkTree: rootGWT, GitDirectory: rootGD, } _, err := rootRepository.run("init") is.NoErr(err) is.True(rootRepository.IsRepo()) - actualRootWT, err := rootRepository.run("rev-parse", "--show-toplevel") + actualRootGWT, err := rootRepository.run("rev-parse", "--show-toplevel") is.NoErr(err) - actualRootWT = strings.TrimSuffix(actualRootWT, "\n") // git adds a new line to the cli output - is.Equal(actualRootWT, rootWT) + actualRootGWT = strings.TrimSuffix(actualRootGWT, "\n") // git adds a new line to the cli output + is.Equal(actualRootGWT, rootGWT) actualGitDir, err := rootRepository.run("rev-parse", "--absolute-git-dir") is.NoErr(err) actualGitDir = strings.TrimSuffix(actualGitDir, "\n") // git adds a new line to the cli output @@ -188,22 +188,22 @@ func TestRepository_run(t *testing.T) { }) // subdirectory: ./foo , ./foo/.git - subWT := dir(currentWorkingDirectory(t), "foo", t) - subGD := dir(subWT, ".git", t) + subGWT := dir(currentWorkingDirectory(t), "foo", t) + subGD := dir(subGWT, ".git", t) t.Run("subdirectory", func(t *testing.T) { is := is.New(t) subfolderRepository := Repository{ - WorkTree: subWT, + GitWorkTree: subGWT, GitDirectory: subGD, } _, err := subfolderRepository.run("init") is.NoErr(err) is.True(subfolderRepository.IsRepo()) - actualWT, err := subfolderRepository.run("rev-parse", "--show-toplevel") + actualGWT, err := subfolderRepository.run("rev-parse", "--show-toplevel") is.NoErr(err) - actualWT = strings.TrimSuffix(actualWT, "\n") // git adds a new line to the cli output - is.Equal(actualWT, subWT) + actualGWT = strings.TrimSuffix(actualGWT, "\n") // git adds a new line to the cli output + is.Equal(actualGWT, subGWT) actualGitDir, err := subfolderRepository.run("rev-parse", "--absolute-git-dir") is.NoErr(err) actualGitDir = strings.TrimSuffix(actualGitDir, "\n") // git adds a new line to the cli output @@ -211,11 +211,11 @@ func TestRepository_run(t *testing.T) { }) // external: /temp/sdfjklds , /temp/sdfjklds/.git - externalWT := tempdir(t, false) - externalGD := dir(externalWT, ".git", t) - tmpWTFileName := "somefile.txt" - tmpWTFilePath := tempfile(t, externalWT, tmpWTFileName) - expectedWTContent, _ := dataFromFile(tmpWTFilePath) + externalGWT := tempdir(t, false) + externalGD := dir(externalGWT, ".git", t) + tmpGWTFileName := "somefile.txt" + tmpGWTFilePath := tempfile(t, externalGWT, tmpGWTFileName) + expectedGWTContent, _ := dataFromFile(tmpGWTFilePath) tmpGDFileName := "somefile.txt" tmpGDFilePath := tempfile(t, externalGD, tmpGDFileName) expectedGDContent, _ := dataFromFile(tmpGDFilePath) @@ -223,19 +223,19 @@ func TestRepository_run(t *testing.T) { t.Run("external directory", func(t *testing.T) { is := is.New(t) externalRepository := Repository{ - WorkTree: externalWT, + GitWorkTree: externalGWT, GitDirectory: externalGD, } _, err := externalRepository.run("init") is.NoErr(err) is.True(externalRepository.IsRepo()) - actualWT, err := externalRepository.run("rev-parse", "--show-toplevel") + actualGWT, err := externalRepository.run("rev-parse", "--show-toplevel") is.NoErr(err) - actualWT = strings.TrimSuffix(actualWT, "\n") // git adds a new line to the cli output + actualGWT = strings.TrimSuffix(actualGWT, "\n") // git adds a new line to the cli output // compare file content instead of having to deal with symlink paths - actualWTContent, err := dataFromFile(actualWT + string(os.PathSeparator) + tmpWTFileName) + actualWTContent, err := dataFromFile(actualGWT + string(os.PathSeparator) + tmpGWTFileName) is.NoErr(err) - is.Equal(expectedWTContent, actualWTContent) + is.Equal(expectedGWTContent, actualWTContent) actualGD, err := externalRepository.run("rev-parse", "--absolute-git-dir") is.NoErr(err)