|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"dehub.dev/src/dehub.git"
|
|
|
|
"dehub.dev/src/dehub.git/cmd/dehub/dcmd"
|
|
|
|
|
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func cmdCommit(ctx context.Context, cmd *dcmd.Cmd) {
|
|
|
|
flag := cmd.FlagSet()
|
|
|
|
accountID := flag.String("account-id", "", "Account to accredit commit with")
|
|
|
|
|
|
|
|
var repo repo
|
|
|
|
repo.initFlags(flag)
|
|
|
|
|
|
|
|
accreditAndCommit := func(commit dehub.Commit) error {
|
|
|
|
|
|
|
|
cfg, err := repo.LoadConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var account dehub.Account
|
|
|
|
var ok bool
|
|
|
|
for _, account = range cfg.Accounts {
|
|
|
|
if account.ID == *accountID {
|
|
|
|
ok = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("account ID %q not found in config", *accountID)
|
|
|
|
} else if l := len(account.Signifiers); l == 0 || l > 1 {
|
|
|
|
return 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 fmt.Errorf("casting %#v to SignifierInterface: %w", sig, err)
|
|
|
|
|
|
|
|
} else if commit, err = repo.AccreditCommit(commit, sigInt); err != nil {
|
|
|
|
return fmt.Errorf("accrediting commit: %w", err)
|
|
|
|
}
|
|
|
|
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
4 years ago
|
|
|
gitCommit, err := repo.Commit(commit, *accountID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("committing to git: %w", err)
|
|
|
|
}
|
|
|
|
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
4 years ago
|
|
|
fmt.Printf("committed to HEAD as %s\n", gitCommit.GitCommit.Hash)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var hasStaged bool
|
|
|
|
body := func() (context.Context, error) {
|
|
|
|
if *accountID == "" {
|
|
|
|
return nil, errors.New("-account-id is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := repo.openRepo(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if hasStaged, err = repo.HasStagedChanges(); err != nil {
|
|
|
|
return nil, fmt.Errorf("determining if any changes have been staged: %w", err)
|
|
|
|
}
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.SubCmd("change", "Commit file changes",
|
|
|
|
func(ctx context.Context, cmd *dcmd.Cmd) {
|
|
|
|
flag := cmd.FlagSet()
|
|
|
|
msg := flag.String("msg", "", "Commit message")
|
|
|
|
cmd.Run(func() (context.Context, error) {
|
|
|
|
if !hasStaged {
|
|
|
|
return nil, errors.New("no changes have been staged for commit")
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
commit, err := repo.NewCommitChange(*msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not construct change commit: %w", err)
|
|
|
|
|
|
|
|
} else if err := accreditAndCommit(commit); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
cmd.SubCmd("credential", "Commit credential of one or more change commits",
|
|
|
|
func(ctx context.Context, cmd *dcmd.Cmd) {
|
|
|
|
flag := cmd.FlagSet()
|
|
|
|
startRev := flag.String("start", "", "Revision of the starting commit to accredit (when accrediting a range of changes)")
|
|
|
|
endRev := flag.String("end", "HEAD", "Revision of the ending commit to accredit (when accrediting a range of changes)")
|
|
|
|
rev := flag.String("rev", "", "Revision of commit to accredit (when accrediting a single commit)")
|
|
|
|
cmd.Run(func() (context.Context, error) {
|
|
|
|
if *rev == "" && *startRev == "" {
|
|
|
|
return nil, errors.New("-rev or -start is required")
|
|
|
|
} else if hasStaged {
|
|
|
|
return nil, errors.New("credential commit cannot have any files changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
var credCommit dehub.Commit
|
|
|
|
if *rev != "" {
|
|
|
|
gitCommit, err := repo.GetGitRevision(plumbing.Revision(*rev))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("resolving revision %q: %w", *rev, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gitCommits := []dehub.GitCommit{gitCommit}
|
|
|
|
if credCommit, err = repo.NewCommitCredentialFromChanges(gitCommits); err != nil {
|
|
|
|
return nil, fmt.Errorf("constructing credential commit: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gitCommits, err := repo.GetGitRevisionRange(
|
|
|
|
plumbing.Revision(*startRev),
|
|
|
|
plumbing.Revision(*endRev),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("resolving revisions %q to %q: %w",
|
|
|
|
*startRev, *endRev, err)
|
|
|
|
} else if credCommit, err = repo.NewCommitCredentialFromChanges(gitCommits); err != nil {
|
|
|
|
return nil, fmt.Errorf("constructing credential commit: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := accreditAndCommit(credCommit); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
cmd.SubCmd("comment", "Commit a comment to a branch",
|
|
|
|
func(ctx context.Context, cmd *dcmd.Cmd) {
|
|
|
|
flag := cmd.FlagSet()
|
|
|
|
msg := flag.String("msg", "", "Comment message")
|
|
|
|
cmd.Run(func() (context.Context, error) {
|
|
|
|
if *msg == "" {
|
|
|
|
var err error
|
|
|
|
if *msg, err = tmpFileMsg(); err != nil {
|
|
|
|
return nil, fmt.Errorf("collecting comment message from user: %w", err)
|
|
|
|
|
|
|
|
} else if *msg == "" {
|
|
|
|
return nil, errors.New("empty comment message, not doing anything")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
commit, err := repo.NewCommitComment(*msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("constructing comment commit: %w", err)
|
|
|
|
}
|
|
|
|
return nil, accreditAndCommit(commit)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
cmd.Run(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdCombine(ctx context.Context, cmd *dcmd.Cmd) {
|
|
|
|
flag := cmd.FlagSet()
|
|
|
|
onto := flag.String("onto", "", "Branch the new commit should be put onto")
|
|
|
|
startRev := flag.String("start", "", "Revision of the starting commit to combine")
|
|
|
|
endRev := flag.String("end", "", "Revision of the ending commit to combine")
|
|
|
|
|
|
|
|
var repo repo
|
|
|
|
repo.initFlags(flag)
|
|
|
|
|
|
|
|
cmd.Run(func() (context.Context, error) {
|
|
|
|
if *onto == "" ||
|
|
|
|
*startRev == "" ||
|
|
|
|
*endRev == "" {
|
|
|
|
return nil, errors.New("-onto, -start, and -end are required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := repo.openRepo(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
commits, err := repo.GetGitRevisionRange(
|
|
|
|
plumbing.Revision(*startRev),
|
|
|
|
plumbing.Revision(*endRev),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error getting commits %q to %q: %w",
|
|
|
|
*startRev, *endRev, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ontoBranch := plumbing.NewBranchReferenceName(*onto)
|
|
|
|
gitCommit, err := repo.CombineCommitChanges(commits, ontoBranch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("new commit %q added to branch %q\n",
|
|
|
|
gitCommit.GitCommit.Hash, ontoBranch.Short())
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
}
|