-
Notifications
You must be signed in to change notification settings - Fork 3
Add commit
and reset
subcommand
#29
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 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b977235
add commit subcommand
SandrineP 4dbb90d
add reset subcommand
SandrineP da951b1
write index
SandrineP eef5385
fix
SandrineP 0c7c928
small fix
SandrineP 7ee91ab
test commit
SandrineP 8082673
update test
SandrineP 516dba5
remove test file
SandrineP 1076d42
fix test
SandrineP 75d16ee
add branch to test
SandrineP fee3016
edit branch name
SandrineP 8b9eaf4
remove mook test file
SandrineP 516e726
test commit
SandrineP 2ba0ac9
edit file path
SandrineP 954587d
address review comments
SandrineP d6c35c3
edit option
SandrineP 67f57dc
remove extra space
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
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,34 @@ | ||
#include <git2.h> | ||
#include <unistd.h> | ||
|
||
#include "commit_subcommand.hpp" | ||
#include "../wrapper/index_wrapper.hpp" | ||
#include "../wrapper/repository_wrapper.hpp" | ||
|
||
|
||
commit_subcommand::commit_subcommand(const libgit2_object&, CLI::App& app) | ||
{ | ||
auto *sub = app.add_subcommand("commit", "Record changes to the repository"); | ||
|
||
sub->add_option("commit_message", m_commit_message, "Commit message"); | ||
|
||
sub->add_flag("-m,--message", m_commit_message_flag, ""); | ||
|
||
sub->callback([this]() { this->run(); }); | ||
}; | ||
|
||
|
||
void commit_subcommand::run() | ||
{ | ||
auto directory = get_current_git_path(); | ||
auto bare = false; | ||
auto repo = repository_wrapper::init(directory, bare); | ||
auto author_committer_signatures = signature_wrapper::get_default_signature_from_env(repo); | ||
SandrineP marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
if (!m_commit_message_flag) | ||
{ | ||
throw std::runtime_error("Please provide a message using the -m flag."); | ||
} | ||
|
||
repo.create_commit(author_committer_signatures, m_commit_message); | ||
} |
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,17 @@ | ||
#pragma once | ||
|
||
#include <CLI/CLI.hpp> | ||
|
||
#include "../utils/common.hpp" | ||
|
||
class commit_subcommand | ||
{ | ||
public: | ||
|
||
explicit commit_subcommand(const libgit2_object&, CLI::App& app); | ||
void run(); | ||
|
||
private: | ||
bool m_commit_message_flag = true; // TODO: change to false when a message can be provided if the "-m" flag is not provided | ||
std::string m_commit_message; | ||
}; |
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,61 @@ | ||
#include "reset_subcommand.hpp" | ||
// #include "../wrapper/index_wrapper.hpp" | ||
#include "../wrapper/repository_wrapper.hpp" | ||
#include <stdexcept> | ||
|
||
enum class reset_type | ||
{ | ||
GIT_RESET_SOFT = 1, | ||
GIT_RESET_MIXED = 2, | ||
GIT_RESET_HARD = 3 | ||
}; | ||
|
||
reset_subcommand::reset_subcommand(const libgit2_object&, CLI::App& app) | ||
{ | ||
auto *sub = app.add_subcommand("reset", "Reset current HEAD to the specified state"); | ||
|
||
sub->add_option("<commit>", m_commit, "The ID of the commit that will become HEAD"); | ||
|
||
sub->add_flag("--soft", m_soft_flag, ""); | ||
sub->add_flag("--mixed", m_mixed_flag, ""); | ||
sub->add_flag("--hard", m_hard_flag, ""); | ||
|
||
sub->callback([this]() { this->run(); }); | ||
}; | ||
|
||
|
||
void reset_subcommand::run() | ||
{ | ||
auto directory = get_current_git_path(); | ||
auto bare = false; | ||
auto repo = repository_wrapper::init(directory, bare); | ||
|
||
auto target = repo.revparse_single(m_commit); | ||
if (!target) | ||
{ | ||
throw std::runtime_error("Target revision not found."); | ||
} | ||
|
||
git_checkout_options options; | ||
git_checkout_options_init(&options, GIT_CHECKOUT_OPTIONS_VERSION); | ||
|
||
git_reset_t reset_type; | ||
if (m_soft_flag) | ||
{ | ||
reset_type = GIT_RESET_SOFT; | ||
} | ||
if (m_mixed_flag) | ||
{ | ||
reset_type = GIT_RESET_MIXED; | ||
} | ||
if (m_hard_flag) | ||
{ | ||
reset_type = GIT_RESET_HARD; | ||
if (m_commit.empty()) | ||
{ | ||
m_commit = "HEAD"; | ||
} | ||
} | ||
|
||
repo.reset(target.value(), reset_type, options); | ||
} |
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,19 @@ | ||
#pragma once | ||
|
||
#include <CLI/CLI.hpp> | ||
|
||
#include "../utils/common.hpp" | ||
|
||
class reset_subcommand | ||
{ | ||
public: | ||
|
||
explicit reset_subcommand(const libgit2_object&, CLI::App& app); | ||
void run(); | ||
|
||
private: | ||
std::string m_commit; | ||
bool m_soft_flag = false; | ||
bool m_mixed_flag = false; | ||
bool m_hard_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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,3 @@ const git_oid& commit_wrapper::oid() const | |
{ | ||
return *git_commit_id(p_resource); | ||
} | ||
|
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
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,17 @@ | ||
#include "../wrapper/repository_wrapper.hpp" | ||
#include "../wrapper/signature_wrapper.hpp" | ||
#include "../utils/git_exception.hpp" | ||
|
||
signature_wrapper::~signature_wrapper() | ||
{ | ||
git_signature_free(p_resource); | ||
p_resource=nullptr; | ||
} | ||
|
||
signature_wrapper::author_committer_signatures signature_wrapper::get_default_signature_from_env(repository_wrapper& rw) | ||
{ | ||
signature_wrapper author; | ||
signature_wrapper committer; | ||
throw_if_error(git_signature_default_from_env(&(author.p_resource), &(committer.p_resource), rw)); | ||
return {std::move(author), std::move(committer)}; | ||
} |
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,26 @@ | ||
#pragma once | ||
|
||
#include <utility> | ||
|
||
#include <git2.h> | ||
|
||
#include "../wrapper/wrapper_base.hpp" | ||
|
||
class repository_wrapper; | ||
|
||
class signature_wrapper : public wrapper_base<git_signature> | ||
{ | ||
public: | ||
using author_committer_signatures = std::pair<signature_wrapper, signature_wrapper>; | ||
|
||
~signature_wrapper(); | ||
|
||
signature_wrapper(signature_wrapper&&) = default; | ||
signature_wrapper& operator=(signature_wrapper&&) = default; | ||
|
||
static author_committer_signatures get_default_signature_from_env(repository_wrapper&); | ||
|
||
private: | ||
|
||
signature_wrapper() = default; | ||
}; |
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 |
---|---|---|
@@ -1 +1 @@ | ||
ref: refs/heads/main | ||
ref: refs/heads/commit_test_branch |
Binary file not shown.
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 |
---|---|---|
@@ -1,2 +1,18 @@ | ||
0000000000000000000000000000000000000000 75743dcbd85064226c77a0b862af817838ae0b2e Sandrine Pataut <[email protected]> 1750769952 +0200 commit (initial): first commit | ||
75743dcbd85064226c77a0b862af817838ae0b2e ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut <[email protected]> 1750771272 +0200 commit: Second commit | ||
ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut <[email protected]> 1753887403 +0200 checkout: moving from main to bla | ||
ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut <[email protected]> 1753887403 +0200 checkout: moving from bla to main | ||
ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut <[email protected]> 1753887403 +0200 checkout: moving from main to bla | ||
ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut <[email protected]> 1753887403 +0200 checkout: moving from bla to main | ||
ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut <[email protected]> 1753887403 +0200 checkout: moving from main to bla | ||
ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut <[email protected]> 1753887403 +0200 checkout: moving from bla to main | ||
ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut <[email protected]> 1753887403 +0200 checkout: moving from main to bla | ||
ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut <[email protected]> 1753887403 +0200 checkout: moving from bla to main | ||
ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut <[email protected]> 1753887505 +0200 checkout: moving from main to commit_test_branch | ||
ee8c4cf874c4f1e3ba755f929fe7811018adee3d cba545ef5cc4ddf12a9744b6a49b20dda1ef1d5c Sandrine Pataut <[email protected]> 1753887505 +0200 commit: test commit | ||
cba545ef5cc4ddf12a9744b6a49b20dda1ef1d5c ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut <[email protected]> 1753887505 +0200 reset: moving to ee8c4cf874c4f1e3ba755f929fe7811018adee3d | ||
ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut <[email protected]> 1753887505 +0200 checkout: moving from commit_test_branch to main | ||
ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut <[email protected]> 1753887505 +0200 checkout: moving from main to commit_test_branch | ||
ee8c4cf874c4f1e3ba755f929fe7811018adee3d 75743dcbd85064226c77a0b862af817838ae0b2e Sandrine Pataut <[email protected]> 1753888486 +0200 reset: moving to 75743dcbd85064226c77a0b862af817838ae0b2e | ||
75743dcbd85064226c77a0b862af817838ae0b2e ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut <[email protected]> 1753888486 +0200 checkout: moving from commit_test_branch to main | ||
ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut <[email protected]> 1753888486 +0200 checkout: moving from main to commit_test_branch |
1 change: 1 addition & 0 deletions
1
test/data/status_data/embedded_git/logs/refs/heads/commit_test_branch
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 @@ | ||
0000000000000000000000000000000000000000 ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut <[email protected]> 1753888486 +0200 branch: Created from ee8c4cf874c4f1e3ba755f929fe7811018adee3d |
Binary file added
BIN
+129 Bytes
test/data/status_data/embedded_git/objects/a3/079a98067bc027a22dc56fe619c5d30f23d3a6
Binary file not shown.
Binary file added
BIN
+175 Bytes
test/data/status_data/embedded_git/objects/cb/a545ef5cc4ddf12a9744b6a49b20dda1ef1d5c
Binary file not shown.
1 change: 1 addition & 0 deletions
1
test/data/status_data/embedded_git/refs/heads/commit_test_branch
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 @@ | ||
ee8c4cf874c4f1e3ba755f929fe7811018adee3d |
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
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.