Fix git remote normalization and add tests

pull/184/head
Simon Let 3 years ago
parent ff62e55d1f
commit 99584049a3
  1. 15
      cmd/cli/main.go
  2. 28
      internal/normalize/normailze.go
  3. 46
      internal/normalize/normalize_test.go
  4. 27
      internal/recordint/searchapp.go
  5. 4
      internal/recutil/recutil.go
  6. 7
      internal/searchapp/query.go
  7. 2
      scripts/hooks.sh

@ -57,22 +57,23 @@ func main() {
func runReshCli(out *output.Output, config cfg.Config) (string, int) {
args := opt.HandleVersionOpts(out, os.Args, version, commit)
const missing = "<missing cdxgtcpboqwrdom>"
flags := pflag.NewFlagSet("", pflag.ExitOnError)
sessionID := flags.String("session-id", "", "Resh generated session ID")
pwd := flags.String("pwd", "", "$PWD - present working directory")
gitOriginRemote := flags.String("git-origin-remote", "<<<MISSING>>>", "> git origin remote")
sessionID := flags.String("session-id", missing, "Resh generated session ID")
pwd := flags.String("pwd", missing, "$PWD - present working directory")
gitOriginRemote := flags.String("git-remote", missing, "> git remote get-url origin")
query := flags.String("query", "", "Search query")
flags.Parse(args)
// TODO: These errors should tell the user that they should not be running the command directly
errMsg := "Failed to get required command-line arguments"
if *sessionID == "" {
if *sessionID == missing {
out.FatalE(errMsg, errors.New("missing required option --session-id"))
}
if *pwd == "" {
if *pwd == missing {
out.FatalE(errMsg, errors.New("missing required option --pwd"))
}
if *gitOriginRemote == "<<<MISSING>>>" {
if *gitOriginRemote == missing {
out.FatalE(errMsg, errors.New("missing required option --git-origin-remote"))
}
dataDir, err := datadir.GetPath()
@ -249,7 +250,7 @@ func (m manager) UpdateData(input string) {
"recordCount", len(m.s.cliRecords),
"itemCount", len(m.s.data),
)
query := searchapp.NewQueryFromString(input, m.host, m.pwd, m.gitOriginRemote, m.config.Debug)
query := searchapp.NewQueryFromString(sugar, input, m.host, m.pwd, m.gitOriginRemote, m.config.Debug)
var data []searchapp.Item
itemSet := make(map[string]int)
m.s.lock.Lock()

@ -0,0 +1,28 @@
package normalize
import (
"net/url"
"strings"
giturls "github.com/whilp/git-urls"
"go.uber.org/zap"
)
// GitRemote helper
// Returns normalized git remote - valid even on error
func GitRemote(sugar *zap.SugaredLogger, gitRemote string) string {
gitRemote = strings.TrimSuffix(gitRemote, ".git")
parsedURL, err := giturls.Parse(gitRemote)
if err != nil {
sugar.Errorw("Failed to parse git remote", zap.Error(err),
"gitRemote", gitRemote,
)
return gitRemote
}
if parsedURL.User == nil || parsedURL.User.Username() == "" {
parsedURL.User = url.User("git")
}
// TODO: figure out what scheme we want
parsedURL.Scheme = "git+ssh"
return parsedURL.String()
}

@ -0,0 +1,46 @@
package normalize_test
import (
"testing"
"github.com/curusarn/resh/internal/normalize"
"go.uber.org/zap"
)
// TestLeftCutPadString
func TestGitRemote(t *testing.T) {
sugar := zap.NewNop().Sugar()
data := [][]string{
{
"git@github.com:curusarn/resh.git", // git
"git@github.com:curusarn/resh", // git no ".git"
"http://github.com/curusarn/resh.git", // http
"https://github.com/curusarn/resh.git", // https
"ssh://git@github.com/curusarn/resh.git", // ssh
"git+ssh://git@github.com/curusarn/resh.git", // git+ssh
},
{
"git@host.example.com:org/user/repo.git", // git
"git@host.example.com:org/user/repo", // git no ".git"
"http://host.example.com/org/user/repo.git", // http
"https://host.example.com/org/user/repo.git", // https
"ssh://git@host.example.com/org/user/repo.git", // ssh
"git+ssh://git@host.example.com/org/user/repo.git", // git+ssh
},
}
for _, arr := range data {
n := len(arr)
for i := 0; i < n-1; i++ {
for j := i + 1; j < n; j++ {
one := normalize.GitRemote(sugar, arr[i])
two := normalize.GitRemote(sugar, arr[j])
if one != two {
t.Fatalf("Normalized git remotes should match for '%s' and '%s'\n -> got: '%s' != '%s'",
arr[i], arr[j], one, two)
}
}
}
}
}

@ -1,12 +1,10 @@
package recordint
import (
"net/url"
"strconv"
"strings"
"github.com/curusarn/resh/internal/normalize"
"github.com/curusarn/resh/record"
giturls "github.com/whilp/git-urls"
"go.uber.org/zap"
)
@ -50,28 +48,9 @@ func NewSearchApp(sugar *zap.SugaredLogger, r *record.V1) SearchApp {
Host: r.Device,
Pwd: r.Pwd,
Home: r.Home,
// TODO: is this the right place to normalize the git remote
GitOriginRemote: normalizeGitRemote(sugar, r.GitOriginRemote),
// TODO: is this the right place to normalize the git remote?
GitOriginRemote: normalize.GitRemote(sugar, r.GitOriginRemote),
ExitCode: r.ExitCode,
Time: time,
}
}
// TODO: maybe move this to a more appropriate place
// normalizeGitRemote helper
func normalizeGitRemote(sugar *zap.SugaredLogger, gitRemote string) string {
gitRemote = strings.TrimSuffix(gitRemote, ".git")
parsedURL, err := giturls.Parse(gitRemote)
if err != nil {
sugar.Errorw("Failed to parse git remote", zap.Error(err),
"gitRemote", gitRemote,
)
return gitRemote
}
if parsedURL.User == nil || parsedURL.User.Username() == "" {
parsedURL.User = url.User("git")
}
// TODO: figure out what scheme we want
parsedURL.Scheme = "git+ssh"
return parsedURL.String()
}

@ -26,11 +26,11 @@ import (
// }
// TODO: maybe more to a more appropriate place
// TODO: cleanup the interface - stop modifying the part1 and returning a ew record at the same time
// TODO: cleanup the interface - stop modifying the part1 and returning a new record at the same time
// Merge two records (part1 - collect + part2 - postcollect)
func Merge(r1 *recordint.Collect, r2 *recordint.Collect) (record.V1, error) {
if r1.SessionID != r2.SessionID {
return record.V1{}, errors.New("Records to merge are not from the same sesion - r1:" + r1.SessionID + " r2:" + r2.SessionID)
return record.V1{}, errors.New("Records to merge are not from the same session - r1:" + r1.SessionID + " r2:" + r2.SessionID)
}
if r1.Rec.RecordID != r2.Rec.RecordID {
return record.V1{}, errors.New("Records to merge do not have the same ID - r1:" + r1.Rec.RecordID + " r2:" + r2.Rec.RecordID)

@ -3,6 +3,9 @@ package searchapp
import (
"sort"
"strings"
"github.com/curusarn/resh/internal/normalize"
"go.uber.org/zap"
)
// Query holds information that is used for result scoring
@ -35,7 +38,7 @@ func filterTerms(terms []string) []string {
}
// NewQueryFromString .
func NewQueryFromString(queryInput string, host string, pwd string, gitOriginRemote string, debug bool) Query {
func NewQueryFromString(sugar *zap.SugaredLogger, queryInput string, host string, pwd string, gitOriginRemote string, debug bool) Query {
terms := strings.Fields(queryInput)
var logStr string
for _, term := range terms {
@ -51,7 +54,7 @@ func NewQueryFromString(queryInput string, host string, pwd string, gitOriginRem
terms: terms,
host: host,
pwd: pwd,
gitOriginRemote: gitOriginRemote,
gitOriginRemote: normalize.GitRemote(sugar, gitOriginRemote),
}
}

@ -125,7 +125,7 @@ __resh_widget_control_R() {
return $?
fi
BUFFER=$(resh-cli -requireVersion "$__RESH_VERSION" \
--git-origin-remote "$git_remote" \
--git-remote "$git_remote" \
--pwd "$PWD" \
--query "$BUFFER" \
--session-id "$__RESH_SESSION_ID" \

Loading…
Cancel
Save