Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Add '-include-branches' option #177

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Gitrob is a tool to help find potentially sensitive files pushed to public repos
Print debugging information
-github-access-token string
GitHub access token to use for API requests
-include-branches
Include repository branches in scan
-load string
Load session file
-no-expand-orgs
Expand Down
189 changes: 99 additions & 90 deletions core/git.go
Original file line number Diff line number Diff line change
@@ -1,120 +1,129 @@
package core

import (
"fmt"
"io/ioutil"
"fmt"
"io/ioutil"

"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/utils/merkletrie"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/utils/merkletrie"
)

const (
EmptyTreeCommitId = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
EmptyTreeCommitId = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
)

func CloneRepository(url *string, branch *string, depth int) (*git.Repository, string, error) {
urlVal := *url
branchVal := *branch
dir, err := ioutil.TempDir("", "gitrob")
if err != nil {
return nil, "", err
}
repository, err := git.PlainClone(dir, false, &git.CloneOptions{
URL: urlVal,
Depth: depth,
ReferenceName: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", branchVal)),
SingleBranch: true,
Tags: git.NoTags,
})
if err != nil {
return nil, dir, err
}
return repository, dir, nil
urlVal := *url
branchVal := *branch
dir, err := ioutil.TempDir("", "gitrob")
if err != nil {
return nil, "", err
}
repository, err := git.PlainClone(dir, false, &git.CloneOptions{
URL: urlVal,
Depth: depth,
ReferenceName: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", branchVal)),
SingleBranch: true,
Tags: git.NoTags,
})
if err != nil {
return nil, dir, err
}
return repository, dir, nil
}

func GetRepositoryHistory(repository *git.Repository) ([]*object.Commit, error) {
var commits []*object.Commit
ref, err := repository.Head()
if err != nil {
return nil, err
}
cIter, err := repository.Log(&git.LogOptions{From: ref.Hash()})
if err != nil {
return nil, err
}
cIter.ForEach(func(c *object.Commit) error {
commits = append(commits, c)
return nil
})
return commits, nil
var commits []*object.Commit
ref, err := repository.Head()
if err != nil {
return nil, err
}
cIter, err := repository.Log(&git.LogOptions{From: ref.Hash()})
if err != nil {
return nil, err
}
cIter.ForEach(func(c *object.Commit) error {
commits = append(commits, c)
return nil
})
return commits, nil
}

func GetChanges(commit *object.Commit, repo *git.Repository) (object.Changes, error) {
parentCommit, err := GetParentCommit(commit, repo)
if err != nil {
return nil, err
}
parentCommit, err := GetParentCommit(commit, repo)
if err != nil {
//this may be the parent commit
parentCommit = commit
//return nil, err
}

commitTree, err := commit.Tree()
if err != nil {
return nil, err
}
commitTree, err := commit.Tree()
if err != nil {
return nil, err
}

parentCommitTree, err := parentCommit.Tree()
if err != nil {
return nil, err
}
parentCommitTree, err := parentCommit.Tree()
if err != nil {
return nil, err
}

changes, err := object.DiffTree(parentCommitTree, commitTree)
if err != nil {
return nil, err
}
return changes, nil
//changes, err := object.DiffTree(parentCommitTree, commitTree)
var changes object.Changes
if parentCommit == commit {
changes, err = object.DiffTree(nil, parentCommitTree)
} else {
changes, err = object.DiffTree(parentCommitTree, commitTree)
}

if err != nil {
return nil, err
}
return changes, nil
}

func GetParentCommit(commit *object.Commit, repo *git.Repository) (*object.Commit, error) {
if commit.NumParents() == 0 {
parentCommit, err := repo.CommitObject(plumbing.NewHash(EmptyTreeCommitId))
if err != nil {
return nil, err
}
return parentCommit, nil
}
parentCommit, err := commit.Parents().Next()
if err != nil {
return nil, err
}
return parentCommit, nil
if commit.NumParents() == 0 {
parentCommit, err := repo.CommitObject(plumbing.NewHash(EmptyTreeCommitId))
if err != nil {
return nil, err
}
return parentCommit, nil
}
parentCommit, err := commit.Parents().Next()
if err != nil {
return nil, err
}
return parentCommit, nil
}

func GetChangeAction(change *object.Change) string {
action, err := change.Action()
if err != nil {
return "Unknown"
}
switch action {
case merkletrie.Insert:
return "Insert"
case merkletrie.Modify:
return "Modify"
case merkletrie.Delete:
return "Delete"
default:
return "Unknown"
}
action, err := change.Action()
if err != nil {
return "Unknown"
}
switch action {
case merkletrie.Insert:
return "Insert"
case merkletrie.Modify:
return "Modify"
case merkletrie.Delete:
return "Delete"
default:
return "Unknown"
}
}

func GetChangePath(change *object.Change) string {
action, err := change.Action()
if err != nil {
return change.To.Name
}
action, err := change.Action()
if err != nil {
return change.To.Name
}

if action == merkletrie.Delete {
return change.From.Name
} else {
return change.To.Name
}
if action == merkletrie.Delete {
return change.From.Name
} else {
return change.To.Name
}
}
Loading