Skip to content

Commit

Permalink
added required utility funcs and exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
iamAbhishekkumar committed Oct 7, 2024
1 parent eb75faf commit baf7471
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 1 deletion.
44 changes: 43 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,48 @@
"ostream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp"
"typeinfo": "cpp",
"chrono": "cpp",
"codecvt": "cpp",
"ctime": "cpp",
"optional": "cpp",
"ratio": "cpp",
"iomanip": "cpp",
"sstream": "cpp",
"any": "cpp",
"barrier": "cpp",
"bitset": "cpp",
"cfenv": "cpp",
"charconv": "cpp",
"cinttypes": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"coroutine": "cpp",
"csetjmp": "cpp",
"csignal": "cpp",
"cstring": "cpp",
"cuchar": "cpp",
"forward_list": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"unordered_set": "cpp",
"regex": "cpp",
"source_location": "cpp",
"fstream": "cpp",
"future": "cpp",
"latch": "cpp",
"mutex": "cpp",
"ranges": "cpp",
"scoped_allocator": "cpp",
"semaphore": "cpp",
"shared_mutex": "cpp",
"span": "cpp",
"stop_token": "cpp",
"syncstream": "cpp",
"thread": "cpp",
"typeindex": "cpp",
"valarray": "cpp",
"variant": "cpp"
}
}
4 changes: 4 additions & 0 deletions include/contants.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef CONSTANTS_H
#define CONSTANTS_H
#define DOTGIT ".git"
#endif
29 changes: 29 additions & 0 deletions include/gitRepo.hpp
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
19 changes: 19 additions & 0 deletions include/gitexceptions.hpp
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
18 changes: 18 additions & 0 deletions include/utils.hpp
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
6 changes: 6 additions & 0 deletions src/gitexceptions.cpp
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(); }
10 changes: 10 additions & 0 deletions src/utils.cpp
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;
}

0 comments on commit baf7471

Please sign in to comment.