Skip to content

Commit

Permalink
Add a method to get n prior commits
Browse files Browse the repository at this point in the history
Summary: Add a method to the existing SCM abstraction to return the commits prior to and including the specified commit.

Reviewed By: strager

Differential Revision: D8226277

fbshipit-source-id: 6d95323cfe3b945fd76254d8c9a401a1299cfe5b
  • Loading branch information
kcoons authored and facebook-github-bot committed Aug 2, 2018
1 parent 3d2c032 commit d8fd190
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
25 changes: 25 additions & 0 deletions scm/Mercurial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,29 @@ time_point<system_clock> Mercurial::convertCommitDate(const char* commitDate) {
}
return system_clock::from_time_t(date);
}

std::vector<w_string> Mercurial::getCommitsPriorToAndIncluding(
w_string_piece commitId,
int numCommits,
w_string requestId) const {
auto revset = to<std::string>(
"reverse(last(_firstancestors(", commitId, "), ", numCommits, "))\n");
ChildProcess proc(
{hgExecutablePath(), "log", "-r", revset, "-T", "{node}\n"},
makeHgOptions(requestId));
auto outputs = proc.communicate();
auto status = proc.wait();
if (status) {
throw std::runtime_error(to<std::string>(
"failed query for hg log; command returned with status ",
status,
" out=",
outputs.first,
" err=",
outputs.second));
}
std::vector<w_string> lines;
w_string_piece(outputs.first).split(lines, '\n');
return lines;
}
} // namespace watchman
4 changes: 4 additions & 0 deletions scm/Mercurial.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class Mercurial : public SCM {
// public for testing
static std::chrono::time_point<std::chrono::system_clock> convertCommitDate(
const char* commitDate);
std::vector<w_string> getCommitsPriorToAndIncluding(
w_string_piece commitId,
int numCommits,
w_string requestId = nullptr) const override;

private:
// Returns options for invoking hg
Expand Down
8 changes: 8 additions & 0 deletions scm/SCM.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ class SCM {
w_string_piece commitId,
w_string requestId = nullptr) const = 0;

// Compute the numCommits commits prior to and including the specified commit
// in source control history. Returns an ordered list with the most recent
// commit (the one specified) first.
virtual std::vector<w_string> getCommitsPriorToAndIncluding(
w_string_piece commitId,
int numCommits,
w_string requestId = nullptr) const = 0;

private:
w_string rootPath_;
w_string scmRoot_;
Expand Down
8 changes: 8 additions & 0 deletions watcher/eden.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,14 @@ class EdenWrappedSCM : public SCM {
return inner_->getCommitDate(commitId, requestId);
}

std::vector<w_string> getCommitsPriorToAndIncluding(
w_string_piece commitId,
int numCommits,
w_string requestId = nullptr) const override {
return inner_->getCommitsPriorToAndIncluding(
commitId, numCommits, requestId);
}

static std::unique_ptr<EdenWrappedSCM> wrap(std::unique_ptr<SCM> inner) {
if (!inner) {
return nullptr;
Expand Down

0 comments on commit d8fd190

Please sign in to comment.