-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgitrepository.go
485 lines (424 loc) · 14.5 KB
/
gitrepository.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package gitrepository
import (
"errors"
"fmt"
"net/url"
"path/filepath"
"regexp"
"strings"
)
// enums: download options
const (
DownloadNone = -1
DownloadFullPackage = iota
DownloadPartialPackage
DownloadSingleFile
DownloadCustomPackage
)
const (
DirectionNone = 0
DirectionUp = iota
DirectionDown
)
// git repository
type GitRepository struct {
TempDir string
SSID string
debugMode bool
Url string // clean url after parse
RawUrl string // user set this dirty url
CloneUrl string
RemoteUrl string // remote url for git [email protected]:username/repo.git
QueryUrl string // for search bar
DirPath string
IsFile bool
Protocol string // https|ssh (scp-style - not supported yet)
Scheme string
Hostname string
RawPath string
Path string // file or folder path in this repository for download
Owner string
Name string // repository name - repo
DummyBranch string // if branch name is empty, use this name
Branch string
IsTagBranch bool // for gitea.com tag based url
ArchiveUrl string // download branch package
FileUrl string // download from single file url
DownloadType int
}
func NewGitRepository(tempDir, ssid, rawUrl, branch string) *GitRepository {
return &GitRepository{
TempDir: tempDir,
SSID: ssid,
debugMode: false,
Url: "",
RawUrl: rawUrl,
CloneUrl: "",
RemoteUrl: "",
QueryUrl: "",
DirPath: "",
IsFile: false,
Protocol: "",
Scheme: "",
Hostname: "",
RawPath: "",
Path: "",
Owner: "",
Name: "",
DummyBranch: "gitd-branch",
Branch: branch,
IsTagBranch: false,
ArchiveUrl: "",
FileUrl: "",
DownloadType: -1,
}
}
// activate debug mode
func (r *GitRepository) ActivateDebugMode() {
r.debugMode = true
}
// activate debug mode
func (r *GitRepository) isDebugModeActive() bool {
return r.debugMode
}
// generate repository dir path
func (r *GitRepository) GetDirPath() string {
branch := r.DummyBranch
if r.Branch != "" {
branch = r.Branch
}
return filepath.Join(r.TempDir, r.SSID, "repository", r.Owner, r.Name, branch)
}
// only https url accepted
/*
https://github.com/<owner>/<repo>
https://github.com/<owner>/<repo>.git -> .git remove
https://github.com/<owner>/<repo>/tree/<branch>/lib -> folder
https://github.com/<owner>/<repo>/blob/<branch>/lib/filesaver.min.js -> single file
https://github.com/<owner>/<repo>/blob/<branch>/internal/url/url.go#L20 -> #L20 removes
https://github.com/<owner>/<repo>/blob/<branch>/internal/url/url.go?deneme=12&obaraks=noway#L20 -> ?deneme=12&obaraks=noway#L20 remove
https://gitlab.com/<owner>/<repo>
https://gitlab.com/<owner>/<repo>.git
https://gitlab.com/<owner>/<repo>/-/tree/<branch>/config -> folder
https://gitlab.com/<owner>/<repo>/-/blob/<branch>/config/knative/api_groups.yml -> single file
https://gitlab.com/<owner>/<repo>/tree/<branch>/config -> folder
https://gitlab.com/<owner>/<repo>/blob/<branch>/config/knative/api_groups.yml -> single file
https://bitbucket.org/<owner>/<repo>.git
https://bitbucket.org/<owner>/<repo>/src/<branch>/ <- https://bitbucket.org/tiagoharris/url-shortener redirect
https://bitbucket.org/<owner>/<repo>/src/<branch>/cmd/ -> folder
https://bitbucket.org/<owner>/<repo>/src/<branch>/cmd/main.go -> single file
https://gitea.com/<owner>/<repo>
https://gitea.com/<owner>/<repo>.git -> .git remove
https://gitea.com/<owner>/<repo>/branch/<branch>/lib -> folder
https://gitea.com/<owner>/<repo>/tag/<branch>/lib -> folder
https://gitea.com/<owner>/<repo>/src/branch/<branch>/lib/filesaver.min.js -> single file
https://gitea.com/<owner>/<repo>/src/tag/<branch>/lib/filesaver.min.js -> single file
https://gitea.com/<owner>/<repo>/src/branch/<branch>/internal/url/url.go#L20 -> #L20 removes
https://gitea.com/<owner>/<repo>/src/tag/<branch>/internal/url/url.go#L20 -> #L20 removes
https://gitea.com/<owner>/<repo>/src/branch/<branch>/internal/url/url.go?deneme=12&obaraks=noway#L20 -> ?deneme=12&obaraks=noway#L20 remove
https://gitea.com/<owner>/<repo>/src/tag/<branch>/internal/url/url.go?deneme=12&obaraks=noway#L20 -> ?deneme=12&obaraks=noway#L20 remove
https://gitee.com/<owner>/<repo>
https://gitee.com/<owner>/<repo>.git -> .git remove
https://gitee.com/<owner>/<repo>/tree/<branch>/lib -> folder
https://gitee.com/<owner>/<repo>/blob/<branch>/lib/filesaver.min.js -> single file
https://gitee.com/<owner>/<repo>/blob/<branch>/internal/url/url.go#L20 -> #L20 removes
https://gitee.com/<owner>/<repo>/blob/<branch>/internal/url/url.go?deneme=12&obaraks=noway#L20 -> ?deneme=12&obaraks=noway#L20 remove
Supported: https://github.com/cli/cli/tree/marwan/localcs/api -> branch: marwan/localcs -> how to split this?
TODO: Url and RawUrl are the same? Why?
*/
func (r *GitRepository) Parse(sub string, direction int, filename string) error {
// first
re := regexp.MustCompile(`(?s)/-/`)
r.RawUrl = re.ReplaceAllString(r.RawUrl, "/")
//r.RawUrl = strings.Replace(r.RawUrl, "/-/", "/", 1) // fix: gitlab strange url
if r.isDebugModeActive() {
fmt.Println("raw url", r.RawUrl, "filename", filename)
}
// has to download single file
// update raw url
if filename != "" {
re2 := regexp.MustCompile(`(?s)/tree/`)
r.RawUrl = re2.ReplaceAllString(r.RawUrl, "/blob/")
// little fixed - we know this is file not directory
r.IsFile = true
if r.isDebugModeActive() {
fmt.Println("raw url", r.RawUrl, "filename", filename)
}
}
// parse url
u, err := url.Parse(r.RawUrl)
if err != nil {
return err
}
// find protocol
r.Protocol = "https"
// set scheme
r.Scheme = u.Scheme
// set hostname - not host
r.Hostname = u.Hostname()
// set path before clear unwanted querystring, fragments
r.RawPath = filepath.Join(u.Path, filename)
r.RawPath = strings.Replace(r.RawPath, u.RawFragment, "", 1)
r.RawPath = strings.Replace(r.RawPath, u.RawQuery, "", 1)
//r.RawPath = strings.Replace(r.RawPath, "/-/", "/", 1) // fix: gitlab strange url
// little fix - file recheck
if !r.IsFile {
r.IsFile = !strings.HasSuffix(r.RawUrl, "/") // only useful for bitbucket.org url
}
r.RawPath = strings.TrimSuffix(r.RawPath, "/") // remove last slashes
if r.isDebugModeActive() {
fmt.Println("raw path", r.RawPath)
}
// repeater counter
repeater := strings.Count(r.RawPath, "/")
if r.isDebugModeActive() {
fmt.Println("repeater", repeater)
}
if repeater < 2 {
return errors.New("not valid git url")
}
// multi slashes branch name
branchNameRepeater := 0
if r.Branch != "" {
branchNameRepeater = strings.Count(r.Branch, "/")
}
// n[1] = owner, n[2] = repo, n[3] = tree|blob, n[4] = branch, n[5] = ../../../...
nStart := 6
if r.Hostname == "gitea.com" {
nStart++
}
n := strings.SplitN(r.RawPath, "/", nStart+branchNameRepeater) // fixed n times all urls
r.Owner = n[1]
r.Name = n[2]
if r.isDebugModeActive() {
fmt.Println("split n: ", n, "branchNameRepeater", branchNameRepeater, "sub", sub)
}
if strings.HasSuffix(r.Name, ".git") {
r.Name = strings.Replace(r.Name, ".git", "", 1)
r.RawPath = strings.Replace(r.RawPath, ".git", "", 1)
}
if repeater >= 3 {
if n[3] == "blob" || n[3] == "tree" || n[3] == "src" {
if branchNameRepeater > 0 {
// branch name contains slash
if r.Hostname == "gitea.com" {
if len(n) > (5 + branchNameRepeater + 1) {
r.Path = n[5+branchNameRepeater+1]
}
} else {
if len(n) > (4 + branchNameRepeater + 1) {
r.Path = n[4+branchNameRepeater+1]
}
}
} else {
if r.Hostname == "gitea.com" {
if n[4] == "tag" {
r.IsTagBranch = true
}
r.Branch = n[5]
if len(n) > 6 {
r.Path = n[6]
}
} else {
r.Branch = n[4]
if len(n) > 5 {
r.Path = n[5]
}
}
}
// Bug and TODO
// Bitbucket.org url has src not tree or blob.
// Gitea.com url has src not tree or blob.
// if url not slashes, after download system failed because IsFile value not correct
// r.IsFile = !strings.HasSuffix(r.Path, "/")
/*if r.Hostname == "gitea.com" {
r.IsFile = false
} else*/if n[3] == "tree" {
r.IsFile = false
} else if n[3] == "blob" {
r.IsFile = true
}
} else {
return errors.New("not valid git branch")
}
} else {
r.IsFile = false
}
// sub folder calculation for jump between folders
if sub == "root" {
// clone url must be return: jump to root folder
if r.Path != "" {
r.RawPath = strings.Replace(r.RawPath, r.Path, "", 1)
r.Path = ""
}
} else if sub != "" {
if r.Path != "" {
index := -1
if direction == DirectionUp {
if strings.Count(r.Path, sub) == 1 {
index = strings.LastIndex(r.Path, sub)
} else {
index = strings.Index(r.Path, sub)
}
}
if index == -1 {
r.Path = filepath.Join(r.Path, sub)
r.RawPath = filepath.Join(r.RawPath, sub)
} else {
r.Path = r.Path[0 : index+len(sub)]
rawIndex := strings.Index(r.RawPath, sub)
r.RawPath = r.RawPath[0 : rawIndex+len(sub)]
}
} else {
r.Path = sub
r.RawPath = filepath.Join(r.RawPath, r.Path)
}
}
// generate real url
r.CloneUrl = r.Scheme + "://" + r.Hostname + "/" + r.Owner + "/" + r.Name + ".git"
r.RemoteUrl = "git@" + r.Hostname + ":" + r.Owner + "/" + r.Name + ".git"
r.Url = r.Scheme + "://" + r.Hostname + r.RawPath
// generate pathDir
r.DirPath = r.GetDirPath()
// Generate Remote Url Addresses
r.ArchiveUrl = r.getArchiveUrl()
r.FileUrl = r.getFileUrl("[PATH]")
r.QueryUrl = r.GetQueryUrl(r.Path)
// Download Type
if r.CloneUrl == r.Url+".git" {
// full package
r.DownloadType = DownloadFullPackage
} else if r.Path == "" {
// full package
r.DownloadType = DownloadFullPackage
} else if r.IsFile {
// single file
r.DownloadType = DownloadSingleFile
} else {
// partial package
r.DownloadType = DownloadPartialPackage
}
if r.isDebugModeActive() {
fmt.Printf("%#v\n", r)
}
return nil
}
func (r *GitRepository) WithoutCloneUrl() string {
return strings.Replace(r.CloneUrl, ".git", "", 1)
}
func (r *GitRepository) UpdateBranch(branch string) {
r.Branch = branch
// Generate Remote Url Addresses
r.ArchiveUrl = r.getArchiveUrl()
r.FileUrl = r.getFileUrl("[PATH]")
}
// generate archive url
// Add: is multiple slash branch name, slashes removes
func (r *GitRepository) getArchiveUrl() string {
switch r.Hostname {
case "gitlab.com":
// https://[HOSTNAME]/[OWNER]/[NAME]/-/archive/[BRANCH]/gitlab-[BRANCH].[EXT]
return fmt.Sprintf("https://%s/%s/%s/-/archive/%s/gitlab-%s.%s", r.Hostname, r.Owner, r.Name, r.Branch, strings.ReplaceAll(r.Branch, "/", "-"), "zip")
case "github.com":
// https://[HOSTNAME]/[OWNER]/[NAME]/archive/refs/heads/[BRANCH].[EXT]
// github archive url redirect always
// TODO: Redirect to https://codeload.github.com/[OWNER]/[NAME]/zip/refs/heads/[BRANCH]
return fmt.Sprintf("https://%s/%s/%s/archive/refs/heads/%s.%s", r.Hostname, r.Owner, r.Name, r.Branch, "zip")
case "bitbucket.org":
// https://[HOSTNAME]/[OWNER]/[NAME]/get/[BRANCH].[EXT]
return fmt.Sprintf("https://%s/%s/%s/get/%s.%s", r.Hostname, r.Owner, r.Name, r.Branch, "zip")
case "gitea.com":
// https://[HOSTNAME]/[OWNER]/[NAME]/archive/[BRANCH].[EXT]
// gitea archive url redirect always
return fmt.Sprintf("https://%s/%s/%s/archive/%s.%s", r.Hostname, r.Owner, r.Name, r.Branch, "zip")
case "gitee.com":
// Not supported right now
return ""
}
return ""
}
// generate file url
func (r *GitRepository) getFileUrl(path string) string {
switch r.Hostname {
case "gitlab.com":
// https://[HOSTNAME]/[OWNER]/[NAME]/-/blob/[BRANCH]/[PATH]
// https://gitlab.com/gitlab-org/gitlab/-/raw/dc-move-assignees-widget/.git-blame-ignore-revs
return fmt.Sprintf("https://%s/%s/%s/-/raw/%s/%s", r.Hostname, r.Owner, r.Name, r.Branch, path)
case "github.com":
// https://[HOSTNAME]/[OWNER]/[NAME]/blob/[BRANCH]/[PATH]
// https://raw.githubusercontent.com/101arrowz/fflate/master/.npmignore
return fmt.Sprintf("https://%s/%s/%s/%s/%s", "raw.githubusercontent.com", r.Owner, r.Name, r.Branch, path)
case "bitbucket.org":
// https://[HOSTNAME]/[OWNER]/[NAME]/raw/[BRANCH]/[PATH]
// https://bitbucket.org/micovery/sock-rpc/raw/v1.0.0/package.json
return fmt.Sprintf("https://%s/%s/%s/raw/%s/%s", r.Hostname, r.Owner, r.Name, r.Branch, path)
case "gitea.com":
// https://[HOSTNAME]/[OWNER]/[NAME]/raw/branch/[BRANCH]/[PATH]
// https://[HOSTNAME]/[OWNER]/[NAME]/raw/tag/[BRANCH]/[PATH]
// https://gitea.com/XIU2/TrackersListCollection/raw/branch/master/LICENSE
// https://gitea.com/XIU2/TrackersListCollection/raw/tag/20201211/LICENSE
branchOrTag := "branch"
if r.IsTagBranch {
branchOrTag = "tag"
}
return fmt.Sprintf("https://%s/%s/%s/raw/%s/%s/%s", r.Hostname, r.Owner, r.Name, branchOrTag, r.Branch, path)
case "gitee.com":
// https://[HOSTNAME]/[OWNER]/[NAME]/raw/[BRANCH]/[PATH]
// https://gitee.com/micovery/sock-rpc/raw/dev/package.json
// https://gitee.com/micovery/sock-rpc/raw/v1.0.0/package.json
return fmt.Sprintf("https://%s/%s/%s/raw/%s/%s", r.Hostname, r.Owner, r.Name, r.Branch, path)
}
return ""
}
// generate folder url
func (r *GitRepository) GetQueryUrl(path string) string {
baseUrl := fmt.Sprintf("%s://%s/%s/%s", r.Scheme, r.Hostname, r.Owner, r.Name)
if r.Branch != "" {
if path != "" && r.IsFile {
index := strings.LastIndex(r.Path, "/")
if index != -1 {
path = r.Path[0:index]
} else {
path = ""
}
}
switch r.Hostname {
case "gitlab.com":
// https://[HOSTNAME]/[OWNER]/[NAME]/-/blob/[BRANCH]/[PATH]
return fmt.Sprintf("%s/tree/%s/", baseUrl, filepath.Join(r.Branch, path))
case "github.com":
// https://[HOSTNAME]/[OWNER]/[NAME]/blob/[BRANCH]/[PATH]
return fmt.Sprintf("%s/tree/%s/", baseUrl, filepath.Join(r.Branch, path))
case "bitbucket.org":
// https://[HOSTNAME]/[OWNER]/[NAME]/src/[BRANCH]/[PATH]
return fmt.Sprintf("%s/src/%s/", baseUrl, filepath.Join(r.Branch, path))
case "gitea.com":
// https://[HOSTNAME]/[OWNER]/[NAME]/src/branch/[BRANCH]/[PATH]
// https://[HOSTNAME]/[OWNER]/[NAME]/src/tag/[TAG]/[PATH]
branchOrTag := "branch"
if r.IsTagBranch {
branchOrTag = "tag"
}
return fmt.Sprintf("%s/src/%s/%s/", baseUrl, branchOrTag, filepath.Join(r.Branch, path))
case "gitee.com":
// https://[HOSTNAME]/[OWNER]/[NAME]/blob/[BRANCH]/[PATH]
return fmt.Sprintf("%s/tree/%s/", baseUrl, filepath.Join(r.Branch, path))
}
}
return baseUrl
}
// find real folder path
func (r *GitRepository) FindRealFolderPath(path string) string {
return r._findRealFolderPath(path)
}
func (r *GitRepository) _findRealFolderPath(path string) string {
if path != "" && r.IsFile {
index := strings.LastIndex(r.Path, "/")
if index != -1 {
path = r.Path[0:index]
} else {
path = ""
}
}
return path
}