Skip to content

fileHistoryWalk does not return all commits #1068

@littlebee

Description

@littlebee

When I compare the history returned by fileHistoryWalk to git log console output, there are a lot of commits missing from the fileHistoryWalk commits.

For example, using the atom editor repo and source:

cd /tmp
git clone https://github.com/atom/atom.git
cd atom
git log src/text-editor.coffee

...then, using a slightly modified examples/walk-history-for-file.js (see below) and compare. The output from the example says there are 129 commits to text-editor.coffee but git log src/text-editor.coffee shows 481 commits to that file.

modified example (changed repo, test tile path and added count output:

var nodegit = require("../"),
    path = require("path"),
    historyFile = "src/text-editor.coffee",
    walker,
    historyCommits = [],
    commit,
    repo;

// This code walks the history of the master branch and prints results
// that look very similar to calling `git log` from the command line

function compileHistory(resultingArrayOfCommits) {
  var lastSha;
  if (historyCommits.length > 0) {
    lastSha = historyCommits[historyCommits.length - 1].commit.sha();
    if (
      resultingArrayOfCommits.length == 1 &&
      resultingArrayOfCommits[0].commit.sha() == lastSha
    ) {
      return;
    }
  }

  resultingArrayOfCommits.forEach(function(entry) {
    historyCommits.push(entry);
  });

  lastSha = historyCommits[historyCommits.length - 1].commit.sha();

  walker = repo.createRevWalk();
  walker.push(lastSha);
  walker.sorting(nodegit.Revwalk.SORT.TIME);

  return walker.fileHistoryWalk(historyFile, 500)
    .then(compileHistory);
}

//nodegit.Repository.open(path.resolve(__dirname, "../.git"))
nodegit.Repository.open("/tmp/atom/.git")
  .then(function(r) {
    repo = r;
    return repo.getMasterCommit();
  })
  .then(function(firstCommitOnMaster){
    // History returns an event.
    walker = repo.createRevWalk();
    walker.push(firstCommitOnMaster.sha());
    walker.sorting(nodegit.Revwalk.SORT.Time);

    return walker.fileHistoryWalk(historyFile, 500);
  })
  .then(compileHistory)
  .then(function() {
    historyCommits.forEach(function(entry) {
      commit = entry.commit;
      console.log("commit " + commit.sha());
      console.log("Author:", commit.author().name() +
        " <" + commit.author().email() + ">");
      console.log("Date:", commit.date());
      console.log("\n    " + commit.message());
    });
    console.log("\n\n" + historyCommits.length + " total commits");
  })
  .done();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions