Skip to content

Checkout subcommand #21

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 4 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ set(GIT2CPP_SRC
${GIT2CPP_SOURCE_DIR}/subcommand/add_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/subcommand/branch_subcommand.cpp
${GIT2CPP_SOURCE_DIR}/subcommand/branch_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/subcommand/checkout_subcommand.cpp
${GIT2CPP_SOURCE_DIR}/subcommand/checkout_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.cpp
${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/subcommand/status_subcommand.cpp
Expand All @@ -51,12 +53,16 @@ set(GIT2CPP_SRC
${GIT2CPP_SOURCE_DIR}/utils/common.hpp
${GIT2CPP_SOURCE_DIR}/utils/git_exception.cpp
${GIT2CPP_SOURCE_DIR}/utils/git_exception.hpp
${GIT2CPP_SOURCE_DIR}/wrapper/annotated_commit_wrapper.cpp
${GIT2CPP_SOURCE_DIR}/wrapper/annotated_commit_wrapper.hpp
${GIT2CPP_SOURCE_DIR}/wrapper/branch_wrapper.cpp
${GIT2CPP_SOURCE_DIR}/wrapper/branch_wrapper.hpp
${GIT2CPP_SOURCE_DIR}/wrapper/commit_wrapper.cpp
${GIT2CPP_SOURCE_DIR}/wrapper/commit_wrapper.hpp
${GIT2CPP_SOURCE_DIR}/wrapper/index_wrapper.cpp
${GIT2CPP_SOURCE_DIR}/wrapper/index_wrapper.hpp
${GIT2CPP_SOURCE_DIR}/wrapper/object_wrapper.cpp
${GIT2CPP_SOURCE_DIR}/wrapper/object_wrapper.hpp
${GIT2CPP_SOURCE_DIR}/wrapper/refs_wrapper.cpp
${GIT2CPP_SOURCE_DIR}/wrapper/refs_wrapper.hpp
${GIT2CPP_SOURCE_DIR}/wrapper/repository_wrapper.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "version.hpp"
#include "subcommand/add_subcommand.hpp"
#include "subcommand/branch_subcommand.hpp"
#include "subcommand/checkout_subcommand.hpp"
#include "subcommand/init_subcommand.hpp"
#include "subcommand/status_subcommand.hpp"

Expand All @@ -25,6 +26,7 @@ int main(int argc, char** argv)
status_subcommand status(lg2_obj, app);
add_subcommand add(lg2_obj, app);
branch_subcommand(lg2_obj, app);
checkout_subcommand(lg2_obj, app);

app.parse(argc, argv);

Expand Down
127 changes: 127 additions & 0 deletions src/subcommand/checkout_subcommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include <iostream>
#include <sstream>

#include "../subcommand/checkout_subcommand.hpp"
#include "../utils/git_exception.hpp"
#include "../wrapper/repository_wrapper.hpp"

checkout_subcommand::checkout_subcommand(const libgit2_object&, CLI::App& app)
{
auto* sub = app.add_subcommand("checkout", "Switch branches or restore working tree files");

sub->add_option("<branch>", m_branch_name, "Branch to checkout");
sub->add_flag("-b", m_create_flag, "Create a new branch before checking it out");
sub->add_flag("-B", m_force_create_flag, "Create a new branch or reset it if it exists before checking it out");
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");

sub->callback([this]() { this->run(); });
}

void checkout_subcommand::run()
{
auto directory = get_current_git_path();
auto repo = repository_wrapper::open(directory);

if (repo.state() != GIT_REPOSITORY_STATE_NONE)
{
throw std::runtime_error("Cannot checkout, repository is in unexpected state");
}

git_checkout_options options;
git_checkout_options_init(&options, GIT_CHECKOUT_OPTIONS_VERSION);

if(m_force_checkout_flag)
{
options.checkout_strategy = GIT_CHECKOUT_FORCE;
}

if (m_create_flag || m_force_create_flag)
{
auto annotated_commit = create_local_branch(repo, m_branch_name, m_force_create_flag);
checkout_tree(repo, annotated_commit, m_branch_name, options);
update_head(repo, annotated_commit, m_branch_name);
}
else
{
auto optional_commit = resolve_local_ref(repo, m_branch_name);
if (!optional_commit)
{
// TODO: handle remote refs
std::ostringstream buffer;
buffer << "error: could not resolve pathspec '" << m_branch_name << "'" << std::endl;
throw std::runtime_error(buffer.str());
}
checkout_tree(repo, *optional_commit, m_branch_name, options);
update_head(repo, *optional_commit, m_branch_name);
}
}

std::optional<annotated_commit_wrapper> checkout_subcommand::resolve_local_ref
(
const repository_wrapper& repo,
const std::string& target_name
)
{
if (auto ref = repo.find_reference_dwim(target_name))
{
return repo.find_annotated_commit(*ref);
}
else if (auto obj = repo.revparse_single(target_name))
{
return repo.find_annotated_commit(obj->oid());
}
else
{
return std::nullopt;
}
}

annotated_commit_wrapper checkout_subcommand::create_local_branch
(
repository_wrapper& repo,
const std::string& target_name,
bool force
)
{
auto branch = repo.create_branch(target_name, force);
return repo.find_annotated_commit(branch);
}

void checkout_subcommand::checkout_tree
(
const repository_wrapper& repo,
const annotated_commit_wrapper& target_annotated_commit,
const std::string& target_name,
const git_checkout_options& options
)
{
auto target_commit = repo.find_commit(target_annotated_commit.oid());
throwIfError(git_checkout_tree(repo, target_commit, &options));
}

void checkout_subcommand::update_head
(
repository_wrapper& repo,
const annotated_commit_wrapper& target_annotated_commit,
const std::string& target_name
)
{
std::string_view annotated_ref = target_annotated_commit.reference_name();
if (!annotated_ref.empty())
{
auto ref = repo.find_reference(annotated_ref);
if (ref.is_remote())
{
auto branch = repo.create_branch(target_name, target_annotated_commit);
repo.set_head(branch.reference_name());
}
else
{
repo.set_head(annotated_ref);
}
}
else
{
repo.set_head_detached(target_annotated_commit);
}
}
52 changes: 52 additions & 0 deletions src/subcommand/checkout_subcommand.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include <optional>
#include <string>

#include <CLI/CLI.hpp>

#include "../utils/common.hpp"
#include "../wrapper/repository_wrapper.hpp"

class checkout_subcommand
{
public:

explicit checkout_subcommand(const libgit2_object&, CLI::App& app);
void run();

private:

std::optional<annotated_commit_wrapper> resolve_local_ref
(
const repository_wrapper& repo,
const std::string& target_name
);

annotated_commit_wrapper create_local_branch
(
repository_wrapper& repo,
const std::string& target_name,
bool force
);

void checkout_tree
(
const repository_wrapper& repo,
const annotated_commit_wrapper& target_annotated_commit,
const std::string& target_name,
const git_checkout_options& options
);

void update_head
(
repository_wrapper& repo,
const annotated_commit_wrapper& target_annotated_commit,
const std::string& target_name
);

std::string m_branch_name = {};
bool m_create_flag = false;
bool m_force_create_flag = false;
bool m_force_checkout_flag = false;
};
23 changes: 23 additions & 0 deletions src/wrapper/annotated_commit_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "../wrapper/annotated_commit_wrapper.hpp"

annotated_commit_wrapper::annotated_commit_wrapper(git_annotated_commit* commit)
: base_type(commit)
{
}

annotated_commit_wrapper::~annotated_commit_wrapper()
{
git_annotated_commit_free(p_resource);
p_resource = nullptr;
}

const git_oid& annotated_commit_wrapper::oid() const
{
return *git_annotated_commit_id(p_resource);
}

std::string_view annotated_commit_wrapper::reference_name() const
{
const char* res = git_annotated_commit_ref(*this);
return res ? res : std::string_view{};
}
29 changes: 29 additions & 0 deletions src/wrapper/annotated_commit_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <string_view>

#include <git2.h>

#include "../wrapper/wrapper_base.hpp"

class annotated_commit_wrapper : public wrapper_base<git_annotated_commit>
{
public:

using base_type = wrapper_base<git_annotated_commit>;

~annotated_commit_wrapper();

annotated_commit_wrapper(annotated_commit_wrapper&&) noexcept = default;
annotated_commit_wrapper& operator=(annotated_commit_wrapper&&) noexcept = default;

const git_oid& oid() const;
std::string_view reference_name() const;

private:

annotated_commit_wrapper(git_annotated_commit* commit);

friend class repository_wrapper;
};

8 changes: 7 additions & 1 deletion src/wrapper/branch_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ branch_wrapper::~branch_wrapper()
std::string_view branch_wrapper::name() const
{
const char* out = nullptr;
throwIfError(git_branch_name(&out, p_resource));
throwIfError(git_branch_name(&out, *this));
return std::string_view(out);
}

std::string_view branch_wrapper::reference_name() const
{
const char* out = git_reference_name(*this);
return out ? out : std::string_view();
}

void delete_branch(branch_wrapper&& branch)
{
throwIfError(git_branch_delete(branch));
Expand Down
1 change: 1 addition & 0 deletions src/wrapper/branch_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class branch_wrapper : public wrapper_base<git_reference>
branch_wrapper& operator=(branch_wrapper&&) = default;

std::string_view name() const;
std::string_view reference_name() const;

private:

Expand Down
21 changes: 12 additions & 9 deletions src/wrapper/commit_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
#include "../utils/git_exception.hpp"
#include "../wrapper/commit_wrapper.hpp"
#include "../wrapper/repository_wrapper.hpp"

commit_wrapper::commit_wrapper(git_commit* commit)
: base_type(commit)
{
}

commit_wrapper::~commit_wrapper()
{
git_commit_free(p_resource);
p_resource = nullptr;
}


commit_wrapper commit_wrapper::from_reference_name(const repository_wrapper& repo, const std::string& ref_name)
commit_wrapper::operator git_object*() const noexcept
{
git_oid oid_parent_commit;
throwIfError(git_reference_name_to_id(&oid_parent_commit, repo, ref_name.c_str()));
return reinterpret_cast<git_object*>(p_resource);
}

commit_wrapper cw;
throwIfError(git_commit_lookup(&(cw.p_resource), repo, &oid_parent_commit));
return cw;
const git_oid& commit_wrapper::oid() const
{
return *git_commit_id(p_resource);
}

15 changes: 8 additions & 7 deletions src/wrapper/commit_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
#pragma once

#include <string>

#include <git2.h>

#include "../wrapper/wrapper_base.hpp"

class repository_wrapper;

class commit_wrapper : public wrapper_base<git_commit>
{
public:

using base_type = wrapper_base<git_commit>;

~commit_wrapper();

commit_wrapper(commit_wrapper&&) noexcept = default;
commit_wrapper& operator=(commit_wrapper&&) noexcept = default;

static commit_wrapper
from_reference_name(const repository_wrapper& repo, const std::string& ref_name = "HEAD");
operator git_object*() const noexcept;

const git_oid& oid() const;

private:

commit_wrapper() = default;
commit_wrapper(git_commit* commit);

friend class repository_wrapper;
};
17 changes: 17 additions & 0 deletions src/wrapper/object_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "../wrapper/object_wrapper.hpp"

object_wrapper::object_wrapper(git_object* obj)
: base_type(obj)
{
}

object_wrapper::~object_wrapper()
{
git_object_free(p_resource);
p_resource = nullptr;
}

const git_oid& object_wrapper::oid() const
{
return *git_object_id(*this);
}
Loading