From fe7689a6b28b644ef309350cf01f563201f44d10 Mon Sep 17 00:00:00 2001
From: silverwind <me@silverwind.io>
Date: Mon, 22 Jul 2019 14:03:15 +0200
Subject: [PATCH] Fix empty commits now showing in repo overview (#7521)

* Fix empty commits now showing in repo overview

* add test

* make fmt
---
 modules/git/commit_info.go                         |   4 +++-
 modules/git/commit_info_test.go                    |  21 ++++++++++++++-------
 modules/git/repo_stats_test.go                     |   8 ++++----
 modules/git/tests/repos/repo1_bare/logs/HEAD       |   1 +
 .../tests/repos/repo1_bare/logs/refs/heads/master  |   1 +
 .../fe/af4ba6bc635fec442f46ddd4512416ec43c2c2      | Bin 0 -> 817 bytes
 .../git/tests/repos/repo1_bare/refs/heads/master   |   2 +-
 7 files changed, 24 insertions(+), 13 deletions(-)
 create mode 100644 modules/git/tests/repos/repo1_bare/logs/HEAD
 create mode 100644 modules/git/tests/repos/repo1_bare/logs/refs/heads/master
 create mode 100644 modules/git/tests/repos/repo1_bare/objects/fe/af4ba6bc635fec442f46ddd4512416ec43c2c2

diff --git a/modules/git/commit_info.go b/modules/git/commit_info.go
index 8417226f8b..3fa80457ac 100644
--- a/modules/git/commit_info.go
+++ b/modules/git/commit_info.go
@@ -68,7 +68,9 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache LastCom
 	// get it for free during the tree traversal and it's used for listing
 	// pages to display information about newest commit for a given path.
 	var treeCommit *Commit
-	if rev, ok := revs[""]; ok {
+	if treePath == "" {
+		treeCommit = commit
+	} else if rev, ok := revs[""]; ok {
 		treeCommit = convertCommit(rev)
 	}
 	return commitsInfo, treeCommit, nil
diff --git a/modules/git/commit_info_test.go b/modules/git/commit_info_test.go
index d7d863b032..71637d188a 100644
--- a/modules/git/commit_info_test.go
+++ b/modules/git/commit_info_test.go
@@ -28,21 +28,27 @@ func cloneRepo(url, dir, name string) (string, error) {
 func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
 	// these test case are specific to the repo1 test repo
 	testCases := []struct {
-		CommitID    string
-		Path        string
-		ExpectedIDs map[string]string
+		CommitID           string
+		Path               string
+		ExpectedIDs        map[string]string
+		ExpectedTreeCommit string
 	}{
 		{"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", "", map[string]string{
 			"file1.txt": "95bb4d39648ee7e325106df01a621c530863a653",
 			"file2.txt": "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
-		}},
+		}, "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2"},
 		{"2839944139e0de9737a044f78b0e4b40d989a9e3", "", map[string]string{
 			"file1.txt":   "2839944139e0de9737a044f78b0e4b40d989a9e3",
 			"branch1.txt": "9c9aef8dd84e02bc7ec12641deb4c930a7c30185",
-		}},
+		}, "2839944139e0de9737a044f78b0e4b40d989a9e3"},
 		{"5c80b0245c1c6f8343fa418ec374b13b5d4ee658", "branch2", map[string]string{
 			"branch2.txt": "5c80b0245c1c6f8343fa418ec374b13b5d4ee658",
-		}},
+		}, "5c80b0245c1c6f8343fa418ec374b13b5d4ee658"},
+		{"feaf4ba6bc635fec442f46ddd4512416ec43c2c2", "", map[string]string{
+			"file1.txt": "95bb4d39648ee7e325106df01a621c530863a653",
+			"file2.txt": "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
+			"foo":       "37991dec2c8e592043f47155ce4808d4580f9123",
+		}, "feaf4ba6bc635fec442f46ddd4512416ec43c2c2"},
 	}
 	for _, testCase := range testCases {
 		commit, err := repo1.GetCommit(testCase.CommitID)
@@ -51,7 +57,8 @@ func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
 		assert.NoError(t, err)
 		entries, err := tree.ListEntries()
 		assert.NoError(t, err)
-		commitsInfo, _, err := entries.GetCommitsInfo(commit, testCase.Path, nil)
+		commitsInfo, treeCommit, err := entries.GetCommitsInfo(commit, testCase.Path, nil)
+		assert.Equal(t, testCase.ExpectedTreeCommit, treeCommit.ID.String())
 		assert.NoError(t, err)
 		assert.Len(t, commitsInfo, len(testCase.ExpectedIDs))
 		for _, commitInfo := range commitsInfo {
diff --git a/modules/git/repo_stats_test.go b/modules/git/repo_stats_test.go
index 2e8565b9e2..1822af0be6 100644
--- a/modules/git/repo_stats_test.go
+++ b/modules/git/repo_stats_test.go
@@ -23,12 +23,12 @@ func TestRepository_GetCodeActivityStats(t *testing.T) {
 	assert.NoError(t, err)
 	assert.NotNil(t, code)
 
-	assert.EqualValues(t, 8, code.CommitCount)
-	assert.EqualValues(t, 2, code.AuthorCount)
-	assert.EqualValues(t, 8, code.CommitCountInAllBranches)
+	assert.EqualValues(t, 9, code.CommitCount)
+	assert.EqualValues(t, 3, code.AuthorCount)
+	assert.EqualValues(t, 9, code.CommitCountInAllBranches)
 	assert.EqualValues(t, 10, code.Additions)
 	assert.EqualValues(t, 1, code.Deletions)
-	assert.Len(t, code.Authors, 2)
+	assert.Len(t, code.Authors, 3)
 	assert.Contains(t, code.Authors, "tris.git@shoddynet.org")
 	assert.EqualValues(t, 3, code.Authors["tris.git@shoddynet.org"])
 	assert.EqualValues(t, 5, code.Authors[""])
diff --git a/modules/git/tests/repos/repo1_bare/logs/HEAD b/modules/git/tests/repos/repo1_bare/logs/HEAD
new file mode 100644
index 0000000000..cef4ca2dcb
--- /dev/null
+++ b/modules/git/tests/repos/repo1_bare/logs/HEAD
@@ -0,0 +1 @@
+37991dec2c8e592043f47155ce4808d4580f9123 feaf4ba6bc635fec442f46ddd4512416ec43c2c2 silverwind <me@silverwind.io> 1563741799 +0200	push
diff --git a/modules/git/tests/repos/repo1_bare/logs/refs/heads/master b/modules/git/tests/repos/repo1_bare/logs/refs/heads/master
new file mode 100644
index 0000000000..cef4ca2dcb
--- /dev/null
+++ b/modules/git/tests/repos/repo1_bare/logs/refs/heads/master
@@ -0,0 +1 @@
+37991dec2c8e592043f47155ce4808d4580f9123 feaf4ba6bc635fec442f46ddd4512416ec43c2c2 silverwind <me@silverwind.io> 1563741799 +0200	push
diff --git a/modules/git/tests/repos/repo1_bare/objects/fe/af4ba6bc635fec442f46ddd4512416ec43c2c2 b/modules/git/tests/repos/repo1_bare/objects/fe/af4ba6bc635fec442f46ddd4512416ec43c2c2
new file mode 100644
index 0000000000000000000000000000000000000000..95edd9a65b2d4f855df57304beea0074786e5d19
GIT binary patch
literal 817
zcmV-11J3+-0hN<U@1sT##dGFU^xaC_K(k4iC}1|T8NB>)b8A*NHU<N>flnW2M#@~~
zlDgF^)zd4fep%ZzbObDv&~Ia36rf~@n56=jayctX63;0xE6M^XB!{AmQUMZFDm-L5
z(iis;@RFjid699MTnGvW5nm#S6@;unGL&;9$goscj%UboytF+S=<QYXZ+g$cpH1=i
z&-M@6{smY;<R!#P3J+G0gOJI-=NpUvy;ybCKr8V3zoi-0HUOa&g2UDd)Z=MyeAgMk
z02*x<O;xpxs^%s&j5tiW`Yf6Uu6fMPKJpqD+Ro0p8NR9BAY2+l-Bj2AbOwATy7{rV
zM6UbrboOhznJdRPG<jqztqHv9wa}ecc4y><aUPD^m4B@xN80tJ-+#Fbm~I=CB=aQB
ze#1tRwKUIozBWnZD7H^*2Pv+aUIgE->#y}{*G6w8YqxnT-$}JU-Es!J&T?cgK_aTv
zBuSI~)SaZtZMTbD{@DBaFfA>{{_FN=J!h+*4ffDUE)HgvyI=Bs#elsSoP#EoTR(YD
z)$Q7x-emS^cvuB!3Gm|uH)p)ujK^kIJrv(LWvXwFE4QPE<7#L`2AJ{HQS?<xgK&<N
z$SqmbKw6$qf^K#h;?sJw)(vk>geiJH+it!1QPNsutD=rutcU^YhCJ|OZ2k4T*P`yo
z*0-JRhEDGrpR*1X<}Ujal5m2`TI2;-reYarBc;AtI%(}O;J{lKSum8c;V%ve4xy_O
z$HX3s2{*i+aaq~*=c_k@c*+_~2dTVJI<v%i;@rL;8U_ryz%T0(I;*%qUrCKu9nD{=
zqFG63e&u`qo%545$m`Y?PciWv)wiymCFXj{KXmWoz<Or#9?tTR>pVx<1C7ZYNBDsK
z*^GNg-V{xF>yLQjNl#b9<9iI}*MrY$wC$Z+%7CUU%*#=bPN{(&&+Ad&Mi9f;NAQIA
zM{?a09ZS#ja<05;g39LJDd*#tKiJh#td%wWWay?rV<9?waUI#mx@Vn{5-l>`=0)kb
voqNo>7xA(*7;)E1n`JkAW573aKkS$v%ou_GD`J?U>BbrSz>@g~RMAuP6o8_s

literal 0
HcmV?d00001

diff --git a/modules/git/tests/repos/repo1_bare/refs/heads/master b/modules/git/tests/repos/repo1_bare/refs/heads/master
index 4804d9d8fe..c5e92eb229 100644
--- a/modules/git/tests/repos/repo1_bare/refs/heads/master
+++ b/modules/git/tests/repos/repo1_bare/refs/heads/master
@@ -1 +1 @@
-37991dec2c8e592043f47155ce4808d4580f9123
+feaf4ba6bc635fec442f46ddd4512416ec43c2c2