diff --git a/github/client_repository_tree.go b/github/client_repository_tree.go index 0c206c1d..9f6c637d 100644 --- a/github/client_repository_tree.go +++ b/github/client_repository_tree.go @@ -78,30 +78,30 @@ func (c *TreeClient) Create(ctx context.Context, tree *gitprovider.TreeInfo) (*g return &responseTreeInfo, nil } -// Get returns a tree +// Get returns a single tree using the SHA1 value for that tree. func (c *TreeClient) Get(ctx context.Context, sha string, recursive bool) (*gitprovider.TreeInfo, error) { // GET /repos/{owner}/{repo}/git/trees repoName := c.ref.GetRepository() repoOwner := c.ref.GetIdentity() - githubTree, _, err := c.c.Client().Git.GetTree(ctx, repoOwner, repoName, sha, true) + githubTree, _, err := c.c.Client().Git.GetTree(ctx, repoOwner, repoName, sha, recursive) if err != nil { return nil, err } - treeEntries := make([]*gitprovider.TreeEntry, 0) - for _, treeEntry := range githubTree.Entries { + treeEntries := make([]*gitprovider.TreeEntry, len(githubTree.Entries)) + for ind, treeEntry := range githubTree.Entries { size := 0 if *treeEntry.Type != "tree" { size = *treeEntry.Size } - treeEntries = append(treeEntries, &gitprovider.TreeEntry{ + treeEntries[ind] = &gitprovider.TreeEntry{ Path: *treeEntry.Path, Mode: *treeEntry.Mode, Type: *treeEntry.Type, Size: size, SHA: *treeEntry.SHA, URL: *treeEntry.URL, - }) + } } treeInfo := gitprovider.TreeInfo{ @@ -136,15 +136,3 @@ func (c *TreeClient) List(ctx context.Context, sha string, recursive bool) ([]*g return treeEntries, nil } - -func createTreeEntry(githubTreeEntry github.TreeEntry) *gitprovider.TreeEntry { - newTreeEntry := gitprovider.TreeEntry{ - Path: *githubTreeEntry.Path, - Mode: *githubTreeEntry.Mode, - Type: *githubTreeEntry.Type, - Size: *githubTreeEntry.Size, - SHA: *githubTreeEntry.SHA, - URL: *githubTreeEntry.URL, - } - return &newTreeEntry -} diff --git a/github/integration_test.go b/github/integration_test.go index 1ad7920c..88f1413d 100644 --- a/github/integration_test.go +++ b/github/integration_test.go @@ -621,7 +621,7 @@ var _ = Describe("GitHub Provider", func() { if treeEntry.Path == "LICENSE" || treeEntry.Path == "README.md" { continue } - Expect(*&treeEntry.Path).To(Equal(*files[ind-itemsToBeIgnored].Path)) + Expect(treeEntry.Path).To(Equal(*files[ind-itemsToBeIgnored].Path)) continue } @@ -638,7 +638,7 @@ var _ = Describe("GitHub Provider", func() { if treeEntry.Path == "LICENSE" || treeEntry.Path == "README.md" { continue } - Expect(*&treeEntry.Path).To(Equal(*files[ind-2].Path)) + Expect(treeEntry.Path).To(Equal(*files[ind-2].Path)) } }) diff --git a/gitprovider/client.go b/gitprovider/client.go index 3be0c428..1d2da343 100644 --- a/gitprovider/client.go +++ b/gitprovider/client.go @@ -244,6 +244,8 @@ type FileClient interface { Get(ctx context.Context, path, branch string, optFns ...FilesGetOption) ([]*CommitFile, error) } +// TreeClient operates on the trees for a Git repository which describe the hierarchy between files in the repository +// This client can be accessed through Repository.Trees() type TreeClient interface { // Create allows for creating or editing tree Create(ctx context.Context, tree *TreeInfo) (*TreeInfo, error) diff --git a/gitprovider/options.go b/gitprovider/options.go index 07008d45..a3c30ba9 100644 --- a/gitprovider/options.go +++ b/gitprovider/options.go @@ -83,13 +83,14 @@ type FilesGetOptions struct { Recursive bool } +// FilesGetOption is an interface for applying options when fetching/getting files type FilesGetOption interface { ApplyFilesGetOptions(target *FilesGetOptions) } +// ApplyFilesGetOptions applies target options onto the invoked opts func (opts *FilesGetOptions) ApplyFilesGetOptions(target *FilesGetOptions) { // Go through each field in opts, and apply it to target if set - if opts.Recursive == true { - target.Recursive = opts.Recursive - } + target.Recursive = opts.Recursive + } diff --git a/gitprovider/resources.go b/gitprovider/resources.go index 65f61020..2450bd75 100644 --- a/gitprovider/resources.go +++ b/gitprovider/resources.go @@ -160,6 +160,7 @@ type PullRequest interface { Get() PullRequestInfo } +// Tree represents a git tree which represents the hierarchy between files in a Git repository type Tree interface { // Object implements the Object interface, // allowing access to the underlying object returned from the API. diff --git a/gitprovider/types_repository.go b/gitprovider/types_repository.go index f8af74f2..27f3afe1 100644 --- a/gitprovider/types_repository.go +++ b/gitprovider/types_repository.go @@ -226,16 +226,30 @@ type PullRequestInfo struct { WebURL string `json:"web_url"` } +// TreeEntry contains info about each tree object's structure in TreeInfo whether it is a file or tree type TreeEntry struct { + // Path is the path of the file/blob or sub tree in a tree Path string `json:"path"` + // Mode of the file/tree. + // (100644:file (blob), 100755:executable (blob), 040000:subdirectory(tree),160000:submodule(commit),120000:blob that specifies the path of a symlink) Mode string `json:"mode"` + // Type is the item type, It is either blob, tree, or commit. Type string `json:"type"` - Size int `json:"size"` - SHA string `json:"sha"` - URL string `json:"url"` + // Size is the size of the file/blob if the type is a blob, it is not populated if the type is a tree + Size int `json:"size"` + // SHA is the SHA1 checksum ID of the object in the tree + SHA string `json:"sha"` + // URL is the url that can be used to retrieve the details of the blob, tree of commit + URL string `json:"url"` } + +// TreeInfo contains high-level information about a git Tree representing the hierarchy between files in a Git repository type TreeInfo struct { - SHA string `json:"sha"` - Tree []*TreeEntry `json:"tree"` - Truncated bool `json:"truncated"` + // SHA is the SHA1 checksum ID of the tree, or the branch name + SHA string `json:"sha"` + // Tree is the list of TreeEntry objects describing the structure of the tree + Tree []*TreeEntry `json:"tree"` + // Truncated represents whether a tree is truncated when fetching a tree + // If truncated is true in the response when fetching a tree, then the number of items in the tree array exceeded the maximum limit + Truncated bool `json:"truncated"` }