💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Gemigit › files › decfa5d8bf03f5592de61e643a… captured on 2022-07-16 at 17:09:40. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

0 package main

1

2 import (

3 "gemigit/db"

4 "gemigit/repo"

5 "io"

6 "log"

7 "strconv"

8

9 "gemigit/config"

10

11 "github.com/go-git/go-git/v5/plumbing/object"

12 "github.com/pitr/gig"

13 )

14

15 func getHttpAddress(user string, repo string) string {

16 ret := "git clone "

17 if config.Cfg.Gemigit.Https {

18 ret += "https://"

19 } else {

20 ret += "http://"

21 }

22 ret += config.Cfg.Gemigit.Domain + "/" + user + "/" + repo + "\n"

23 return ret

24 }

25

26 func showRepoHeader(user string, reponame string, owner bool) (string, error) {

27 ret := ""

28 if owner {

29 ret += "=>/account Go back\n\n"

30 } else {

31 ret += "=>/repo Go back\n\n"

32 }

33 ret += "# " + reponame

34 if !owner {

35 ret += " by " + user + "\n=>/repo/" + user + " View account\n"

36 } else {

37 ret += "\n"

38 }

39 desc, err := db.GetRepoDesc(reponame, user)

40 if err != nil {

41 return "", err

42 }

43 if desc != "" {

44 ret += "> " + desc + "\n"

45 }

46 ret += "> " + getHttpAddress(user, reponame)

47 if owner {

48 ret += "\n"

49 ret += "=>/account/repo/" + reponame + "/chname Change repository name\n"

50 ret += "=>/account/repo/" + reponame + "/chdesc Change repository description\n"

51 ret += "=>/account/repo/" + reponame + "/delrepo Delete repository\n"

52 ret += "=>/account/repo/" + reponame + "/togglepublic Make the repository "

53 b, err := db.IsRepoPublic(reponame, user)

54 if err != nil {

55 return "", err

56 }

57 if b {

58 ret += "private\n\n"

59 } else {

60 ret += "public\n\n"

61 }

62 ret += "=>/account/repo/" + reponame + " Log\n"

63 ret += "=>/account/repo/" + reponame + "/files Files\n"

64 file, err := repo.GetFile(reponame, user, "LICENSE")

65 if file != nil && err == nil {

66 ret += "=>/account/repo/" + reponame + "/license License\n"

67 }

68 file, err = repo.GetFile(reponame, user, "README")

69 if file != nil && err == nil {

70 ret += "=>/account/repo/" + reponame + "/readme Readme\n"

71 }

72 } else {

73 ret += "\n=>/repo/" + user + "/" + reponame + " Log\n"

74 ret += "=>/repo/" + user + "/" + reponame + "/files Files\n"

75 file, err := repo.GetFile(reponame, user, "LICENSE")

76 if file != nil && err == nil {

77 ret += "=>/repo/" + user + "/" + reponame + "/license License\n"

78 }

79 file, err = repo.GetFile(reponame, user, "README")

80 if file != nil && err == nil {

81 ret += "=>/repo/" + user + "/" + reponame + "/readme Readme\n"

82 }

83 }

84

85 return ret, nil

86 }

87

88 func showRepoFiles(user string, reponame string, owner bool) (string, error) {

89 files, err := repo.GetFiles(reponame, user)

90 if err != nil {

91 log.Println(err.Error())

92 }

93 ret := "\n## Files\n\n"

94 if files != nil {

95 err = files.ForEach(func(f *object.File) error {

96 if owner {

97 ret += "=>/account/repo/"

98 } else {

99 ret += "=>/repo/" + user + "/"

100 }

101 ret += reponame + "/files/" + f.Blob.Hash.String() + " " + f.Mode.String() + " " + f.Name + " " + strconv.Itoa(int(f.Size)) + "\n"

102 return nil

103 })

104 if err != nil {

105 return "", err

106 }

107 } else {

108 ret += "Empty repository\n"

109 }

110 return ret, nil

111 }

112

113 func showRepoCommits(user string, reponame string) (string, error) {

114 commits, err := repo.GetCommits(reponame, user)

115 if err != nil {

116 return "", err

117 }

118 ret := "\n## Commits\n\n"

119 if commits != nil {

120 err = commits.ForEach(func(c *object.Commit) error {

121 ret += "* " + c.Hash.String() + ", by " + c.Author.Name + " on " + c.Author.When.Format("2006-01-02 15:04:05") + "\n"

122 ret += "> " + c.Message + "\n"

123 return nil

124 })

125 if err != nil {

126 return "", err

127 }

128 } else {

129 ret += "Empty repository\n"

130 }

131 return ret, nil

132 }

133

134 func showRepoFile(user string, reponame string, file string) (string, error) {

135 out, err := repo.GetFile(reponame, user, file)

136 if err != nil {

137 return "", err

138 }

139 reader, err := out.Reader()

140 if err != nil {

141 return "", err

142 }

143 buf, err := io.ReadAll(reader)

144 if err != nil {

145 return "", err

146 }

147 return string(buf), nil

148 }

149

150 func showLicense(user string, reponame string) (string, error) {

151 ret := "\n## License\n\n"

152 file, err := showRepoFile(user, reponame, "LICENSE")

153 if err != nil {

154 return "", err

155 }

156 ret += file

157 return ret, nil

158 }

159

160 func showReadme(user string, reponame string) (string, error) {

161 ret := "\n## Readme\n\n"

162 file, err := showRepoFile(user, reponame, "README")

163 if err != nil {

164 return "", err

165 }

166 ret += file

167 return ret, nil

168 }

169

170 func repoRequest(c gig.Context, param string, owner bool) error {

171 username := ""

172 if owner {

173 user, exist := db.GetUser(c.CertHash())

174 if !exist {

175 return c.NoContent(gig.StatusBadRequest, "Invalid username")

176 }

177 username = user.Name

178 } else {

179 username = c.Param("user")

180 }

181 ret, err := showRepoHeader(username, c.Param("repo"), owner)

182 if err != nil {

183 return c.NoContent(gig.StatusBadRequest, err.Error())

184 }

185

186 var buf string

187 switch param {

188 case "":

189 buf, err = showRepoCommits(username, c.Param("repo"))

190 case "files":

191 buf, err = showRepoFiles(username, c.Param("repo"), owner)

192 case "license":

193 buf, err = showLicense(username, c.Param("repo"))

194 case "readme":

195 buf, err = showReadme(username, c.Param("repo"))

196 }

197 if err != nil {

198 return c.NoContent(gig.StatusBadRequest, err.Error())

199 }

200 ret += buf

201 return c.Gemini(ret)

202 }

203