-
Notifications
You must be signed in to change notification settings - Fork 3
/
commit_view.go
134 lines (118 loc) · 3.02 KB
/
commit_view.go
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package db
import (
"database/sql"
"time"
"github.com/knoebber/dotfile/dotfile"
"github.com/pkg/errors"
)
// CommitSummary summarizes a commit.
type CommitSummary struct {
Hash string
Message string
Current bool
Timestamp int64
DateString string
ForkedFromUsername *string // The owner of the file that this commit was forked from.
// TODO ForkedFromAlias - for the case that new owner changes the alias, link breaks.
}
// CommitView is used for an individual commit view.
type CommitView struct {
CommitSummary
Path string
Content []byte
}
// CommitList gets a summary of all commits for a file.
func CommitList(e Executor, username, alias string, timezone *string) ([]CommitSummary, error) {
var (
forkedFrom *int64
result []CommitSummary
)
rows, err := e.Query(`
SELECT hash,
forked_from,
message,
current_commit_id = commits.id AS current,
timestamp
FROM commits
JOIN files ON commits.file_id = files.id
JOIN users ON files.user_id = users.id
WHERE username = ? AND alias = ?
ORDER BY timestamp DESC
`, username, alias)
if err != nil {
return nil, errors.Wrapf(err, "querying commits for user %q file %q", username, alias)
}
defer rows.Close()
for rows.Next() {
c := CommitSummary{}
if err := rows.Scan(
&c.Hash,
&forkedFrom,
&c.Message,
&c.Current,
&c.Timestamp,
); err != nil {
return nil, errors.Wrapf(err, "scanning commits for user %q file %q", username, alias)
}
c.DateString = formatTime(time.Unix(c.Timestamp, 0), timezone)
if forkedFrom != nil {
username, err := usernameFromCommitID(e, *forkedFrom)
if err != nil {
return nil, err
}
c.ForkedFromUsername = &username
}
result = append(result, c)
}
if len(result) == 0 {
return nil, sql.ErrNoRows
}
return result, nil
}
// UncompressCommit gets a commit and uncompresses its contents.
func UncompressCommit(e Executor, username, alias, hash string, timezone *string) (*CommitView, error) {
var (
forkedFrom *int64
revision []byte
)
result := new(CommitView)
err := e.QueryRow(`
SELECT hash,
forked_from,
message,
path,
current_commit_id = commits.id AS current,
revision,
timestamp
FROM commits
JOIN files ON commits.file_id = files.id
JOIN users ON files.user_id = users.id
WHERE username = ? AND alias = ? AND hash = ?
`, username, alias, hash).
Scan(
&result.Hash,
&forkedFrom,
&result.Message,
&result.Path,
&result.Current,
&revision,
&result.Timestamp,
)
if err != nil {
return nil, errors.Wrapf(err, "querying for uncompressed %q %q %q", username, alias, hash)
}
result.DateString = formatTime(time.Unix(result.Timestamp, 0), timezone)
if forkedFrom != nil {
username, err := usernameFromCommitID(e, *forkedFrom)
if err != nil {
return nil, err
}
result.ForkedFromUsername = &username
}
uncompressed, err := dotfile.Uncompress(revision)
if err != nil {
return nil, err
}
result.Content = uncompressed.Bytes()
return result, nil
}