-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcommit.go
More file actions
105 lines (91 loc) · 2.64 KB
/
commit.go
File metadata and controls
105 lines (91 loc) · 2.64 KB
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
package git
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/corecollectives/mist/constants"
"github.com/corecollectives/mist/github"
"github.com/corecollectives/mist/models"
)
func GetLatestCommit(appID int64, userID int64) (*models.LatestCommit, error) {
gitProvider, err := models.GetGitProviderNameByAppID(appID)
if err != nil {
fmt.Printf("error getting provider name by app id", err.Error())
return nil, err
}
gitCloneUrl, err := models.GetCloneUrlfromAppID(appID)
if err != nil {
return nil, err
}
if gitProvider == nil && gitCloneUrl != nil {
return latestRemoteCommit(*gitCloneUrl)
} else if gitProvider == nil && gitCloneUrl == nil {
return nil, fmt.Errorf("git url or provider not given")
}
if *gitProvider == models.GitProviderGitHub {
return github.GetLatestCommit(appID, userID)
}
return nil, fmt.Errorf("failed to get latest commit")
}
// for repositories which are linked via git clone url and not any github provider
// we temporarily clone it (not fully) to get the latest commit information
// the path for this temp repo is `/var/lib/mist/git-meta/repo-`
func latestRemoteCommit(repoURL string) (*models.LatestCommit, error) {
tmpPath := filepath.Join(constants.Constants["RootPath"].(string), "git-meta")
if err := os.MkdirAll(tmpPath, 0o755); err != nil {
return nil, err
}
tmpDir, err := os.MkdirTemp(tmpPath, "repo-")
if err != nil {
return nil, err
}
defer os.RemoveAll(tmpDir)
cmd := exec.Command("git", "init")
cmd.Dir = tmpDir
if out, err := cmd.CombinedOutput(); err != nil {
return nil, fmt.Errorf("git init failed: %w: %s", err, out)
}
cmd = exec.Command("git", "remote", "add", "origin", repoURL)
cmd.Dir = tmpDir
if out, err := cmd.CombinedOutput(); err != nil {
return nil, fmt.Errorf("git remote add failed: %w: %s", err, out)
}
// use `--filter=blob:none` to not to fetch any actual file at this point, we only fetching the
// git metadata not the actual repo
cmd = exec.Command(
"git",
"fetch",
"--depth=1",
"--filter=blob:none",
"origin",
"HEAD",
)
cmd.Dir = tmpDir
if out, err := cmd.CombinedOutput(); err != nil {
return nil, fmt.Errorf("git fetch failed: %w: %s", err, out)
}
cmd = exec.Command(
"git",
"show",
"-s",
"--format=%H%n%an%n%s",
"FETCH_HEAD",
)
cmd.Dir = tmpDir
out, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("git show failed: %w", err)
}
parts := strings.SplitN(strings.TrimSpace(string(out)), "\n", 3)
if len(parts) < 3 {
return nil, fmt.Errorf("unexpected git show output")
}
return &models.LatestCommit{
SHA: parts[0],
Author: parts[1],
Message: parts[2],
URL: "",
}, nil
}