Welcome to mirror list, hosted at ThFree Co, Russian Federation.

revert.go « git2go « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ca11169bf3e40e7d4a9bad7f30e651765a2fd7af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package git2go

import (
	"context"
	"errors"
	"fmt"
	"io"
	"time"

	"gitlab.com/gitlab-org/gitaly/internal/gitaly/config"
)

// RevertResult contains results from a revert.
type RevertResult struct {
	// CommitID is the object ID of the generated revert commit.
	CommitID string `json:"commit_id"`
}

// SerializeTo serializes the revert result and writes it into the writer.
func (r RevertResult) SerializeTo(w io.Writer) error {
	return serializeTo(w, r)
}

type RevertCommand struct {
	// Repository is the path to execute the revert in.
	Repository string `json:"repository"`
	// AuthorName is the author name of revert commit.
	AuthorName string `json:"author_name"`
	// AuthorMail is the author mail of revert commit.
	AuthorMail string `json:"author_mail"`
	// AuthorDate is the author date of revert commit.
	AuthorDate time.Time `json:"author_date"`
	// Message is the message to be used for the revert commit.
	Message string `json:"message"`
	// Ours is the commit that the revert is applied to.
	Ours string `json:"ours"`
	// Revert is the commit to be reverted.
	Revert string `json:"revert"`
	// Mainline is the parent to be considered the mainline
	Mainline uint `json:"mainline"`
}

func (r RevertCommand) Run(ctx context.Context, cfg config.Cfg) (RevertResult, error) {
	if err := r.verify(); err != nil {
		return RevertResult{}, fmt.Errorf("revert: %w: %s", ErrInvalidArgument, err.Error())
	}

	serialized, err := serialize(r)
	if err != nil {
		return RevertResult{}, err
	}

	stdout, err := run(ctx, cfg, "revert", serialized)
	if err != nil {
		return RevertResult{}, err
	}

	var response RevertResult
	if err := deserialize(stdout, &response); err != nil {
		return RevertResult{}, err
	}

	return response, nil
}

func (r RevertCommand) verify() error {
	if r.Repository == "" {
		return errors.New("missing repository")
	}
	if r.AuthorName == "" {
		return errors.New("missing author name")
	}
	if r.AuthorMail == "" {
		return errors.New("missing author mail")
	}
	if r.Message == "" {
		return errors.New("missing message")
	}
	if r.Ours == "" {
		return errors.New("missing ours")
	}
	if r.Revert == "" {
		return errors.New("missing revert")
	}
	return nil
}

// RevertCommandFromSerialized deserializes the revert request from its JSON representation encoded with base64.
func RevertCommandFromSerialized(serialized string) (RevertCommand, error) {
	var request RevertCommand
	if err := deserialize(serialized, &request); err != nil {
		return RevertCommand{}, err
	}

	if err := request.verify(); err != nil {
		return RevertCommand{}, fmt.Errorf("revert: %w: %s", ErrInvalidArgument, err.Error())
	}

	return request, nil
}