-
Notifications
You must be signed in to change notification settings - Fork 5
Add stash subcommand #80
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 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Next
Next commit
Add stash subcommand
- Loading branch information
commit eedd91fc913baca9452723453fb0344d65a8aa99
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,92 @@ | ||
| #include <git2/deprecated.h> | ||
| #include <git2/oid.h> | ||
| #include <git2/stash.h> | ||
| #include <iostream> | ||
|
|
||
| #include <git2/remote.h> | ||
|
|
||
| #include "../subcommand/stash_subcommand.hpp" | ||
| #include "../subcommand/status_subcommand.hpp" | ||
| #include "../wrapper/repository_wrapper.hpp" | ||
|
|
||
| bool has_subcommand(CLI::App* cmd) | ||
| { | ||
| std::vector<std::string> subs = { "push", "pop", "list", "apply" }; | ||
| return std::any_of(subs.begin(), subs.end(), [cmd](const std::string& s) { return cmd->got_subcommand(s); }); | ||
| } | ||
|
|
||
| stash_subcommand::stash_subcommand(const libgit2_object&, CLI::App& app) | ||
| { | ||
| auto* stash = app.add_subcommand("stash", "Stash the changes in a dirty working directory away"); | ||
| auto* push = stash->add_subcommand("push", ""); | ||
| auto* list = stash->add_subcommand("list", ""); | ||
| auto* pop = stash->add_subcommand("pop", ""); | ||
| auto* apply = stash->add_subcommand("apply", ""); | ||
|
|
||
| push->add_option("-m,--message", m_message, ""); | ||
| pop->add_option("--index", m_index, ""); | ||
| apply->add_option("--index", m_index, ""); | ||
|
|
||
| stash->callback([this,stash]() | ||
| { | ||
| if (!has_subcommand(stash)) | ||
| { | ||
| this->run_push(); | ||
| } | ||
| }); | ||
| push->callback([this]() { this->run_push(); }); | ||
| list->callback([this]() { this->run_list(); }); | ||
| pop->callback([this]() { this->run_pop(); }); | ||
| apply->callback([this]() { this->run_apply(); }); | ||
| } | ||
|
|
||
| void stash_subcommand::run_push() | ||
| { | ||
| auto directory = get_current_git_path(); | ||
| auto repo = repository_wrapper::open(directory); | ||
| auto author_committer_signatures = signature_wrapper::get_default_signature_from_env(repo); | ||
|
|
||
| git_oid stash_id; | ||
| throw_if_error(git_stash_save(&stash_id, repo, author_committer_signatures.first, m_message.c_str(), GIT_STASH_DEFAULT)); | ||
| auto stash = repo.find_commit(stash_id); | ||
| std::cout << "Saved working directory and index state " << stash.summary() << std::endl; | ||
| } | ||
|
|
||
| static int list_stash_cb(size_t index, const char* message, const git_oid* stash_id, void* payload) | ||
| { | ||
| std::cout << "stash@{" << index << "}: " << message << std::endl; | ||
| return 0; | ||
| } | ||
|
|
||
| void stash_subcommand::run_list() | ||
| { | ||
| auto directory = get_current_git_path(); | ||
| auto repo = repository_wrapper::open(directory); | ||
|
|
||
| throw_if_error(git_stash_foreach(repo, list_stash_cb, NULL)); | ||
| } | ||
|
|
||
| void stash_subcommand::run_pop() | ||
| { | ||
| auto directory = get_current_git_path(); | ||
| auto repo = repository_wrapper::open(directory); | ||
|
|
||
| std::string stash_spec = "stash@{" + std::to_string(m_index) + "}"; | ||
| auto stash_obj = repo.revparse_single(stash_spec); | ||
| git_oid stash_id = stash_obj->oid(); | ||
| char id_string[GIT_OID_HEXSZ + 1]; | ||
| git_oid_tostr(id_string, sizeof(id_string), &stash_id); | ||
|
|
||
| throw_if_error(git_stash_pop(repo, m_index, NULL)); | ||
| status_run(); | ||
| std::cout << "Dropped refs/stash@{" << m_index << "} (" << id_string << ")" << std::endl; | ||
| } | ||
|
|
||
| void stash_subcommand::run_apply() | ||
| { | ||
| auto directory = get_current_git_path(); | ||
| auto repo = repository_wrapper::open(directory); | ||
|
|
||
| throw_if_error(git_stash_apply(repo, m_index, NULL)); | ||
| status_run(); | ||
| } |
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,20 @@ | ||
| #pragma once | ||
|
|
||
| #include <CLI/CLI.hpp> | ||
|
|
||
| #include "../utils/common.hpp" | ||
|
|
||
| class stash_subcommand | ||
| { | ||
| public: | ||
|
|
||
| explicit stash_subcommand(const libgit2_object&, CLI::App& app); | ||
| void run_push(); | ||
| void run_list(); | ||
| void run_pop(); | ||
| void run_apply(); | ||
|
|
||
| std::vector<std::string> m_options; | ||
| std::string m_message = ""; | ||
| size_t m_index = 0; | ||
| }; |
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,139 @@ | ||
| import subprocess | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| def test_stash_push(xtl_clone, commit_env_config, git2cpp_path, tmp_path): | ||
| assert (tmp_path / "xtl").exists() | ||
| xtl_path = tmp_path / "xtl" | ||
|
|
||
| p = xtl_path / "mook_file.txt" | ||
| p.write_text("blabla") | ||
|
|
||
| cmd_add = [git2cpp_path, "add", "mook_file.txt"] | ||
| p_add = subprocess.run(cmd_add, cwd=xtl_path, text=True) | ||
| assert p_add.returncode == 0 | ||
|
|
||
| stash_path = xtl_path / ".git/refs/stash" | ||
| assert not stash_path.exists() | ||
|
|
||
| cmd_stash = [git2cpp_path, "stash"] | ||
| p_stash = subprocess.run(cmd_stash, capture_output=True, cwd=xtl_path, text=True) | ||
| assert p_stash.returncode == 0 | ||
| assert stash_path.exists() | ||
|
|
||
|
|
||
| def test_stash_list(xtl_clone, commit_env_config, git2cpp_path, tmp_path): | ||
| assert (tmp_path / "xtl").exists() | ||
| xtl_path = tmp_path / "xtl" | ||
|
|
||
| p = xtl_path / "mook_file.txt" | ||
| p.write_text("blabla") | ||
|
|
||
| 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_list = [git2cpp_path, "stash", "list"] | ||
| p_list = subprocess.run(cmd_list, capture_output=True, cwd=xtl_path, text=True) | ||
| assert p_list.returncode == 0 | ||
| assert "stash@{0}" not in p_list.stdout | ||
|
|
||
| cmd_stash = [git2cpp_path, "stash"] | ||
| p_stash = subprocess.run(cmd_stash, capture_output=True, cwd=xtl_path, text=True) | ||
| assert p_stash.returncode == 0 | ||
|
|
||
| p_list_2 = subprocess.run(cmd_list, capture_output=True, cwd=xtl_path, text=True) | ||
| assert p_list_2.returncode == 0 | ||
| assert "stash@{0}" in p_list_2.stdout | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("index_flag", ["", "--index"]) | ||
| def test_stash_pop(xtl_clone, commit_env_config, git2cpp_path, tmp_path, index_flag): | ||
| assert (tmp_path / "xtl").exists() | ||
| xtl_path = tmp_path / "xtl" | ||
|
|
||
| index = 0 if index_flag == "" else 1 | ||
|
|
||
| for i in range(index + 1): | ||
| p = xtl_path / f"mook_file_{i}.txt" | ||
| p.write_text(f"blabla{i}") | ||
|
|
||
| cmd_add = [git2cpp_path, "add", f"mook_file_{i}.txt"] | ||
| p_add = subprocess.run(cmd_add, cwd=xtl_path, text=True) | ||
| assert p_add.returncode == 0 | ||
|
|
||
| cmd_stash = [git2cpp_path, "stash"] | ||
| p_stash = subprocess.run( | ||
| cmd_stash, capture_output=True, cwd=xtl_path, text=True | ||
| ) | ||
| assert p_stash.returncode == 0 | ||
|
|
||
| cmd_status = [git2cpp_path, "status"] | ||
| p_status = subprocess.run( | ||
| cmd_status, capture_output=True, cwd=xtl_path, text=True | ||
| ) | ||
| assert p_status.returncode == 0 | ||
| assert "mook_file" not in p_status.stdout | ||
|
|
||
| cmd_pop = [git2cpp_path, "stash", "pop"] | ||
| if index_flag != "": | ||
| cmd_pop.append(index_flag) | ||
| cmd_pop.append("1") | ||
| p_pop = subprocess.run(cmd_pop, capture_output=True, cwd=xtl_path, text=True) | ||
| assert p_pop.returncode == 0 | ||
| assert "mook_file_0" in p_pop.stdout | ||
| assert "Dropped refs/stash@{" + str(index) + "}" in p_pop.stdout | ||
|
|
||
| cmd_list = [git2cpp_path, "stash", "list"] | ||
| p_list = subprocess.run(cmd_list, capture_output=True, cwd=xtl_path, text=True) | ||
| assert p_list.returncode == 0 | ||
| if index_flag == "": | ||
| assert p_list.stdout == "" | ||
| else: | ||
| assert "stash@{0}" in p_list.stdout | ||
| assert "stash@{1}" not in p_list.stdout | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("index_flag", ["", "--index"]) | ||
| def test_stash_apply(xtl_clone, commit_env_config, git2cpp_path, tmp_path, index_flag): | ||
| assert (tmp_path / "xtl").exists() | ||
| xtl_path = tmp_path / "xtl" | ||
|
|
||
| index = 0 if index_flag == "" else 1 | ||
|
|
||
| for i in range(index + 1): | ||
| p = xtl_path / f"mook_file_{i}.txt" | ||
| p.write_text(f"blabla{i}") | ||
|
|
||
| cmd_add = [git2cpp_path, "add", f"mook_file_{i}.txt"] | ||
| p_add = subprocess.run(cmd_add, cwd=xtl_path, text=True) | ||
| assert p_add.returncode == 0 | ||
|
|
||
| cmd_stash = [git2cpp_path, "stash"] | ||
| p_stash = subprocess.run( | ||
| cmd_stash, capture_output=True, cwd=xtl_path, text=True | ||
| ) | ||
| assert p_stash.returncode == 0 | ||
|
|
||
| cmd_status = [git2cpp_path, "status"] | ||
| p_status = subprocess.run( | ||
| cmd_status, capture_output=True, cwd=xtl_path, text=True | ||
| ) | ||
| assert p_status.returncode == 0 | ||
| assert "mook_file" not in p_status.stdout | ||
|
|
||
| cmd_apply = [git2cpp_path, "stash", "apply"] | ||
| if index_flag != "": | ||
| cmd_apply.append(index_flag) | ||
| cmd_apply.append("1") | ||
| p_apply = subprocess.run(cmd_apply, capture_output=True, cwd=xtl_path, text=True) | ||
| assert p_apply.returncode == 0 | ||
| assert "mook_file_0" in p_apply.stdout | ||
|
|
||
| cmd_list = [git2cpp_path, "stash", "list"] | ||
| p_list = subprocess.run(cmd_list, capture_output=True, cwd=xtl_path, text=True) | ||
| assert p_list.returncode == 0 | ||
| assert "stash@{0}" in p_list.stdout | ||
| if index_flag != "": | ||
| assert "stash@{1}" in p_list.stdout |
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.