/
beargit.c
executable file
·606 lines (513 loc) · 18.5 KB
/
beargit.c
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "beargit.h"
#include "util.h"
/* Implementation Notes:
*
* - Functions return 0 if successful, 1 if there is an error.
* - All error conditions in the function description need to be implemented
* and written to stderr. We catch some additional errors for you in main.c.
* - Output to stdout needs to be exactly as specified in the function description.
* - Only edit this file (beargit.c)
* - You are given the following helper functions:
* * fs_mkdir(dirname): create directory <dirname>
* * fs_rm(filename): delete file <filename>
* * fs_mv(src,dst): move file <src> to <dst>, overwriting <dst> if it exists
* * fs_cp(src,dst): copy file <src> to <dst>, overwriting <dst> if it exists
* * write_string_to_file(filename,str): write <str> to filename (overwriting contents)
* * read_string_from_file(filename,str,size): read a string of at most <size> (incl.
* NULL character) from file <filename> and store it into <str>. Note that <str>
* needs to be large enough to hold that string.
* - You NEED to test your code. The autograder we provide does not contain the
* full set of tests that we will run on your code. See "Step 5" in the homework spec.
*/
/* beargit init
*
* - Create .beargit directory
* - Create empty .beargit/.index file
* - Create .beargit/.prev file containing 0..0 commit id
*
* Output (to stdout):
* - None if successful
*/
int beargit_init(void) {
fs_mkdir(".beargit");
FILE* findex = fopen(".beargit/.index", "w");
fclose(findex);
FILE* fbranches = fopen(".beargit/.branches", "w");
fprintf(fbranches, "%s\n", "master");
fclose(fbranches);
write_string_to_file(".beargit/.prev", "0000000000000000000000000000000000000000");
write_string_to_file(".beargit/.current_branch", "master");
return 0;
} // given
/* beargit add <filename>
*
* - Append filename to list in .beargit/.index if it isn't in there yet
*
* Possible errors (to stderr):
* >> ERROR: File <filename> already added
*
* Output (to stdout):
* - None if successful
*/
int beargit_add(const char* filename) {
FILE* findex = fopen(".beargit/.index", "r");
FILE *fnewindex = fopen(".beargit/.newindex", "w");
char line[FILENAME_SIZE];
while(fgets(line, sizeof(line), findex)) {
strtok(line, "\n");
if (strcmp(line, filename) == 0) {
fprintf(stderr, "ERROR: File %s already added\n", filename);
fclose(findex);
fclose(fnewindex);
fs_rm(".beargit/.newindex");
return 3;
}
fprintf(fnewindex, "%s\n", line);
}
fprintf(fnewindex, "%s\n", filename);
fclose(findex);
fclose(fnewindex);
fs_mv(".beargit/.newindex", ".beargit/.index");
return 0;
} // given
/* beargit status
*
* See "Step 1" in the homework 1 spec.
*
*/
int beargit_status() {
/* COMPLETE THE REST */
FILE* findex = fopen(".beargit/.index", "r");
char line[FILENAME_SIZE];
printf("Tracked files:\n\n");
int counter = 0;
while(fgets(line, sizeof(line), findex)) {
strtok(line, "\n"); /* Split string to files */
printf(" %s\n", line);
counter++;
}
printf("\n%d files total\n", counter);
fclose(findex);
return 0;
} // done
/* beargit rm <filename>
*
* See "Step 2" in the homework 1 spec.
*
*/
int beargit_rm(const char* filename) {
/* COMPLETE THE REST */
FILE* findex = fopen(".beargit/.index", "r");
FILE *fnewindex = fopen(".beargit/.newindex", "w");
int same_ctr = 0;
char line[FILENAME_SIZE];
while(fgets(line, sizeof(line), findex)) {
strtok(line, "\n");
if (strcmp(line, filename) != 0) { // if file and name r not the same
// copy the file for the newindex
fprintf(fnewindex, "%s\n", line);
} else { // if the file and name r the same, increment the same_ctr
same_ctr++;
}
}
if (same_ctr == 0) {
fprintf(stderr, "ERROR: File %s not tracked\n", filename);
return 1;
} else {
fs_mv(".beargit/.newindex", ".beargit/.index");
}
//fprintf(fnewindex, "%s\n", filename);
fclose(findex);
fclose(fnewindex);
return 0;
}
/* beargit commit -m <msg>
*
* See "Step 3" in the homework 1 spec.
*
*/
const char* go_bears = "GO BEARS!";
int is_commit_msg_ok(const char* msg) {
//const char* go_bears = "GO BEARS!"; //used to comapre if the commit msg contains this strings
int contains_GO_BEARS = 0; //used as a indicator of whether find this phrase in commit_msg. if find, set to 1.
int i;
for (i =0; msg[i]!='\0'; i++)//scan from the beging of the commit_msg
{
if (msg[i]=='G') //if find a ‘G’, then check if the following 8 characters will form “GO BEARS!”
{
int i3 = i; //record the position of i;
int count = 0; //record how many letters are the same
for (int i2 = 0; i2<9; i2++)
{
if (msg[i3++]==go_bears[i2])
count++;
}
if (count==9)
return 1;
}
}
return contains_GO_BEARS;
}
//if contains 30 zeros, return 1
//if not, return 0
int check_if_30_zeros (char* testing_ID)
{
int i;
for (i=0; i<30; i++)
{
if (testing_ID[i]!='0')
return 0;
}
return 1;
}
void convert_30_zeros_to_1 (char* IDs)
{
int i;
for (i=0; i<30; i++)
IDs[i] = '1';
}
//char new_id contains the last 30 characters of the commit id
//need to combine with the 10 branch digit
void generate_last_30_commit_id (char *old_id, char* new_id)
{
if (check_if_30_zeros(old_id))
convert_30_zeros_to_1(old_id);
//copy everything from old id to new id
int i;
for (i=0; i<30; i++)
{
new_id[i] = old_id[i];
}
//plus 1 to the previous id
for (i=29; i>=0; i--)
{
switch (new_id[i])
{
case '1':
new_id[i] = '6';
return;
break;
case '6':
new_id[i] = 'c';
return;
break;
case 'c':
new_id[i] = '1';
break;
}
}
}
void next_commit_id_part1(char* commit_id) {
/* COMPLETE THE REST */
// The first COMMIT_ID_BRANCH_BYTES=10 characters of the commit ID will
// be used to encode the current branch number. This is necessary to avoid
// duplicate IDs in different branches, as they can have the same pre-
// decessor (so next_commit_id has to depend on something else).
char new_id [30];
generate_last_30_commit_id(commit_id, new_id);
int i;
for (i = 0; i < 30; i++) {
commit_id[i] = new_id[i];
} // does the job but does not ensure unique
}
int is_HEAD() {
char HEAD_STRING[BRANCHNAME_SIZE];
read_string_from_file(".beargit/.current_branch", HEAD_STRING, BRANCHNAME_SIZE);
int i = strlen(HEAD_STRING);
if (i != 0) {
return 1;
}
return 0;
}
int beargit_commit(const char* msg) {
if (!is_commit_msg_ok(msg)) { // checking if true for go_bears
// if not ok, error is thrown
fprintf(stderr, "ERROR: Message must contain \"%s\"\n", go_bears);
return 1;
}
if (!is_HEAD()) {
fprintf(stderr, "ERROR: Need to be on HEAD of a branch to commit\n");
return 1;
}
// Read the ID of the previous last commit from .beargit/.prev
char commit_id[COMMIT_ID_SIZE];
read_string_from_file(".beargit/.prev", commit_id, COMMIT_ID_SIZE);
next_commit_id(commit_id); // getting a new id
/* COMPLETE THE REST */
// Generate a new directory .beargit/<newid> and
// copy .beargit/.index, .beargit/.prev and all
// tracked files into the directory.
// Generate a new directory .beargit/<newid> and copy
// .beargit/.index, .beargit/.prev and all tracked files
// into the directory.
char newid_dir[FILENAME_SIZE];
strcpy(newid_dir, ".beargit/");
strcat(newid_dir, commit_id); // newid_dir = .beargit/<newID>
fs_mkdir(newid_dir); // making .beargit/<newid>
char prev_dir[FILENAME_SIZE];
strcpy(prev_dir, newid_dir);
strcat(prev_dir, "/.prev"); // prev_dir = .beargit/<newID>/.prev
fs_cp(".beargit/.prev", prev_dir); // copying .beargit/.prev to .beargit/<newID>/.prev
char index_dir[FILENAME_SIZE];
strcpy(index_dir, newid_dir);
strcat(index_dir, "/.index"); // index_dir = .beargit/<newID>/.index
fs_cp(".beargit/.index", index_dir); // copying .beargit/.index to .beargit/<newID>/.index
// copy the tracked files into .beargit/<newID>
FILE* findex = fopen(".beargit/.index", "r");
char line[FILENAME_SIZE];
while(fgets(line, sizeof(line), findex)) {
strtok(line, "\n");
// read one line each run. line = <tracked_file>
char track_dir[FILENAME_SIZE];
strcpy(track_dir, newid_dir); // track_dir = .beargit/<newID>
strcat(track_dir, "/"); // track_dir = .beargit/<newID>/
strcat(track_dir, line); // track_dir = .beargit/<newID>/line
// track_dir is dst
char ori_dir[FILENAME_SIZE];
strcpy(ori_dir, line); // ori_dir = line
// ori_dir is src
fs_cp(ori_dir, track_dir);
}
fclose(findex);
// Store the commit message (<msg>) into .beargit/<newID>/.msg
strcat(newid_dir, "/.msg"); // newid_dir = .beargit/<newID>/.msg
write_string_to_file(newid_dir, msg);
// Write the new ID into .beargit/.prev.
write_string_to_file(".beargit/.prev", commit_id);
return 0;
}
/* beargit log
*
* See "Step 4" in the homework 1 spec.
*
*/
const char* init_id = "0000000000000000000000000000000000000000";
int beargit_log(int limit) {
/* COMPLETE THE REST */
char commit_id[COMMIT_ID_SIZE];
read_string_from_file(".beargit/.prev", commit_id, COMMIT_ID_SIZE);
if (strcmp(init_id, commit_id) == 0) { // if there has been no commits
// throw error
fprintf(stderr, "ERROR: There are no commits!\n");
return 1;
} else { // if there has been commits
if (limit > 0) { // if limit is greater than 0
// print limit amount
char prev_commit[COMMIT_ID_SIZE];
// read the prev_commit from .prev
read_string_from_file(".beargit/.prev", prev_commit, COMMIT_ID_SIZE);
// while limit != 0, and prev_commit != 40s 0
//
//printf("\n");
while ((limit != 0) && (strcmp(init_id, prev_commit) != 0)) {
limit--; // limit down by 1
printf("\ncommit %s\n", prev_commit);
char prev_msg_dir[FILENAME_SIZE];
strcpy(prev_msg_dir, ".beargit/");
strcat(prev_msg_dir, prev_commit); // .beargit/<prev_id>
strcat(prev_msg_dir, "/.msg"); // .beargit/<prev_id>/.msg
char prev_msg[MSG_SIZE];
read_string_from_file(prev_msg_dir, prev_msg, MSG_SIZE);
printf(" %s", prev_msg);
char temp_prev_commit[FILENAME_SIZE];
strcpy(temp_prev_commit, ".beargit/"); // .beargit/
strcat(temp_prev_commit, prev_commit); // .beargit/<prev_id>
strcat(temp_prev_commit, "/.prev"); // .beargit/<previ_id>/.prev
// overwrite prev_commit with .prev from its .prev
read_string_from_file(temp_prev_commit, prev_commit, COMMIT_ID_SIZE);
printf("\n");
}
printf("\n");
}
}
return 0;
}
const char* digits = "61c";
void next_commit_id(char* commit_id) {
char current_branch[BRANCHNAME_SIZE];
read_string_from_file(".beargit/.current_branch", current_branch, BRANCHNAME_SIZE);
// The first COMMIT_ID_BRANCH_BYTES=10 characters of the commit ID will
// be used to encode the current branch number. This is necessary to avoid
// duplicate IDs in different branches, as they can have the same pre-
// decessor (so next_commit_id has to depend on something else).
int n = get_branch_number(current_branch);
for (int i = 0; i < COMMIT_ID_BRANCH_BYTES; i++) {
commit_id[i] = digits[n%3];
n /= 3;
} // this is only for first 10 indexes
// Use next_commit_id to fill in the rest of the commit ID.
next_commit_id_part1(commit_id + COMMIT_ID_BRANCH_BYTES);
}
// This helper function returns the branch number for a specific branch, or
// returns -1 if the branch does not exist.
int get_branch_number(const char* branch_name) {
FILE* fbranches = fopen(".beargit/.branches", "r");
int branch_index = -1;
int counter = 0;
char line[FILENAME_SIZE];
while(fgets(line, sizeof(line), fbranches)) {
strtok(line, "\n");
if (strcmp(line, branch_name) == 0) {
branch_index = counter;
}
counter++;
}
fclose(fbranches);
return branch_index;
}
/* beargit branch
*
* See "Step 5" in the homework 1 spec.
*
*/
int beargit_branch() {
/* COMPLETE THE REST */
FILE* findex = fopen(".beargit/.branches", "r");
char line[BRANCHNAME_SIZE];
char curren_branch_name [BRANCHNAME_SIZE];
read_string_from_file(".beargit/.current_branch", curren_branch_name, BRANCHNAME_SIZE);
while(fgets(line, sizeof(line), findex)) {
strtok(line, "\n"); /* Split string to files */
if (!strcmp(line, curren_branch_name))
{
printf("* %s\n", line);
}
else
{
printf(" %s\n", line);
}
}
fclose(findex);
return 0;
}
/* beargit checkout
*
* See "Step 6" in the homework 1 spec.
*
*/
int checkout_commit(const char* commit_id) {
/* COMPLETE THE REST */
// Going through the index of the current index file,
// delete all those files (in the current directory;
// i.e., the directory where we ran beargit).
//sprintf(current_index_file, ".beargit/.index/%s", commit_id);
// fs_rm(".beargit/.index");
if (strcmp(commit_id, init_id) != 0) {
FILE* findex = fopen(".beargit/.index", "r");
FILE *findex_w = fopen(".beargit/.index", "w");
char line[FILENAME_SIZE];
while(fgets(line, sizeof(line), findex)) {
strtok(line, "\n"); /* Split string to files */
fs_rm(line);
}
fclose(findex);
// Copy the index from the commit that is being
// checked out to the .beargit directory, and use
// it to copy all that commit's tracked files from
// the commit's directory into the current directory.
char newindex_dir[FILENAME_SIZE];
strcpy(newindex_dir, ".beargit/"); // .beargit/
strcat(newindex_dir, commit_id); // .beargit/<new_ID>
strcat(newindex_dir, "/.index"); // .beargit/<new_ID>/.index
FILE *fnewindex = fopen(newindex_dir, "r");
char rec_file[FILENAME_SIZE];
char direc[FILENAME_SIZE+15];
fs_cp(newindex_dir, ".beargit/.index");
while(fgets(rec_file, sizeof(rec_file), fnewindex)) {
strtok(rec_file, "\n"); /* Split string to files */
sprintf(direc, ".beargit/%s/%s", commit_id, rec_file);
fs_cp(direc, rec_file);
}
fclose(fnewindex);
fclose(findex_w);
} else {
FILE* findex = fopen(".beargit/.index", "r");
FILE *findex_w = fopen(".beargit/.index", "w");
char line[FILENAME_SIZE];
while(fgets(line, sizeof(line), findex)) {
strtok(line, "\n"); /* Split string to files */
fs_rm(line);
}
fclose(findex);
fclose(findex_w);
}
// Write the ID of the commit that is being checked
// out into .prev.
write_string_to_file(".beargit/.prev", commit_id);
return 0;
}
int is_it_a_commit_id(const char* commit_id) {
/* COMPLETE THE REST */
int i;
int n = strlen(commit_id);
for (i = 0; i < n; i++) {
// if commit[i] is not 6, 1, or c
if ((commit_id[i] != '1') && (commit_id[i] != '6') && (commit_id[i] != 'c')) {
return 0;
}
}
return 1;
}
int beargit_checkout(const char* arg, int new_branch) {
// Get the current branch
char current_branch[BRANCHNAME_SIZE];
read_string_from_file(".beargit/.current_branch", current_branch, BRANCHNAME_SIZE);
//MISTAKE 1: the second arguments should be current_branch rather than "current_branch"
// If not detached, update the current branch by storing the current HEAD into that branch's file...
// Even if we cancel later, this is still ok.
if (strlen(current_branch)) {
char current_branch_file[BRANCHNAME_SIZE+50];
sprintf(current_branch_file, ".beargit/.branch_%s", current_branch);
fs_cp(".beargit/.prev", current_branch_file);
}
// Check whether the argument is a commit ID. If yes, we just stay in detached mode
// without actually having to change into any other branch.
if (is_it_a_commit_id(arg)) {
char commit_dir[FILENAME_SIZE] = ".beargit/";
strcat(commit_dir, arg);
if (!fs_check_dir_exists(commit_dir)) {
fprintf(stderr, "ERROR: Commit %s does not exist\n", arg);
return 1;
}
// Set the current branch to none (i.e., detached).
write_string_to_file(".beargit/.current_branch", "");
return checkout_commit(arg);
}
// Just a better name, since we now know the argument is a branch name.
const char* branch_name = arg;
// Read branches file (giving us the HEAD commit id for that branch).
// branch_exists=1: exits
// branch_exists=0: not exist
int branch_exists = (get_branch_number(branch_name) >= 0);
// Check for errors.
if (!(!branch_exists || !new_branch)) {
fprintf(stderr, "ERROR: A branch named %s already exists\n", branch_name);
return 1;
//MISTAKE 2: no branch exist error should only been print if new_branch evaluate to false and branch_exist ealuate to false.
} else if (!branch_exists && !new_branch) {
fprintf(stderr, "ERROR: No branch %s exists\n", branch_name);
return 1;
}
// File for the branch we are changing into.
//MISTAKES 3: if use char* branch_file, it is only for reading, can apply strcat
//should change to char branch_file [] = " "
char branch_file[] = ".beargit/.branch_";
strcat(branch_file, branch_name);
// Update the branch file if new branch is created (now it can't go wrong anymore)
if (new_branch) {
FILE* fbranches = fopen(".beargit/.branches", "a");
fprintf(fbranches, "%s\n", branch_name);
fclose(fbranches);
fs_cp(".beargit/.prev", branch_file);
}
write_string_to_file(".beargit/.current_branch", branch_name);
// Read the head commit ID of this branch.
char branch_head_commit_id[COMMIT_ID_SIZE];
read_string_from_file(branch_file, branch_head_commit_id, COMMIT_ID_SIZE);
// Check out the actual commit.
return checkout_commit(branch_head_commit_id);
}