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

reduce git commands when get many commits info #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
35 changes: 27 additions & 8 deletions commit_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package git

import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"os/exec"
"path"
Expand Down Expand Up @@ -71,6 +73,26 @@ func (state *getCommitsInfoState) getTargetedEntryPath() string {
return targetedEntryPath
}

func parseSimpleCommit(output []byte) (*Commit, error) {
fields := bytes.SplitN(output, []byte{' '}, 3)
if len(fields) != 3 {
return nil, errors.New("The line should have 3 fields")
}

secs, err := strconv.ParseInt(string(fields[1]), 10, 64)
if err != nil {
return nil, err
}

return &Commit{
ID: MustID(fields[0]),
CommitMessage: string(fields[2]),
Committer: &Signature{
When: time.Unix(secs, 0),
},
}, nil
}

// repeatedly perform targeted searches for unpopulated entries
func targetedSearch(state *getCommitsInfoState, done chan error, cache LastCommitCache) {
for {
Expand All @@ -86,22 +108,19 @@ func targetedSearch(state *getCommitsInfoState, done chan error, cache LastCommi
continue
}
}
command := NewCommand("rev-list", "-1", state.headCommit.ID.String(), "--", entryPath)
output, err := command.RunInDir(state.headCommit.repo.Path)
command := NewCommand("log", "-1", "--pretty=format:%H %ct %s", state.headCommit.ID.String(), "--", entryPath)
output, err := command.RunInDirBytes(state.headCommit.repo.Path)
if err != nil {
done <- err
return
}
id, err := NewIDFromString(strings.TrimSpace(output))
if err != nil {
done <- err
return
}
commit, err := state.headCommit.repo.getCommit(id)

commit, err := parseSimpleCommit(output)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but repo.getCommit returns way more info about commit than this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That gives too much info than we need and would need another cli call which would add too much overhead

Copy link
Member Author

@lunny lunny Feb 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. But this function only be used on showing repo homepage. On that page, we only need entryPath, last commit's commitid, commit message and commit time. So other information will spend many time but unnecessary. Maybe I should define a new struct to only include that messages.

if err != nil {
done <- err
return
}

state.update(entryPath, commit)
if cache != nil {
cache.Put(state.headCommit.repo.Path, state.headCommit.ID.String(), entryPath, commit)
Expand Down