Browse Source

Add `hercules --head`

Signed-off-by: Vadim Markovtsev <vadim@sourced.tech>
Vadim Markovtsev 5 years ago
parent
commit
7d25700732
4 changed files with 34 additions and 4 deletions
  1. 1 1
      .travis.yml
  2. 8 2
      cmd/hercules/root.go
  3. 14 0
      internal/core/pipeline.go
  4. 11 1
      internal/core/pipeline_test.go

+ 1 - 1
.travis.yml

@@ -52,8 +52,8 @@ install:
   - travis_retry make TAGS=tensorflow
 script:
   - set -e
-  - if [ $TRAVIS_GO_VERSION != "1.10.x" ]; then test -z "$(gofmt -s -l . | grep -v vendor/)"; fi
   - export GO111MODULE=on
+  - test -z "$(gofmt -s -l . | grep -v vendor/)"
   - go vet -tags tensorflow ./...
   - golint -set_exit_status $(go list ./... | grep -v /vendor/)
   - cd python && flake8 && cd ..

+ 8 - 2
cmd/hercules/root.go

@@ -190,6 +190,7 @@ targets can be added using the --plugin system.`,
 		}
 		firstParent := getBool("first-parent")
 		commitsFile := getString("commits")
+		head := getBool("head")
 		protobuf := getBool("pb")
 		profile := getBool("profile")
 		disableStatus := getBool("quiet")
@@ -244,8 +245,12 @@ targets can be added using the --plugin system.`,
 		var commits []*object.Commit
 		var err error
 		if commitsFile == "" {
-			fmt.Fprint(os.Stderr, "git log...\r")
-			commits, err = pipeline.Commits(firstParent)
+			if !head {
+				fmt.Fprint(os.Stderr, "git log...\r")
+				commits, err = pipeline.Commits(firstParent)
+			} else {
+				commits, err = pipeline.HeadCommit()
+			}
 		} else {
 			commits, err = hercules.LoadCommitsFromFile(commitsFile, repository)
 		}
@@ -488,6 +493,7 @@ func init() {
 		panic(err)
 	}
 	hercules.PathifyFlagValue(rootFlags.Lookup("commits"))
+	rootFlags.Bool("head", false, "Analyze only the latest commit.")
 	rootFlags.Bool("first-parent", false, "Follow only the first parent in the commit history - "+
 		"\"git log --first-parent\".")
 	rootFlags.Bool("pb", false, "The output format will be Protocol Buffers instead of YAML.")

+ 14 - 0
internal/core/pipeline.go

@@ -500,6 +500,20 @@ func (pipeline *Pipeline) Commits(firstParent bool) ([]*object.Commit, error) {
 	return result, nil
 }
 
+// HeadCommit returns the latest commit in the repository (HEAD).
+func (pipeline *Pipeline) HeadCommit() ([]*object.Commit, error) {
+	repository := pipeline.repository
+	headref, err := repository.Head()
+	if err != nil {
+		return nil, err
+	}
+	commit, err := repository.CommitObject(headref.Hash())
+	if err != nil {
+		return nil, err
+	}
+	return []*object.Commit{commit}, nil
+}
+
 type sortablePipelineItems []PipelineItem
 
 func (items sortablePipelineItems) Len() int {

+ 11 - 1
internal/core/pipeline_test.go

@@ -428,7 +428,7 @@ func TestPipelineCommitsFull(t *testing.T) {
 func TestPipelineCommitsFirstParent(t *testing.T) {
 	pipeline := NewPipeline(test.Repository)
 	commits, err := pipeline.Commits(true)
-	assert.Nil(t, err)
+	assert.NoError(t, err)
 	assert.True(t, len(commits) >= 100)
 	hashMap := map[plumbing.Hash]bool{}
 	for _, c := range commits {
@@ -441,6 +441,16 @@ func TestPipelineCommitsFirstParent(t *testing.T) {
 		"a3ee37f91f0d705ec9c41ae88426f0ae44b2fbc3"))
 }
 
+func TestPipelineHeadCommit(t *testing.T) {
+	pipeline := NewPipeline(test.Repository)
+	commits, err := pipeline.HeadCommit()
+	assert.NoError(t, err)
+	assert.Len(t, commits, 1)
+	assert.True(t, len(commits[0].ParentHashes) > 0)
+	head, _ := test.Repository.Head()
+	assert.Equal(t, head.Hash(), commits[0].Hash)
+}
+
 func TestLoadCommitsFromFile(t *testing.T) {
 	tmp, err := ioutil.TempFile("", "hercules-test-")
 	assert.Nil(t, err)