diff --git a/commit.go b/commit.go index b1e135a..8e4c4d9 100644 --- a/commit.go +++ b/commit.go @@ -24,6 +24,10 @@ type Commit struct { // submodules map[string]*SubModule } +func (c *Commit) GetCommitOfRelPath(relpath string) (*Commit, error) { + return c.repo.getCommitOfRelPath(c.ID, relpath) +} + // AddAllChanges marks local changes to be ready for commit. func AddChanges(repoPath string, all bool, files ...string) error { cmd := NewCommand("add") diff --git a/repo_commit.go b/repo_commit.go index cc76f91..b730b63 100644 --- a/repo_commit.go +++ b/repo_commit.go @@ -122,6 +122,20 @@ func (repo *Repository) GetCommitOfBranch(branch string) (*Commit, error) { return repo.GetCommit(commitID) } +func (repo *Repository) getCommitOfRelPath(id sha1, relpath string) (*Commit, error) { + stdout, err := NewCommand("log", "-1", _PRETTY_LOG_FORMAT, id.String(), "--", relpath).RunInDir(repo.Path) + if err != nil { + return nil, err + } + + id, err = NewIDFromString(stdout) + if err != nil { + return nil, err + } + + return repo.getCommit(id) +} + // GetCommitByPath returns the last commit of relative path. func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) { stdout, err := NewCommand("log", "-1", _PRETTY_LOG_FORMAT, "--", relpath).RunInDirBytes(repo.Path)