cmake/Source/CTest/cmCTestGIT.cxx

655 lines
19 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2009-10-04 10:30:41 +03:00
#include "cmCTestGIT.h"
2020-02-01 23:06:01 +01:00
#include <cstdio>
#include <cstdlib>
#include <ctime>
2020-08-30 11:54:41 +02:00
#include <utility>
2020-02-01 23:06:01 +01:00
#include <vector>
2024-04-14 22:45:38 +02:00
#include <cmext/algorithm>
2017-07-20 19:35:53 +02:00
#include "cmsys/FStream.hxx"
2009-10-04 10:30:41 +03:00
2017-04-14 19:02:05 +02:00
#include "cmCTest.h"
#include "cmCTestVC.h"
2023-07-02 19:51:09 +02:00
#include "cmList.h"
2017-04-14 19:02:05 +02:00
#include "cmProcessOutput.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
2024-04-14 22:45:38 +02:00
#include "cmUVProcessChain.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
2017-04-14 19:02:05 +02:00
2011-06-19 15:41:06 +03:00
static unsigned int cmCTestGITVersion(unsigned int epic, unsigned int major,
unsigned int minor, unsigned int fix)
{
// 1.6.5.0 maps to 10605000
2022-03-29 21:10:50 +02:00
return epic * 10000000 + major * 100000 + minor * 1000 + fix;
2011-06-19 15:41:06 +03:00
}
2016-07-09 11:21:54 +02:00
cmCTestGIT::cmCTestGIT(cmCTest* ct, std::ostream& log)
: cmCTestGlobalVC(ct, log)
2009-10-04 10:30:41 +03:00
{
this->PriorRev = this->Unknown;
2011-06-19 15:41:06 +03:00
this->CurrentGitVersion = 0;
2009-10-04 10:30:41 +03:00
}
2019-11-11 23:01:05 +01:00
cmCTestGIT::~cmCTestGIT() = default;
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
class cmCTestGIT::OneLineParser : public cmCTestVC::LineParser
2009-10-04 10:30:41 +03:00
{
public:
2016-07-09 11:21:54 +02:00
OneLineParser(cmCTestGIT* git, const char* prefix, std::string& l)
: Line1(l)
{
2009-10-04 10:30:41 +03:00
this->SetLog(&git->Log, prefix);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
private:
std::string& Line1;
2018-01-26 17:06:56 +01:00
bool ProcessLine() override
2016-07-09 11:21:54 +02:00
{
2009-10-04 10:30:41 +03:00
// Only the first line is of interest.
this->Line1 = this->Line;
return false;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
};
std::string cmCTestGIT::GetWorkingRevision()
{
// Run plumbing "git rev-list" to get work tree revision.
2024-04-14 22:45:38 +02:00
std::string git = this->CommandLineTool;
std::vector<std::string> git_rev_list = { git, "rev-list", "-n",
"1", "HEAD", "--" };
2009-10-04 10:30:41 +03:00
std::string rev;
OneLineParser out(this, "rl-out> ", rev);
OutputLogger err(this->Log, "rl-err> ");
this->RunChild(git_rev_list, &out, &err);
return rev;
}
2017-07-20 19:35:53 +02:00
bool cmCTestGIT::NoteOldRevision()
2009-10-04 10:30:41 +03:00
{
this->OldRevision = this->GetWorkingRevision();
2018-08-09 18:06:22 +02:00
cmCTestLog(this->CTest, HANDLER_OUTPUT,
" Old revision of repository is: " << this->OldRevision
<< "\n");
2009-10-04 10:30:41 +03:00
this->PriorRev.Rev = this->OldRevision;
2017-07-20 19:35:53 +02:00
return true;
2009-10-04 10:30:41 +03:00
}
2017-07-20 19:35:53 +02:00
bool cmCTestGIT::NoteNewRevision()
2009-10-04 10:30:41 +03:00
{
this->NewRevision = this->GetWorkingRevision();
2018-08-09 18:06:22 +02:00
cmCTestLog(this->CTest, HANDLER_OUTPUT,
" New revision of repository is: " << this->NewRevision
<< "\n");
2017-07-20 19:35:53 +02:00
return true;
2009-10-04 10:30:41 +03:00
}
2010-11-13 01:00:53 +02:00
std::string cmCTestGIT::FindGitDir()
{
std::string git_dir;
// Run "git rev-parse --git-dir" to locate the real .git directory.
2024-04-14 22:45:38 +02:00
std::string git = this->CommandLineTool;
std::vector<std::string> git_rev_parse = { git, "rev-parse", "--git-dir" };
2010-11-13 01:00:53 +02:00
std::string git_dir_line;
OneLineParser rev_parse_out(this, "rev-parse-out> ", git_dir_line);
OutputLogger rev_parse_err(this->Log, "rev-parse-err> ");
2024-04-14 22:45:38 +02:00
if (this->RunChild(git_rev_parse, &rev_parse_out, &rev_parse_err,
std::string{}, cmProcessOutput::UTF8)) {
2010-11-13 01:00:53 +02:00
git_dir = git_dir_line;
2016-07-09 11:21:54 +02:00
}
if (git_dir.empty()) {
2010-11-13 01:00:53 +02:00
git_dir = ".git";
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
// Git reports a relative path only when the .git directory is in
// the current directory.
2016-07-09 11:21:54 +02:00
if (git_dir[0] == '.') {
2010-11-13 01:00:53 +02:00
git_dir = this->SourceDirectory + "/" + git_dir;
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
#if defined(_WIN32) && !defined(__CYGWIN__)
2016-07-09 11:21:54 +02:00
else if (git_dir[0] == '/') {
2010-11-13 01:00:53 +02:00
// Cygwin Git reports a full path that Cygwin understands, but we
// are a Windows application. Run "cygpath" to get Windows path.
2020-02-01 23:06:01 +01:00
std::string cygpath_exe =
cmStrCat(cmSystemTools::GetFilenamePath(git), "/cygpath.exe");
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileExists(cygpath_exe)) {
2024-04-14 22:45:38 +02:00
std::vector<std::string> cygpath = { cygpath_exe, "-w", git_dir };
2010-11-13 01:00:53 +02:00
OneLineParser cygpath_out(this, "cygpath-out> ", git_dir_line);
OutputLogger cygpath_err(this->Log, "cygpath-err> ");
2024-04-14 22:45:38 +02:00
if (this->RunChild(cygpath, &cygpath_out, &cygpath_err, std::string{},
2017-04-14 19:02:05 +02:00
cmProcessOutput::UTF8)) {
2010-11-13 01:00:53 +02:00
git_dir = git_dir_line;
}
}
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
#endif
return git_dir;
}
std::string cmCTestGIT::FindTopDir()
{
std::string top_dir = this->SourceDirectory;
// Run "git rev-parse --show-cdup" to locate the top of the tree.
2024-04-14 22:45:38 +02:00
std::string git = this->CommandLineTool;
std::vector<std::string> git_rev_parse = { git, "rev-parse", "--show-cdup" };
2010-11-13 01:00:53 +02:00
std::string cdup;
OneLineParser rev_parse_out(this, "rev-parse-out> ", cdup);
OutputLogger rev_parse_err(this->Log, "rev-parse-err> ");
2024-04-14 22:45:38 +02:00
if (this->RunChild(git_rev_parse, &rev_parse_out, &rev_parse_err, "",
2017-04-14 19:02:05 +02:00
cmProcessOutput::UTF8) &&
2016-07-09 11:21:54 +02:00
!cdup.empty()) {
2010-11-13 01:00:53 +02:00
top_dir += "/";
top_dir += cdup;
2015-04-27 22:25:09 +02:00
top_dir = cmSystemTools::CollapseFullPath(top_dir);
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
return top_dir;
}
2010-06-23 01:18:35 +03:00
bool cmCTestGIT::UpdateByFetchAndReset()
2009-10-04 10:30:41 +03:00
{
2024-04-14 22:45:38 +02:00
std::string git = this->CommandLineTool;
2009-10-04 10:30:41 +03:00
2010-06-23 01:18:35 +03:00
// Use "git fetch" to get remote commits.
2024-04-14 22:45:38 +02:00
std::vector<std::string> git_fetch;
2010-06-23 01:18:35 +03:00
git_fetch.push_back(git);
2024-04-14 22:45:38 +02:00
git_fetch.emplace_back("fetch");
2009-10-04 10:30:41 +03:00
// Add user-specified update options.
std::string opts = this->CTest->GetCTestConfiguration("UpdateOptions");
2016-07-09 11:21:54 +02:00
if (opts.empty()) {
2009-10-04 10:30:41 +03:00
opts = this->CTest->GetCTestConfiguration("GITUpdateOptions");
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
std::vector<std::string> args = cmSystemTools::ParseArguments(opts);
2024-04-14 22:45:38 +02:00
cm::append(git_fetch, args);
2010-06-23 01:18:35 +03:00
// Fetch upstream refs.
OutputLogger fetch_out(this->Log, "fetch-out> ");
OutputLogger fetch_err(this->Log, "fetch-err> ");
2024-04-14 22:45:38 +02:00
if (!this->RunUpdateCommand(git_fetch, &fetch_out, &fetch_err)) {
2010-06-23 01:18:35 +03:00
return false;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2010-06-23 01:18:35 +03:00
// Identify the merge head that would be used by "git pull".
std::string sha1;
{
2016-07-09 11:21:54 +02:00
std::string fetch_head = this->FindGitDir() + "/FETCH_HEAD";
cmsys::ifstream fin(fetch_head.c_str(), std::ios::in | std::ios::binary);
if (!fin) {
this->Log << "Unable to open " << fetch_head << "\n";
return false;
2010-11-13 01:00:53 +02:00
}
2016-07-09 11:21:54 +02:00
std::string line;
while (sha1.empty() && cmSystemTools::GetLineFromStream(fin, line)) {
this->Log << "FETCH_HEAD> " << line << "\n";
2017-07-20 19:35:53 +02:00
if (line.find("\tnot-for-merge\t") == std::string::npos) {
2016-07-09 11:21:54 +02:00
std::string::size_type pos = line.find('\t');
2017-07-20 19:35:53 +02:00
if (pos != std::string::npos) {
2020-08-30 11:54:41 +02:00
sha1 = std::move(line);
sha1.resize(pos);
2010-06-23 01:18:35 +03:00
}
}
}
2016-07-09 11:21:54 +02:00
if (sha1.empty()) {
this->Log << "FETCH_HEAD has no upstream branch candidate!\n";
return false;
2010-11-13 01:00:53 +02:00
}
2010-06-23 01:18:35 +03:00
}
// Reset the local branch to point at that tracked from upstream.
2024-04-14 22:45:38 +02:00
std::vector<std::string> git_reset = { git, "reset", "--hard", sha1 };
2010-06-23 01:18:35 +03:00
OutputLogger reset_out(this->Log, "reset-out> ");
OutputLogger reset_err(this->Log, "reset-err> ");
2024-04-14 22:45:38 +02:00
return this->RunChild(git_reset, &reset_out, &reset_err);
2010-06-23 01:18:35 +03:00
}
bool cmCTestGIT::UpdateByCustom(std::string const& custom)
{
2023-07-02 19:51:09 +02:00
cmList git_custom_command{ custom, cmList::EmptyElements::Yes };
2024-04-14 22:45:38 +02:00
std::vector<std::string> git_custom;
git_custom.reserve(git_custom_command.size());
cm::append(git_custom, git_custom_command);
2010-06-23 01:18:35 +03:00
OutputLogger custom_out(this->Log, "custom-out> ");
OutputLogger custom_err(this->Log, "custom-err> ");
2024-04-14 22:45:38 +02:00
return this->RunUpdateCommand(git_custom, &custom_out, &custom_err);
2010-06-23 01:18:35 +03:00
}
bool cmCTestGIT::UpdateInternal()
{
std::string custom = this->CTest->GetCTestConfiguration("GITUpdateCustom");
2016-07-09 11:21:54 +02:00
if (!custom.empty()) {
2010-06-23 01:18:35 +03:00
return this->UpdateByCustom(custom);
2016-07-09 11:21:54 +02:00
}
2010-06-23 01:18:35 +03:00
return this->UpdateByFetchAndReset();
}
bool cmCTestGIT::UpdateImpl()
{
2016-07-09 11:21:54 +02:00
if (!this->UpdateInternal()) {
2010-06-23 01:18:35 +03:00
return false;
2016-07-09 11:21:54 +02:00
}
2010-06-23 01:18:35 +03:00
2010-11-13 01:00:53 +02:00
std::string top_dir = this->FindTopDir();
2024-04-14 22:45:38 +02:00
std::string git = this->CommandLineTool;
std::string recursive = "--recursive";
std::string sync_recursive = "--recursive";
2011-06-19 15:41:06 +03:00
2016-07-09 11:21:54 +02:00
// Git < 1.6.5 did not support submodule --recursive
2024-04-14 22:45:38 +02:00
bool support_recursive = true;
2016-07-09 11:21:54 +02:00
if (this->GetGitVersion() < cmCTestGITVersion(1, 6, 5, 0)) {
2024-04-14 22:45:38 +02:00
support_recursive = false;
2016-07-09 11:21:54 +02:00
// No need to require >= 1.6.5 if there are no submodules.
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileExists(top_dir + "/.gitmodules")) {
2016-07-09 11:21:54 +02:00
this->Log << "Git < 1.6.5 cannot update submodules recursively\n";
2011-06-19 15:41:06 +03:00
}
2016-07-09 11:21:54 +02:00
}
// Git < 1.8.1 did not support sync --recursive
2024-04-14 22:45:38 +02:00
bool support_sync_recursive = true;
2016-07-09 11:21:54 +02:00
if (this->GetGitVersion() < cmCTestGITVersion(1, 8, 1, 0)) {
2024-04-14 22:45:38 +02:00
support_sync_recursive = false;
2016-07-09 11:21:54 +02:00
// No need to require >= 1.8.1 if there are no submodules.
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileExists(top_dir + "/.gitmodules")) {
2016-07-09 11:21:54 +02:00
this->Log << "Git < 1.8.1 cannot synchronize submodules recursively\n";
}
}
2011-06-19 15:41:06 +03:00
2010-06-23 01:18:35 +03:00
OutputLogger submodule_out(this->Log, "submodule-out> ");
OutputLogger submodule_err(this->Log, "submodule-err> ");
2016-07-09 11:21:54 +02:00
bool ret;
std::string init_submodules =
this->CTest->GetCTestConfiguration("GITInitSubmodules");
2020-02-01 23:06:01 +01:00
if (cmIsOn(init_submodules)) {
2024-04-14 22:45:38 +02:00
std::vector<std::string> git_submodule_init = { git, "submodule", "init" };
2016-07-09 11:21:54 +02:00
ret = this->RunChild(git_submodule_init, &submodule_out, &submodule_err,
2024-04-14 22:45:38 +02:00
top_dir);
2016-07-09 11:21:54 +02:00
if (!ret) {
return false;
}
}
2024-04-14 22:45:38 +02:00
std::vector<std::string> git_submodule_sync = { git, "submodule", "sync" };
if (support_sync_recursive) {
git_submodule_sync.push_back(sync_recursive);
}
2016-07-09 11:21:54 +02:00
ret = this->RunChild(git_submodule_sync, &submodule_out, &submodule_err,
2024-04-14 22:45:38 +02:00
top_dir);
2016-07-09 11:21:54 +02:00
if (!ret) {
return false;
}
2024-04-14 22:45:38 +02:00
std::vector<std::string> git_submodule = { git, "submodule", "update" };
if (support_recursive) {
git_submodule.push_back(recursive);
}
2010-11-13 01:00:53 +02:00
return this->RunChild(git_submodule, &submodule_out, &submodule_err,
2024-04-14 22:45:38 +02:00
top_dir);
2009-10-04 10:30:41 +03:00
}
2011-06-19 15:41:06 +03:00
unsigned int cmCTestGIT::GetGitVersion()
{
2016-07-09 11:21:54 +02:00
if (!this->CurrentGitVersion) {
2024-04-14 22:45:38 +02:00
std::string git = this->CommandLineTool;
std::vector<std::string> git_version = { git, "--version" };
2011-06-19 15:41:06 +03:00
std::string version;
OneLineParser version_out(this, "version-out> ", version);
OutputLogger version_err(this->Log, "version-err> ");
2016-07-09 11:21:54 +02:00
unsigned int v[4] = { 0, 0, 0, 0 };
if (this->RunChild(git_version, &version_out, &version_err) &&
sscanf(version.c_str(), "git version %u.%u.%u.%u", &v[0], &v[1], &v[2],
&v[3]) >= 3) {
2011-06-19 15:41:06 +03:00
this->CurrentGitVersion = cmCTestGITVersion(v[0], v[1], v[2], v[3]);
}
2016-07-09 11:21:54 +02:00
}
2011-06-19 15:41:06 +03:00
return this->CurrentGitVersion;
}
2009-10-04 10:30:41 +03:00
/* Diff format:
:src-mode dst-mode src-sha1 dst-sha1 status\0
src-path\0
[dst-path\0]
The format is repeated for every file changed. The [dst-path\0]
line appears only for lines with status 'C' or 'R'. See 'git help
diff-tree' for details.
*/
2016-07-09 11:21:54 +02:00
class cmCTestGIT::DiffParser : public cmCTestVC::LineParser
2009-10-04 10:30:41 +03:00
{
public:
2016-07-09 11:21:54 +02:00
DiffParser(cmCTestGIT* git, const char* prefix)
: LineParser('\0', false)
, GIT(git)
{
2009-10-04 10:30:41 +03:00
this->SetLog(&git->Log, prefix);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2020-02-01 23:06:01 +01:00
using Change = cmCTestGIT::Change;
2009-10-04 10:30:41 +03:00
std::vector<Change> Changes;
2016-07-09 11:21:54 +02:00
2009-10-04 10:30:41 +03:00
protected:
cmCTestGIT* GIT;
2016-07-09 11:21:54 +02:00
enum DiffFieldType
{
DiffFieldNone,
DiffFieldChange,
DiffFieldSrc,
DiffFieldDst
};
2022-08-04 22:12:04 +02:00
DiffFieldType DiffField = DiffFieldNone;
2009-10-04 10:30:41 +03:00
Change CurChange;
void DiffReset()
2016-07-09 11:21:54 +02:00
{
2009-10-04 10:30:41 +03:00
this->DiffField = DiffFieldNone;
this->Changes.clear();
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2018-01-26 17:06:56 +01:00
bool ProcessLine() override
2016-07-09 11:21:54 +02:00
{
if (this->Line[0] == ':') {
2009-10-04 10:30:41 +03:00
this->DiffField = DiffFieldChange;
this->CurChange = Change();
2016-07-09 11:21:54 +02:00
}
if (this->DiffField == DiffFieldChange) {
2009-10-04 10:30:41 +03:00
// :src-mode dst-mode src-sha1 dst-sha1 status
2016-07-09 11:21:54 +02:00
if (this->Line[0] != ':') {
2009-10-04 10:30:41 +03:00
this->DiffField = DiffFieldNone;
return true;
2016-07-09 11:21:54 +02:00
}
const char* src_mode_first = this->Line.c_str() + 1;
const char* src_mode_last = this->ConsumeField(src_mode_first);
2009-10-04 10:30:41 +03:00
const char* dst_mode_first = this->ConsumeSpace(src_mode_last);
2016-07-09 11:21:54 +02:00
const char* dst_mode_last = this->ConsumeField(dst_mode_first);
2009-10-04 10:30:41 +03:00
const char* src_sha1_first = this->ConsumeSpace(dst_mode_last);
2016-07-09 11:21:54 +02:00
const char* src_sha1_last = this->ConsumeField(src_sha1_first);
2009-10-04 10:30:41 +03:00
const char* dst_sha1_first = this->ConsumeSpace(src_sha1_last);
2016-07-09 11:21:54 +02:00
const char* dst_sha1_last = this->ConsumeField(dst_sha1_first);
const char* status_first = this->ConsumeSpace(dst_sha1_last);
const char* status_last = this->ConsumeField(status_first);
if (status_first != status_last) {
2009-10-04 10:30:41 +03:00
this->CurChange.Action = *status_first;
this->DiffField = DiffFieldSrc;
2016-07-09 11:21:54 +02:00
} else {
2009-10-04 10:30:41 +03:00
this->DiffField = DiffFieldNone;
}
2016-07-09 11:21:54 +02:00
} else if (this->DiffField == DiffFieldSrc) {
2009-10-04 10:30:41 +03:00
// src-path
2016-07-09 11:21:54 +02:00
if (this->CurChange.Action == 'C') {
2009-10-04 10:30:41 +03:00
// Convert copy to addition of destination.
this->CurChange.Action = 'A';
this->DiffField = DiffFieldDst;
2016-07-09 11:21:54 +02:00
} else if (this->CurChange.Action == 'R') {
2009-10-04 10:30:41 +03:00
// Convert rename to deletion of source and addition of destination.
this->CurChange.Action = 'D';
this->CurChange.Path = this->Line;
this->Changes.push_back(this->CurChange);
this->CurChange = Change('A');
this->DiffField = DiffFieldDst;
2016-07-09 11:21:54 +02:00
} else {
2009-10-04 10:30:41 +03:00
this->CurChange.Path = this->Line;
this->Changes.push_back(this->CurChange);
this->DiffField = this->DiffFieldNone;
}
2016-07-09 11:21:54 +02:00
} else if (this->DiffField == DiffFieldDst) {
2009-10-04 10:30:41 +03:00
// dst-path
this->CurChange.Path = this->Line;
this->Changes.push_back(this->CurChange);
this->DiffField = this->DiffFieldNone;
}
2016-07-09 11:21:54 +02:00
return true;
}
2009-10-04 10:30:41 +03:00
const char* ConsumeSpace(const char* c)
2016-07-09 11:21:54 +02:00
{
2024-04-14 22:45:38 +02:00
while (*c && cmIsSpace(*c)) {
2016-07-09 11:21:54 +02:00
++c;
2009-10-04 10:30:41 +03:00
}
return c;
2016-07-09 11:21:54 +02:00
}
const char* ConsumeField(const char* c)
{
2024-04-14 22:45:38 +02:00
while (*c && !cmIsSpace(*c)) {
2016-07-09 11:21:54 +02:00
++c;
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
return c;
}
2009-10-04 10:30:41 +03:00
};
/* Commit format:
commit ...\n
tree ...\n
parent ...\n
author ...\n
committer ...\n
\n
Log message indented by (4) spaces\n
(even blank lines have the spaces)\n
2010-06-28 22:39:51 +03:00
[[
2009-10-04 10:30:41 +03:00
\n
[Diff format]
2010-06-28 22:39:51 +03:00
OR
\0
]]
2009-10-04 10:30:41 +03:00
The header may have more fields. See 'git help diff-tree'.
*/
2016-07-09 11:21:54 +02:00
class cmCTestGIT::CommitParser : public cmCTestGIT::DiffParser
2009-10-04 10:30:41 +03:00
{
public:
2016-07-09 11:21:54 +02:00
CommitParser(cmCTestGIT* git, const char* prefix)
: DiffParser(git, prefix)
{
2009-10-04 10:30:41 +03:00
this->Separator = SectionSep[this->Section];
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
private:
2020-02-01 23:06:01 +01:00
using Revision = cmCTestGIT::Revision;
2016-07-09 11:21:54 +02:00
enum SectionType
{
SectionHeader,
SectionBody,
SectionDiff,
SectionCount
};
2009-10-04 10:30:41 +03:00
static char const SectionSep[SectionCount];
2022-08-04 22:12:04 +02:00
SectionType Section = SectionHeader;
2009-10-04 10:30:41 +03:00
Revision Rev;
struct Person
{
std::string Name;
std::string EMail;
2019-11-11 23:01:05 +01:00
unsigned long Time = 0;
long TimeZone = 0;
2009-10-04 10:30:41 +03:00
};
void ParsePerson(const char* str, Person& person)
2016-07-09 11:21:54 +02:00
{
2009-10-04 10:30:41 +03:00
// Person Name <person@domain.com> 1234567890 +0000
const char* c = str;
2024-04-14 22:45:38 +02:00
while (*c && cmIsSpace(*c)) {
2016-07-09 11:21:54 +02:00
++c;
}
2009-10-04 10:30:41 +03:00
const char* name_first = c;
2016-07-09 11:21:54 +02:00
while (*c && *c != '<') {
++c;
}
2009-10-04 10:30:41 +03:00
const char* name_last = c;
2024-04-14 22:45:38 +02:00
while (name_last != name_first && cmIsSpace(*(name_last - 1))) {
2016-07-09 11:21:54 +02:00
--name_last;
}
person.Name.assign(name_first, name_last - name_first);
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
const char* email_first = *c ? ++c : c;
while (*c && *c != '>') {
++c;
}
const char* email_last = *c ? c++ : c;
person.EMail.assign(email_first, email_last - email_first);
2009-10-04 10:30:41 +03:00
2018-01-26 17:06:56 +01:00
person.Time = strtoul(c, const_cast<char**>(&c), 10);
person.TimeZone = strtol(c, const_cast<char**>(&c), 10);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2018-01-26 17:06:56 +01:00
bool ProcessLine() override
2016-07-09 11:21:54 +02:00
{
if (this->Line.empty()) {
if (this->Section == SectionBody && this->LineEnd == '\0') {
2010-06-28 22:39:51 +03:00
// Skip SectionDiff
this->NextSection();
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
this->NextSection();
} else {
switch (this->Section) {
case SectionHeader:
this->DoHeaderLine();
break;
case SectionBody:
this->DoBodyLine();
break;
case SectionDiff:
this->DiffParser::ProcessLine();
break;
case SectionCount:
break; // never happens
2009-10-04 10:30:41 +03:00
}
}
2016-07-09 11:21:54 +02:00
return true;
}
2009-10-04 10:30:41 +03:00
void NextSection()
2016-07-09 11:21:54 +02:00
{
2022-08-04 22:12:04 +02:00
this->Section =
static_cast<SectionType>((this->Section + 1) % SectionCount);
2009-10-04 10:30:41 +03:00
this->Separator = SectionSep[this->Section];
2016-07-09 11:21:54 +02:00
if (this->Section == SectionHeader) {
2009-10-04 10:30:41 +03:00
this->GIT->DoRevision(this->Rev, this->Changes);
this->Rev = Revision();
this->DiffReset();
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
void DoHeaderLine()
2016-07-09 11:21:54 +02:00
{
2009-10-04 10:30:41 +03:00
// Look for header fields that we need.
2018-04-23 21:13:27 +02:00
if (cmHasLiteralPrefix(this->Line, "commit ")) {
2019-11-11 23:01:05 +01:00
this->Rev.Rev = this->Line.substr(7);
2018-04-23 21:13:27 +02:00
} else if (cmHasLiteralPrefix(this->Line, "author ")) {
2009-10-04 10:30:41 +03:00
Person author;
2016-07-09 11:21:54 +02:00
this->ParsePerson(this->Line.c_str() + 7, author);
2009-10-04 10:30:41 +03:00
this->Rev.Author = author.Name;
2010-06-23 01:18:35 +03:00
this->Rev.EMail = author.EMail;
2011-01-16 11:35:12 +01:00
this->Rev.Date = this->FormatDateTime(author);
2018-04-23 21:13:27 +02:00
} else if (cmHasLiteralPrefix(this->Line, "committer ")) {
2011-01-16 11:35:12 +01:00
Person committer;
2016-07-09 11:21:54 +02:00
this->ParsePerson(this->Line.c_str() + 10, committer);
2011-01-16 11:35:12 +01:00
this->Rev.Committer = committer.Name;
this->Rev.CommitterEMail = committer.EMail;
this->Rev.CommitDate = this->FormatDateTime(committer);
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
void DoBodyLine()
2016-07-09 11:21:54 +02:00
{
2009-10-04 10:30:41 +03:00
// Commit log lines are indented by 4 spaces.
2016-07-09 11:21:54 +02:00
if (this->Line.size() >= 4) {
2009-10-04 10:30:41 +03:00
this->Rev.Log += this->Line.substr(4);
}
2016-07-09 11:21:54 +02:00
this->Rev.Log += "\n";
}
2011-01-16 11:35:12 +01:00
std::string FormatDateTime(Person const& person)
2016-07-09 11:21:54 +02:00
{
2011-01-16 11:35:12 +01:00
// Convert the time to a human-readable format that is also easy
// to machine-parse: "CCYY-MM-DD hh:mm:ss".
time_t seconds = static_cast<time_t>(person.Time);
struct tm* t = gmtime(&seconds);
char dt[1024];
2022-03-29 21:10:50 +02:00
snprintf(dt, sizeof(dt), "%04d-%02d-%02d %02d:%02d:%02d",
t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour,
t->tm_min, t->tm_sec);
2011-01-16 11:35:12 +01:00
std::string out = dt;
// Add the time-zone field "+zone" or "-zone".
char tz[32];
2016-07-09 11:21:54 +02:00
if (person.TimeZone >= 0) {
2022-03-29 21:10:50 +02:00
snprintf(tz, sizeof(tz), " +%04ld", person.TimeZone);
2016-07-09 11:21:54 +02:00
} else {
2022-03-29 21:10:50 +02:00
snprintf(tz, sizeof(tz), " -%04ld", -person.TimeZone);
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
out += tz;
return out;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
};
2016-07-09 11:21:54 +02:00
char const cmCTestGIT::CommitParser::SectionSep[SectionCount] = { '\n', '\n',
'\0' };
2009-10-04 10:30:41 +03:00
2017-07-20 19:35:53 +02:00
bool cmCTestGIT::LoadRevisions()
2009-10-04 10:30:41 +03:00
{
// Use 'git rev-list ... | git diff-tree ...' to get revisions.
std::string range = this->OldRevision + ".." + this->NewRevision;
2024-04-14 22:45:38 +02:00
std::string git = this->CommandLineTool;
std::vector<std::string> git_rev_list = { git, "rev-list", "--reverse",
range, "--" };
std::vector<std::string> git_diff_tree = {
git, "diff-tree", "--stdin", "--always",
"-z", "-r", "--pretty=raw", "--encoding=utf-8"
2016-07-09 11:21:54 +02:00
};
2019-11-11 23:01:05 +01:00
this->Log << cmCTestGIT::ComputeCommandLine(git_rev_list) << " | "
<< cmCTestGIT::ComputeCommandLine(git_diff_tree) << "\n";
2009-10-04 10:30:41 +03:00
2024-04-14 22:45:38 +02:00
cmUVProcessChainBuilder builder;
builder.AddCommand(git_rev_list)
.AddCommand(git_diff_tree)
.SetWorkingDirectory(this->SourceDirectory);
2009-10-04 10:30:41 +03:00
CommitParser out(this, "dt-out> ");
OutputLogger err(this->Log, "dt-err> ");
2024-04-14 22:45:38 +02:00
cmCTestGIT::RunProcess(builder, &out, &err, cmProcessOutput::UTF8);
2009-10-04 10:30:41 +03:00
// Send one extra zero-byte to terminate the last record.
out.Process("", 1);
2017-07-20 19:35:53 +02:00
return true;
2009-10-04 10:30:41 +03:00
}
2017-07-20 19:35:53 +02:00
bool cmCTestGIT::LoadModifications()
2009-10-04 10:30:41 +03:00
{
2024-04-14 22:45:38 +02:00
std::string git = this->CommandLineTool;
2009-10-04 10:30:41 +03:00
2010-03-17 14:00:29 +02:00
// Use 'git update-index' to refresh the index w.r.t. the work tree.
2024-04-14 22:45:38 +02:00
std::vector<std::string> git_update_index = { git, "update-index",
"--refresh" };
2010-03-17 14:00:29 +02:00
OutputLogger ui_out(this->Log, "ui-out> ");
OutputLogger ui_err(this->Log, "ui-err> ");
2024-04-14 22:45:38 +02:00
this->RunChild(git_update_index, &ui_out, &ui_err, "",
2017-04-14 19:02:05 +02:00
cmProcessOutput::UTF8);
2010-03-17 14:00:29 +02:00
// Use 'git diff-index' to get modified files.
2024-04-14 22:45:38 +02:00
std::vector<std::string> git_diff_index = { git, "diff-index", "-z", "HEAD",
"--" };
2009-10-04 10:30:41 +03:00
DiffParser out(this, "di-out> ");
OutputLogger err(this->Log, "di-err> ");
2024-04-14 22:45:38 +02:00
this->RunChild(git_diff_index, &out, &err, "", cmProcessOutput::UTF8);
2009-10-04 10:30:41 +03:00
2018-01-26 17:06:56 +01:00
for (Change const& c : out.Changes) {
this->DoModification(PathModified, c.Path);
2016-07-09 11:21:54 +02:00
}
2017-07-20 19:35:53 +02:00
return true;
2009-10-04 10:30:41 +03:00
}