Merge branch '7468-stacked-diff-add-git-config' into 'gmh-stacked-diffs'

feat(stacked-diffs): adding git config and folder creation

See merge request https://gitlab.com/gitlab-org/cli/-/merge_requests/1430

Merged-by: Shekhar Patnaik <spatnaik@gitlab.com>
Approved-by: Huzaifa Iftikhar <hiftikhar@gitlab.com>
Approved-by: Shekhar Patnaik <spatnaik@gitlab.com>
Reviewed-by: Gary Holtz <gholtz@gitlab.com>
Reviewed-by: Shekhar Patnaik <spatnaik@gitlab.com>
Co-authored-by: glab test bot <glabbot@beepboop.com>
Co-authored-by: Gary Holtz <gholtz@gitlab.com>
This commit is contained in:
Shekhar Patnaik 2024-04-26 06:43:57 +00:00
commit 5e515292f4
2 changed files with 134 additions and 0 deletions

50
pkg/git/stacked.go Normal file
View File

@ -0,0 +1,50 @@
package git
import (
"fmt"
"os"
"path"
"gitlab.com/gitlab-org/cli/internal/run"
)
type StackRef struct {
Previous string `json:"previous"`
Next string `json:"next"`
MR string `json:"mr"`
ChangeId string `json:"change-id"`
}
func SetLocalConfig(key, value string) error {
found, err := configValueExists(key, value)
if err != nil {
return fmt.Errorf("git config value exists: %w", err)
}
if found {
return nil
}
addCmd := GitCommand("config", "--local", key, value)
_, err = run.PrepareCmd(addCmd).Output()
if err != nil {
return fmt.Errorf("setting local git config: %w", err)
}
return nil
}
func AddStackRefDir(dir string) (string, error) {
baseDir, err := ToplevelDir()
if err != nil {
return "", fmt.Errorf("finding top level git directory: %w", err)
}
createdDir := path.Join(baseDir, "/.git/refs/stacked/", dir)
err = os.MkdirAll(createdDir, 0o700)
if err != nil {
return "", fmt.Errorf("creating stacked diff directory: %w", err)
}
return createdDir, nil
}

84
pkg/git/stacked_test.go Normal file
View File

@ -0,0 +1,84 @@
package git
import (
"os"
"path"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func Test_AddStackRefDir(t *testing.T) {
tests := []struct {
name string
branch string
want string
}{
{
name: "normal filename",
branch: "thing",
want: "thing",
},
{
name: "advanced filename",
branch: "something-with-dashes",
want: "something-with-dashes",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
baseDir := initGitRepo(t)
_, err := AddStackRefDir(tt.branch)
require.NoError(t, err)
refDir := path.Join(baseDir, "/.git/refs/stacked/")
_, err = os.Stat(path.Join(refDir, tt.branch))
require.NoError(t, err)
})
}
}
func TestSetLocalConfig(t *testing.T) {
tests := []struct {
name string
value string
existingConfig bool
}{
{
name: "config already exists",
value: "exciting new value",
existingConfig: true,
},
{
name: "config doesn't exist",
value: "default value",
existingConfig: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := initGitRepo(t)
defer os.RemoveAll(tempDir)
if tt.existingConfig {
_ = GitCommand("config", "--local", "this.glabstacks", "prev-value")
}
err := SetLocalConfig("this.glabstacks", tt.value)
require.NoError(t, err)
config, err := GetAllConfig("this.glabstacks")
require.NoError(t, err)
// GetAllConfig() appends a new line. Let's get rid of that.
compareString := strings.TrimSuffix(string(config), "\n")
if compareString != tt.value {
t.Errorf("config value = %v, want %v", compareString, tt.value)
}
})
}
}