Skip to content

Commit

Permalink
replace os.exit by error checks
Browse files Browse the repository at this point in the history
  • Loading branch information
iignatevich committed Nov 21, 2023
1 parent 1527817 commit 7ba44bf
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
19 changes: 14 additions & 5 deletions bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ var unversionableFiles = map[string]bool{
"README.md": true,
}

// CheckIfError should be used to naively panics if an error is not nil.
func CheckIfError(err error) {
// PromptError prints an error.
func PromptError(err error) {
if err == nil {
return
}

fmt.Printf("\x1b[31;1m%s\x1b[0m\n", fmt.Sprintf("error: %s\n", err))
os.Exit(1)
}

// Resource represents a ansible resource
Expand Down Expand Up @@ -159,7 +158,11 @@ func (k *bumpUpdatedService) ServiceInfo() launchr.ServiceInfo {

func (k *bumpUpdatedService) Bump() error {
fmt.Println("Bump updated versions...")
git := getRepo()
git, err := getRepo()
if err != nil {
return err
}

if git.IsOwnCommit() {
fmt.Println("Skipping bump, as the latest commit is already by the bumper tool.")
return nil
Expand All @@ -176,7 +179,13 @@ func (k *bumpUpdatedService) Bump() error {
return nil
}

_, err = k.updateResources(resources, git.GetLastCommitShortHash())
version, err := git.GetLastCommitShortHash()
if err != nil {
fmt.Println("Can't retrieve commit hash")
return err
}

_, err = k.updateResources(resources, version)
if err != nil {
fmt.Println("There is an error during resources update")
return err
Expand Down
45 changes: 32 additions & 13 deletions git.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,73 @@ type BumperRepo struct {
commitMessage string
}

func getRepo() *BumperRepo {
func getRepo() (*BumperRepo, error) {
r, err := git.PlainOpen("./")
CheckIfError(err)
if err != nil {
return nil, err
}

return &BumperRepo{
git: r,
name: "Bumper",
mail: "[email protected]",
commitMessage: "versions bump",
}
}, nil
}

// IsOwnCommit checks if the latest commit in the Git repository was made by the bumper.
func (r *BumperRepo) IsOwnCommit() bool {
ref, err := r.git.Head()
CheckIfError(err)
if err != nil {
PromptError(err)
return false
}

commit, err := r.git.CommitObject(ref.Hash())
CheckIfError(err)
if err != nil {
PromptError(err)
return false
}

return r.name == commit.Author.Name && r.mail == commit.Author.Email
}

// GetLastCommitShortHash gets the short hash of the latest commit in the Git repository.
func (r *BumperRepo) GetLastCommitShortHash() string {
func (r *BumperRepo) GetLastCommitShortHash() (string, error) {
ref, err := r.git.Head()
CheckIfError(err)
if err != nil {
return "", err
}

return ref.Hash().String()[:13]
return ref.Hash().String()[:13], nil
}

// getLatestModifiedFiles gets a list of files modified in the latest commit in the Git repository.
func (r *BumperRepo) getLatestModifiedFiles() ([]string, error) {
var modifiedFiles []string

headRef, err := r.git.Head()
CheckIfError(err)
if err != nil {
return nil, err
}

headCommit, err := r.git.CommitObject(headRef.Hash())
CheckIfError(err)
if err != nil {
return nil, err
}

headTree, _ := headCommit.Tree()

parentCommit, err := headCommit.Parent(0)
CheckIfError(err)
parentTree, _ := parentCommit.Tree()
if err != nil {
return nil, err
}

parentTree, _ := parentCommit.Tree()
diff, err := parentTree.Diff(headTree)
CheckIfError(err)
if err != nil {
return nil, err
}

for _, ch := range diff {
action, _ := ch.Action()
Expand Down

0 comments on commit 7ba44bf

Please sign in to comment.