Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

Prefix commit ID in git log pretty to ease detection #71

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
15 changes: 11 additions & 4 deletions repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,25 @@ type Repository struct {
tagCache *ObjectCache
}

const prettyLogFormat = `--pretty=format:%H`
const prettyLogCommitPrefix = `commit:`
const prettyLogFormat = `--pretty=format:` + prettyLogCommitPrefix + `%H`

func (repo *Repository) parsePrettyFormatLogToList(logs []byte) (*list.List, error) {
func (repo *Repository) parsePrettyFormatLogToList(prefix string, logs []byte) (*list.List, error) {
l := list.New()
if len(logs) == 0 {
return l, nil
}
prefixLen := len(prefix)

parts := bytes.Split(logs, []byte{'\n'})

for _, commitID := range parts {
commit, err := repo.GetCommit(string(commitID))
for _, line := range parts {
commitID := string(line)
// Skip lines that does not contain Commit ID
if prefixLen > len(commitID) || prefix != commitID[0:prefixLen] {
continue
}
commit, err := repo.GetCommit(commitID[prefixLen:])
if err != nil {
return nil, err
}
Expand Down
14 changes: 9 additions & 5 deletions repo_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ func (repo *Repository) getCommitByPathWithID(id SHA1, relpath string) (*Commit,
if err != nil {
return nil, err
}
lenPrefix := len(prettyLogCommitPrefix)
if lenPrefix < len(stdout) && stdout[0:lenPrefix] == prettyLogCommitPrefix {
stdout = stdout[lenPrefix:]
}

id, err = NewIDFromString(stdout)
if err != nil {
Expand All @@ -183,7 +187,7 @@ func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) {
return nil, err
}

commits, err := repo.parsePrettyFormatLogToList(stdout)
commits, err := repo.parsePrettyFormatLogToList(prettyLogCommitPrefix, stdout)
if err != nil {
return nil, err
}
Expand All @@ -199,7 +203,7 @@ func (repo *Repository) commitsByRange(id SHA1, page int) (*list.List, error) {
if err != nil {
return nil, err
}
return repo.parsePrettyFormatLogToList(stdout)
return repo.parsePrettyFormatLogToList(prettyLogCommitPrefix, stdout)
}

func (repo *Repository) searchCommits(id SHA1, keyword string, all bool) (*list.List, error) {
Expand All @@ -211,7 +215,7 @@ func (repo *Repository) searchCommits(id SHA1, keyword string, all bool) (*list.
if err != nil {
return nil, err
}
return repo.parsePrettyFormatLogToList(stdout)
return repo.parsePrettyFormatLogToList(prettyLogCommitPrefix, stdout)
}

func (repo *Repository) getFilesChanged(id1 string, id2 string) ([]string, error) {
Expand All @@ -234,7 +238,7 @@ func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (
if err != nil {
return nil, err
}
return repo.parsePrettyFormatLogToList(stdout)
return repo.parsePrettyFormatLogToList(prettyLogCommitPrefix, stdout)
}

// FilesCountBetween return the number of files changed between two commits
Expand All @@ -253,7 +257,7 @@ func (repo *Repository) CommitsBetween(last *Commit, before *Commit) (*list.List
if err != nil {
return nil, err
}
return repo.parsePrettyFormatLogToList(bytes.TrimSpace(stdout))
return repo.parsePrettyFormatLogToList("", bytes.TrimSpace(stdout))
}

// Fallback to stupid solution, which iterates all commits of the repository
Expand Down
2 changes: 1 addition & 1 deletion repo_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (repo *Repository) GetPullRequestInfo(basePath, baseBranch, headBranch stri
if err != nil {
return nil, err
}
prInfo.Commits, err = repo.parsePrettyFormatLogToList(logs)
prInfo.Commits, err = repo.parsePrettyFormatLogToList(prettyLogCommitPrefix, logs)
if err != nil {
return nil, fmt.Errorf("parsePrettyFormatLogToList: %v", err)
}
Expand Down
15 changes: 14 additions & 1 deletion tree_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,29 @@ func getNextCommitInfos(state *getCommitInfoState) error {
if err != nil {
return err
}
prefixLen := len(prettyLogCommitPrefix)
lines := strings.Split(logOutput, "\n")
i := 0
for i < len(lines) {
state.nextCommit(lines[i])
commitID := lines[i]
if prefixLen > len(commitID) || commitID[0:prefixLen] != prettyLogCommitPrefix {
return fmt.Errorf("Expected commit ID but received: %s", commitID)
}
if prefixLen > 0 {
commitID = commitID[prefixLen:]
}
state.nextCommit(commitID)
i++
for ; i < len(lines); i++ {
entryPath := lines[i]
if entryPath == "" {
break
}
// Check if it is not next commit
if prefixLen < len(entryPath) && entryPath[0:prefixLen] == prettyLogCommitPrefix {
i--
break
}
if entryPath[0] == '"' {
entryPath, err = strconv.Unquote(entryPath)
if err != nil {
Expand Down