-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.cpp
More file actions
45 lines (40 loc) · 1.47 KB
/
init.cpp
File metadata and controls
45 lines (40 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <filesystem>
#include <fstream>
#include <unordered_map>
#include <ctime>
#include <sstream>
using namespace std;
namespace fs = filesystem;
//Since we were allowed to use SHA-1 or a custom hash we used our own simpler custom hash.
string Hash(const string& content) {
hash<string> hasher;
return to_string(hasher(content));
}
//Here we declared some global paths to avoid code duplication.
const string MINI_GIT_DIR = ".minigit";
const string OBJECTS_DIR = MINI_GIT_DIR + "/objects";
const string COMMITS_DIR = MINI_GIT_DIR + "/commits";
const string HEAD_FILE = MINI_GIT_DIR + "/HEAD.txt";
const string INDEX_FILE = MINI_GIT_DIR + "/index.txt";
const string BRANCHES_FILE = MINI_GIT_DIR + "/branches.txt";
/*This function is written to get the current timestamp, and it's usefull because the
current timestamp is used when a new file is commited and displayed on the log.*/
string getCurrentTime() {
time_t now = time(0);
char* dt = ctime(&now);
return string(dt);
}
void init() {
if (fs::exists(MINI_GIT_DIR)) {
cout << "MiniGit is already initialized." << endl;
return;
}
fs::create_directory(MINI_GIT_DIR);
fs::create_directory(OBJECTS_DIR);
fs::create_directory(COMMITS_DIR);
ofstream(HEAD_FILE) << "main"; // default branch
ofstream(BRANCHES_FILE) << "main:null\n"; // branch -> commit
ofstream(INDEX_FILE); // empty staging area
cout << "Initialized empty MiniGit repository." << endl;
}