From 510044337d9e2d1981580b851dabe7ba82e342c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauris=20Buk=C5=A1is-Haberkorns?= Date: Wed, 28 Jun 2017 01:48:06 +0300 Subject: [PATCH] Change pretty format to prefix commit ID so when parsing thay can be recognized better --- repo.go | 15 +++++++++++---- repo_commit.go | 14 +++++++++----- repo_pull.go | 2 +- tree_entry.go | 15 ++++++++++++++- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/repo.go b/repo.go index f87c73d35..59fb894b3 100644 --- a/repo.go +++ b/repo.go @@ -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 } diff --git a/repo_commit.go b/repo_commit.go index 64248d031..33a6b17bf 100644 --- a/repo_commit.go +++ b/repo_commit.go @@ -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 { @@ -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 } @@ -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) { @@ -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) { @@ -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 @@ -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 diff --git a/repo_pull.go b/repo_pull.go index 1c45a4e02..527fae333 100644 --- a/repo_pull.go +++ b/repo_pull.go @@ -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) } diff --git a/tree_entry.go b/tree_entry.go index 7ea4d211a..2191300b7 100644 --- a/tree_entry.go +++ b/tree_entry.go @@ -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 {