Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for backslashes in remote branch name #380

Merged
merged 3 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/spr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func main() {
}

cfg := config_parser.ParseConfig(gitcmd)

err = config_parser.CheckConfig(cfg)
if err != nil {
fmt.Println(err)
os.Exit(2)
}
gitcmd = realgit.NewGitCmd(cfg)

ctx := context.Background()
Expand Down
19 changes: 19 additions & 0 deletions config/config_parser/config_parser.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package config_parser

import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"

"github.com/ejoffe/rake"
"github.com/ejoffe/spr/config"
Expand Down Expand Up @@ -49,9 +51,26 @@ func ParseConfig(gitcmd git.GitInterface) *config.Config {
rake.LoadSources(cfg.State,
rake.YamlFileWriter(InternalConfigFilePath()))

// init case : if yaml config files not found : create them
if _, err := os.Stat(RepoConfigFilePath(gitcmd)); errors.Is(err, os.ErrNotExist) {
rake.LoadSources(cfg.Repo,
rake.YamlFileWriter(RepoConfigFilePath(gitcmd)))
}

if _, err := os.Stat(UserConfigFilePath()); errors.Is(err, os.ErrNotExist) {
rake.LoadSources(cfg.User,
rake.YamlFileWriter(UserConfigFilePath()))
}
return cfg
}

func CheckConfig(cfg *config.Config) error {
if strings.Contains(cfg.Repo.GitHubBranch, "/") {
return errors.New("Remote branch name must not contain backslashes '/'")
}
return nil
}

func RepoConfigFilePath(gitcmd git.GitInterface) string {
rootdir := gitcmd.RootDir()
filepath := filepath.Clean(path.Join(rootdir, ".spr.yml"))
Expand Down
31 changes: 24 additions & 7 deletions github/githubclient/star.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,22 @@ const (

func (c *client) MaybeStar(ctx context.Context, cfg *config.Config) {
if !cfg.State.Stargazer && cfg.State.RunCount%promptCycle == 0 {
if c.isStar(ctx) {
starred, err := c.isStar(ctx)
if err != nil {
fmt.Println("enjoying git spr? [Y/n]")
fmt.Print(" please add a star at https://github.com/ejoffe/spr")
reader := bufio.NewReader(os.Stdin)
line, _ := reader.ReadString('\n')
line = strings.TrimSpace(line)
if line != "n" {
cfg.State.Stargazer = true
rake.LoadSources(cfg.State,
rake.YamlFileWriter(config_parser.InternalConfigFilePath()))
fmt.Println("Thank You! Happy Coding!")
}
}

if starred {
log.Debug().Bool("stargazer", true).Msg("MaybeStar")
cfg.State.Stargazer = true
rake.LoadSources(cfg.State,
Expand All @@ -39,30 +54,32 @@ func (c *client) MaybeStar(ctx context.Context, cfg *config.Config) {
cfg.State.Stargazer = true
rake.LoadSources(cfg.State,
rake.YamlFileWriter(config_parser.InternalConfigFilePath()))
fmt.Println("Thank you! Happy Coding!")
fmt.Println("Thank You! Happy Coding!")
}
}
}
}

func (c *client) isStar(ctx context.Context) bool {
func (c *client) isStar(ctx context.Context) (bool, error) {
iteration := 0
cursor := ""
for {
resp, err := c.api.StarCheck(ctx, &cursor)
check(err)
if err != nil {
return false, err
}

edgeCount := len(*resp.Viewer.StarredRepositories.Edges)
if edgeCount == 0 {
log.Debug().Bool("stargazer", false).Msg("MaybeStar::isStar")
return false
return false, nil
}

sprRepo := fmt.Sprintf("%s/%s", sprRepoOwner, sprRepoName)
for _, node := range *resp.Viewer.StarredRepositories.Nodes {
if node.NameWithOwner == sprRepo {
log.Debug().Bool("stargazer", true).Msg("MaybeStar::isStar")
return true
return true, nil
}
}

Expand All @@ -73,7 +90,7 @@ func (c *client) isStar(ctx context.Context) bool {
if iteration > 10 {
// too many stars in the sky
log.Debug().Bool("stargazer", false).Msg("MaybeStar::isStar (too many stars)")
return false
return false, nil
}
}
}
Expand Down
Loading