Skip to content

Commit 438f947

Browse files
committed
add validation logic to handle new commits added to a fresh repo
1 parent 0d5a027 commit 438f947

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

git/git_unpushed_commits.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ func commitModel(commit object.Commit) string {
4949
return commitString
5050
}
5151

52+
func nilCommit() []*string {
53+
logger.Log("No new commits available to push", global.StatusWarning)
54+
return nil
55+
}
56+
5257
// UnPushedCommits compares the local branch and the remote branch to extract the commits which are not pushed to the remote
5358
func UnPushedCommits(repo *git.Repository, remoteRef string) []*string {
5459
var commitArray []*string
@@ -59,8 +64,12 @@ func UnPushedCommits(repo *git.Repository, remoteRef string) []*string {
5964
remoteCommit, _ := repo.CommitObject(*revHash)
6065

6166
head, _ := repo.Head()
62-
localCommit, _ := repo.CommitObject(head.Hash())
6367

68+
if head == nil {
69+
return nilCommit()
70+
}
71+
72+
localCommit, _ := repo.CommitObject(head.Hash())
6473
isAncestor, _ = localCommit.IsAncestor(remoteCommit)
6574

6675
if !isAncestor {
@@ -70,7 +79,22 @@ func UnPushedCommits(repo *git.Repository, remoteRef string) []*string {
7079
All: false,
7180
})
7281

82+
if logItr == nil {
83+
if localCommit != nil {
84+
commitString := commitModel(*localCommit)
85+
logger.Log(fmt.Sprintf("New commit available for pushing to remote -> %s", commitString), global.StatusInfo)
86+
commitArray = append(commitArray, &commitString)
87+
return commitArray
88+
} else {
89+
return nilCommit()
90+
}
91+
}
92+
7393
_ = logItr.ForEach(func(commit *object.Commit) error {
94+
if commit == nil {
95+
logger.Log("Commit object is nil", global.StatusError)
96+
return types.Error{Msg: "Commit object is nil"}
97+
}
7498
if commit.Hash == remoteCommit.Hash {
7599
logger.Log(fmt.Sprintf("Same commits found in remote and local trees -> %s", commit.Hash.String()), global.StatusInfo)
76100
return types.Error{Msg: "Same commit"}
@@ -84,8 +108,7 @@ func UnPushedCommits(repo *git.Repository, remoteRef string) []*string {
84108
logItr.Close()
85109
return commitArray
86110
} else {
87-
logger.Log("No new commits available to push", global.StatusWarning)
88-
return nil
111+
return nilCommit()
89112
}
90113

91114
}

0 commit comments

Comments
 (0)