コード例 #1
0
ファイル: repository.go プロジェクト: nathandao/git2neo4j
// revWalkHandler goes through each commit and creates relevant info for each
// commit.
func revWalkHandler(commit *git.Commit) bool {
	// Commit info.
	hash := commit.Id().String()
	if hash == "" {
		return false
	}
	message := strings.Replace(commit.Message(), "\"", "'", -1)
	// Parent info.
	parentCount := commit.ParentCount()
	var buffer bytes.Buffer
	for i := uint(0); i < parentCount; i = i + 1 {
		buffer.WriteString(commit.ParentId(i).String())
		if i < parentCount-1 {
			buffer.WriteString(" ")
		}
	}
	parents := buffer.String()
	// Author info.
	authorSig := commit.Author()
	authorName := authorSig.Name
	authorEmail := authorSig.Email
	authorTime := authorSig.When.Format("2006-01-02 15:04:05 +0000")
	authorTimestamp := strconv.Itoa(int(authorSig.When.Unix()))
	// Committer info.
	committerSig := commit.Committer()
	commitTime := committerSig.When.Format("2006-01-02 15:04:05 +0000")
	commitTimestamp := strconv.Itoa(int(committerSig.When.Unix()))
	// Write to csvFile
	r := commit.Owner()
	csvPath, err := TemporaryCsvPath(r)
	if err != nil {
		panic(err)
	}
	csvFile, err := os.OpenFile(csvPath, os.O_RDWR|os.O_APPEND, 0666)
	if err != nil {
		panic(err)
	}
	defer csvFile.Close()
	w := csv.NewWriter(csvFile)
	err = w.Write([]string{
		hash, message, authorName, authorEmail, authorTime, authorTimestamp,
		commitTime, commitTimestamp, parents,
	})
	if err != nil {
		panic(err)
	}
	w.Flush()
	if err = w.Error(); err != nil {
		panic(err)
	}
	return true
}
コード例 #2
0
ファイル: repository.go プロジェクト: nathandao/git2neo4j
// revWalkSyncHandler adds new commits to the repo graph.
// The walk stops when reaching the previous lastest commit in the database.
func revWalkSyncHandler(commit *git.Commit) bool {
	// Get repo path.
	r := commit.Owner()
	path := r.Path()
	// Commit info.
	hash := commit.Id().String()
	settings := GetSettings()
	db, err := neoism.Connect(settings.DbUrl)
	if err != nil {
		panic(err)
	}
	res := []struct {
		Hash string `json:"c.hash"`
	}{}
	cq := neoism.CypherQuery{
		Statement: `
MATCH (:Repository {path: {path}})-[:HAS_COMMIT]->(c:Commit {hash: {hash}})
RETURN c.hash
`,
		Parameters: neoism.Props{
			"hash": hash,
			"path": path,
		},
		Result: &res,
	}
	if err = db.Cypher(&cq); err != nil {
		panic(err)
	}
	// When reaching an existing commit in the database, stop the walk.
	if size := len(res); size > 0 {
		return false
	}
	// Else, add the commit to the database.
	message := strings.Replace(commit.Message(), "\"", "'", -1)
	// Parent info.
	parentCount := commit.ParentCount()
	var buffer bytes.Buffer
	for i := uint(0); i < parentCount; i = i + 1 {
		buffer.WriteString(commit.ParentId(i).String())
		if i < parentCount-1 {
			buffer.WriteString(" ")
		}
	}
	parents := buffer.String()
	// Author info.
	authorSig := commit.Author()
	authorName := authorSig.Name
	authorEmail := authorSig.Email
	authorTime := authorSig.When.Format("2006-01-02 15:04:05 +0000")
	authorTimestamp := strconv.Itoa(int(authorSig.When.Unix()))
	// Committer info.
	committerSig := commit.Committer()
	commitTime := committerSig.When.Format("2006-01-02 15:04:05 +0000")
	commitTimestamp := strconv.Itoa(int(committerSig.When.Unix()))
	// Add accoding info to the graph.
	commitCq := neoism.CypherQuery{
		Statement: `
MATCH (r:Repository {path: {path}})
CREATE (r)-[:HAS_COMMIT]->(c:Commit {
  message: {message},
  author_time: {author_time},
  author_timestamp: toInt({author_timestamp}),
  commit_time: {commit_time},
  commit_timestamp: toInt({commit_timestamp}),
  hash: {hash},
  parents: split({parents}, ' ')})

MERGE (u:User:Author {email: {author_email}}) ON CREATE SET u.name = {author_name}
MERGE (u)-[:AUTHORED]->(c)
MERGE (c)-[:AUTHORED_BY]->(u)
MERGE (u)-[:CONTRIBUTED_TO]->(r)

WITH c
WHERE {parents} <> ''
FOREACH (parent_hash in split({parents}, ' ') |
  MERGE (parent:Commit {hash: parent_hash})
  MERGE (c)-[:HAS_PARENT]->(parent))
`,
		Parameters: neoism.Props{
			"path":             path,
			"hash":             hash,
			"message":          message,
			"author_name":      authorName,
			"author_email":     authorEmail,
			"author_time":      authorTime,
			"author_timestamp": authorTimestamp,
			"commit_time":      commitTime,
			"commit_timestamp": commitTimestamp,
			"parents":          parents,
		},
	}
	if err = db.Cypher(&commitCq); err != nil {
		return false
	}
	return true
}