forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dcb84b1
commit acdada8
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package git | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
const ( | ||
VersionUndefined = "undefined" | ||
) | ||
|
||
func getRepoRoot() string { | ||
GOPATH := os.Getenv("GOPATH") | ||
return filepath.Join(GOPATH, "src", "github.com", "v2ray", "v2ray-core") | ||
} | ||
|
||
func RevParse(args ...string) (string, error) { | ||
args = append([]string{"rev-parse"}, args...) | ||
cmd := exec.Command("git", args...) | ||
cmd.Dir = getRepoRoot() | ||
output, err := cmd.Output() | ||
if err != nil { | ||
return "", err | ||
} | ||
return strings.TrimSpace(string(output)), nil | ||
} | ||
|
||
func NameRev(args ...string) (string, error) { | ||
args = append([]string{"name-rev"}, args...) | ||
cmd := exec.Command("git", args...) | ||
cmd.Dir = getRepoRoot() | ||
output, err := cmd.Output() | ||
if err != nil { | ||
return "", err | ||
} | ||
return strings.TrimSpace(string(output)), nil | ||
} | ||
|
||
func RepoVersion(rev string) (string, error) { | ||
rev, err := RevParse(rev) | ||
if err != nil { | ||
return "", err | ||
} | ||
version, err := NameRev("name-rev", "--tags", "--name-only", rev) | ||
if err != nil { | ||
return "", err | ||
} | ||
if strings.HasSuffix(version, "^0") { | ||
version = version[:len(version)-2] | ||
} | ||
return version, nil | ||
} | ||
|
||
func RepoVersionHead() (string, error) { | ||
return RepoVersion("HEAD") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package git | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/v2ray/v2ray-core/testing/unit" | ||
) | ||
|
||
func TestRevParse(t *testing.T) { | ||
assert := unit.Assert(t) | ||
|
||
rev, err := RevParse("HEAD") | ||
assert.Error(err).IsNil() | ||
assert.Int(len(rev)).GreaterThan(0) | ||
|
||
rev, err = RevParse("v0.8") | ||
assert.Error(err).IsNil() | ||
assert.String(rev).Equals("de7a1d30c3e6bda6a1297b5815369fcfa0e74f0e") | ||
} | ||
|
||
func TestRepoVersion(t *testing.T) { | ||
assert := unit.Assert(t) | ||
|
||
version, err := RepoVersionHead() | ||
assert.Error(err).IsNil() | ||
assert.Int(len(version)).GreaterThan(0) | ||
|
||
version, err = RepoVersion("de7a1d30c3e6bda6a1297b5815369fcfa0e74f0e") | ||
assert.Error(err).IsNil() | ||
assert.String(version).Equals("v0.8") | ||
} |