Skip to content

Feat/recognise shorthands #55

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 3 commits into from
May 17, 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
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@
*.out
*.app
app/gyt
tests/tests
build/**/*

# Tests
tests/tests
tests/_deps
tests/.ninja*
tests/app/gyt
tests/Testing
tests/build.ninja

# CMake files
*.cmake
**/CMakeFiles/
Expand Down
10 changes: 10 additions & 0 deletions doc/commands/branches.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Branches

A branch:
- is a ref under ref/heads

# commands that creates branches

`git branch -c`
`git checkout -b`
`git switch -c`
3 changes: 1 addition & 2 deletions src/commands/cat-file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ void catfile(std::vector<std::string> &args) {
try {
std::optional<GitRepository> repo = GitRepository::find();
if (repo) {
// TODO use GitObject::find once it's fixed
GitObject *obj = GitObject::read(*repo, hash);
GitObject *obj = GitObject::read(*repo, GitObject::find(*repo, hash));
std::cout << obj->serialise(*repo);
}
} catch (std::runtime_error &err) {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/checkout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ void checkout(std::vector<std::string> &args) {
try {
std::optional<GitRepository> repo = GitRepository::find();
if (repo) {
GitCommit *commit =
dynamic_cast<GitCommit *>(GitObject::read(*repo, hash));
GitCommit *commit = dynamic_cast<GitCommit *>(
GitObject::read(*repo, GitObject::find(*repo, hash)));
if (!commit) {
throw std::runtime_error("Invalid commit object: " + hash);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void log(std::vector<std::string> &args) {
try {
std::optional<GitRepository> repo = GitRepository::find();
if (repo) {
std::string commit = commitArg.getValue();
std::string commit = GitObject::find(*repo, commitArg.getValue());
GitCommit *commitObj;

do {
Expand Down
1 change: 1 addition & 0 deletions src/commands/ls-tree.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "commands/ls-tree.h"
#include <iostream>
#include <string>

#include "object.h"
#include "repository.h"
Expand Down
21 changes: 15 additions & 6 deletions src/object.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "object.h"
#include "blob.h"
#include "boost/algorithm/string/case_conv.hpp"
#include "commit.h"
#include "tree.h"
#include "util.h"
Expand Down Expand Up @@ -90,18 +91,26 @@ std::string GitObject::write(GitRepository &repo, std::string &type,
}
}

// TODO support hashes here
// XXX: food for thought: how does git handle the situation where
// the branch name is the same as a tag?
std::string GitObject::find(GitRepository &repo, const std::string &name,
bool follow) {
fs::path ref_path;
if (name == "HEAD") {
std::string ref_name = boost::algorithm::to_lower_copy(name);
if (ref_name == "head") {
ref_path = repo.repo_path("HEAD");
} else if (fs::exists(repo.repo_path("refs/heads/" + ref_name))) {
ref_path = repo.repo_path("refs/heads/" + ref_name);
} else if (fs::exists(repo.repo_path("refs/tags/" + ref_name))) {
ref_path = repo.repo_path("refs/tags/" + ref_name);
} else if (fs::exists(repo.repo_path("refs/remotes/" + ref_name))) {
ref_path = repo.repo_path("refs/remotes/" + ref_name);
} else if (fs::exists(repo.repo_path(get_commit_path(ref_name)))) {
return ref_name;
} else {
ref_path = repo.repo_path("refs/heads/" + name);
throw std::runtime_error(name + ": not a valid reference");
}
std::string file_data = read_file(ref_path, true);
std::string commit_hash = file_data.substr(5);
return read_file(repo.repo_path(commit_hash), true);
return resolve_ref(ref_path, repo);
}

std::string GitObject::get_type() const { return this->format; }
7 changes: 3 additions & 4 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@ fi
echo "Selected build type: $BUILD_TYPE"
echo "Building the project... This will take a while to install dependencies for the first time."

mv ../tests/gitrepo/.notgit ../tests/gitrepo/.git
rm -rf ../tests/gitrepo/.git
cp -R ../tests/gitrepo/.notgit ../tests/gitrepo/.git

#Run CMake with the selected build type
cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -G Ninja
cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DBUILD_TESTS=ON -G Ninja

#Build the project
ninja

# test
ctest --output-on-failure

mv ../tests/gitrepo/.git ../tests/gitrepo/.notgit

#symlink - so I can run it like gyt[arguments....]
sudo rm /usr/local/bin/gyt
sudo ln -s "$(pwd)/app/gyt" /usr/local/bin/gyt
5 changes: 2 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
add_executable(tests tests.cpp repository_t.cpp tag_t.cpp)
add_executable(tests tests.cpp repository_t.cpp tag_t.cpp object_t.cpp utils/gitreposetup.cpp)
target_include_directories(tests PUBLIC ../ext)

target_link_libraries(tests PUBLIC boost_libraries repository commands)
file(COPY ${CMAKE_SOURCE_DIR}/tests/gitrepo
DESTINATION ${CMAKE_BINARY_DIR}/tests
FILES_MATCHING PATTERN ".*" PATTERN "*" EXCLUDE)
DESTINATION ${CMAKE_BINARY_DIR}/tests)

# allow user to run tests with `make test` or `ctest`
include(../cmake/Catch.cmake)
Expand Down
Loading
Loading