A read-only clone of the dehub project, for until dehub.dev can be brought back online.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
dehub/cmd/dehub/cmd_commit.go

85 lines
2.2 KiB

package main
import (
"context"
"dehub"
"errors"
"fmt"
"dehub/cmd/dehub/dcmd"
)
func cmdCommit(ctx context.Context, cmd *dcmd.Cmd) {
flag := cmd.FlagSet()
msg := flag.String("msg", "", "Commit message")
accountID := flag.String("account-id", "", "Account to sign commit as")
cmd.Run(func() (context.Context, error) {
repo := ctxRepo(ctx)
// Don't bother checking any of the parameters, especially commit
// message, if there's no staged changes,
hasStaged, err := repo.HasStagedChanges()
if err != nil {
return nil, fmt.Errorf("error determining if any changes have been staged: %w", err)
} else if !hasStaged {
return nil, errors.New("no changes have been staged for commit")
}
if *accountID == "" {
return nil, errors.New("-account-id is required")
}
if *msg == "" {
var err error
if *msg, err = tmpFileMsg(); err != nil {
return nil, fmt.Errorf("error collecting commit message from user: %w", err)
} else if *msg == "" {
return nil, errors.New("empty commit message, not doing anything")
}
}
cfg, err := repo.LoadConfig()
if err != nil {
return nil, err
}
var account dehub.Account
var ok bool
for _, account = range cfg.Accounts {
if account.ID == *accountID {
ok = true
break
}
}
if !ok {
return nil, fmt.Errorf("account ID %q not found in config", *accountID)
} else if l := len(account.Signifiers); l == 0 || l > 1 {
return nil, fmt.Errorf("account %q has %d signifiers, only one is supported right now", *accountID, l)
}
sig := account.Signifiers[0]
sigInt, err := sig.Interface(*accountID)
if err != nil {
return nil, fmt.Errorf("could not cast %+v to SignifierInterface: %w", sig, err)
}
commit, err := repo.NewCommitChange(*msg)
if err != nil {
return nil, fmt.Errorf("could not construct change commit: %w", err)
}
commit, err = repo.AccreditCommit(commit, sigInt)
if err != nil {
return nil, fmt.Errorf("could not accredit commit: %w", err)
}
hash, err := repo.Commit(commit, *accountID)
if err != nil {
return nil, fmt.Errorf("could not commit change commit: %w", err)
}
fmt.Printf("changes committed to HEAD as %s\n", hash)
return nil, nil
})
}