Skip to content

Commit

Permalink
analytics: fix git remote reporting (#1145)
Browse files Browse the repository at this point in the history
  • Loading branch information
landism authored Feb 11, 2019
1 parent 0730378 commit ea6d728
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
8 changes: 4 additions & 4 deletions internal/cli/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func globalTags() map[string]string {
}

// store a hash of the git remote to help us guess how many users are running it on the same repository
origin := normalizeGitRemote(gitOrigin())
origin := normalizeGitRemote(gitOrigin("."))
if origin != "" {
h := md5.Sum([]byte(origin))
ret["git.origin"] = base64.StdEncoding.EncodeToString(h[:])
Expand All @@ -93,14 +93,14 @@ func globalTags() map[string]string {
return ret
}

func gitOrigin() string {
cmd := exec.Command("git", "remote", "get", "origin")
func gitOrigin(fromDir string) string {
cmd := exec.Command("git", "-C", fromDir, "remote", "get-url", "origin")
b, err := cmd.Output()
if err != nil {
return ""
}

return string(b)
return strings.TrimRight(string(b), "\n")
}

func normalizeGitRemote(s string) string {
Expand Down
22 changes: 22 additions & 0 deletions internal/cli/analytics_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package cli

import (
"os/exec"
"testing"

"github.com/windmilleng/tilt/internal/testutils/tempdir"

"github.com/stretchr/testify/assert"
)

Expand All @@ -21,3 +24,22 @@ func TestNormalizeGitRemoteTrailingSlash(t *testing.T) {
func TestNormalizedGitRemoteUsername(t *testing.T) {
assert.Equal(t, normalizeGitRemote("https://github.com/windmilleng/tilt"), normalizeGitRemote("[email protected]:windmilleng/tilt.git"))
}

func TestGitOrigin(t *testing.T) {
tf := tempdir.NewTempDirFixture(t)
defer tf.TearDown()

err := exec.Command("git", "init", tf.Path()).Run()
if err != nil {
t.Fatalf("failed to init git repo: %+v", err)
}
err = exec.Command("git", "-C", tf.Path(), "remote", "add", "origin", "https://github.com/windmilleng/tilt").Run()
if err != nil {
t.Fatalf("failed to set origin's url: %+v", err)
}
origin := gitOrigin(tf.Path())

// we can't just compare raw urls because of https://git-scm.com/docs/git-config#git-config-urlltbasegtinsteadOf
// e.g., circleci images set `url.ssh://[email protected]=https://github.com`
assert.Equal(t, "//github.com/windmilleng/tilt", normalizeGitRemote(origin))
}

0 comments on commit ea6d728

Please sign in to comment.