77 lines
2.2 KiB
Go
77 lines
2.2 KiB
Go
package gitcmd
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
func (r *Repository) refSHA(ref string) (string, error) {
|
|
out, _, err := NewCommand("rev-parse", "--verify", "--quiet").
|
|
AddDynamicArguments(ref).
|
|
WithDir(r.Path).RunStdString(r.ctx)
|
|
if err != nil {
|
|
return "", nil
|
|
}
|
|
return strings.TrimSpace(out), nil
|
|
}
|
|
|
|
func (r *Repository) hashObject(content, dir string) (string, error) {
|
|
out, _, err := NewCommand("hash-object", "-w", "--stdin").
|
|
WithStdin(strings.NewReader(content)).
|
|
WithDir(dir).RunStdString(r.ctx)
|
|
if err != nil {
|
|
return "", fmt.Errorf("hash-object: %w", err)
|
|
}
|
|
return strings.TrimSpace(out), nil
|
|
}
|
|
|
|
func (r *Repository) commitTree(tree, message, authorName, authorEmail, dir string, parents ...string) (string, error) {
|
|
cmd := NewCommand("commit-tree").AddDynamicArguments(tree).WithDir(dir)
|
|
for _, p := range parents {
|
|
cmd.Add("-p").AddDynamicArguments(p)
|
|
}
|
|
cmd.WithEnv(
|
|
"GIT_AUTHOR_NAME="+authorName,
|
|
"GIT_AUTHOR_EMAIL="+authorEmail,
|
|
"GIT_COMMITTER_NAME="+authorName,
|
|
"GIT_COMMITTER_EMAIL="+authorEmail,
|
|
).WithStdin(strings.NewReader(message))
|
|
out, _, err := cmd.RunStdString(r.ctx)
|
|
if err != nil {
|
|
return "", fmt.Errorf("commit-tree: %w", err)
|
|
}
|
|
return strings.TrimSpace(out), nil
|
|
}
|
|
|
|
func (r *Repository) updateRef(ref, newSHA, expectedSHA string) error {
|
|
cmd := NewCommand("update-ref").AddDynamicArguments(ref, newSHA).WithDir(r.Path)
|
|
if expectedSHA != "" {
|
|
cmd.AddDynamicArguments(expectedSHA)
|
|
}
|
|
if _, _, err := cmd.RunStdString(r.ctx); err != nil {
|
|
return fmt.Errorf("update-ref %s: %w", ref, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *Repository) isAncestor(ancestor, descendant string) bool {
|
|
err := NewCommand("merge-base", "--is-ancestor").
|
|
AddDynamicArguments(ancestor, descendant).
|
|
WithDir(r.Path).Run(r.ctx)
|
|
return err == nil
|
|
}
|
|
|
|
func (r *Repository) removePathFromIndex(dir, path string, env ...string) error {
|
|
entry := "0 " + strings.Repeat("0", 40) + "\t" + path + "\x00"
|
|
cmd := NewCommand("update-index", "--remove", "-z", "--index-info").
|
|
WithStdin(strings.NewReader(entry)).
|
|
WithDir(dir)
|
|
if len(env) > 0 {
|
|
cmd.WithEnv(env...)
|
|
}
|
|
if _, _, err := cmd.RunStdString(r.ctx); err != nil {
|
|
return fmt.Errorf("update-index --index-info (remove): %w", err)
|
|
}
|
|
return nil
|
|
}
|