|
| 1 | +#include <iostream> |
| 2 | +#include <sstream> |
| 3 | + |
| 4 | +#include "../subcommand/checkout_subcommand.hpp" |
| 5 | +#include "../utils/git_exception.hpp" |
| 6 | +#include "../wrapper/repository_wrapper.hpp" |
| 7 | + |
| 8 | +checkout_subcommand::checkout_subcommand(const libgit2_object&, CLI::App& app) |
| 9 | +{ |
| 10 | + auto* sub = app.add_subcommand("checkout", "Switch branches or restore working tree files"); |
| 11 | + |
| 12 | + sub->add_option("<branch>", m_branch_name, "Branch to checkout"); |
| 13 | + sub->add_flag("-b", m_create_flag, "Create a new branch before checking it out"); |
| 14 | + sub->add_flag("-B", m_force_create_flag, "Create a new branch or reset it if it exists before checking it out"); |
| 15 | + sub->add_flag("-f, --force", m_force_checkout_flag, "When switching branches, proceed even if the index or the working tree differs from HEAD, and even if there are untracked files in the way"); |
| 16 | + |
| 17 | + sub->callback([this]() { this->run(); }); |
| 18 | +} |
| 19 | + |
| 20 | +void checkout_subcommand::run() |
| 21 | +{ |
| 22 | + auto directory = get_current_git_path(); |
| 23 | + auto repo = repository_wrapper::open(directory); |
| 24 | + |
| 25 | + if (repo.state() != GIT_REPOSITORY_STATE_NONE) |
| 26 | + { |
| 27 | + throw std::runtime_error("Cannot checkout, repository is in unexpected state"); |
| 28 | + } |
| 29 | + |
| 30 | + git_checkout_options options; |
| 31 | + git_checkout_options_init(&options, GIT_CHECKOUT_OPTIONS_VERSION); |
| 32 | + |
| 33 | + if(m_force_checkout_flag) |
| 34 | + { |
| 35 | + options.checkout_strategy = GIT_CHECKOUT_FORCE; |
| 36 | + } |
| 37 | + |
| 38 | + if (m_create_flag || m_force_create_flag) |
| 39 | + { |
| 40 | + auto annotated_commit = create_local_branch(repo, m_branch_name, m_force_create_flag); |
| 41 | + checkout_tree(repo, annotated_commit, m_branch_name, options); |
| 42 | + update_head(repo, annotated_commit, m_branch_name); |
| 43 | + } |
| 44 | + else |
| 45 | + { |
| 46 | + auto optional_commit = resolve_local_ref(repo, m_branch_name); |
| 47 | + if (!optional_commit) |
| 48 | + { |
| 49 | + // TODO: handle remote refs |
| 50 | + std::ostringstream buffer; |
| 51 | + buffer << "error: could not resolve pathspec '" << m_branch_name << "'" << std::endl; |
| 52 | + throw std::runtime_error(buffer.str()); |
| 53 | + } |
| 54 | + checkout_tree(repo, *optional_commit, m_branch_name, options); |
| 55 | + update_head(repo, *optional_commit, m_branch_name); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +std::optional<annotated_commit_wrapper> checkout_subcommand::resolve_local_ref |
| 60 | +( |
| 61 | + const repository_wrapper& repo, |
| 62 | + const std::string& target_name |
| 63 | +) |
| 64 | +{ |
| 65 | + if (auto ref = repo.find_reference_dwim(target_name)) |
| 66 | + { |
| 67 | + return repo.find_annotated_commit(*ref); |
| 68 | + } |
| 69 | + else if (auto obj = repo.revparse_single(target_name)) |
| 70 | + { |
| 71 | + return repo.find_annotated_commit(obj->oid()); |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + return std::nullopt; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +annotated_commit_wrapper checkout_subcommand::create_local_branch |
| 80 | +( |
| 81 | + repository_wrapper& repo, |
| 82 | + const std::string& target_name, |
| 83 | + bool force |
| 84 | +) |
| 85 | +{ |
| 86 | + auto branch = repo.create_branch(target_name, force); |
| 87 | + return repo.find_annotated_commit(branch); |
| 88 | +} |
| 89 | + |
| 90 | +void checkout_subcommand::checkout_tree |
| 91 | +( |
| 92 | + const repository_wrapper& repo, |
| 93 | + const annotated_commit_wrapper& target_annotated_commit, |
| 94 | + const std::string& target_name, |
| 95 | + const git_checkout_options& options |
| 96 | +) |
| 97 | +{ |
| 98 | + auto target_commit = repo.find_commit(target_annotated_commit.oid()); |
| 99 | + throwIfError(git_checkout_tree(repo, target_commit, &options)); |
| 100 | +} |
| 101 | + |
| 102 | +void checkout_subcommand::update_head |
| 103 | +( |
| 104 | + repository_wrapper& repo, |
| 105 | + const annotated_commit_wrapper& target_annotated_commit, |
| 106 | + const std::string& target_name |
| 107 | +) |
| 108 | +{ |
| 109 | + std::string_view annotated_ref = target_annotated_commit.reference_name(); |
| 110 | + if (!annotated_ref.empty()) |
| 111 | + { |
| 112 | + auto ref = repo.find_reference(annotated_ref); |
| 113 | + if (ref.is_remote()) |
| 114 | + { |
| 115 | + auto branch = repo.create_branch(target_name, target_annotated_commit); |
| 116 | + repo.set_head(branch.reference_name()); |
| 117 | + } |
| 118 | + else |
| 119 | + { |
| 120 | + repo.set_head(annotated_ref); |
| 121 | + } |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + repo.set_head_detached(target_annotated_commit); |
| 126 | + } |
| 127 | +} |
0 commit comments