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 beargit_reset(const char* commit_id, const char* filename) { // Check if the file is in the commit directory /* COMPLETE THIS PART */ // Copy the file to the current working directory /* COMPLETE THIS PART */ // Add the file if it wasn't already there /* COMPLETE THIS PART */ if (!is_it_a_commit_id(commit_id)) { fprintf(stderr, "ERROR: Commit %s does not exist.\n", commit_id); return 1; } char restore_file[BEARGIT_LENGTH + COMMIT_ID_SIZE + FILENAME_SIZE +1]; sprintf(restore_file, ".beargit/%s/%s", commit_id, filename); if(access(restore_file, F_OK) == -1){ fprintf(stderr, "ERROR: %s is not in the index of commit %s.\n", filename, commit_id); return 1; } fs_cp(restore_file, filename); char file[FILENAME_SIZE]; FILE* index = fopen(".beargit/.index", "a+"); while(fgets(file, sizeof(file), index)){ strtok(file,"\n"); if(!strcmp(file, filename)) { fclose(index); return 0; } } fprintf(index, "%s\n",filename); fclose(index); return 0; }
int checkout_commit(const char* commit_id) { /* COMPLETE THE REST */ if (!strcmp(commit_id, "0000000000000000000000000000000000000000")) { delete_tracked_files(); //write_string_to_file(".beargit/.index", ""); FILE *index_new = fopen(".beargit/.index", "w"); fclose(index_new); write_string_to_file(".beargit/.prev", commit_id); return 0; } delete_tracked_files(); char path[BEARGIT_LENGTH + COMMIT_ID_SIZE + 7]; sprintf(path, ".beargit/%s/.index", commit_id); load_files_from_commit(commit_id); fs_cp(path, ".beargit/.index"); // sprintf(path, ".beargit/%s/.prev", commit_id); // fs_cp(commit_id, ".beargit/.prev"); write_string_to_file(".beargit/.prev", commit_id); return 0; }
int checkout_commit(const char* commit_id) { if (strcmp(commit_id, d) == 0) { FILE* findex = fopen(".beargit/.index", "r"); char line[FILENAME_SIZE]; while(fgets(line, sizeof(line), findex)){ strtok(line, "\n"); char gg[FILENAME_SIZE]; sprintf(gg, "%s", line); fs_rm(gg); } fclose(findex); FILE* troll = fopen(".beargit/.index", "w"); fclose(troll); } else { FILE* findex = fopen(".beargit/.index", "r"); char line[FILENAME_SIZE]; while(fgets(line, sizeof(line), findex)){ strtok(line, "\n"); char gg[FILENAME_SIZE]; sprintf(gg, "%s", line); fs_rm(gg); } fclose(findex); char dir[FILENAME_SIZE]; sprintf(dir, ".beargit/%s/.index", commit_id); fs_cp(dir, ".beargit/.index"); // commit id change FILE* cIndex = fopen(dir , "r"); char cline[FILENAME_SIZE]; while (fgets(cline,sizeof(cline),cIndex)) { strtok(cline, "\n"); char fName[FILENAME_SIZE]; sprintf(fName, ".beargit/%s/%s", commit_id, cline); fs_cp(fName, cline); // needs to be fixed, it's not copying } fclose(cIndex); } write_string_to_file(".beargit/.prev", commit_id); return 0; }
int beargit_merge(const char* arg) { // Get the commit_id or throw an error char commit_id[COMMIT_ID_SIZE]; if (!is_it_a_commit_id(arg)) { if (get_branch_number(arg) == -1) { fprintf(stderr, "ERROR: No branch or commit %s exists.\n", arg); return 1; } char branch_file[FILENAME_SIZE]; snprintf(branch_file, FILENAME_SIZE, ".beargit/.branch_%s", arg); read_string_from_file(branch_file, commit_id, COMMIT_ID_SIZE); } else { snprintf(commit_id, COMMIT_ID_SIZE, "%s", arg); } // Iterate through each line of the commit_id index and determine how you // should copy the index file over /* COMPLETE THE REST */ char commit_index[BEARGIT_LENGTH + COMMIT_ID_SIZE + 10]; sprintf(commit_index,".beargit/%s/.index", commit_id); FILE *index = fopen(commit_index, "r"); char file[FILENAME_SIZE]; while(fgets(file, sizeof(file), index)) { strtok(file, "\n"); if(file_in_index(file)){ char confilcted_file[FILENAME_SIZE +COMMIT_ID_SIZE+10]; char file_name_in_commit[BEARGIT_LENGTH + COMMIT_ID_SIZE + FILENAME_SIZE+10]; sprintf(file_name_in_commit, ".beargit/%s/%s", commit_id, file); sprintf(confilcted_file, "%s.%s",file,commit_id); fs_cp(file_name_in_commit, confilcted_file); fprintf(stdout, "%s conflicted copy created\n", file); } else { char file_name_in_commit[BEARGIT_LENGTH + COMMIT_ID_SIZE + FILENAME_SIZE+10]; sprintf(file_name_in_commit, ".beargit/%s/%s", commit_id, file); fs_cp(file_name_in_commit, file); fprintf(stdout, "%s added\n", file); } } fclose(index); return 0; }
// .beargit/.index .beargit/commit_id/ void copyTo (char* source, char* dest) { FILE* sourceFile = fopen(source, "r"); char fName[FILENAME_SIZE]; char toDir[FILENAME_SIZE]; while (fgets(fName, FILENAME_SIZE, sourceFile)) { strtok(fName, "\n"); strcpy(toDir, dest); strcat(toDir, fName); //.beargit/commit_id/fName fs_cp(fName, toDir); } fclose(sourceFile); }
void load_files_from_commit(const char *commit_id) { char commit_index[BEARGIT_LENGTH + COMMIT_ID_SIZE + 7]; sprintf(commit_index, ".beargit/%s/.index", commit_id); char file_name[FILENAME_SIZE]; FILE *index = fopen(commit_index, "r"); char file_name_in_commit[BEARGIT_LENGTH + COMMIT_ID_SIZE + FILENAME_SIZE + 1]; while (fgets(file_name, sizeof(file_name), index)) { strtok(file_name, "\n"); sprintf(file_name_in_commit, ".beargit/%s/%s", commit_id, file_name); fs_cp(file_name_in_commit, file_name); } fclose(index); }
void copy_files(char *file_location, char *final_destination, int is_hidden) { char destination_of_file[BEARGIT_LENGTH + COMMIT_ID_SIZE + FILENAME_SIZE]; if (is_hidden) { char *helper_pointer = file_location; helper_pointer += BEARGIT_LENGTH; sprintf(destination_of_file, "%s/%s", final_destination,helper_pointer); // fprintf(stdout, "%s\n", destination_of_file); } else { sprintf(destination_of_file, "%s/%s", final_destination,file_location); } fs_cp(file_location, destination_of_file); }
// Used to copy a file unsigned int fs_cp(char * name, char * newname, int from_inode, int to_inode) { int i1 = fs_open_file(name, from_inode, O_RD, EXT2_S_IFREG); if(i1 < 0) { return i1; // If there's an error with the first name then there's nothing to do actually. } inode_read(i1, &n); int i2; if(!(n.mode & EXT2_S_IFDIR)) { i2 = fs_open_file(newname, to_inode, O_WR, n.mode & (~EXT2_S_IFDIR)); inode_read(i1, &n); if(i2 < 0) { i2 = fs_open_file(newname, to_inode, O_WR | O_NEW , n.mode & (~EXT2_S_IFDIR)); } if(i2 < 0) { return ERR_EXISTS; } } else { i2 = fs_mkdir(newname, to_inode); if(i2 < 0) { return ERR_EXISTS; } } inode_read(i1, &n); block data; unsigned long offset = 0; while(fs_read_file(i1, (void *) &data, sizeof(block), &offset) > 0) { if(n.mode & EXT2_S_IFDIR) { int off = 0; dir_op_offset = 0; dir_entry * old_dot = NULL; dir_entry * dot = iterate_dir_entry(&data); // Iterates dir entries while (dot != NULL) { if (dot == NULL) { break; } else { if (dot->name_len > 0 && dot->inode != i1 && dot->inode != n._dir_inode && i2 != 0) { int _cp = fs_cp(dot->name, dot->name, i1, i2); if(_cp < 0) { return _cp; } off = dir_op_offset; dot->name_len = 0; dir_op_offset = off; } } old_dot = dot; dot = iterate_dir_entry(&data); } inode_read(i1, &n); } else { fs_write_file(i2, (void *)&data, sizeof(block)); } } return i2; }
// INT 80h Handler, kernel entry. void int_80() { if(krn) { return; } krn++; int systemCall = kernel_buffer[0]; int fd = kernel_buffer[1]; int buffer = kernel_buffer[2]; int count = kernel_buffer[3]; int i, j; Process * current; Process * p; int inode; int _fd; // Yeah, wanna know why we don't access an array directly? ... Because of big bugs we might have. switch(systemCall) { case READY: kernel_buffer[KERNEL_RETURN] = kernel_ready(); break; case WRITE: current = getp(); kernel_buffer[KERNEL_RETURN] = fd_write(current->file_descriptors[fd],(char *)buffer,count); break; case READ: current = getp(); kernel_buffer[KERNEL_RETURN] = fd_read(current->file_descriptors[fd],(char *)buffer,count); break; case MKFIFO: _fd = process_getfreefd(); fd = fd_open(_FD_FIFO, (void *)kernel_buffer[1],kernel_buffer[2]); if(_fd != -1 && fd != -1) { getp()->file_descriptors[_fd] = fd; kernel_buffer[KERNEL_RETURN] = _fd; } else { kernel_buffer[KERNEL_RETURN] = -1; } break; case OPEN: _fd = process_getfreefd(); fd = fd_open(_FD_FILE, (void *) kernel_buffer[1], kernel_buffer[2]); if(_fd != -1 && fd >= 0) { getp()->file_descriptors[_fd] = fd; kernel_buffer[KERNEL_RETURN] = _fd; } else { kernel_buffer[KERNEL_RETURN] = fd; } break; case CLOSE: kernel_buffer[KERNEL_RETURN] = fd_close(getp()->file_descriptors[fd]); break; case PCREATE: kernel_buffer[KERNEL_RETURN] = sched_pcreate(kernel_buffer[1],kernel_buffer[2],kernel_buffer[3]); break; case PRUN: kernel_buffer[KERNEL_RETURN] = sched_prun(kernel_buffer[1]); break; case PDUP2: kernel_buffer[KERNEL_RETURN] = sched_pdup2(kernel_buffer[1],kernel_buffer[2],kernel_buffer[3]); break; case GETPID: kernel_buffer[KERNEL_RETURN] = sched_getpid(); break; case WAITPID: kernel_buffer[KERNEL_RETURN] = sched_waitpid(kernel_buffer[1]); break; case PTICKS: kernel_buffer[KERNEL_RETURN] = (int) storage_index(); break; case PNAME: p = process_getbypid(kernel_buffer[1]); if(p == NULL) { kernel_buffer[KERNEL_RETURN] = (int) NULL; } else { kernel_buffer[KERNEL_RETURN] = (int) p->name; } break; case PSTATUS: p = process_getbypid(kernel_buffer[1]); if(p == NULL) { kernel_buffer[KERNEL_RETURN] = (int) -1; } else { kernel_buffer[KERNEL_RETURN] = (int) p->state; } break; case PPRIORITY: p = process_getbypid(kernel_buffer[1]); if(p == NULL) { kernel_buffer[KERNEL_RETURN] = (int) -1; } else { kernel_buffer[KERNEL_RETURN] = (int) p->priority; } break; case PGID: p = process_getbypid(kernel_buffer[1]); if(p == NULL) { kernel_buffer[KERNEL_RETURN] = (int) -1; } else { kernel_buffer[KERNEL_RETURN] = (int) p->gid; } break; case PGETPID_AT: p = process_getbypindex(kernel_buffer[1]); if (p->state != -1) { kernel_buffer[KERNEL_RETURN] = (int) p->pid; } else { kernel_buffer[KERNEL_RETURN] = -1; } break; case KILL: kernel_buffer[KERNEL_RETURN - 1] = kernel_buffer[1]; kernel_buffer[KERNEL_RETURN - 2] = kernel_buffer[2]; break; case PSETP: p = process_getbypid(kernel_buffer[1]); if(p == NULL) { kernel_buffer[KERNEL_RETURN] = (int) -1; } else { if(kernel_buffer[2] <= 4 && kernel_buffer[2] >= 0) { p->priority = kernel_buffer[2]; } kernel_buffer[KERNEL_RETURN] = (int) p->gid; } break; case SETSCHED: sched_set_mode(kernel_buffer[1]); break; case PWD: kernel_buffer[KERNEL_RETURN] = (int) fs_pwd(); break; case CD: kernel_buffer[KERNEL_RETURN] = (int) fs_cd(kernel_buffer[1]); break; case FINFO: fs_finfo(kernel_buffer[1], kernel_buffer[2]); break; case MOUNT: fs_init(); break; case MKDIR: kernel_buffer[KERNEL_RETURN] = (int) fs_mkdir(kernel_buffer[1],current_ttyc()->pwd); break; case RM: inode = fs_indir(kernel_buffer[1],current_ttyc()->pwd); if (inode) { kernel_buffer[KERNEL_RETURN] = (int) fs_rm(inode,0); } else { kernel_buffer[KERNEL_RETURN] = ERR_NO_EXIST; } break; case GETUID: if(kernel_buffer[1] == 0) { kernel_buffer[KERNEL_RETURN] = (int) current_ttyc()->uid; } else { kernel_buffer[KERNEL_RETURN] = (int) user_exists(kernel_buffer[1]); } break; case GETGID: if(kernel_buffer[1] == 0) { kernel_buffer[KERNEL_RETURN] = (int) user_gid(current_ttyc()->uid); } else { kernel_buffer[KERNEL_RETURN] = (int) user_gid(kernel_buffer[1]); } break; case MAKEUSER: kernel_buffer[KERNEL_RETURN] = user_create(kernel_buffer[1], kernel_buffer[2], user_gid(current_ttyc()->uid)); break; case SETGID: kernel_buffer[KERNEL_RETURN] = user_setgid(kernel_buffer[1], kernel_buffer[2]); break; case UDELETE: kernel_buffer[KERNEL_RETURN] = user_delete(kernel_buffer[1]); break; case UEXISTS: kernel_buffer[KERNEL_RETURN] = user_exists(kernel_buffer[1]); break; case ULOGIN: kernel_buffer[KERNEL_RETURN] = user_login(kernel_buffer[1], kernel_buffer[2]); break; case ULOGOUT: kernel_buffer[KERNEL_RETURN] = user_logout(); break; case CHOWN: kernel_buffer[KERNEL_RETURN] = fs_chown(kernel_buffer[1], kernel_buffer[2]); break; case CHMOD: kernel_buffer[KERNEL_RETURN] = fs_chmod(kernel_buffer[1], kernel_buffer[2]); break; case GETOWN: kernel_buffer[KERNEL_RETURN] = fs_getown(kernel_buffer[1]); break; case GETMOD: kernel_buffer[KERNEL_RETURN] = fs_getmod(kernel_buffer[1]); break; case CP: kernel_buffer[KERNEL_RETURN] = fs_cp(kernel_buffer[1], kernel_buffer[2], current_ttyc()->pwd, current_ttyc()->pwd); break; case MV: kernel_buffer[KERNEL_RETURN] = fs_mv(kernel_buffer[1], kernel_buffer[2], current_ttyc()->pwd); break; case LINK: kernel_buffer[KERNEL_RETURN] = fs_open_link(kernel_buffer[1], kernel_buffer[2], current_ttyc()->pwd); break; case FSSTAT: kernel_buffer[KERNEL_RETURN] = fs_stat(kernel_buffer[1]); break; case SLEEP: kernel_buffer[KERNEL_RETURN] = scheduler_sleep(kernel_buffer[1]); break; default: break; } krn--; }
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); // If not detached, leave the current branch by storing the current HEAD into that branch's file... 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 change to 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); // ...and setting the current branch to none (i.e., detached). write_string_to_file(".beargit/.current_branch", ""); return checkout_commit(arg); } // Read branches file (giving us the HEAD commit id for that branch). int branch_exists = (get_branch_number(arg) >= 0); // Check for errors. if (!(!branch_exists || !new_branch)) { fprintf(stderr, "ERROR: A branch named %s already exists.\n", arg); return 1; } else if (!branch_exists && !new_branch) { fprintf(stderr, "ERROR: No branch or commit %s exists.\n", arg); return 1; } // Just a better name, since we now know the argument is a branch name. const char* branch_name = arg; // File for the branch we are changing into. char branch_file[BRANCHNAME_SIZE + 50] = ".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); }
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); }
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; }
int beargit_commit(const char* msg) { if (!is_commit_msg_ok(msg)) { fprintf(stderr, "ERROR: Message must contain \"%s\"\n", go_bears); return 1; } //////////////////////// FIX char curr[BRANCHNAME_SIZE]; read_string_from_file(".beargit/.current_branch", curr, BRANCHNAME_SIZE); // if (strlen(curr) == 0) { // fprintf(stderr,"ERROR: Need to be on HEAD of a branch to commit\n"); // return 1; // } int check = 1; FILE* fbranches = fopen(".beargit/.branches", "r"); char line[FILENAME_SIZE]; while(fgets(line, sizeof(line), fbranches)) { strtok(line, "\n"); if (strcmp(line, curr) == 0) { check = 0; break; } } fclose(fbranches); if (strlen(curr) == 0 || check == 1) { fprintf(stderr,"ERROR: Need to be on HEAD of a branch to commit\n"); return 1; } char commit_id[COMMIT_ID_SIZE]; read_string_from_file(".beargit/.prev", commit_id, COMMIT_ID_SIZE); next_commit_id(commit_id); /* COMPLETE THE REST */ // beargitID char beargitID[FILENAME_SIZE]; strcpy(beargitID, ".beargit/"); strcat(beargitID, commit_id); // .beargit/commit_id char temp[FILENAME_SIZE]; strcpy(temp, beargitID); strcat(temp, "/"); // .beargit/commit_id/ fs_mkdir(beargitID); // FILE* yohoho = fopen(beargitID, "w"); char bgIndex[FILENAME_SIZE]; char bgCommitMSG[FILENAME_SIZE]; strcpy(bgIndex, beargitID); strcpy(bgCommitMSG, beargitID); strcat(bgIndex, "/.index"); // .beargit/commit_id/.prev strcat(beargitID, "/.prev"); // .beargit/commit_id/.index strcat(bgCommitMSG, "/.msg"); // .beargit/commit_id/.msg // copy from .beargit/.prev to .beargit/commit_id/.prev fs_cp(".beargit/.prev", beargitID); // copy from .beargit/.prev to .beargit/commit_id/.prev fs_cp(".beargit/.index", bgIndex); copyTo(".beargit/.index", temp); write_string_to_file(bgCommitMSG, msg); // printf(">>> msg saved as %s\n", bgCommitMSG); write_string_to_file(".beargit/.prev", commit_id); // printf(">>> id saved as %s\n", commit_id); return 0; }