1package git2go
2
3import (
4	"bytes"
5	"context"
6	"encoding/gob"
7	"fmt"
8
9	"gitlab.com/gitlab-org/gitaly/v14/internal/git"
10	"gitlab.com/gitlab-org/gitaly/v14/internal/git/repository"
11)
12
13// IndexError is an error that was produced by performing an invalid operation on the index.
14type IndexError string
15
16// Error returns the error message of the index error.
17func (err IndexError) Error() string { return string(err) }
18
19// InvalidArgumentError is returned when an invalid argument is provided.
20type InvalidArgumentError string
21
22func (err InvalidArgumentError) Error() string { return string(err) }
23
24// FileNotFoundError is returned when an action attempts to operate on a non-existing file.
25type FileNotFoundError string
26
27func (err FileNotFoundError) Error() string {
28	return fmt.Sprintf("file not found: %q", string(err))
29}
30
31// FileExistsError is returned when an action attempts to overwrite an existing file.
32type FileExistsError string
33
34func (err FileExistsError) Error() string {
35	return fmt.Sprintf("file exists: %q", string(err))
36}
37
38// DirectoryExistsError is returned when an action attempts to overwrite a directory.
39type DirectoryExistsError string
40
41func (err DirectoryExistsError) Error() string {
42	return fmt.Sprintf("directory exists: %q", string(err))
43}
44
45// CommitParams contains the information and the steps to build a commit.
46type CommitParams struct {
47	// Repository is the path of the repository to operate on.
48	Repository string
49	// Author is the author of the commit.
50	Author Signature
51	// Committer is the committer of the commit.
52	Committer Signature
53	// Message is message of the commit.
54	Message string
55	// Parent is the OID of the commit to use as the parent of this commit.
56	Parent string
57	// Actions are the steps to build the commit.
58	Actions []Action
59}
60
61// Commit builds a commit from the actions, writes it to the object database and
62// returns its object id.
63func (b Executor) Commit(ctx context.Context, repo repository.GitRepo, params CommitParams) (git.ObjectID, error) {
64	input := &bytes.Buffer{}
65	if err := gob.NewEncoder(input).Encode(params); err != nil {
66		return "", err
67	}
68
69	output, err := b.run(ctx, repo, input, "commit")
70	if err != nil {
71		return "", err
72	}
73
74	var result Result
75	if err := gob.NewDecoder(output).Decode(&result); err != nil {
76		return "", err
77	}
78
79	if result.Error != nil {
80		return "", result.Error
81	}
82
83	commitID, err := git.NewObjectIDFromHex(result.CommitID)
84	if err != nil {
85		return "", fmt.Errorf("could not parse commit ID: %w", err)
86	}
87
88	return commitID, nil
89}
90