From 38a5ca6b26a3586079f114e295bfe23ad444ccec Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 16 Nov 2016 15:14:36 +0800 Subject: [PATCH] change returned unexported types to exported types --- commit.go | 12 ++++++------ repo.go | 4 ++-- repo_commit.go | 16 ++++++++-------- repo_tag.go | 2 +- repo_tree.go | 2 +- sha1.go | 33 +++++++++++++++++---------------- tag.go | 4 ++-- tree.go | 6 +++--- tree_entry.go | 2 +- utlis.go | 14 ++++++++------ 10 files changed, 49 insertions(+), 46 deletions(-) diff --git a/commit.go b/commit.go index c980266..dc5f2bd 100644 --- a/commit.go +++ b/commit.go @@ -18,13 +18,13 @@ import ( // Commit represents a git commit. type Commit struct { Tree - ID sha1 // The ID of this commit object + ID SHA1 // The ID of this commit object Author *Signature Committer *Signature CommitMessage string - parents []sha1 // SHA1 strings - submoduleCache *objectCache + parents []SHA1 // SHA1 strings + submoduleCache *ObjectCache } // Message returns the commit message. Same as retrieving CommitMessage directly. @@ -39,9 +39,9 @@ func (c *Commit) Summary() string { // ParentID returns oid of n-th parent (0-based index). // It returns nil if no such parent exists. -func (c *Commit) ParentID(n int) (sha1, error) { +func (c *Commit) ParentID(n int) (SHA1, error) { if n >= len(c.parents) { - return sha1{}, ErrNotExist{"", ""} + return SHA1{}, ErrNotExist{"", ""} } return c.parents[n], nil } @@ -208,7 +208,7 @@ func (c *Commit) GetFilesChangedSinceCommit(pastCommit string) ([]string, error) } // GetSubModules get all the sub modules of current revision git tree -func (c *Commit) GetSubModules() (*objectCache, error) { +func (c *Commit) GetSubModules() (*ObjectCache, error) { if c.submoduleCache != nil { return c.submoduleCache, nil } diff --git a/repo.go b/repo.go index 01d8627..e596b74 100644 --- a/repo.go +++ b/repo.go @@ -18,8 +18,8 @@ import ( type Repository struct { Path string - commitCache *objectCache - tagCache *objectCache + commitCache *ObjectCache + tagCache *ObjectCache } const prettyLogFormat = `--pretty=format:%H` diff --git a/repo_commit.go b/repo_commit.go index 4b40a18..133d794 100644 --- a/repo_commit.go +++ b/repo_commit.go @@ -41,7 +41,7 @@ func (repo *Repository) GetTagCommitID(name string) (string, error) { // \n\n separate headers from message func parseCommitData(data []byte) (*Commit, error) { commit := new(Commit) - commit.parents = make([]sha1, 0, 1) + commit.parents = make([]SHA1, 0, 1) // we now have the contents of the commit object. Let's investigate... nextline := 0 l: @@ -90,7 +90,7 @@ l: return commit, nil } -func (repo *Repository) getCommit(id sha1) (*Commit, error) { +func (repo *Repository) getCommit(id SHA1) (*Commit, error) { c, ok := repo.commitCache.Get(id.String()) if ok { log("Hit cache: %s", id) @@ -151,7 +151,7 @@ func (repo *Repository) GetTagCommit(name string) (*Commit, error) { return repo.GetCommit(commitID) } -func (repo *Repository) getCommitByPathWithID(id sha1, relpath string) (*Commit, error) { +func (repo *Repository) getCommitByPathWithID(id SHA1, relpath string) (*Commit, error) { // File name starts with ':' must be escaped. if relpath[0] == ':' { relpath = `\` + relpath @@ -187,7 +187,7 @@ func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) { // CommitsRangeSize the default commits range size var CommitsRangeSize = 50 -func (repo *Repository) commitsByRange(id sha1, page int) (*list.List, error) { +func (repo *Repository) commitsByRange(id SHA1, page int) (*list.List, error) { stdout, err := NewCommand("log", id.String(), "--skip="+strconv.Itoa((page-1)*CommitsRangeSize), "--max-count="+strconv.Itoa(CommitsRangeSize), prettyLogFormat).RunInDirBytes(repo.Path) if err != nil { @@ -196,7 +196,7 @@ func (repo *Repository) commitsByRange(id sha1, page int) (*list.List, error) { return repo.parsePrettyFormatLogToList(stdout) } -func (repo *Repository) searchCommits(id sha1, keyword string) (*list.List, error) { +func (repo *Repository) searchCommits(id SHA1, keyword string) (*list.List, error) { stdout, err := NewCommand("log", id.String(), "-100", "-i", "--grep="+keyword, prettyLogFormat).RunInDirBytes(repo.Path) if err != nil { return nil, err @@ -290,7 +290,7 @@ func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) { } // commitsBefore the limit is depth, not total number of returned commits. -func (repo *Repository) commitsBefore(l *list.List, parent *list.Element, id sha1, current, limit int) error { +func (repo *Repository) commitsBefore(l *list.List, parent *list.Element, id SHA1, current, limit int) error { // Reach the limit if limit > 0 && current > limit { return nil @@ -349,12 +349,12 @@ func (repo *Repository) commitsBefore(l *list.List, parent *list.Element, id sha return nil } -func (repo *Repository) getCommitsBefore(id sha1) (*list.List, error) { +func (repo *Repository) getCommitsBefore(id SHA1) (*list.List, error) { l := list.New() return l, repo.commitsBefore(l, nil, id, 1, 0) } -func (repo *Repository) getCommitsBeforeLimit(id sha1, num int) (*list.List, error) { +func (repo *Repository) getCommitsBeforeLimit(id SHA1, num int) (*list.List, error) { l := list.New() return l, repo.commitsBefore(l, nil, id, 1, num) } diff --git a/repo_tag.go b/repo_tag.go index 40d1d83..dfaa41f 100644 --- a/repo_tag.go +++ b/repo_tag.go @@ -29,7 +29,7 @@ func (repo *Repository) CreateTag(name, revision string) error { return err } -func (repo *Repository) getTag(id sha1) (*Tag, error) { +func (repo *Repository) getTag(id SHA1) (*Tag, error) { t, ok := repo.tagCache.Get(id.String()) if ok { log("Hit cache: %s", id) diff --git a/repo_tree.go b/repo_tree.go index 64d0313..6e3843f 100644 --- a/repo_tree.go +++ b/repo_tree.go @@ -4,7 +4,7 @@ package git -func (repo *Repository) getTree(id sha1) (*Tree, error) { +func (repo *Repository) getTree(id SHA1) (*Tree, error) { treePath := filepathFromSHA1(repo.Path, id.String()) if isFile(treePath) { _, err := NewCommand("ls-tree", id.String()).RunInDir(repo.Path) diff --git a/sha1.go b/sha1.go index b86a98d..3f99535 100644 --- a/sha1.go +++ b/sha1.go @@ -12,11 +12,12 @@ import ( const emptySHA = "0000000000000000000000000000000000000000" -type sha1 [20]byte +// SHA1 a git commit name +type SHA1 [20]byte -// Equal returns true if s has the same sha1 as caller. -// Support 40-length-string, []byte, sha1. -func (id sha1) Equal(s2 interface{}) bool { +// Equal returns true if s has the same SHA1 as caller. +// Support 40-length-string, []byte, SHA1. +func (id SHA1) Equal(s2 interface{}) bool { switch v := s2.(type) { case string: if len(v) != 40 { @@ -32,7 +33,7 @@ func (id sha1) Equal(s2 interface{}) bool { return false } } - case sha1: + case SHA1: for i, v := range v { if id[i] != v { return false @@ -45,7 +46,7 @@ func (id sha1) Equal(s2 interface{}) bool { } // String returns string (hex) representation of the Oid. -func (id sha1) String() string { +func (id SHA1) String() string { result := make([]byte, 0, 40) hexvalues := []byte("0123456789abcdef") for i := 0; i < 20; i++ { @@ -55,32 +56,32 @@ func (id sha1) String() string { return string(result) } -// MustID always creates a new sha1 from a [20]byte array with no validation of input. -func MustID(b []byte) sha1 { - var id sha1 +// MustID always creates a new SHA1 from a [20]byte array with no validation of input. +func MustID(b []byte) SHA1 { + var id SHA1 for i := 0; i < 20; i++ { id[i] = b[i] } return id } -// NewID creates a new sha1 from a [20]byte array. -func NewID(b []byte) (sha1, error) { +// NewID creates a new SHA1 from a [20]byte array. +func NewID(b []byte) (SHA1, error) { if len(b) != 20 { - return sha1{}, fmt.Errorf("Length must be 20: %v", b) + return SHA1{}, fmt.Errorf("Length must be 20: %v", b) } return MustID(b), nil } // MustIDFromString always creates a new sha from a ID with no validation of input. -func MustIDFromString(s string) sha1 { +func MustIDFromString(s string) SHA1 { b, _ := hex.DecodeString(s) return MustID(b) } -// NewIDFromString creates a new sha1 from a ID string of length 40. -func NewIDFromString(s string) (sha1, error) { - var id sha1 +// NewIDFromString creates a new SHA1 from a ID string of length 40. +func NewIDFromString(s string) (SHA1, error) { + var id SHA1 s = strings.TrimSpace(s) if len(s) != 40 { return id, fmt.Errorf("Length must be 40: %s", s) diff --git a/tag.go b/tag.go index 3b0798f..f2a3d31 100644 --- a/tag.go +++ b/tag.go @@ -9,9 +9,9 @@ import "bytes" // Tag represents a Git tag. type Tag struct { Name string - ID sha1 + ID SHA1 repo *Repository - Object sha1 // The id of this commit object + Object SHA1 // The id of this commit object Type string Tagger *Signature Message string diff --git a/tree.go b/tree.go index 62a1e71..05e7afd 100644 --- a/tree.go +++ b/tree.go @@ -12,7 +12,7 @@ import ( // Tree represents a flat directory listing. type Tree struct { - ID sha1 + ID SHA1 repo *Repository // parent tree @@ -23,7 +23,7 @@ type Tree struct { } // NewTree create a new tree according the repository and commit id -func NewTree(repo *Repository, id sha1) *Tree { +func NewTree(repo *Repository, id SHA1) *Tree { return &Tree{ ID: id, repo: repo, @@ -91,7 +91,7 @@ func parseTreeData(tree *Tree, data []byte) ([]*TreeEntry, error) { return nil, err } entry.ID = id - pos += step + 1 // Skip half of sha1. + pos += step + 1 // Skip half of SHA1. step = bytes.IndexByte(data[pos:], '\n') diff --git a/tree_entry.go b/tree_entry.go index 33a715b..d3eefee 100644 --- a/tree_entry.go +++ b/tree_entry.go @@ -34,7 +34,7 @@ const ( // TreeEntry the leaf in the git tree type TreeEntry struct { - ID sha1 + ID SHA1 Type ObjectType mode EntryMode diff --git a/utlis.go b/utlis.go index 63c890c..8f01032 100644 --- a/utlis.go +++ b/utlis.go @@ -12,26 +12,28 @@ import ( "sync" ) -// objectCache provides thread-safe cache opeations. -type objectCache struct { +// ObjectCache provides thread-safe cache opeations. +type ObjectCache struct { lock sync.RWMutex cache map[string]interface{} } -func newObjectCache() *objectCache { - return &objectCache{ +func newObjectCache() *ObjectCache { + return &ObjectCache{ cache: make(map[string]interface{}, 10), } } -func (oc *objectCache) Set(id string, obj interface{}) { +// Set add obj to cache +func (oc *ObjectCache) Set(id string, obj interface{}) { oc.lock.Lock() defer oc.lock.Unlock() oc.cache[id] = obj } -func (oc *objectCache) Get(id string) (interface{}, bool) { +// Get get cached obj by id +func (oc *ObjectCache) Get(id string) (interface{}, bool) { oc.lock.RLock() defer oc.lock.RUnlock()