-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.hpp
166 lines (153 loc) · 3.93 KB
/
status.hpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
namespace gitstatus
{
#include <iostream>
#include <bits/stdc++.h>
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
#include <dirent.h>
#include <sys/types.h>
#include <fstream>
#define VERSION_FILE "version_no.txt"
#define INDEX_FILE "index.txt"
using namespace std;
string get_sha(string file_name)
{
string f1 = "sha1sum " + file_name + " > temp.txt";
//cout<<f1<<endl;
char buff[255];
strcpy(buff, f1.c_str());
system(buff);
FILE *fp = fopen("temp.txt", "r+");
char buffer[100];
fread(buffer, sizeof(char), 100, fp);
string input = buffer;
stringstream ss(input);
string sha;
ss >> sha;
fclose(fp);
int status = remove("temp.txt");
if (status)
{
cout << "fail to delete file" << endl;
;
}
return sha;
}
vector<string> get_files_from(string path)
{
vector<string> toret;
struct dirent *entry;
DIR *dir = opendir(path.c_str());
if (dir == NULL)
{
cout << "Error in opening Directory";
exit(EXIT_FAILURE);
}
while ((entry = readdir(dir)) != NULL)
{
//cout << entry->d_name << endl;
string fname = entry->d_name;
if (fname == "." || fname == ".." || fname == ".mygit" || fname == "a.out" || fname == ".vscode" || fname=="mygit")
{
continue;
}
else
{
toret.push_back(fname);
}
}
closedir(dir);
return toret;
}
map<string, string> get_map_from(string path)
{
map<string, string> toret;
fstream f(path, std::ios_base::in);
if (f.is_open())
{
string line;
while (getline(f, line)) //reading line by line of file
{
stringstream ss(line); //tokenising using space filename sha
string filename = "";
string sha = "";
ss >> filename;
ss >> sha;
toret[filename] = sha;
}
}
else
{
perror("unable to open file inf get_map_from function");
exit(1);
}
return toret;
}
int status()
{
char cwd[PATH_MAX]; //defined in limits
string mygit_path = "";
if (getcwd(cwd, sizeof(cwd)) != NULL)
{
mygit_path = cwd;
mygit_path += "/.mygit/";
}
else
{
perror("unable to get current working directory");
exit(1);
}
//READING VERSION NUMBER AND MAP OF ALREADY ADDED FILES
//-----------------------------------------------------
string vcpath = mygit_path + VERSION_FILE;
fstream vcfile(vcpath, std::ios_base::in);
string vc;
vcfile >> vc;
//cout << vc << endl; /* code */
vcfile.close();
string indexpath = mygit_path + vc + "/" + INDEX_FILE;
//cout << "indexpath : " << indexpath << endl;
vector<string> filelist = get_files_from(cwd);
int numoffiles = filelist.size();
map<string, string> filemap = get_map_from(indexpath);
vector<string> untracked; //new
vector<string> modified;
vector<string> deleted;
for (int i = 0; i < numoffiles; i++)
{
string shaf = get_sha(filelist[i]);
if (filemap.find(filelist[i]) == filemap.end())
{
//cout << "filelist[i]" << filelist[i] << endl;
untracked.push_back(filelist[i]);
}
else
{
if (filemap[filelist[i]] != shaf)
{
modified.push_back(filelist[i]);
}
}
}
sort(filelist.begin(), filelist.end());
for (auto f : filemap)
{
if (find(filelist.begin(), filelist.end(), f.first) == filelist.end())
{
deleted.push_back(f.first);
}
}
cout << "no. of modified files " << modified.size() << endl;
cout << "no. of untracked files " << untracked.size() << endl;
cout << "no. of deleted files " << deleted.size() << endl;
if (modified.size() == 0 && untracked.size() == 0 && deleted.size() == 0)
{
return 1;
}
else
{
return 0;
}
}
}