-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
git.go
95 lines (77 loc) · 2.11 KB
/
git.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
package extractor
import (
"errors"
"github.com/dunglas/calavera/schema"
gogit "gopkg.in/src-d/go-git.v4"
"os"
"path/filepath"
"strings"
)
// Git extracts metadata from the Git repository containing Markdown files.
type Git struct {
inputDirectory string
gitDirectory string
repository *gogit.Repository
}
func findGitDir(path string) (string, error) {
if _, err := os.Stat(path + "/.git/config"); err == nil {
return path + "/.git", nil
}
parentDir := filepath.Dir(path)
if strings.HasSuffix(parentDir, "/") {
return "", errors.New("No Git repository found")
}
return findGitDir(parentDir)
}
// NewGit returns a new instance properly configured of the Git extractor
func NewGit(inputDirectory string) (*Git, error) {
var err error
var gitDirectory string
if gitDirectory, err = findGitDir(inputDirectory); nil != err {
return nil, err
}
if r, err := gogit.NewFilesystemRepository(gitDirectory); nil == err {
return &Git{
inputDirectory: inputDirectory,
gitDirectory: gitDirectory,
repository: r,
}, nil
}
return nil, err
}
// Extract extracts the list of contributors to the file, and date of modifications.
func (git Git) Extract(creativeWork *schema.CreativeWork, path string) error {
path, _ = filepath.Rel(filepath.Dir(git.gitDirectory), git.inputDirectory+"/"+path)
ref, err := git.repository.Head()
if nil != err {
return err
}
c, err := git.repository.Commit(ref.Hash())
if nil != err {
return err
}
revs, err := gogit.References(c, path)
if err != nil {
return err
}
for _, v := range revs {
if !authorExists(creativeWork.Author, v.Author.Email) {
author := schema.NewPerson(v.Author.Name, v.Author.Email)
creativeWork.Author = append([]schema.Person{*author}, creativeWork.Author...)
}
creativeWork.DateModified = &v.Author.When
if nil == creativeWork.DateCreated {
creativeWork.DateCreated = &v.Author.When
}
}
return nil
}
// authorExists tests if an author is already in the list
func authorExists(authors []schema.Person, email string) bool {
for _, p := range authors {
if email == p.Email {
return true
}
}
return false
}