Skip to content

Commit 19484bd

Browse files
Feat/show ref (#53)
* Implement show-ref and closes #19
1 parent 0ed9ebb commit 19484bd

File tree

19 files changed

+255
-145
lines changed

19 files changed

+255
-145
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
InsertNewlineAtEOF: true

CMakeLists.txt

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,56 @@
11
cmake_minimum_required(VERSION 3.8.2)
22

3-
# Set a name and a version number for your project:
4-
project(
5-
cpp-project-template
6-
VERSION 0.0.1
7-
LANGUAGES CXX)
8-
9-
set(CMAKE_AR /usr/bin/ar)
10-
11-
# Enable C language
12-
enable_language(C)
13-
14-
# this needs to be in the top level CMakeLists.txt to enable tests
15-
include(CTest)
16-
17-
set(BOOST_ENABLE_CMAKE ON)
18-
set(Boost_CMAKE_CXX_STANDARD 17)
19-
set(CMAKE_CXX_STANDARD 17)
20-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
21-
22-
set(BOOST_INCLUDE_LIBRARIES iostreams uuid algorithm)
23-
24-
include(FetchContent)
25-
FetchContent_Declare(
26-
Boost
27-
GIT_REPOSITORY https://github.com/boostorg/boost.git
28-
GIT_TAG boost-1.83.0
29-
GIT_SHALLOW TRUE)
30-
FetchContent_MakeAvailable(Boost)
31-
32-
# compile the library
33-
add_subdirectory(src)
34-
35-
# compile the application
36-
add_subdirectory(app)
37-
38-
# optionally add doxygen target to generate documentation
39-
option(BUILD_DOCS "Enable building of documentation (requires Doxygen)" OFF)
40-
if(BUILD_DOCS)
41-
find_package(Doxygen REQUIRED)
42-
set(DOXYGEN_EXCLUDE_PATTERNS "${CMAKE_SOURCE_DIR}/ext/*")
43-
doxygen_add_docs(doxygen ${CMAKE_SOURCE_DIR}
44-
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
45-
endif()
46-
47-
# compile the tests
48-
option(BUILD_TESTS "Enable building of documentation (requires Doxygen)" OFF)
49-
if(BUILD_TESTS)
50-
add_subdirectory(tests)
51-
endif()
3+
#Set a name and a version number for your project:
4+
project(cpp - project - template VERSION 0.0.1 LANGUAGES CXX)
5+
6+
set(CMAKE_AR / usr / bin / ar)
7+
8+
#Enable C language
9+
enable_language(C)
10+
11+
#this needs to be in the top level CMakeLists.txt to enable tests
12+
include(CTest)
13+
14+
set(BOOST_ENABLE_CMAKE ON) set(Boost_CMAKE_CXX_STANDARD 17) set(
15+
CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED
16+
ON)
17+
18+
set(BOOST_INCLUDE_LIBRARIES iostreams uuid algorithm)
19+
20+
include(FetchContent) FetchContent_Declare(
21+
Boost GIT_REPOSITORY
22+
https : // github.com/boostorg/boost.git
23+
GIT_TAG boost -
24+
1.83.0 GIT_SHALLOW
25+
TRUE) FetchContent_MakeAvailable(Boost)
26+
27+
#compile the library
28+
add_subdirectory(src)
29+
30+
#compile the application
31+
add_subdirectory(app)
32+
33+
#optionally add doxygen target to generate documentation
34+
option(BUILD_DOCS
35+
"Enable building of "
36+
"documentation (requires "
37+
"Doxygen)" OFF) if (BUILD_DOCS)
38+
find_package(Doxygen REQUIRED) set(
39+
DOXYGEN_EXCLUDE_PATTERNS
40+
"${CMAKE_SOURCE_DIR}/ext/*")
41+
doxygen_add_docs(
42+
doxygen ${
43+
CMAKE_SOURCE_DIR} WORKING_DIRECTORY
44+
${CMAKE_CURRENT_BINARY_DIR})
45+
endif()
46+
47+
#compile the tests
48+
option(BUILD_TESTS
49+
"Enable "
50+
"building of "
51+
"documentation "
52+
"(requires "
53+
"Doxygen"
54+
")" OFF) if (BUILD_TESTS)
55+
add_subdirectory(
56+
tests) endif()

LICENSE

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
MIT License
22

3-
Copyright (c) 2020 SSC
3+
Copyright(c) 2020 SSC
44

5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
5+
Permission is hereby granted,
6+
free of charge,
7+
to any person obtaining a copy of this software and associated documentation
8+
files(the "Software"),
9+
to deal in the Software without restriction,
10+
including without limitation the rights to use, copy, modify, merge, publish
11+
,
12+
distribute, sublicense, and / or sell copies of the Software,
13+
and to permit persons to whom the Software is furnished to do so,
14+
subject to the following conditions :
1115

12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
16+
The above copyright notice and this permission notice shall be included in
17+
all copies
18+
or
19+
substantial portions of the Software.
1420

15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
21+
THE SOFTWARE IS PROVIDED "AS IS",
22+
WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
23+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
26+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27+
TORT OR OTHERWISE, ARISING FROM,
28+
OUT OF OR IN
29+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Git C++lient
1+
#Git C++ lient
22

33
## Contents
44

@@ -45,4 +45,4 @@ cmake .. -DCMAKE_BUILD_TYPE=Release -DBUILD_DOCS=ON
4545
make doxygen
4646
```
4747

48-
This will generate the documentation in the `html` folder.
48+
This will generate the documentation in the `html` folder.

app/gyt.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "commands/init.h"
55
#include "commands/log.h"
66
#include "commands/ls-tree.h"
7+
#include "commands/show-ref.h"
78
#include <iostream>
89
#include <string>
910
#include <vector>
@@ -30,6 +31,8 @@ int main(int argc, char **argv) {
3031
commands::lstree(args);
3132
} else if (command == "checkout") {
3233
commands::checkout(args);
34+
} else if (command == "show-ref") {
35+
commands::showref(args);
3336
} else {
3437
std::cerr << "Unknown command: " << command << "\n";
3538
return -1;

doc/commands/show-ref.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Refs
2+
3+
A ref lives in .git/refs and contains the SHA-1 hash of an object's hash.
4+
5+
## `show-ref`
6+
7+
`gyt show-ref`: shows all refs

include/commands/show-ref.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef SHOWREF_H
2+
#define SHOWREF_H
3+
4+
#include <string>
5+
#include <vector>
6+
namespace commands {
7+
void showref(std::vector<std::string> &args);
8+
} // namespace commands
9+
10+
#endif // SHOWREF_H

include/util.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef UTIL_H
22
#define UTIL_H
33

4+
#include "repository.h"
45
#include <filesystem>
56
#include <string>
67
namespace fs = std::filesystem;
@@ -12,4 +13,7 @@ std::string binaryToHex(const std::string &binary);
1213
std::string hexToBinary(const std::string &hexString);
1314
std::string get_file_type(int mode);
1415
fs::perms get_unix_permissions(int mode);
15-
#endif // UTIL_H
16+
std::string remove_file_prefix(const fs::path &path,
17+
const fs::path &repo_prefix);
18+
std::string resolve_ref(const fs::path &ref_path, GitRepository &repo);
19+
#endif // UTIL_H

run.sh

100755100644
Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
1-
#!/bin/bash
2-
set -e
1+
#!/ bin / bash
2+
set - e
33

4-
# Set the default build type to Debug if no environment is specified
5-
BUILD_TYPE=${BUILD_TYPE:-Debug}
4+
#Set the default build type to Debug if no environment is specified
5+
BUILD_TYPE = ${BUILD_TYPE : -Debug}
66

7-
# If the environment is specified as "prod", set the build type to Release
8-
if [ "$1" == "-p" ]; then
9-
BUILD_TYPE=Release
10-
fi
7+
#If the environment is specified as "prod", set the build type to Release
8+
if["$1" == "-p"];
9+
then BUILD_TYPE = Release fi
1110

12-
# remove old build if any
13-
if [ -f "app/gyt" ]; then
14-
rm -rf app/gyt
15-
fi
11+
#remove old build if any
12+
if[-f "app/gyt"];
13+
then rm -
14+
rf app / gyt fi
1615

17-
# Print the selected build type
18-
echo "Selected build type: $BUILD_TYPE"
19-
echo "Building the project... This will take a while to install dependencies for the first time."
16+
#Print the selected build type
17+
echo "Selected build type: $BUILD_TYPE" echo
18+
"Building the project... This will take a while to install "
19+
"dependencies for the first time."
2020

21-
# Run CMake with the selected build type
22-
cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -G Ninja
21+
#Run CMake with the selected build type
22+
cmake..-
23+
DCMAKE_BUILD_TYPE = $BUILD_TYPE - DCMAKE_EXPORT_COMPILE_COMMANDS =
24+
1 -
25+
G Ninja
2326

24-
# Build the project
25-
ninja
27+
#Build the project
28+
ninja
2629

27-
# symlink - so I can run it like gyt [arguments....]
28-
sudo rm /usr/local/bin/gyt
29-
sudo ln -s "$(pwd)/app/gyt" /usr/local/bin/gyt
30+
#symlink - so I can run it like gyt[arguments....]
31+
sudo rm /
32+
usr / local / bin / gyt sudo ln -
33+
s "$(pwd)/app/gyt" / usr / local / bin / gyt

src/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ target_link_libraries(object PRIVATE repository boost_libraries)
2424
target_include_directories(object PUBLIC ../include)
2525
target_compile_features(object PUBLIC cxx_std_17)
2626

27-
add_library(commands init.cpp cat-file.cpp hash-object.cpp log.cpp ls-tree.cpp
28-
checkout.cpp)
27+
file(GLOB COMMANDS_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/commands/*.cpp")
28+
add_library(commands ${COMMANDS_SOURCES})
2929
target_link_libraries(commands PRIVATE repository object boost_libraries)
3030
target_include_directories(commands PUBLIC ../include)
3131
target_compile_features(commands PUBLIC cxx_std_17)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/commands/show-ref.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "commands/show-ref.h"
2+
#include <iostream>
3+
4+
#include "repository.h"
5+
#include "tclap/CmdLine.h"
6+
#include "util.h"
7+
8+
namespace commands {
9+
void showref(std::vector<std::string> &args) {
10+
TCLAP::CmdLine cmd("show-ref", ' ', "0.1");
11+
12+
// TODO defines arguments
13+
cmd.parse(args);
14+
15+
// process args
16+
try {
17+
std::optional<GitRepository> repo = GitRepository::find();
18+
if (repo) {
19+
// TODO filter refs by pattern
20+
21+
// recursively traverse these directories: refs/heads, refs/remotes,
22+
// stash, tags. basically everything in .git/refs
23+
// don't care about filtering first?
24+
25+
// if it's a file, print the ref
26+
fs::path refs_path = repo->repo_path("refs");
27+
fs::path gitdir = repo->repo_path("");
28+
for (const auto &entry : fs::recursive_directory_iterator(refs_path)) {
29+
fs::path path = entry.path();
30+
if (fs::is_regular_file(path)) {
31+
std::string sha = resolve_ref(path, *repo);
32+
std::cout << sha << " " << remove_file_prefix(path, gitdir) << "\n";
33+
}
34+
}
35+
}
36+
} catch (std::runtime_error &err) {
37+
std::cerr << err.what() << "\n";
38+
}
39+
}
40+
} // namespace commands

0 commit comments

Comments
 (0)