-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added required utility funcs and exceptions
- Loading branch information
1 parent
eb75faf
commit baf7471
Showing
7 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,4 @@ | ||
#ifndef CONSTANTS_H | ||
#define CONSTANTS_H | ||
#define DOTGIT ".git" | ||
#endif |
This file contains 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,29 @@ | ||
#ifndef GITREPO_H | ||
#define GITREPO_H | ||
#include <contants.hpp> | ||
#include <filesystem> | ||
#include <gitexceptions.hpp> | ||
#include <string> | ||
|
||
namespace fs = std::filesystem; | ||
|
||
class GitRepo { | ||
private: | ||
std::string worktree; | ||
std::string gitdir; | ||
std::string conf; | ||
|
||
public: | ||
GitRepo(const std::string path, bool force = false) { | ||
worktree = path; | ||
gitdir = fs::path(worktree) / DOTGIT; | ||
|
||
if (!(force || (fs::exists(gitdir) && fs::is_directory(gitdir)))) { | ||
throw Exception("Not a Git repository" + worktree); | ||
} | ||
} | ||
|
||
std::string getGitDir() const { return this->gitdir; } | ||
}; | ||
|
||
#endif |
This file contains 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,19 @@ | ||
#ifndef GITEXCEPTIONS_H | ||
#define GITEXCEPTIONS_H | ||
#include <exception> | ||
#include <iostream> | ||
#include <string> | ||
|
||
class Exception : public std::exception { | ||
private: | ||
std::string message; | ||
|
||
public: | ||
// Constructor accepts a const char* that is used to set | ||
// the exception message | ||
Exception(const std::string& msg) : message(msg) {} | ||
|
||
// Override the what() method to return our message | ||
const char* what() const throw(); | ||
}; | ||
#endif |
This file contains 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,18 @@ | ||
#ifndef UTILS_H | ||
#define UTILS_H | ||
#include <gitRepo.hpp> | ||
#include <string> | ||
class Utils { | ||
private: | ||
public: | ||
// static variadic fucntion | ||
template <typename... Paths> | ||
static std::string repo_path(GitRepo repo, Paths... paths); | ||
// TODO : repo_file, repo_dir | ||
}; | ||
|
||
Utils::Utils(/* args */) {} | ||
|
||
Utils::~Utils() {} | ||
|
||
#endif |
This file contains 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,6 @@ | ||
#include <../include/gitexceptions.hpp> | ||
|
||
Exception::Exception(const std::string& msg) : message(msg) {} | ||
|
||
// Override the what() method to return our message | ||
const char* Exception::what() const throw() { return message.c_str(); } |
This file contains 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,10 @@ | ||
#include <../include/utils.hpp> | ||
|
||
template <typename... Paths> | ||
std::string Utils::repo_path(GitRepo repo, Paths... paths) { | ||
fs::path combined_path; | ||
// Fold expression (C++17) to combine all paths | ||
// "combined_path /= some_path" use to append one path to another | ||
combined_path /= (... /= fs::path(paths)); | ||
return fs::path(repo.getGitDir()) / combined_path; | ||
} |