From f3c3e37b34aa8b3d213e1f3d32733746678b1f90 Mon Sep 17 00:00:00 2001 From: Eric Fritz Date: Thu, 27 Feb 2020 14:01:10 -0600 Subject: [PATCH 1/6] Ensure we infer the root when no value is supplied. --- cmd/src/lsif_upload.go | 43 +++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/cmd/src/lsif_upload.go b/cmd/src/lsif_upload.go index 1a292f1ce3..d9c5068dd3 100644 --- a/cmd/src/lsif_upload.go +++ b/cmd/src/lsif_upload.go @@ -13,6 +13,7 @@ import ( "net/url" "os" "os/exec" + "path/filepath" "strings" @@ -20,6 +21,23 @@ import ( "github.com/mattn/go-isatty" ) +// stringFlag is used to wrap the value of the -root argument so that we can +// distinguish the cases where no root is supplied and an empty root is supplied. +type stringFlag struct { + set bool + value string +} + +func (f *stringFlag) Set(v string) error { + f.value = v + f.set = true + return nil +} + +func (f *stringFlag) String() string { + return f.value +} + func init() { usage := ` Examples: @@ -48,10 +66,12 @@ Examples: commitFlag = flagSet.String("commit", "", `The 40-character hash of the commit. Defaults to the currently checked-out commit.`) fileFlag = flagSet.String("file", "./dump.lsif", `The path to the LSIF dump file.`) githubTokenFlag = flagSet.String("github-token", "", `A GitHub access token with 'public_repo' scope that Sourcegraph uses to verify you have access to the repository.`) - rootFlag = flagSet.String("root", "", `The path in the repository that matches the LSIF projectRoot (e.g. cmd/project1). Defaults to the empty string, which refers to the top level of the repository.`) + rootFlag stringFlag apiFlags = newAPIFlags(flagSet) ) + flagSet.Var(&rootFlag, "root", `The path in the repository that matches the LSIF projectRoot (e.g. cmd/project1). Defaults to the empty string, which refers to the top level of the repository.`) + handler := func(args []string) error { flagSet.Parse(args) @@ -91,7 +111,8 @@ Examples: } fmt.Println("File: " + *fileFlag) - if rootFlag == nil { + root := rootFlag.String() + if !rootFlag.set { checkError := func(err error) { if err != nil { fmt.Println(err) @@ -110,18 +131,18 @@ Examples: rel, err := filepath.Rel(strings.TrimSpace(string(topLevel)), absFile) checkError(err) - *rootFlag = filepath.Dir(rel) + root = filepath.Dir(rel) } - *rootFlag = filepath.Clean(*rootFlag) - if strings.HasPrefix(*rootFlag, "..") { - fmt.Println("-root is outside the repository: " + *rootFlag) + root = filepath.Clean(root) + if strings.HasPrefix(root, "..") { + fmt.Println("-root is outside the repository: " + root) os.Exit(1) } - if *rootFlag == "." { - *rootFlag = "" + if root == "." || root == "/" { + root = "" } - fmt.Println("Root: " + *rootFlag) + fmt.Println("Root: " + root) // First, build the URL which is used to both make the request // and to emit a cURL command. This is a little different than @@ -134,8 +155,8 @@ Examples: if *githubTokenFlag != "" { qs.Add("github_token", *githubTokenFlag) } - if *rootFlag != "" { - qs.Add("root", *rootFlag) + if root != "" { + qs.Add("root", root) } url, err := url.Parse(cfg.Endpoint + "/.api/lsif/upload") From 0745b28e5407fede5a75639f3ead1be89f773326 Mon Sep 17 00:00:00 2001 From: Eric Fritz Date: Sun, 1 Mar 2020 21:52:22 -0600 Subject: [PATCH 2/6] WIP. --- cmd/src/lsif_upload.go | 49 ++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/cmd/src/lsif_upload.go b/cmd/src/lsif_upload.go index d9c5068dd3..b755129372 100644 --- a/cmd/src/lsif_upload.go +++ b/cmd/src/lsif_upload.go @@ -13,7 +13,6 @@ import ( "net/url" "os" "os/exec" - "path/filepath" "strings" @@ -21,21 +20,14 @@ import ( "github.com/mattn/go-isatty" ) -// stringFlag is used to wrap the value of the -root argument so that we can -// distinguish the cases where no root is supplied and an empty root is supplied. -type stringFlag struct { - set bool - value string -} - -func (f *stringFlag) Set(v string) error { - f.value = v - f.set = true - return nil -} - -func (f *stringFlag) String() string { - return f.value +func isFlagSet(fs *flag.FlagSet, name string) bool { + var found bool + fs.Visit(func(f *flag.Flag) { + if f.Name == name { + found = true + } + }) + return found } func init() { @@ -64,14 +56,12 @@ Examples: var ( repoFlag = flagSet.String("repo", "", `The name of the repository. By default, derived from the origin remote.`) commitFlag = flagSet.String("commit", "", `The 40-character hash of the commit. Defaults to the currently checked-out commit.`) + rootFlag = flagSet.String("root", `The path in the repository that matches the LSIF projectRoot (e.g. cmd/project1). Defaults to the empty string, which refers to the top level of the repository.`) fileFlag = flagSet.String("file", "./dump.lsif", `The path to the LSIF dump file.`) githubTokenFlag = flagSet.String("github-token", "", `A GitHub access token with 'public_repo' scope that Sourcegraph uses to verify you have access to the repository.`) - rootFlag stringFlag apiFlags = newAPIFlags(flagSet) ) - flagSet.Var(&rootFlag, "root", `The path in the repository that matches the LSIF projectRoot (e.g. cmd/project1). Defaults to the empty string, which refers to the top level of the repository.`) - handler := func(args []string) error { flagSet.Parse(args) @@ -111,8 +101,7 @@ Examples: } fmt.Println("File: " + *fileFlag) - root := rootFlag.String() - if !rootFlag.set { + if !isFlagSet(rootFlag) { checkError := func(err error) { if err != nil { fmt.Println(err) @@ -131,18 +120,18 @@ Examples: rel, err := filepath.Rel(strings.TrimSpace(string(topLevel)), absFile) checkError(err) - root = filepath.Dir(rel) + rootFlag = filepath.Dir(rel) } - root = filepath.Clean(root) - if strings.HasPrefix(root, "..") { - fmt.Println("-root is outside the repository: " + root) + *rootFlag = filepath.Clean(*rootFlag) + if strings.HasPrefix(*rootFlag, "..") { + fmt.Println("-root is outside the repository: " + *rootFlag) os.Exit(1) } - if root == "." || root == "/" { - root = "" + if *rootFlag == "." || *rootFlag == "/" { + *rootFlag = "" } - fmt.Println("Root: " + root) + fmt.Println("Root: " + *rootFlag) // First, build the URL which is used to both make the request // and to emit a cURL command. This is a little different than @@ -155,8 +144,8 @@ Examples: if *githubTokenFlag != "" { qs.Add("github_token", *githubTokenFlag) } - if root != "" { - qs.Add("root", root) + if *rootFlag != "" { + qs.Add("root", *rootFlag) } url, err := url.Parse(cfg.Endpoint + "/.api/lsif/upload") From cb28c38be87a1c7827be007d8d73a77ceab98855 Mon Sep 17 00:00:00 2001 From: Eric Fritz Date: Sun, 1 Mar 2020 21:55:47 -0600 Subject: [PATCH 3/6] WIP. --- cmd/src/lsif_upload.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cmd/src/lsif_upload.go b/cmd/src/lsif_upload.go index b755129372..fe09d94841 100644 --- a/cmd/src/lsif_upload.go +++ b/cmd/src/lsif_upload.go @@ -56,7 +56,7 @@ Examples: var ( repoFlag = flagSet.String("repo", "", `The name of the repository. By default, derived from the origin remote.`) commitFlag = flagSet.String("commit", "", `The 40-character hash of the commit. Defaults to the currently checked-out commit.`) - rootFlag = flagSet.String("root", `The path in the repository that matches the LSIF projectRoot (e.g. cmd/project1). Defaults to the empty string, which refers to the top level of the repository.`) + rootFlag = flagSet.String("root", "", `The path in the repository that matches the LSIF projectRoot (e.g. cmd/project1). Defaults to the empty string, which refers to the top level of the repository.`) fileFlag = flagSet.String("file", "./dump.lsif", `The path to the LSIF dump file.`) githubTokenFlag = flagSet.String("github-token", "", `A GitHub access token with 'public_repo' scope that Sourcegraph uses to verify you have access to the repository.`) apiFlags = newAPIFlags(flagSet) @@ -101,7 +101,7 @@ Examples: } fmt.Println("File: " + *fileFlag) - if !isFlagSet(rootFlag) { + if !isFlagSet(flagSet, "root") { checkError := func(err error) { if err != nil { fmt.Println(err) @@ -120,7 +120,8 @@ Examples: rel, err := filepath.Rel(strings.TrimSpace(string(topLevel)), absFile) checkError(err) - rootFlag = filepath.Dir(rel) + rf := filepath.Dir(rel) + rootFlag = &rf } *rootFlag = filepath.Clean(*rootFlag) From a5e99a0e28a16a3c7879ecdd7f1093821c926856 Mon Sep 17 00:00:00 2001 From: Eric Fritz Date: Sun, 1 Mar 2020 21:56:07 -0600 Subject: [PATCH 4/6] WIP. --- cmd/src/lsif_upload.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/src/lsif_upload.go b/cmd/src/lsif_upload.go index fe09d94841..4e5045f177 100644 --- a/cmd/src/lsif_upload.go +++ b/cmd/src/lsif_upload.go @@ -56,8 +56,8 @@ Examples: var ( repoFlag = flagSet.String("repo", "", `The name of the repository. By default, derived from the origin remote.`) commitFlag = flagSet.String("commit", "", `The 40-character hash of the commit. Defaults to the currently checked-out commit.`) - rootFlag = flagSet.String("root", "", `The path in the repository that matches the LSIF projectRoot (e.g. cmd/project1). Defaults to the empty string, which refers to the top level of the repository.`) fileFlag = flagSet.String("file", "./dump.lsif", `The path to the LSIF dump file.`) + rootFlag = flagSet.String("root", "", `The path in the repository that matches the LSIF projectRoot (e.g. cmd/project1). Defaults to the empty string, which refers to the top level of the repository.`) githubTokenFlag = flagSet.String("github-token", "", `A GitHub access token with 'public_repo' scope that Sourcegraph uses to verify you have access to the repository.`) apiFlags = newAPIFlags(flagSet) ) From d365e5c135c6702edf5a99031a344f1852476166 Mon Sep 17 00:00:00 2001 From: Eric Fritz Date: Sun, 1 Mar 2020 21:56:29 -0600 Subject: [PATCH 5/6] WIP. --- cmd/src/lsif_upload.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/src/lsif_upload.go b/cmd/src/lsif_upload.go index 4e5045f177..9aee2ada5d 100644 --- a/cmd/src/lsif_upload.go +++ b/cmd/src/lsif_upload.go @@ -57,8 +57,8 @@ Examples: repoFlag = flagSet.String("repo", "", `The name of the repository. By default, derived from the origin remote.`) commitFlag = flagSet.String("commit", "", `The 40-character hash of the commit. Defaults to the currently checked-out commit.`) fileFlag = flagSet.String("file", "./dump.lsif", `The path to the LSIF dump file.`) - rootFlag = flagSet.String("root", "", `The path in the repository that matches the LSIF projectRoot (e.g. cmd/project1). Defaults to the empty string, which refers to the top level of the repository.`) githubTokenFlag = flagSet.String("github-token", "", `A GitHub access token with 'public_repo' scope that Sourcegraph uses to verify you have access to the repository.`) + rootFlag = flagSet.String("root", "", `The path in the repository that matches the LSIF projectRoot (e.g. cmd/project1). Defaults to the empty string, which refers to the top level of the repository.`) apiFlags = newAPIFlags(flagSet) ) From 2b41427871cade515a8289b286d8acb84ffb6d94 Mon Sep 17 00:00:00 2001 From: Eric Fritz Date: Sun, 1 Mar 2020 21:57:02 -0600 Subject: [PATCH 6/6] WIP. --- cmd/src/lsif_upload.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/src/lsif_upload.go b/cmd/src/lsif_upload.go index 9aee2ada5d..daf34b2280 100644 --- a/cmd/src/lsif_upload.go +++ b/cmd/src/lsif_upload.go @@ -120,8 +120,8 @@ Examples: rel, err := filepath.Rel(strings.TrimSpace(string(topLevel)), absFile) checkError(err) - rf := filepath.Dir(rel) - rootFlag = &rf + relDir := filepath.Dir(rel) + rootFlag = &relDir } *rootFlag = filepath.Clean(*rootFlag)