-
Notifications
You must be signed in to change notification settings - Fork 0
/
add2.hpp
371 lines (328 loc) · 8.49 KB
/
add2.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
namespace gitadd
{
#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 current_path_global;
char cwd[PATH_MAX];
string mygit_path1;
string vcpath;
string vc;
string indexpath;
vector<string> filelist;
map<string, string> filemap;
vector<string> untracked;
vector<string> modified;
vector<string> deleted;
int numoffiles;
bool is_regular_file(const char *path)
{
struct stat path_stat;
stat(path, &path_stat);
return S_ISREG(path_stat.st_mode);
}
string get_sha(string file_name)
{
string f1 = "sha1sum " + file_name + " > temp.txt";
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;
}
int process_untracked(vector<string> &untrackedv)
{
for (int i = 0; i < untrackedv.size(); i++)
{
string filename = untrackedv[i];
//copy file from local folder to current version folder
string cwd1 = cwd;
string inppath = cwd1 + "/" + filename;
string filesha = get_sha(inppath);
string outpath = mygit_path1 + vc + "/" + filename;
//
string command_copy = "cp ";
command_copy = command_copy + inppath + " ";
command_copy = command_copy + cwd1 + "/.mygit/global/" + filename;
//cout << command_copy << endl;
system(command_copy.c_str());
/*
copying the untracked files to global folder
*/
//
ifstream fin;
ofstream fout;
fin.open(inppath, ios::in);
fout.open(outpath, ios::out);
string line;
while (fin)
{
getline(fin, line);
fout << line << endl;
}
fin.close();
fout.close();
filemap[filename] = filesha;
}
//copying untracked files to global , globalindex[untrackedfiles version_no]
//updating entry in global_index.txt file for untracked files only
string globindexpath = string(cwd) + "/.mygit/global_index.txt";
cout << "global path : " << globindexpath << endl;
ofstream globindexfile(globindexpath, ios::app);
for (int i = 0; i < untrackedv.size(); i++)
{
string filename = untrackedv[i];
string cwd1 = cwd;
string inppath = cwd1 + "/" + filename;
string outpath = current_path_global + "/" + filename;
globindexfile << filename << " " << vc << "\n";
ifstream fin;
ofstream fout;
fin.open(inppath, ios::in);
fout.open(outpath, ios::out);
string line;
while (fin)
{
getline(fin, line);
fout << line << endl;
}
fin.close();
fout.close();
}
globindexfile.close();
//
ofstream fout1;
fout1.open(indexpath, ios::out);
for (auto itr = filemap.begin(); itr != filemap.end(); itr++)
{
string line;
line += itr->first + " " + itr->second;
fout1 << line << endl;
}
fout1.close();
return 1;
}
int process_modified(vector<string> modifiedv)
{
for (int i = 0; i < modifiedv.size(); i++)
{
string filename = modifiedv[i];
string cwd1 = cwd;
string inpath = cwd1 + "/" + filename;
string outpath = mygit_path1 + vc + "/" + filename;
ifstream fin(inpath);
ofstream fout(outpath);
string line;
while (fin)
{
getline(fin, line);
fout << line << endl;
}
fin.close();
fout.close();
string filesha = get_sha(filename);
filemap[filename] = filesha;
}
ofstream fout1;
fout1.open(indexpath, ios::out);
for (auto itr = filemap.begin(); itr != filemap.end(); itr++)
{
string line;
line += itr->first + " " + itr->second;
fout1 << line << endl;
}
fout1.close();
return 1;
}
int process_deleted(vector<string> deletedv)
{
string inpath = mygit_path1 + vc + "/";
for (int i = 0; i < deletedv.size(); i++)
{
string filename = inpath + deletedv[i];
filemap.erase(deletedv[i]);
// cout<<"path to delete:"<<filename<<endl;
remove(filename.c_str());
}
ofstream fout1;
fout1.open(indexpath, ios::trunc | ios::app);
for (auto itr = filemap.begin(); itr != filemap.end(); itr++)
{
string line;
line += itr->first + " " + itr->second;
fout1 << line << endl;
}
fout1.close();
return 1;
}
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;
string filepath = path + "/" + fname;
if (is_regular_file(filepath.c_str()))
{
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))
{
stringstream ss(line);
string filename = "";
string sha = "";
ss >> filename;
ss >> sha;
toret[filename] = sha;
}
}
else
{
perror("Unable to open file information get_map_from function");
exit(1);
}
return toret;
}
int add()
{
mygit_path1 = "";
if (getcwd(cwd, sizeof(cwd)) != NULL)
{
mygit_path1 = cwd;
mygit_path1 += "/.mygit/";
}
else
{
perror("Unable to get current working directory!\n");
exit(1);
}
vcpath = mygit_path1 + VERSION_FILE;
fstream vcfile(vcpath, std::ios_base::in);
vcfile >> vc;
;
indexpath = mygit_path1 + vc + "/" + INDEX_FILE;
filelist = get_files_from(cwd);
numoffiles = filelist.size();
filemap = get_map_from(indexpath);
for (int i = 0; i < numoffiles; i++)
{
string shaf = get_sha(filelist[i]);
if (filemap.find(filelist[i]) == filemap.end())
{
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);
}
}
if (modified.size() != 0)
{
cout << "Modified:" << endl;
for (int i = 0; i < modified.size(); i++)
{
cout << modified[i] << endl;
}
}
if (untracked.size() != 0)
{
cout << "Untracked:" << endl;
for (int i = 0; i < untracked.size(); i++)
{
cout << untracked[i] << endl;
}
}
if (deleted.size() != 0)
{
cout << "Deleted:" << endl;
for (int i = 0; i < deleted.size(); i++)
{
cout << deleted[i] << endl;
}
}
int pmodified = 0, puntracked = 0, pdeleted = 0;
if (modified.size() != 0)
{
pmodified = process_modified(modified);
if (pmodified)
{
modified.clear();
}
}
if (untracked.size() != 0)
{
puntracked = process_untracked(untracked);
if (puntracked)
untracked.clear();
if (untracked.size())
cout << "untracked clear!" << endl;
}
if (deleted.size() != 0)
{
pdeleted = process_deleted(deleted);
if (pdeleted)
deleted.clear();
}
if (modified.size() == 0 && untracked.size() == 0 && deleted.size() == 0)
{
return 1;
}
else
{
return 0;
}
}
} // namespace gitadd