browse.go

  1package repository
  2
  3import (
  4	"fmt"
  5	"io"
  6	"strconv"
  7	"time"
  8)
  9
 10// ChangeStatus describes how a file was affected by a commit.
 11type ChangeStatus string
 12
 13const (
 14	ChangeStatusAdded    ChangeStatus = "added"
 15	ChangeStatusModified ChangeStatus = "modified"
 16	ChangeStatusDeleted  ChangeStatus = "deleted"
 17	ChangeStatusRenamed  ChangeStatus = "renamed"
 18)
 19
 20func (s ChangeStatus) MarshalGQL(w io.Writer) {
 21	switch s {
 22	case ChangeStatusAdded:
 23		fmt.Fprint(w, strconv.Quote("ADDED"))
 24	case ChangeStatusModified:
 25		fmt.Fprint(w, strconv.Quote("MODIFIED"))
 26	case ChangeStatusDeleted:
 27		fmt.Fprint(w, strconv.Quote("DELETED"))
 28	case ChangeStatusRenamed:
 29		fmt.Fprint(w, strconv.Quote("RENAMED"))
 30	default:
 31		panic(fmt.Sprintf("unknown ChangeStatus value %q", string(s)))
 32	}
 33}
 34
 35func (s *ChangeStatus) UnmarshalGQL(v any) error {
 36	str, ok := v.(string)
 37	if !ok {
 38		return fmt.Errorf("enums must be strings")
 39	}
 40	switch str {
 41	case "ADDED":
 42		*s = ChangeStatusAdded
 43	case "MODIFIED":
 44		*s = ChangeStatusModified
 45	case "DELETED":
 46		*s = ChangeStatusDeleted
 47	case "RENAMED":
 48		*s = ChangeStatusRenamed
 49	default:
 50		return fmt.Errorf("%q is not a valid ChangeStatus", str)
 51	}
 52	return nil
 53}
 54
 55// DiffLineType is the role of a line within a unified diff hunk.
 56type DiffLineType string
 57
 58const (
 59	DiffLineContext DiffLineType = "context"
 60	DiffLineAdded   DiffLineType = "added"
 61	DiffLineDeleted DiffLineType = "deleted"
 62)
 63
 64func (t DiffLineType) MarshalGQL(w io.Writer) {
 65	switch t {
 66	case DiffLineContext:
 67		fmt.Fprint(w, strconv.Quote("CONTEXT"))
 68	case DiffLineAdded:
 69		fmt.Fprint(w, strconv.Quote("ADDED"))
 70	case DiffLineDeleted:
 71		fmt.Fprint(w, strconv.Quote("DELETED"))
 72	default:
 73		panic(fmt.Sprintf("unknown DiffLineType value %q", string(t)))
 74	}
 75}
 76
 77func (t *DiffLineType) UnmarshalGQL(v any) error {
 78	str, ok := v.(string)
 79	if !ok {
 80		return fmt.Errorf("enums must be strings")
 81	}
 82	switch str {
 83	case "CONTEXT":
 84		*t = DiffLineContext
 85	case "ADDED":
 86		*t = DiffLineAdded
 87	case "DELETED":
 88		*t = DiffLineDeleted
 89	default:
 90		return fmt.Errorf("%q is not a valid DiffLineType", str)
 91	}
 92	return nil
 93}
 94
 95// CommitMeta holds the metadata for a single commit, suitable for listing.
 96type CommitMeta struct {
 97	Hash        Hash
 98	Message     string
 99	AuthorName  string
100	AuthorEmail string
101	Date        time.Time
102	Parents     []Hash
103}
104
105// ChangedFile describes a file that was modified in a commit.
106type ChangedFile struct {
107	Path    string
108	OldPath *string // non-nil for renames
109	Status  ChangeStatus
110}
111
112// CommitDetail extends CommitMeta with the full message and the list of
113// changed files (relative to the first parent).
114type CommitDetail struct {
115	CommitMeta
116	FullMessage string
117	Files       []ChangedFile
118}
119
120// DiffLine represents one line in a unified diff hunk.
121type DiffLine struct {
122	Type    DiffLineType
123	Content string
124	OldLine int
125	NewLine int
126}
127
128// DiffHunk is a contiguous block of changes in a unified diff.
129type DiffHunk struct {
130	OldStart int
131	OldLines int
132	NewStart int
133	NewLines int
134	Lines    []DiffLine
135}
136
137// FileDiff is the diff for a single file in a commit.
138type FileDiff struct {
139	Path     string
140	OldPath  *string // non-nil for renames
141	IsBinary bool
142	IsNew    bool
143	IsDelete bool
144	Hunks    []DiffHunk
145}
146
147// BranchInfo describes a local branch returned by RepoBrowse.Branches.
148type BranchInfo struct {
149	Name      string
150	Hash      Hash // commit hash
151	IsDefault bool // true for the branch HEAD points to
152}
153
154// TagInfo describes a tag returned by RepoBrowse.Tags.
155type TagInfo struct {
156	Name string
157	// Hash is always the target commit hash.  For annotated tags the tag
158	// object is dereferenced; for lightweight tags this is the ref hash.
159	Hash Hash
160}