void simple_log_test(void) { struct commit* commit_list = NULL; int retval; retval = beargit_init(); CU_ASSERT(0==retval); FILE* asdf = fopen("asdf.txt", "w"); fclose(asdf); retval = beargit_add("asdf.txt"); CU_ASSERT(0==retval); run_commit(&commit_list, "GO BEARS!1"); run_commit(&commit_list, "GO BEARS!2"); run_commit(&commit_list, "GO BEARS!3"); retval = beargit_log(INT_MAX); CU_ASSERT(0==retval); struct commit* cur_commit = commit_list; const int LINE_SIZE = 512; char line[LINE_SIZE]; FILE* fstdout = fopen("TEST_STDOUT", "r"); CU_ASSERT_PTR_NOT_NULL(fstdout); while (cur_commit != NULL) { char refline[LINE_SIZE]; // First line is empty CU_ASSERT_PTR_NOT_NULL(fgets(line, LINE_SIZE, fstdout)); CU_ASSERT(!strcmp(line,"\n")); // Second line is commit -- don't check the ID. CU_ASSERT_PTR_NOT_NULL(fgets(line, LINE_SIZE, fstdout)); CU_ASSERT(!strncmp(line,"commit", strlen("commit"))); // Third line is msg sprintf(refline, " %s\n", cur_commit->msg); CU_ASSERT_PTR_NOT_NULL(fgets(line, LINE_SIZE, fstdout)); CU_ASSERT_STRING_EQUAL(line, refline); cur_commit = cur_commit->next; } // Last line is empty CU_ASSERT_PTR_NOT_NULL(fgets(line, LINE_SIZE, fstdout)); CU_ASSERT(!strcmp(line,"\n")); CU_ASSERT_PTR_NULL(fgets(line, LINE_SIZE, fstdout)); // It's the end of output CU_ASSERT(feof(fstdout)); fclose(fstdout); free_commit_list(&commit_list); }
int main(int argc, char **argv) { if (argc < 2) { fprintf(stderr, "Usage: %s <command> [<args>]\n", argv[0]); return 2; } // TODO: If students aren't going to write this themselves, replace by clean // implementation using function pointers. if (strcmp(argv[1], "init") == 0) { if (check_initialized()) { fprintf(stderr, "ERROR: Repository is already initialized\n"); return 1; } return beargit_init(); } else { if (!check_initialized()) { fprintf(stderr, "ERROR: Repository is not initialized\n"); return 1; } if (strcmp(argv[1], "add") == 0 || strcmp(argv[1], "rm") == 0) { if (argc < 3 || !check_filename(argv[2])) { fprintf(stderr, "ERROR: No or invalid filename given\n"); return 1; } if (strcmp(argv[1], "rm") == 0) { return beargit_rm(argv[2]); } else { return beargit_add(argv[2]); } } else if (strcmp(argv[1], "commit") == 0) { if (argc < 4 || strcmp(argv[2], "-m") != 0) { fprintf(stderr, "ERROR: Need a commit message (-m <msg>)\n"); return 1; } if (strlen(argv[3]) > MSG_SIZE-1) { fprintf(stderr, "ERROR: Message is too long!\n"); return 1; } return beargit_commit(argv[3]); } else if (strcmp(argv[1], "status") == 0) { return beargit_status(); } else if (strcmp(argv[1], "log") == 0) { return beargit_log(); } else if (strcmp(argv[1], "branch") == 0) { return beargit_branch(); } else if (strcmp(argv[1], "checkout") == 0) { int branch_new = 0; char* arg = NULL; for (int i = 2; i < argc; i++) { if (argv[i][0] == '-') { if (strcmp(argv[i], "-b") == 0) { branch_new = 1; continue; } else { fprintf(stderr, "ERROR: Invalid argument: %s", argv[i]); return 1; } } if (arg) { fprintf(stderr, "ERROR: Too many arguments for checkout!"); return 1; } arg = argv[i]; } return beargit_checkout(arg, branch_new); } else { fprintf(stderr, "ERROR: Unknown command \"%s\"\n", argv[1]); return 1; } } }