forked from toolshed/abra
		
	| @ -1,8 +1,9 @@ | ||||
| package git | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"errors" | ||||
|  | ||||
| 	"coopcloud.tech/abra/pkg/i18n" | ||||
| 	"coopcloud.tech/abra/pkg/log" | ||||
| 	"github.com/go-git/go-git/v5" | ||||
| 	"github.com/go-git/go-git/v5/plumbing" | ||||
| @ -13,28 +14,28 @@ import ( | ||||
| func Init(repoPath string, commit bool, gitName, gitEmail string) error { | ||||
| 	repo, err := git.PlainInit(repoPath, false) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("git init: %s", err) | ||||
| 		return errors.New(i18n.G("git init: %s", err)) | ||||
| 	} | ||||
|  | ||||
| 	if err = SwitchToMain(repo); err != nil { | ||||
| 		return fmt.Errorf("git branch rename: %s", err) | ||||
| 		return errors.New(i18n.G("git branch rename: %s", err)) | ||||
| 	} | ||||
|  | ||||
| 	log.Debugf("initialised new git repo in %s", repoPath) | ||||
| 	log.Debug(i18n.G("initialised new git repo in %s", repoPath)) | ||||
|  | ||||
| 	if commit { | ||||
| 		commitRepo, err := git.PlainOpen(repoPath) | ||||
| 		if err != nil { | ||||
| 			return fmt.Errorf("git open: %s", err) | ||||
| 			return errors.New(i18n.G("git open: %s", err)) | ||||
| 		} | ||||
|  | ||||
| 		commitWorktree, err := commitRepo.Worktree() | ||||
| 		if err != nil { | ||||
| 			return fmt.Errorf("git worktree: %s", err) | ||||
| 			return errors.New(i18n.G("git worktree: %s", err)) | ||||
| 		} | ||||
|  | ||||
| 		if err := commitWorktree.AddWithOptions(&git.AddOptions{All: true}); err != nil { | ||||
| 			return fmt.Errorf("git add: %s", err) | ||||
| 			return errors.New(i18n.G("git add: %s", err)) | ||||
| 		} | ||||
|  | ||||
| 		var author *object.Signature | ||||
| @ -43,10 +44,10 @@ func Init(repoPath string, commit bool, gitName, gitEmail string) error { | ||||
| 		} | ||||
|  | ||||
| 		if _, err = commitWorktree.Commit("init", &git.CommitOptions{Author: author}); err != nil { | ||||
| 			return fmt.Errorf("git commit: %s", err) | ||||
| 			return errors.New(i18n.G("git commit: %s", err)) | ||||
| 		} | ||||
|  | ||||
| 		log.Debugf("init committed all files for new git repo in %s", repoPath) | ||||
| 		log.Debug(i18n.G("init committed all files for new git repo in %s", repoPath)) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| @ -56,20 +57,20 @@ func Init(repoPath string, commit bool, gitName, gitEmail string) error { | ||||
| func SwitchToMain(repo *git.Repository) error { | ||||
| 	ref := plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.ReferenceName("refs/heads/main")) | ||||
| 	if err := repo.Storer.SetReference(ref); err != nil { | ||||
| 		return fmt.Errorf("set reference: %s", err) | ||||
| 		return errors.New(i18n.G("set reference: %s", err)) | ||||
| 	} | ||||
|  | ||||
| 	cfg, err := repo.Config() | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("repo config: %s", err) | ||||
| 		return errors.New(i18n.G("repo config: %s", err)) | ||||
| 	} | ||||
|  | ||||
| 	cfg.Init.DefaultBranch = "main" | ||||
| 	if err := repo.SetConfig(cfg); err != nil { | ||||
| 		return fmt.Errorf("repo set config: %s", err) | ||||
| 		return errors.New(i18n.G("repo set config: %s", err)) | ||||
| 	} | ||||
|  | ||||
| 	log.Debug("set 'main' as the default branch") | ||||
| 	log.Debug(i18n.G("set 'main' as the default branch")) | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user