示例#1
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;
  }

  char commit_id[COMMIT_ID_SIZE];
  read_string_from_file(".beargit/.prev", commit_id, COMMIT_ID_SIZE);
  next_commit_id(commit_id);
  //fprintf(stdout, "Before Kostya's mistake");

  /* COMPLETE THE REST */
  if(*commit_id == '\0'){
    fprintf(stderr, "ERROR:  Need to be on HEAD of a branch to commit.\n");
    return 1;
  }
  char bear_directory[BEARGIT_LENGTH + COMMIT_ID_SIZE];
  char fileNames[FILENAME_SIZE];
  sprintf(bear_directory, ".beargit/%s", commit_id);
  fs_mkdir(bear_directory);
  copy_files(".beargit/.index", bear_directory, 1);
  copy_files(".beargit/.prev", bear_directory, 1);

  FILE *tracked_files = fopen(".beargit/.index", "r");

  while(fgets(fileNames,sizeof(fileNames), tracked_files)) {
      strtok(fileNames, "\n");
      copy_files(fileNames, bear_directory, 0);
  }
  sprintf(fileNames,"%s/.msg", bear_directory);
  write_string_to_file(fileNames,msg);

  write_string_to_file(".beargit/.prev", commit_id);
  fclose(tracked_files);
  return 0;
}
示例#2
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;
}
示例#3
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;
}