Skip to content

Commit 5d6cc13

Browse files
committed
add additional logic in unpushed commit listing to consider all local commits as unpushed when the remote has no commits (resolves #101)
1 parent 438f947 commit 5d6cc13

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

git/git_repo_validate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package git
33
import (
44
"fmt"
55
"github.com/go-git/go-git/v5"
6+
"github.com/neel1996/gitconvex-server/global"
67
"go/types"
78
"os"
89
)
@@ -22,7 +23,7 @@ func RepoValidator(repoPath string) (string, error) {
2223

2324
_, headErr := repo.Head()
2425
if headErr != nil {
25-
return "", types.Error{Msg: "The selected folder is not a valid git repo"}
26+
logger.Log(fmt.Sprintf("Mind that the repo has no HEAD and a fresh commit is required -> %s", headErr.Error()), global.StatusWarning)
2627
}
2728

2829
return "Repo is valid!", nil

git/git_unpushed_commits.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ func UnPushedCommits(repo *git.Repository, remoteRef string) []*string {
6363
revHash, _ := repo.ResolveRevision(plumbing.Revision(remoteRef))
6464
remoteCommit, _ := repo.CommitObject(*revHash)
6565

66+
// Retuning nil commit response if repo has no HEAD
6667
head, _ := repo.Head()
67-
6868
if head == nil {
6969
return nilCommit()
7070
}
@@ -95,6 +95,14 @@ func UnPushedCommits(repo *git.Repository, remoteRef string) []*string {
9595
logger.Log("Commit object is nil", global.StatusError)
9696
return types.Error{Msg: "Commit object is nil"}
9797
}
98+
99+
// If the remote branch has no commits to compare, then the first local commit will be returned
100+
if remoteCommit == nil {
101+
commitString := commitModel(*commit)
102+
logger.Log(fmt.Sprintf("New commit available for pushing to remote -> %s", commitString), global.StatusInfo)
103+
commitArray = append(commitArray, &commitString)
104+
return nil
105+
}
98106
if commit.Hash == remoteCommit.Hash {
99107
logger.Log(fmt.Sprintf("Same commits found in remote and local trees -> %s", commit.Hash.String()), global.StatusInfo)
100108
return types.Error{Msg: "Same commit"}

0 commit comments

Comments
 (0)