windows: fix get-commit-db

This commit is contained in:
Megamouse 2021-08-28 18:14:47 +02:00
parent 1060e93783
commit 2431fcc2a1

View file

@ -307,7 +307,7 @@ QCoreApplication* createApplication(int& argc, char* argv[])
if (const int i_rounding = find_arg(arg_rounding, argc, argv); i_rounding != -1)
{
if (const int i_rounding_2 = (argc > (i_rounding + 1)) ? (i_rounding + 1) : 0; i_rounding_2)
if (const int i_rounding_2 = i_rounding + 1; argc > i_rounding_2)
{
if (const auto arg_val = argv[i_rounding_2]; !check_dpi_rounding_arg(arg_val))
{
@ -533,7 +533,7 @@ int main(int argc, char** argv)
parser.addOption(QCommandLineOption(arg_q_debug, "Log qDebug to RPCS3.log."));
parser.addOption(QCommandLineOption(arg_error, "For internal usage."));
parser.addOption(QCommandLineOption(arg_updating, "For internal usage."));
parser.addOption(QCommandLineOption(arg_commit_db, "Update commits.lst cache."));
parser.addOption(QCommandLineOption(arg_commit_db, "Update commits.lst cache. Optional arguments: <path> <sha>"));
parser.process(app->arguments());
// Don't start up the full rpcs3 gui if we just want the version or help.
@ -542,10 +542,52 @@ int main(int argc, char** argv)
if (parser.isSet(arg_commit_db))
{
fs::file file(argc > 2 ? argv[2] : "bin/git/commits.lst", fs::read + fs::write + fs::append + fs::create);
#ifdef _WIN32
if (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())
[[maybe_unused]] const auto con_out = freopen("CONOUT$", "w", stdout);
[[maybe_unused]] const auto con_err = freopen("CONOUT$", "w", stderr);
if (file)
std::string path;
#else
std::string path = "bin/git/commits.lst";
#endif
std::string from_sha;
if (const int i_arg_commit_db = find_arg(arg_commit_db, argc, argv); i_arg_commit_db != -1)
{
if (int i = i_arg_commit_db + 1; argc > i)
{
path = argv[i++];
if (argc > i)
{
from_sha = argv[i];
}
}
#ifdef _WIN32
else
{
fprintf(stderr, "Missing path argument.\n");
return 1;
}
#endif
}
else
{
fprintf(stderr, "Can not find argument --%s\n", arg_commit_db);
return 1;
}
fs::file file(path, fs::read + fs::write + fs::append + fs::create);
if (!file)
{
fprintf(stderr, "Failed to open file: '%s' (errno=%d)\n", path.c_str(), errno);
return 1;
}
fprintf(stdout, "\nAppending commits to '%s' ...\n", path.c_str());
// Get existing list
std::string data = file.to_string();
std::vector<std::string> list = fmt::split(data, {"\n"});
@ -553,12 +595,7 @@ int main(int argc, char** argv)
const bool was_empty = data.empty();
// SHA to start
std::string from, last;
if (argc > 3)
{
from = argv[3];
}
std::string last;
if (!list.empty())
{
@ -566,7 +603,7 @@ int main(int argc, char** argv)
QByteArray buf(list.back().c_str(), list.back().size());
QJsonDocument doc = QJsonDocument::fromJson(buf);
if (doc.isObject())
if (doc.isObject() && doc["sha"].isString())
{
last = doc["sha"].toString().toStdString();
}
@ -602,10 +639,12 @@ int main(int argc, char** argv)
while (page <= 55)
{
fprintf(stdout, "Fetching page %d ...\n", page);
std::string url = "https://api.github.com/repos/RPCS3/rpcs3/commits?per_page=";
fmt::append(url, "%u&page=%u", per_page, page++);
if (!from.empty())
fmt::append(url, "&sha=%s", from);
if (!from_sha.empty())
fmt::append(url, "&sha=%s", from_sha);
err = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
if (err != CURLE_OK)
@ -633,8 +672,12 @@ int main(int argc, char** argv)
for (auto&& ref : info.array())
{
if (ref.isObject())
if (!ref.isObject())
{
page = -1;
break;
}
count++;
QJsonObject result, author, committer;
@ -668,7 +711,7 @@ int main(int argc, char** argv)
buf = out.toJson(QJsonDocument::JsonFormat::Compact);
buf += "\n";
if (was_empty || !from.empty())
if (was_empty || !from_sha.empty())
{
data = buf.toStdString() + std::move(data);
}
@ -683,12 +726,6 @@ int main(int argc, char** argv)
list.emplace_back(buf.data(), buf.size());
}
}
else
{
page = -1;
break;
}
}
buf.clear();
@ -698,7 +735,7 @@ int main(int argc, char** argv)
}
}
if (was_empty || !from.empty())
if (was_empty || !from_sha.empty())
{
file.trunc(0);
file.write(data);
@ -713,13 +750,8 @@ int main(int argc, char** argv)
}
curl_slist_free_all(hhdr);
}
else
{
fprintf(stderr, "Failed to open file: %s.\n", argv[2]);
return 1;
}
fprintf(stdout, "Finished fetching commits\n", path.c_str());
return 0;
}