Example #1
0
// BranchBehind is not yet implemented. Determines if the current branch
// behind master and requires that it be "merged up".
func (r *Reviewer) BranchBehind() (bool, error) {
	if r.Repo == nil {
		return false, errors.New("repo not initialized")
	}

	var (
		rg      runGuard
		mBranch *gg.Branch
		mCom    *gg.Commit
		hRef    *gg.Reference
		hCom    *gg.Commit
		behind  bool
	)

	defer func() {
		objs := [...]freeable{
			mBranch,
			mCom,
			hRef,
			hCom,
		}
		for _, obj := range objs {
			obj.Free()
		}
	}()

	rg.maybeRun(func() {
		var err error
		if mBranch, err = r.Repo.LookupBranch("master", gg.BranchLocal); err != nil {
			rg.err = err
			rg.msg = "Issue opening master branch"
		}
	})
	rg.maybeRun(func() {
		var err error
		if mCom, err = r.Repo.LookupCommit(mBranch.Reference.Target()); err != nil {
			rg.err = err
			rg.msg = "Issue opening master commit"
		}
	})
	rg.maybeRun(func() {
		var err error
		if hRef, err = r.Repo.Head(); err != nil {
			rg.err = err
			rg.msg = "Issue opening HEAD reference"
		}
	})
	rg.maybeRun(func() {
		var err error
		if hCom, err = r.Repo.LookupCommit(hRef.Target()); err != nil {
			rg.err = err
			rg.msg = "Issue opening HEAD commit"
		}
	})
	rg.maybeRun(func() {
		behind = hCom.Committer().When.Before(mCom.Committer().When)
	})

	return behind, rg.err
}
Example #2
0
func (r *Repository) makeCommit(c *git2go.Commit) *vcs.Commit {
	var parents []vcs.CommitID
	if pc := c.ParentCount(); pc > 0 {
		parents = make([]vcs.CommitID, pc)
		for i := 0; i < int(pc); i++ {
			parents[i] = vcs.CommitID(c.ParentId(uint(i)).String())
		}
	}

	au, cm := c.Author(), c.Committer()
	return &vcs.Commit{
		ID:        vcs.CommitID(c.Id().String()),
		Author:    vcs.Signature{au.Name, au.Email, pbtypes.NewTimestamp(au.When)},
		Committer: &vcs.Signature{cm.Name, cm.Email, pbtypes.NewTimestamp(cm.When)},
		Message:   strings.TrimSuffix(c.Message(), "\n"),
		Parents:   parents,
	}
}
Example #3
0
// 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
}
Example #4
0
// 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
}
Example #5
0
// FindFiles returns a list of paths to files that have been changed
// in this branch with respect to `master`.
func (r *Reviewer) FindFiles() ([]string, error) {
	var (
		rg      runGuard
		lines   []string
		mBranch *gg.Branch
		hRef    *gg.Reference
		hCom    *gg.Commit
		mCom    *gg.Commit
		mTree   *gg.Tree
		hTree   *gg.Tree
		opts    gg.DiffOptions
		diff    *gg.Diff
	)

	if r.Repo == nil {
		return lines, errors.New("repo not initialized")
	}

	defer func() {
		objs := [...]freeable{
			mBranch,
			hRef,
			hCom,
			mCom,
			mTree,
			hTree,
		}

		for _, obj := range objs {
			if obj != nil {
				obj.Free()
			}
		}

		if err := diff.Free(); err != nil && r.Verbose {
			fmt.Printf("Issue cleaning up diff: '%s'\n", err)
		}
	}()

	rg.maybeRun(func() {
		var err error
		if mBranch, err = r.Repo.LookupBranch("master", gg.BranchLocal); err != nil {
			rg.err = err
			rg.msg = "issue opening master branch"
		}
	})

	rg.maybeRun(func() {
		var err error
		if mCom, err = r.Repo.LookupCommit(mBranch.Reference.Target()); err != nil {
			rg.err = err
			rg.msg = "issue opening commit at master"
		}
	})

	rg.maybeRun(func() {
		var err error
		if hRef, err = r.Repo.Head(); err != nil {
			rg.err = err
			rg.msg = "issue opening repo at HEAD"
		}
	})

	rg.maybeRun(func() {
		var err error
		if hCom, err = r.Repo.LookupCommit(hRef.Target()); err != nil {
			rg.err = err
			rg.msg = "issue opening commit at HEAD"
		}
	})

	rg.maybeRun(func() {
		var err error
		if mTree, err = mCom.Tree(); err != nil {
			rg.err = err
			rg.msg = "issue opening tree at master"
		}
	})

	rg.maybeRun(func() {
		var err error
		if hTree, err = hCom.Tree(); err != nil {
			rg.err = err
			rg.msg = "issue opening tree at HEAD"
		}
	})

	rg.maybeRun(func() {
		var err error
		if opts, err = gg.DefaultDiffOptions(); err != nil {
			rg.err = err
			rg.msg = "issue creating diff options"
		}
	})

	rg.maybeRun(func() {
		var err error
		if diff, err = r.Repo.DiffTreeToTree(mTree, hTree, &opts); err != nil {
			rg.err = err
			rg.msg = "issue finding diff"
		}
	})

	rg.maybeRun(func() {
		diff.ForEach(func(file gg.DiffDelta, progress float64) (
			gg.DiffForEachHunkCallback, error) {

			lines = append(lines, file.OldFile.Path)
			return nil, nil
		}, gg.DiffDetailFiles)
	})

	if rg.err != nil && rg.msg != "" && r.Verbose {
		fmt.Printf("Error finding diff files: '%s'\n", rg.msg)
	}

	return lines, rg.err
}
Example #6
0
func CommitToRef(r *git.Repository, tree *git.Tree, parent *git.Commit, refname, msg string) (*git.Commit, error) {
	// Retry loop in case of conflict
	// FIXME: use a custom inter-process lock as a first attempt for performance
	var (
		needMerge bool
		tmpCommit *git.Commit
	)
	for {
		if !needMerge {
			// Create simple commit
			commit, err := mkCommit(r, refname, msg, tree, parent)
			if isGitConcurrencyErr(err) {
				needMerge = true
				continue
			}
			return commit, err
		} else {
			if tmpCommit == nil {
				var err error
				// Create a temporary intermediary commit, to pass to MergeCommits
				// NOTE: this commit will not be part of the final history.
				tmpCommit, err = mkCommit(r, "", msg, tree, parent)
				if err != nil {
					return nil, err
				}
				defer tmpCommit.Free()
			}
			// Lookup tip from ref
			tip := lookupTip(r, refname)
			if tip == nil {
				// Ref may have been deleted after previous merge error
				needMerge = false
				continue
			}

			// Merge simple commit with the tip
			opts, err := git.DefaultMergeOptions()
			if err != nil {
				return nil, err
			}
			idx, err := r.MergeCommits(tmpCommit, tip, &opts)
			if err != nil {
				return nil, err
			}
			conflicts, err := idx.ConflictIterator()
			if err != nil {
				return nil, err
			}
			defer conflicts.Free()
			for {
				c, err := conflicts.Next()
				if isGitIterOver(err) {
					break
				} else if err != nil {
					return nil, err
				}
				if c.Our != nil {
					idx.RemoveConflict(c.Our.Path)
					if err := idx.Add(c.Our); err != nil {
						return nil, fmt.Errorf("error resolving merge conflict for '%s': %v", c.Our.Path, err)
					}
				}
			}
			mergedId, err := idx.WriteTreeTo(r)
			if err != nil {
				return nil, fmt.Errorf("WriteTree: %v", err)
			}
			mergedTree, err := lookupTree(r, mergedId)
			if err != nil {
				return nil, err
			}
			// Create new commit from merged tree (discarding simple commit)
			commit, err := mkCommit(r, refname, msg, mergedTree, parent, tip)
			if isGitConcurrencyErr(err) {
				// FIXME: enforce a maximum number of retries to avoid infinite loops
				continue
			}
			return commit, err
		}
	}
	return nil, fmt.Errorf("too many failed merge attempts, giving up")
}