-
Notifications
You must be signed in to change notification settings - Fork 0
/
mygit.cpp
375 lines (343 loc) · 8.82 KB
/
mygit.cpp
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
369
370
371
372
373
#include <unistd.h>
#include <bits/stdc++.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fstream>
#include <dirent.h>
#include "push.hpp"
#include "pull.hpp"
#include "merge.hpp"
using namespace std;
#include "status.hpp"
//#include "rollback.hpp"
#include "add2.hpp"
#include "gitlog.hpp"
#include "commit.hpp"
#include "rollback.hpp"
#include "retrieve_ver_no.hpp"
#include "retrieve_sha_file.hpp"
#include "retrieve_files_vno.hpp"
//string current_path_global;
namespace retrieve_sha_file
{
void retrieve_sha_file();
} // namespace retrieve_sha_file
namespace gitret_files{
int ret_files_func();
}
namespace retrieve_ver_no
{
string retrieve_ver_no();
} // namespace retrieve_ver_no
namespace gitrollback{
int roll_back( );
}
namespace gitstatus
{
int status();
}
namespace gitadd
{
int add();
}
namespace gitpush
{
int push();
}
namespace gitpull
{
int pull();
}
namespace gitmerge
{
int merge();
}
namespace gitlog
{
void writeinlog();
void printlog();
} // namespace gitlog
namespace gitCommit
{
int commit();
}
using namespace gitstatus;
using namespace gitadd;
using namespace gitlog;
using namespace gitCommit;
using namespace retrieve_ver_no;
int init()
{
bool gitdircreate = false;
bool gitchdirmygit = false;
bool gitv0create = false;
bool gitchdirv0 = false;
bool gitcreateindexv0 = false;
char buffer[PATH_MAX];
getcwd(buffer, sizeof(buffer));
string current_path(buffer);
if (mkdir(".mygit", 0777) == -1)
{
cerr << "Error1: " << strerror(errno) << endl;
return 0;
}
else
{
gitdircreate = true;
// cout<<".Mygit directory created"<<endl;
}
if (chdir(".mygit") == -1)
{
cerr << "Error2: " << strerror(errno) << endl;
return 0;
}
else
{
gitchdirmygit = true;
// cout<<"Moved to .mygit directory!!"<<endl;
}
if (mkdir("global", 0777) == -1)
{
cerr << "Error2: " << strerror(errno) << endl;
return 0;
}
if (chdir("global") == -1)
{
cerr << "Error2: " << strerror(errno) << endl;
return 0;
}
vector<string> files = gitadd::get_files_from(current_path);
for (int i = 0; i < files.size(); i++)
{
string filename = files[i];
//copy file from local folder to current version folder
string cwd1 = current_path;
string inppath = cwd1 + "/" + filename;
char temp_global[PATH_MAX];
getcwd(temp_global, sizeof(temp_global));
current_path_global = temp_global;
//changes made here made current_path_global is a global variable
string outpath = current_path_global + "/" + filename;
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();
}
string gitpath = current_path + "/.mygit";
if (chdir(gitpath.c_str()) == -1)
{
cerr << "Error2: " << strerror(errno) << endl;
return 0;
}
// ofstream globfile("global_index.txt", ios::out);
// if (globfile)
// {
// int i = 0;
// for (; i <= files.size() - 1; i++)
// {
// string s = files[i];
// s = s + " 0\n";
// globfile << s;
// }
// }
// globfile.close();
/*
comment above section of code because we are already changing
in untracked module,so when untracked get called,all entries save to
global_index.txt
*/
ofstream fout;
fout.open("version_no.txt", ios::out);
if (fout)
{
string num = "0";
fout << num << endl;
}
fout.close();
ofstream flog;
flog.open("mygit_log.txt", ios::out);
flog.close();
if (mkdir("0", 0777) == -1)
{
cerr << "Error3: " << strerror(errno) << endl;
return 0;
}
else
{
gitv0create = true;
// cout<<"version0 directory created"<<endl;
}
if (chdir("0") == -1)
{
cerr << "Error4: " << strerror(errno) << endl;
return 0;
}
else
{
gitchdirv0 = true;
// cout<<"Moved to .version0 directory!!"<<endl;
}
ofstream fout2;
fout2.open("index.txt");
// cout<<"Created index.txt in v0 file"<<endl;
gitcreateindexv0 = true;
fout.close();
if (gitcreateindexv0 && gitdircreate && gitv0create && gitchdirmygit && gitchdirv0)
return 1;
else
return 0;
};
// void add(){
// };
int main(int argc, char *argv[])
{
string cmd;
cmd = argv[1];
//cout << argv[1] << endl;
if (cmd == "init")
{
if (init())
{
string temp2 = "Mygit initialised!!";
writeinlog(temp2);
cout << temp2 << endl;
exit(EXIT_SUCCESS);
}
else
{
string temp = "Sorry! Error in initialisation!!";
writeinlog(temp);
cout << temp << endl;
exit(EXIT_SUCCESS);
}
}
else if (cmd == "status")
{
if (gitstatus::status())
{
string temp = "Status executed!";
writeinlog(temp);
cout << temp << endl;
exit(EXIT_SUCCESS);
}
else
{
string temp = "Error in status!";
writeinlog(temp);
cout<<temp<<endl;
exit(EXIT_SUCCESS);
}
}
else if (cmd == "add")
{
if (gitadd::add())
{
string temp = "Mygit add executed";
writeinlog(temp);
cout << temp << endl;
exit(EXIT_SUCCESS);
}
}
else if (cmd == "log")
{
gitlog::printlog();
string temp = "Mygit log executed";
writeinlog(temp);
cout << temp << endl;
exit(EXIT_SUCCESS);
}
else if (cmd == "commit")
{
gitCommit::commit();
string temp = "Mygit commit executed";
writeinlog(temp);
cout << temp << endl;
exit(EXIT_SUCCESS);
}
else if (cmd == "retrieve_ver_no")
{
retrieve_ver_no::retrieve_ver_no();
string temp = "Mygit version retrieve executed";
writeinlog(temp);
cout << temp << endl;
exit(EXIT_SUCCESS);
}
else if (cmd == "retrieve")
{
string cmd1 = argv[2];
if (cmd1 != "-a")
{
retrieve_sha_file::retrieve_sha_file(argv[2], argv[3]);
string temp = "Mygit retrieve sha vno executed";
writeinlog(temp);
cout << temp << endl;
exit(EXIT_SUCCESS);
}
else
{
string ver_no = retrieve_ver_no::retrieve_ver_no();
cout<<"ver_no "<<endl;
int curr_ver = atoi(ver_no.c_str());
string vrno = argv[3];
int vno = stoi(vrno);
gitret_files::ret_files_func(vno,curr_ver);
string temp = "Mygit retrieve -a vno executed";
writeinlog(temp);
cout << temp << endl;
exit(EXIT_SUCCESS);
}
}
else if (cmd == "rollback"){
string ver_no = retrieve_ver_no::retrieve_ver_no();
cout<<"ver_no "<<ver_no<<endl;
int curr_ver = atoi(ver_no.c_str());
cout<<"aa"<<endl;
gitrollback::roll_back(curr_ver);
string temp = "Mygit rollback executed";
writeinlog(temp);
cout << temp << endl;
exit(EXIT_SUCCESS);
}
else if ( cmd == "diff" )
{
string inp1=argv[2];
string inp2=argv[3];
string command_diff = "diff "+ inp1 +" "+inp2;
system(command_diff.c_str());
// cout<<"inp1 "<<inp1<<endl;
// cout<<"inp2 "<<inp2<<endl;
string temp = "Mygit diff executed";
writeinlog(temp);
cout << temp << endl;
exit(EXIT_SUCCESS);
}
else if( cmd == "push")
{
gitpush::push();
exit(EXIT_SUCCESS);
}
else if( cmd == "pull")
{
gitpull::pull();
exit(EXIT_SUCCESS);
}
else if(cmd == "merge")
{
gitmerge::merge();
exit(EXIT_SUCCESS);
}
else
{
cout << "Wrong Command" << endl;
exit(EXIT_SUCCESS);
}
}
}