Skip to content

Commit feead81

Browse files
committed
progress log
1 parent dcad713 commit feead81

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

src/utility/git.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,50 @@ enum class rev_match {
357357
return git_error::ok;
358358
}
359359

360+
[[nodiscard]] static git_error repository_checkout(::git_repository *repository, std::string const& rev)
361+
{
362+
::git_object *rev_obj = nullptr;
363+
::git_reference *rev_ref = nullptr;
364+
if (auto const result = ::git_revparse_ext(&rev_obj, &rev_ref, repository, rev.c_str()); result != GIT_OK) {
365+
if (result == GIT_ENOTFOUND) {
366+
// The rev was not found in the repository.
367+
return git_error::not_found;
368+
}
369+
return make_git_error(result);
370+
}
371+
auto const d1 = defer{[&] {
372+
::git_object_free(rev_obj);
373+
::git_reference_free(rev_ref);
374+
}};
375+
376+
::git_object *peeled_rev_obj = nullptr;
377+
if (auto const result = ::git_object_peel(&peeled_rev_obj, rev_obj, GIT_OBJECT_COMMIT); result != GIT_OK) {
378+
return make_git_error(result);
379+
}
380+
auto const d2 = defer{[&] { ::git_object_free(peeled_rev_obj); }};
381+
382+
auto checkout_options = ::git_checkout_options{};
383+
if (::git_checkout_options_init(&checkout_options, GIT_CHECKOUT_OPTIONS_VERSION) != 0) {
384+
return git_error::error;
385+
}
386+
checkout_options.checkout_strategy = GIT_CHECKOUT_FORCE;
387+
388+
if (auto const result = ::git_checkout_tree(repository, peeled_rev_obj, &checkout_options); result != GIT_OK) {
389+
return make_git_error(result);
390+
}
391+
392+
auto const *commit_oid = ::git_object_id(peeled_rev_obj);
393+
if (commit_oid == nullptr) {
394+
return git_error::error;
395+
}
396+
397+
if (auto const result = ::git_repository_set_head_detached(repository, commit_oid); result != GIT_OK) {
398+
return make_git_error(result);
399+
}
400+
401+
return git_error::ok;
402+
}
403+
360404
[[nodiscard]] git_error git_fetch_and_update(std::string const& url, std::string const& rev, std::filesystem::path path, git_checkout_flags flags)
361405
{
362406
auto const& _ = git_lib_initialize();

src/utility/log.hpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
#pragma once
3+
4+
#include <utility>
5+
#include <print>
6+
7+
namespace hk {
8+
9+
enum class log_mask {
10+
/** Display progress for downloading.
11+
*/
12+
progress,
13+
14+
/** Use ANSI codes to display progress prettier.
15+
*/
16+
color,
17+
18+
/** Display debug information.
19+
*/
20+
debug,
21+
22+
/** Display verbose information when using resources outside the compiler.
23+
*/
24+
verbose,
25+
26+
/** Display warnings and errors for the compiler.
27+
*
28+
* Normally enabled, but disabled in case of a language server.
29+
*/
30+
language,
31+
};
32+
33+
[[nodiscard]] constexpr log_mask operator|(log_mask lhs, log_mask rhs) noexcept
34+
{
35+
return static_cast<log_mask>(std::to_underlying(lhs) | std::to_underlying(rhs));
36+
}
37+
38+
[[nodiscard]] constexpr log_mask operator&(log_mask lhs, log_mask rhs) noexcept
39+
{
40+
return static_cast<log_mask>(std::to_underlying(lhs) | std::to_underlying(rhs));
41+
}
42+
43+
[[nodiscard]] constexpr bool to_bool(log_mask rhs) noexcept
44+
{
45+
return static_cast<bool>(std::to_underlying(rhs));
46+
}
47+
48+
inline log_mask global_log_mask = log_mask::progress | log_mask::color | log_mask::language;
49+
50+
template<log_mask Mask, typename... Args>
51+
void log(std::format_string<Args...> fmt, Args &&... args)
52+
{
53+
if (to_bool(Mask & global_log_mask)) {
54+
std::print(fmt, std::forward<Args>(args)...);
55+
}
56+
}
57+
58+
}

src/utility/progress.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
#pragma once
3+
4+
#include "log.hpp"
5+
6+
namespace hk {
7+
8+
enum class progress_state {
9+
/** Progress has not started yet.
10+
*/
11+
idle,
12+
13+
/** Currently progressing.
14+
*/
15+
running,
16+
17+
/** Progress has finished successfully.
18+
*/
19+
success,
20+
21+
/** Progress has failed.
22+
*/
23+
failed,
24+
};
25+
26+
void set_progress(float ratio);
27+
28+
void set_progress(std::string_view message);
29+
30+
/** Set the progress state.
31+
*
32+
* When set to:
33+
* - Idle shows: [ ] Message.
34+
* - Running shows: [========= ] Message.
35+
* - Running success [===SUCCESS===] Message.
36+
* - Running failed [ FAIL ] Message.
37+
*/
38+
void set_progress(progress_state state);
39+
40+
41+
}

0 commit comments

Comments
 (0)