int git_pkt_buffer_have(git_oid *oid, git_buf *buf) { char oidhex[GIT_OID_HEXSZ + 1]; memset(oidhex, 0x0, sizeof(oidhex)); git_oid_fmt(oidhex, oid); return git_buf_printf(buf, "%s%s\n", pkt_have_prefix, oidhex); }
void check_blame_hunk_index(git_repository *repo, git_blame *blame, int idx, int start_line, int len, char boundary, const char *commit_id, const char *orig_path) { char expected[41] = {0}, actual[41] = {0}; const git_blame_hunk *hunk = git_blame_get_hunk_byindex(blame, idx); cl_assert(hunk); if (!strncmp(commit_id, "0000", 4)) { strcpy(expected, "0000000000000000000000000000000000000000"); } else { git_object *obj; cl_git_pass(git_revparse_single(&obj, repo, commit_id)); git_oid_fmt(expected, git_object_id(obj)); git_object_free(obj); } if (hunk->final_start_line_number != start_line) { hunk_message(idx, hunk, "mismatched start line number: expected %d, got %d", start_line, hunk->final_start_line_number); } cl_assert_equal_i(hunk->final_start_line_number, start_line); if (hunk->lines_in_hunk != len) { hunk_message(idx, hunk, "mismatched line count: expected %d, got %d", len, hunk->lines_in_hunk); } cl_assert_equal_i(hunk->lines_in_hunk, len); git_oid_fmt(actual, &hunk->final_commit_id); if (strcmp(expected, actual)) { hunk_message(idx, hunk, "has mismatched original id (got %s, expected %s)\n", actual, expected); } cl_assert_equal_s(actual, expected); if (strcmp(hunk->orig_path, orig_path)) { hunk_message(idx, hunk, "has mismatched original path (got '%s', expected '%s')\n", hunk->orig_path, orig_path); } cl_assert_equal_s(hunk->orig_path, orig_path); if (hunk->boundary != boundary) { hunk_message(idx, hunk, "doesn't match boundary flag (got %d, expected %d)\n", hunk->boundary, boundary); } cl_assert_equal_i(boundary, hunk->boundary); }
static VALUE rb_git_indexentry_oid_GET(VALUE self) { git_index_entry *entry; char out[40]; Data_Get_Struct(self, git_index_entry, entry); git_oid_fmt(out, &entry->oid); return rb_str_new(out, 40); }
PyObject* Error_set_oid(int err, const git_oid *oid, size_t len) { char hex[GIT_OID_HEXSZ + 1]; git_oid_fmt(hex, oid); hex[len] = '\0'; return Error_set_str(err, hex); }
int git_revert( git_repository *repo, git_commit *commit, const git_revert_options *given_opts) { git_revert_options opts; git_reference *our_ref = NULL; git_commit *our_commit = NULL; char commit_oidstr[GIT_OID_HEXSZ + 1]; const char *commit_msg; git_buf their_label = GIT_BUF_INIT; git_index *index = NULL; git_indexwriter indexwriter = GIT_INDEXWRITER_INIT; int error; assert(repo && commit); GITERR_CHECK_VERSION(given_opts, GIT_REVERT_OPTIONS_VERSION, "git_revert_options"); if ((error = git_repository__ensure_not_bare(repo, "revert")) < 0) return error; git_oid_fmt(commit_oidstr, git_commit_id(commit)); commit_oidstr[GIT_OID_HEXSZ] = '\0'; if ((commit_msg = git_commit_summary(commit)) == NULL) { error = -1; goto on_error; } if ((error = git_buf_printf(&their_label, "parent of %.7s... %s", commit_oidstr, commit_msg)) < 0 || (error = revert_normalize_opts(repo, &opts, given_opts, git_buf_cstr(&their_label))) < 0 || (error = git_indexwriter_init_for_operation(&indexwriter, repo, &opts.checkout_opts.checkout_strategy)) < 0 || (error = write_revert_head(repo, commit_oidstr)) < 0 || (error = write_merge_msg(repo, commit_oidstr, commit_msg)) < 0 || (error = git_repository_head(&our_ref, repo)) < 0 || (error = git_reference_peel((git_object **)&our_commit, our_ref, GIT_OBJ_COMMIT)) < 0 || (error = git_revert_commit(&index, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 || (error = git_merge__check_result(repo, index)) < 0 || (error = git_merge__append_conflicts_to_merge_msg(repo, index)) < 0 || (error = git_checkout_index(repo, index, &opts.checkout_opts)) < 0 || (error = git_indexwriter_commit(&indexwriter)) < 0) goto on_error; goto done; on_error: revert_state_cleanup(repo); done: git_indexwriter_cleanup(&indexwriter); git_index_free(index); git_commit_free(our_commit); git_reference_free(our_ref); git_buf_free(&their_label); return error; }
static int show_ref__cb(git_remote_head *head, void *payload) { char oid[GIT_OID_HEXSZ + 1] = {0}; (void)payload; git_oid_fmt(oid, &head->oid); printf("%s\t%s\n", oid, head->name); return 0; }
static int update_cb(const char *refname, const git_oid *a, const git_oid *b, void *data) { char a_str[GIT_OID_HEXSZ+1], b_str[GIT_OID_HEXSZ+1]; data = data; git_oid_fmt(b_str, b); b_str[GIT_OID_HEXSZ] = '\0'; if (git_oid_iszero(a)) { printf("[new] %.20s %s\n", b_str, refname); } else { git_oid_fmt(a_str, a); a_str[GIT_OID_HEXSZ] = '\0'; printf("[updated] %.10s..%.10s %s\n", a_str, b_str, refname); } return 0; }
static VALUE rb_git_tree_entry_sha_GET(VALUE self) { rugged_tree_entry *tree_entry; char out[40]; Data_Get_Struct(self, rugged_tree_entry, tree_entry); git_oid_fmt(out, git_tree_entry_id(tree_entry->entry)); return rugged_str_new(out, 40, NULL); }
static void log_oid(request_rec *r, const char *name, const git_oid *oid) { char hash[41]; hash[40] = '\0'; git_oid_fmt(hash, oid); ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "%s: %s", name, hash); }
static int update_tips(const char *refname, const git_oid *first, const git_oid *second, void *payload) { char first_oid_s[GIT_OID_HEXSZ+1]; char second_oid_s[GIT_OID_HEXSZ+1]; char textfield_update_string[MAXLINE]; memset(textfield_update_string, 0, sizeof(textfield_update_string)); (void) payload; // not used memset(first_oid_s, 0, GIT_OID_HEXSZ+1); memset(second_oid_s, 0, GIT_OID_HEXSZ+1); git_oid_fmt(second_oid_s, second); second_oid_s[GIT_OID_HEXSZ] = '\0'; /* The first oid indicates if it's a new object or not - first oid == 0 -> second oid is a new object - first oid != 0 -> update of an old object */ if (git_oid_iszero(first)) { snprintf(textfield_update_string, sizeof(textfield_update_string), _("[new] %.20s %s"), second_oid_s, refname); write_info_msg(textfield_update_string); } else { git_oid_fmt(first_oid_s, first); first_oid_s[GIT_OID_HEXSZ] = '\0'; snprintf(textfield_update_string, sizeof(textfield_update_string), _("[updated] %.10s..%.10s %s"), first_oid_s, second_oid_s, refname); write_info_msg(textfield_update_string); } return 0; }
static int rebase_commit_merge( git_oid *commit_id, git_rebase *rebase, const git_signature *author, const git_signature *committer, const char *message_encoding, const char *message) { git_rebase_operation *operation; git_reference *head = NULL; git_commit *head_commit = NULL, *commit = NULL; git_index *index = NULL; char old_idstr[GIT_OID_HEXSZ], new_idstr[GIT_OID_HEXSZ]; int error; operation = git_array_get(rebase->operations, rebase->current); assert(operation); if ((error = rebase_ensure_not_dirty(rebase->repo, false, true, GIT_EUNMERGED)) < 0 || (error = git_repository_head(&head, rebase->repo)) < 0 || (error = git_reference_peel((git_object **)&head_commit, head, GIT_OBJ_COMMIT)) < 0 || (error = git_repository_index(&index, rebase->repo)) < 0 || (error = rebase_commit__create(&commit, rebase, index, head_commit, author, committer, message_encoding, message)) < 0 || (error = git_reference__update_for_commit( rebase->repo, NULL, "HEAD", git_commit_id(commit), "rebase")) < 0) goto done; git_oid_fmt(old_idstr, &operation->id); git_oid_fmt(new_idstr, git_commit_id(commit)); if ((error = rebase_setupfile(rebase, REWRITTEN_FILE, O_CREAT|O_WRONLY|O_APPEND, "%.*s %.*s\n", GIT_OID_HEXSZ, old_idstr, GIT_OID_HEXSZ, new_idstr)) < 0) goto done; git_oid_cpy(commit_id, git_commit_id(commit)); done: git_index_free(index); git_reference_free(head); git_commit_free(head_commit); git_commit_free(commit); return error; }
PyObject * TreeEntry_repr(TreeEntry *self) { char str[GIT_OID_HEXSZ + 1] = { 0 }; const char *typename; typename = git_object_type2string(git_tree_entry_type(self->entry)); git_oid_fmt(str, git_tree_entry_id(self->entry)); return PyString_FromFormat("pygit2.TreeEntry('%s', %s, %s)", git_tree_entry_name(self->entry), typename, str); }
static int commit_error(git_commit_list_node *commit, const char *msg) { char commit_oid[GIT_OID_HEXSZ + 1]; git_oid_fmt(commit_oid, &commit->oid); commit_oid[GIT_OID_HEXSZ] = '\0'; giterr_set(GITERR_ODB, "Failed to parse commit %s - %s", commit_oid, msg); return -1; }
int index_pack(git_repository *repo, int argc, char **argv) { git_indexer *idx; git_transfer_progress stats = {0, 0}; int error, fd; char hash[GIT_OID_HEXSZ + 1] = {0}; ssize_t read_bytes; char buf[512]; repo = repo; if (argc < 2) { fprintf(stderr, "I need a packfile\n"); return EXIT_FAILURE; } if (git_indexer_new(&idx, ".", 0, NULL, NULL, NULL) < 0) { puts("bad idx"); return -1; } if ((fd = open(argv[1], 0)) < 0) { perror("open"); return -1; } do { read_bytes = read(fd, buf, sizeof(buf)); if (read_bytes < 0) break; if ((error = git_indexer_append(idx, buf, read_bytes, &stats)) < 0) goto cleanup; index_cb(&stats, NULL); } while (read_bytes > 0); if (read_bytes < 0) { error = -1; perror("failed reading"); goto cleanup; } if ((error = git_indexer_commit(idx, &stats)) < 0) goto cleanup; printf("\rIndexing %d of %d\n", stats.indexed_objects, stats.total_objects); git_oid_fmt(hash, git_indexer_hash(idx)); puts(hash); cleanup: close(fd); git_indexer_free(idx); return error; }
static int revert_seterr(git_commit *commit, const char *fmt) { char commit_oidstr[GIT_OID_HEXSZ + 1]; git_oid_fmt(commit_oidstr, git_commit_id(commit)); commit_oidstr[GIT_OID_HEXSZ] = '\0'; giterr_set(GITERR_REVERT, fmt, commit_oidstr); return -1; }
/* Helpers */ static void test_object(const char *spec, const char *expected_oid) { char objstr[64] = {0}; cl_git_pass(git_revparse_single(&g_obj, g_repo, spec)); git_oid_fmt(objstr, git_object_id(g_obj)); cl_assert_equal_s(objstr, expected_oid); git_object_free(g_obj); g_obj = NULL; }
int index_pack(git_repository *repo, int argc, char **argv) { git_indexer_stream *idx; git_indexer_stats stats = {0, 0}; int error, fd; char hash[GIT_OID_HEXSZ + 1] = {0}; ssize_t read_bytes; char buf[512]; if (argc < 2) { fprintf(stderr, "I need a packfile\n"); return EXIT_FAILURE; } if (git_indexer_stream_new(&idx, ".git") < 0) { puts("bad idx"); return -1; } if ((fd = open(argv[1], 0)) < 0) { perror("open"); return -1; } do { read_bytes = read(fd, buf, sizeof(buf)); if (read_bytes < 0) break; if ((error = git_indexer_stream_add(idx, buf, read_bytes, &stats)) < 0) goto cleanup; printf("\rIndexing %d of %d", stats.processed, stats.total); } while (read_bytes > 0); if (read_bytes < 0) { error = -1; perror("failed reading"); goto cleanup; } if ((error = git_indexer_stream_finalize(idx, &stats)) < 0) goto cleanup; printf("\rIndexing %d of %d\n", stats.processed, stats.total); git_oid_fmt(hash, git_indexer_stream_hash(idx)); puts(hash); cleanup: close(fd); git_indexer_stream_free(idx); return error; }
static void assert_head_reflog(git_repository *repo, size_t idx, const char *old_id, const char *new_id, const char *message) { git_reflog *log; const git_reflog_entry *entry; char id_str[GIT_OID_HEXSZ + 1] = {0}; cl_git_pass(git_reflog_read(&log, repo, GIT_HEAD_FILE)); entry = git_reflog_entry_byindex(log, idx); git_oid_fmt(id_str, git_reflog_entry_id_old(entry)); cl_assert_equal_s(old_id, id_str); git_oid_fmt(id_str, git_reflog_entry_id_new(entry)); cl_assert_equal_s(new_id, id_str); cl_assert_equal_s(message, git_reflog_entry_message(entry)); git_reflog_free(log); }
static VALUE rb_git_raw_to_hex(VALUE self, VALUE raw) { git_oid oid; char out[40]; Check_Type(raw, T_STRING); git_oid_mkraw(&oid, RSTRING_PTR(raw)); git_oid_fmt(out, &oid); return rugged_str_new(out, 40, NULL); }
static int rebase_setupfiles(git_rebase *rebase) { char onto[GIT_OID_HEXSZ], orig_head[GIT_OID_HEXSZ]; git_oid_fmt(onto, &rebase->onto_id); git_oid_fmt(orig_head, &rebase->orig_head_id); if (p_mkdir(rebase->state_path, REBASE_DIR_MODE) < 0) { giterr_set(GITERR_OS, "Failed to create rebase directory '%s'", rebase->state_path); return -1; } if (git_repository__set_orig_head(rebase->repo, &rebase->orig_head_id) < 0 || rebase_setupfile(rebase, HEAD_NAME_FILE, -1, "%s\n", rebase->orig_head_name) < 0 || rebase_setupfile(rebase, ONTO_FILE, -1, "%.*s\n", GIT_OID_HEXSZ, onto) < 0 || rebase_setupfile(rebase, ORIG_HEAD_FILE, -1, "%.*s\n", GIT_OID_HEXSZ, orig_head) < 0 || rebase_setupfile(rebase, QUIET_FILE, -1, rebase->quiet ? "t\n" : "\n") < 0) return -1; return rebase_setupfiles_merge(rebase); }
int parse_pkt_line(git_repository *repo, int argc, char **argv) { char *line = argv[1]; char oid[GIT_OID_HEXSZ+1] = {0}; const char *after = line; int error; unsigned int len; git_pkt *pkt; /* * Assume that the user has piped a file in */ fseek(stdin, 0, SEEK_END); len = ftell(stdin); fseek(stdin, 0, SEEK_SET); line = malloc(len+1); if(line == NULL) return GIT_ENOMEM; memset(line, 0, len+1); fread(line, len, sizeof(char), stdin); after = line; while(*after != '\0'){ error = git_pkt_parse_line(&pkt, line, &after, len); if (error < GIT_SUCCESS) return error; line = (char *) after; switch(pkt->type){ case GIT_PKT_REF: { git_remote_head *h = &(((git_pkt_ref *)pkt)->head); const char *caps = ((git_pkt_ref *)pkt)->capabilities; //puts("Found a ref pkt"); git_oid_fmt(oid, &h->oid); printf("%s\t%s\n", oid, h->name); if(caps != NULL) printf(" Capabilities: %s\n", caps); break; } case GIT_PKT_FLUSH: puts("flush! do something!"); default: printf("default: %d\n", *after); } } return error; }
static int get_commit_index(git_oid *raw_oid) { int i; char oid[GIT_OID_HEXSZ]; git_oid_fmt(oid, raw_oid); for (i = 0; i < commit_count; ++i) if (memcmp(oid, commit_ids[i], GIT_OID_HEXSZ) == 0) return i; return -1; }
PyObject * git_oid_to_py_str(const git_oid *oid) { char hex[GIT_OID_HEXSZ]; git_oid_fmt(hex, oid); #if PY_MAJOR_VERSION == 2 return PyBytes_FromStringAndSize(hex, GIT_OID_HEXSZ); #else return to_unicode_n(hex, GIT_OID_HEXSZ, "utf-8", "strict"); #endif }
int main (int argc, char** argv) { git_repository *repo; git_index *index; unsigned int i, ecount; char *dir = "."; char out[41]; out[40] = '\0'; if (argc > 1) dir = argv[1]; if (argc > 2) { fprintf(stderr, "usage: showindex [<repo-dir>]\n"); return 1; } if (git_repository_open_ext(&repo, dir, 0, NULL) < 0) { fprintf(stderr, "could not open repository: %s\n", dir); return 1; } git_repository_index(&index, repo); git_index_read(index); ecount = git_index_entrycount(index); if (!ecount) printf("Empty index\n"); for (i = 0; i < ecount; ++i) { const git_index_entry *e = git_index_get_byindex(index, i); git_oid_fmt(out, &e->oid); printf("File Path: %s\n", e->path); printf(" Stage: %d\n", git_index_entry_stage(e)); printf(" Blob SHA: %s\n", out); printf("File Size: %d\n", (int)e->file_size); printf(" Device: %d\n", (int)e->dev); printf(" Inode: %d\n", (int)e->ino); printf(" UID: %d\n", (int)e->uid); printf(" GID: %d\n", (int)e->gid); printf(" ctime: %d\n", (int)e->ctime.seconds); printf(" mtime: %d\n", (int)e->mtime.seconds); printf("\n"); } git_index_free(index); git_repository_free(repo); return 0; }
int git_rebase_finish( git_rebase *rebase, const git_signature *signature, const git_rebase_options *given_opts) { git_rebase_options opts; git_reference *terminal_ref = NULL, *branch_ref = NULL, *head_ref = NULL; git_commit *terminal_commit = NULL; git_buf branch_msg = GIT_BUF_INIT, head_msg = GIT_BUF_INIT; char onto[GIT_OID_HEXSZ]; int error; assert(rebase); GITERR_CHECK_VERSION(given_opts, GIT_REBASE_OPTIONS_VERSION, "git_rebase_options"); if ((error = rebase_normalize_opts(rebase->repo, &opts, given_opts)) < 0) goto done; git_oid_fmt(onto, &rebase->onto_id); if ((error = git_buf_printf(&branch_msg, "rebase finished: %s onto %.*s", rebase->orig_head_name, GIT_OID_HEXSZ, onto)) < 0 || (error = git_buf_printf(&head_msg, "rebase finished: returning to %s", rebase->orig_head_name)) < 0 || (error = git_repository_head(&terminal_ref, rebase->repo)) < 0 || (error = git_reference_peel((git_object **)&terminal_commit, terminal_ref, GIT_OBJ_COMMIT)) < 0 || (error = git_reference_create_matching(&branch_ref, rebase->repo, rebase->orig_head_name, git_commit_id(terminal_commit), 1, &rebase->orig_head_id, branch_msg.ptr)) < 0 || (error = git_reference_symbolic_create(&head_ref, rebase->repo, GIT_HEAD_FILE, rebase->orig_head_name, 1, head_msg.ptr)) < 0 || (error = rebase_copy_notes(rebase, signature, &opts)) < 0) goto done; error = rebase_cleanup(rebase); done: git_buf_free(&head_msg); git_buf_free(&branch_msg); git_commit_free(terminal_commit); git_reference_free(head_ref); git_reference_free(branch_ref); git_reference_free(terminal_ref); rebase_opts_free(&opts); return error; }
static int buffer_want_with_caps(const git_remote_head *head, transport_smart_caps *caps, git_buf *buf) { git_buf str = GIT_BUF_INIT; char oid[GIT_OID_HEXSZ +1] = {0}; size_t len; /* Prefer multi_ack_detailed */ if (caps->multi_ack_detailed) git_buf_puts(&str, GIT_CAP_MULTI_ACK_DETAILED " "); else if (caps->multi_ack) git_buf_puts(&str, GIT_CAP_MULTI_ACK " "); /* Prefer side-band-64k if the server supports both */ if (caps->side_band_64k) git_buf_printf(&str, "%s ", GIT_CAP_SIDE_BAND_64K); else if (caps->side_band) git_buf_printf(&str, "%s ", GIT_CAP_SIDE_BAND); if (caps->include_tag) git_buf_puts(&str, GIT_CAP_INCLUDE_TAG " "); if (caps->thin_pack) git_buf_puts(&str, GIT_CAP_THIN_PACK " "); if (caps->ofs_delta) git_buf_puts(&str, GIT_CAP_OFS_DELTA " "); if (git_buf_oom(&str)) return -1; len = strlen("XXXXwant ") + GIT_OID_HEXSZ + 1 /* NUL */ + git_buf_len(&str) + 1 /* LF */; if (len > 0xffff) { giterr_set(GITERR_NET, "tried to produce packet with invalid length %" PRIuZ, len); return -1; } git_buf_grow_by(buf, len); git_oid_fmt(oid, &head->oid); git_buf_printf(buf, "%04xwant %s %s\n", (unsigned int)len, oid, git_buf_cstr(&str)); git_buf_dispose(&str); GITERR_CHECK_ALLOC_BUF(buf); return 0; }
/* * call-seq: * Rugged.raw_to_hex(buffer) -> hex_oid * * Turn a buffer of 20 bytes (representing a SHA1 OID) into its * readable hexadecimal representation. * * Rugged.raw_to_hex("\330xk\374\227H^\215{\031\262\037\270\214\216\361\361\231\374?") * #=> "d8786bfc97485e8d7b19b21fb88c8ef1f199fc3f" */ static VALUE rb_git_raw_to_hex(VALUE self, VALUE raw) { git_oid oid; char out[40]; Check_Type(raw, T_STRING); if (RSTRING_LEN(raw) != GIT_OID_RAWSZ) rb_raise(rb_eTypeError, "Invalid buffer size for an OID"); git_oid_fromraw(&oid, (const unsigned char *)RSTRING_PTR(raw)); git_oid_fmt(out, &oid); return rb_str_new(out, 40); }
static int peel_error(int error, const git_oid *oid, git_otype type) { const char *type_name; char hex_oid[GIT_OID_HEXSZ + 1]; type_name = git_object_type2string(type); git_oid_fmt(hex_oid, oid); hex_oid[GIT_OID_HEXSZ] = '\0'; giterr_set(GITERR_OBJECT, "The git_object of id '%s' can not be " "successfully peeled into a %s (git_otype=%i).", hex_oid, type_name, type); return error; }
void test_reset_soft__resetting_to_the_commit_pointed_at_by_the_Head_does_not_change_the_target_of_the_Head(void) { git_oid oid; char raw_head_oid[GIT_OID_HEXSZ + 1]; cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); git_oid_fmt(raw_head_oid, &oid); raw_head_oid[GIT_OID_HEXSZ] = '\0'; retrieve_target_from_oid(&target, repo, raw_head_oid); cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); cl_git_pass(git_oid_streq(&oid, raw_head_oid)); }
/* Helpers */ static void test_object_inrepo(const char *spec, const char *expected_oid, git_repository *repo) { char objstr[64] = {0}; git_object *obj = NULL; int error; error = git_revparse_single(&obj, repo, spec); if (expected_oid != NULL) { cl_assert_equal_i(0, error); git_oid_fmt(objstr, git_object_id(obj)); cl_assert_equal_s(objstr, expected_oid); } else cl_assert_equal_i(GIT_ENOTFOUND, error); git_object_free(obj); }