| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| #include <format> | ||
| #include <git2.h> | ||
| #include <git2/revwalk.h> | ||
| #include <git2/types.h> | ||
| #include <string_view> | ||
|
|
||
| #include "log_subcommand.hpp" | ||
| #include "../wrapper/repository_wrapper.hpp" | ||
| #include "../wrapper/commit_wrapper.hpp" | ||
|
|
||
| log_subcommand::log_subcommand(const libgit2_object&, CLI::App& app) | ||
| { | ||
| auto *sub = app.add_subcommand("log", "Shows commit logs"); | ||
|
|
||
| sub->add_flag("--format", m_format_flag, "Pretty-print the contents of the commit logs in a given format, where <format> can be one of full and fuller"); | ||
| sub->add_option("-n,--max-count", m_max_count_flag, "Limit the output to <number> commits."); | ||
| // sub->add_flag("--oneline", m_oneline_flag, "This is a shorthand for --pretty=oneline --abbrev-commit used together."); | ||
|
|
||
| sub->callback([this]() { this->run(); }); | ||
| }; | ||
|
|
||
| void print_time(git_time intime, std::string prefix) | ||
| { | ||
| char sign, out[32]; | ||
| struct tm *intm; | ||
| int offset, hours, minutes; | ||
| time_t t; | ||
|
|
||
| offset = intime.offset; | ||
| if (offset < 0) { | ||
| sign = '-'; | ||
| offset = -offset; | ||
| } | ||
| else | ||
| { | ||
| sign = '+'; | ||
| } | ||
|
|
||
| hours = offset / 60; | ||
| minutes = offset % 60; | ||
|
|
||
| t = (time_t)intime.time + (intime.offset * 60); | ||
|
|
||
| intm = gmtime(&t); | ||
| strftime(out, sizeof(out), "%a %b %e %T %Y", intm); | ||
|
|
||
| std::cout << prefix << out << " " << sign << std::format("{:02d}", hours) << std::format("{:02d}", minutes) <<std::endl; | ||
| } | ||
|
|
||
| void print_commit(const commit_wrapper& commit, std::string m_format_flag) | ||
| { | ||
| std::string buf = commit.commit_oid_tostr(); | ||
|
|
||
| signature_wrapper author = signature_wrapper::get_commit_author(commit); | ||
| signature_wrapper committer = signature_wrapper::get_commit_committer(commit); | ||
|
|
||
| std::cout << "\033[0;33m" << "commit " << buf << "\033[0m" << std::endl; | ||
| if (m_format_flag=="fuller") | ||
| { | ||
| std::cout << "Author:\t " << author.name() << " " << author.email() << std::endl; | ||
| print_time(author.when(), "AuthorDate: "); | ||
| std::cout << "Commit:\t " << committer.name() << " " << committer.email() << std::endl; | ||
| print_time(committer.when(), "CommitDate: "); | ||
| } | ||
| else | ||
| { | ||
| std::cout << "Author:\t" << author.name() << " " << author.email() << std::endl; | ||
| if (m_format_flag=="full") | ||
| { | ||
| std::cout << "Commit:\t" << committer.name() << " " << committer.email() << std::endl; | ||
| } | ||
| else | ||
| { | ||
| print_time(author.when(), "Date:\t"); | ||
| } | ||
| } | ||
| std::cout << "\n " << git_commit_message(commit) << "\n" << std::endl; | ||
| } | ||
|
|
||
| void log_subcommand::run() | ||
| { | ||
| auto directory = get_current_git_path(); | ||
| auto bare = false; | ||
| auto repo = repository_wrapper::init(directory, bare); | ||
| // auto branch_name = repo.head().short_name(); | ||
|
|
||
| git_revwalk* walker; | ||
| git_revwalk_new(&walker, repo); | ||
| git_revwalk_push_head(walker); | ||
|
|
||
| std::size_t i=0; | ||
| git_oid commit_oid; | ||
| while (!git_revwalk_next(&commit_oid, walker) && i<m_max_count_flag) | ||
| { | ||
| commit_wrapper commit = repo.find_commit(commit_oid); | ||
| print_commit(commit, m_format_flag); | ||
| ++i; | ||
| } | ||
|
|
||
| git_revwalk_free(walker); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #pragma once | ||
|
|
||
| #include <CLI/CLI.hpp> | ||
| #include <cstddef> | ||
| #include <limits> | ||
|
|
||
| #include "../utils/common.hpp" | ||
|
|
||
|
|
||
| class log_subcommand | ||
| { | ||
| public: | ||
|
|
||
| explicit log_subcommand(const libgit2_object&, CLI::App& app); | ||
| void run(); | ||
|
|
||
| private: | ||
| std::string m_format_flag; | ||
| int m_max_count_flag=std::numeric_limits<int>::max(); | ||
| // bool m_oneline_flag = false; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import subprocess | ||
|
|
||
| import pytest | ||
|
|
||
| @pytest.mark.parametrize("format_flag", ["", "--format=full", "--format=fuller"]) | ||
| def test_log(xtl_clone, git_config, git2cpp_path, tmp_path, monkeypatch, format_flag): | ||
| assert (tmp_path / "xtl").exists() | ||
| xtl_path = tmp_path / "xtl" | ||
|
|
||
| p = xtl_path / "mook_file.txt" | ||
| p.write_text('') | ||
|
|
||
| cmd_add = [git2cpp_path, 'add', "mook_file.txt"] | ||
| p_add = subprocess.run(cmd_add, cwd=xtl_path, text=True) | ||
| assert p_add.returncode == 0 | ||
|
|
||
| cmd_commit = [git2cpp_path, 'commit', "-m", "test commit"] | ||
| p_commit = subprocess.run(cmd_commit, cwd=xtl_path, text=True) | ||
| assert p_commit.returncode == 0 | ||
|
|
||
| cmd_log = [git2cpp_path, 'log'] | ||
| if format_flag != "": | ||
| cmd_log.append(format_flag) | ||
| p_log = subprocess.run(cmd_log, capture_output=True, cwd=xtl_path, text=True) | ||
| assert p_log.returncode == 0 | ||
| assert "Jane Doe" in p_log.stdout | ||
| assert "test commit" in p_log.stdout | ||
|
|
||
| if format_flag == "": | ||
| assert "Commit" not in p_log.stdout | ||
| else: | ||
| assert "Commit" in p_log.stdout | ||
| if format_flag == "--format=full": | ||
| assert "Date" not in p_log.stdout | ||
| else: | ||
| assert "CommitDate" in p_log.stdout | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("max_count_flag", ["", "-n", "--max-count"]) | ||
| def test_max_count(xtl_clone, git_config, git2cpp_path, tmp_path, monkeypatch, max_count_flag): | ||
| assert (tmp_path / "xtl").exists() | ||
| xtl_path = tmp_path / "xtl" | ||
|
|
||
| cmd_log = [git2cpp_path, 'log'] | ||
| if max_count_flag != "": | ||
| cmd_log.append(max_count_flag) | ||
| cmd_log.append("2") | ||
| p_log = subprocess.run(cmd_log, capture_output=True, cwd=xtl_path, text=True) | ||
| assert p_log.returncode == 0 | ||
|
|
||
| if max_count_flag == "": | ||
| assert p_log.stdout.count("Author") > 2 | ||
| else: | ||
| assert p_log.stdout.count("Author") == 2 |
| Back | FazBrowse Home | New Git URL |
Uh oh!
There was an error while loading. Please reload this page.