-
Notifications
You must be signed in to change notification settings - Fork 3
Add log subcommand
#38
New issue
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a509df8
add log subcommand
SandrineP 16cfe1c
add committer option
SandrineP b08b8ca
fix memry issue and add test
SandrineP 1b02641
free the walker
SandrineP bf9dbf8
add --max-count
SandrineP 4dca90d
address review comment
SandrineP File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.