Skip to content

Commit ac3ba4c

Browse files
committed
only add empty commit when empty repo
1 parent 10a205b commit ac3ba4c

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

pkg/core/core.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,25 @@ func (c *Core) initCalendarRepo(name string) (*gogit.Repository, error) {
231231
return nil, err
232232
}
233233

234-
if err := firstCommit(repo); err != nil {
235-
return nil, fmt.Errorf("initial commit failed: %w", err)
234+
// create initial commit if the repository has no commits yet
235+
if err := ensureInitialCommit(repo); err != nil {
236+
return nil, fmt.Errorf("ensure initial commit failed: %w", err)
236237
}
238+
237239
return repo, nil
238240
}
239241

240-
func firstCommit(repo *gogit.Repository) error {
242+
// ensureInitialCommit creates an empty initial commit only if none exist.
243+
func ensureInitialCommit(repo *gogit.Repository) error {
244+
_, err := repo.Head()
245+
if err == nil {
246+
// HEAD exists -> repo already has commits
247+
return nil
248+
}
249+
if !errors.Is(err, plumbing.ErrReferenceNotFound) {
250+
return fmt.Errorf("check head: %w", err)
251+
}
252+
241253
wt, err := repo.Worktree()
242254
if err != nil {
243255
return err

0 commit comments

Comments
 (0)