void test_refs_branches_move__can_not_move_a_branch_if_its_destination_name_collide_with_an_existing_one(void) { git_reference *original_ref, *new_ref; git_config *config; git_buf buf = GIT_BUF_INIT; char *original_remote, *original_merge; const char *str; cl_git_pass(git_repository_config_snapshot(&config, repo)); cl_git_pass(git_config_get_string_buf(&buf, config, "branch.master.remote")); original_remote = git_buf_detach(&buf); cl_git_pass(git_config_get_string_buf(&buf, config, "branch.master.merge")); original_merge = git_buf_detach(&buf); git_config_free(config); cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2")); cl_assert_equal_i(GIT_EEXISTS, git_branch_move(&new_ref, original_ref, "master", 0)); cl_assert(giterr_last()->message != NULL); cl_git_pass(git_repository_config_snapshot(&config, repo)); cl_git_pass(git_config_get_string(&str, config, "branch.master.remote")); cl_assert_equal_s(original_remote, str); cl_git_pass(git_config_get_string(&str, config, "branch.master.merge")); cl_assert_equal_s(original_merge, str); git_config_free(config); cl_assert_equal_i(GIT_EEXISTS, git_branch_move(&new_ref, original_ref, "cannot-fetch", 0)); cl_assert(giterr_last()->message != NULL); cl_git_pass(git_repository_config_snapshot(&config, repo)); cl_git_pass(git_config_get_string(&str, config, "branch.master.remote")); cl_assert_equal_s(original_remote, str); cl_git_pass(git_config_get_string(&str, config, "branch.master.merge")); cl_assert_equal_s(original_merge, str); git_config_free(config); git_reference_free(original_ref); cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/track-local")); cl_assert_equal_i(GIT_EEXISTS, git_branch_move(&new_ref, original_ref, "master", 0)); cl_assert(giterr_last()->message != NULL); cl_git_pass(git_repository_config_snapshot(&config, repo)); cl_git_pass(git_config_get_string(&str, config, "branch.master.remote")); cl_assert_equal_s(original_remote, str); cl_git_pass(git_config_get_string(&str, config, "branch.master.merge")); cl_assert_equal_s(original_merge, str); git__free(original_remote); git__free(original_merge); git_reference_free(original_ref); git_config_free(config); }
static int load_workdir(git_repository *repo, git_buf *parent_path) { int error; git_config *config; const char *worktree; git_buf worktree_buf = GIT_BUF_INIT; if (repo->is_bare) return 0; if (git_repository_config__weakptr(&config, repo) < 0) return -1; error = git_config_get_string(&worktree, config, "core.worktree"); if (!error && worktree != NULL) repo->workdir = git__strdup(worktree); else if (error != GIT_ENOTFOUND) return error; else { giterr_clear(); if (parent_path && git_path_isdir(parent_path->ptr)) repo->workdir = git_buf_detach(parent_path); else { git_path_dirname_r(&worktree_buf, repo->path_repository); git_path_to_dir(&worktree_buf); repo->workdir = git_buf_detach(&worktree_buf); } } GITERR_CHECK_ALLOC(repo->workdir); return 0; }
static int load_workdir(git_repository *repo, git_config *config, git_buf *parent_path) { int error; const git_config_entry *ce; git_buf worktree = GIT_BUF_INIT; if (repo->is_bare) return 0; if ((error = git_config__lookup_entry( &ce, config, "core.worktree", false)) < 0) return error; if (ce && ce->value) { if ((error = git_path_prettify_dir( &worktree, ce->value, repo->path_repository)) < 0) return error; repo->workdir = git_buf_detach(&worktree); } else if (parent_path && git_path_isdir(parent_path->ptr)) repo->workdir = git_buf_detach(parent_path); else { if (git_path_dirname_r(&worktree, repo->path_repository) < 0 || git_path_to_dir(&worktree) < 0) return -1; repo->workdir = git_buf_detach(&worktree); } GITERR_CHECK_ALLOC(repo->workdir); return 0; }
static int attr_cache__lookup_path( char **out, git_config *cfg, const char *key, const char *fallback) { git_buf buf = GIT_BUF_INIT; int error; git_config_entry *entry = NULL; *out = NULL; if ((error = git_config__lookup_entry(&entry, cfg, key, false)) < 0) return error; if (entry) { const char *cfgval = entry->value; /* expand leading ~/ as needed */ if (cfgval && cfgval[0] == '~' && cfgval[1] == '/' && !git_sysdir_find_global_file(&buf, &cfgval[2])) *out = git_buf_detach(&buf); else if (cfgval) *out = git__strdup(cfgval); } else if (!git_sysdir_find_xdg_file(&buf, fallback)) *out = git_buf_detach(&buf); git_config_entry_free(entry); git_buf_free(&buf); return error; }
static int rebase_init( git_rebase *rebase, git_repository *repo, const git_annotated_commit *branch, const git_annotated_commit *upstream, const git_annotated_commit *onto, const git_rebase_options *opts) { git_buf state_path = GIT_BUF_INIT; int error; if ((error = git_buf_joinpath(&state_path, repo->path_repository, REBASE_MERGE_DIR)) < 0) return error; rebase->repo = repo; rebase->type = GIT_REBASE_TYPE_MERGE; rebase->state_path = git_buf_detach(&state_path); rebase->orig_head_name = git__strdup(branch->ref_name ? branch->ref_name : ORIG_DETACHED_HEAD); rebase->quiet = opts->quiet; git_oid_cpy(&rebase->orig_head_id, git_annotated_commit_id(branch)); git_oid_cpy(&rebase->onto_id, git_annotated_commit_id(onto)); if (!rebase->orig_head_name || !rebase->state_path) return -1; error = rebase_init_merge(rebase, repo, branch, upstream, onto); git_buf_free(&state_path); return error; }
int git_repository_open_ext( git_repository **repo_ptr, const char *start_path, uint32_t flags, const char *ceiling_dirs) { int error; git_buf path = GIT_BUF_INIT, parent = GIT_BUF_INIT; git_repository *repo; *repo_ptr = NULL; if ((error = find_repo(&path, &parent, start_path, flags, ceiling_dirs)) < 0) return error; repo = repository_alloc(); GITERR_CHECK_ALLOC(repo); repo->path_repository = git_buf_detach(&path); GITERR_CHECK_ALLOC(repo->path_repository); if ((error = load_config_data(repo)) < 0 || (error = load_workdir(repo, &parent)) < 0) { git_repository_free(repo); return error; } git_buf_free(&parent); *repo_ptr = repo; return 0; }
const char *git_commit_summary(git_commit *commit) { git_buf summary = GIT_BUF_INIT; const char *msg, *space; assert(commit); if (!commit->summary) { for (msg = git_commit_message(commit), space = NULL; *msg; ++msg) { if (msg[0] == '\n' && (!msg[1] || msg[1] == '\n')) break; else if (msg[0] == '\n') git_buf_putc(&summary, ' '); else if (git__isspace(msg[0])) space = space ? space : msg; else if (space) { git_buf_put(&summary, space, (msg - space) + 1); space = NULL; } else git_buf_putc(&summary, *msg); } commit->summary = git_buf_detach(&summary); if (!commit->summary) commit->summary = git__strdup(""); } return commit->summary; }
int git_error_state_capture(git_error_state *state, int error_code) { git_error *error = GIT_GLOBAL->last_error; git_buf *error_buf = &GIT_GLOBAL->error_buf; memset(state, 0, sizeof(git_error_state)); if (!error_code) return 0; state->error_code = error_code; state->oom = (error == &g_git_oom_error); if (error) { state->error_msg.klass = error->klass; if (state->oom) state->error_msg.message = g_git_oom_error.message; else state->error_msg.message = git_buf_detach(error_buf); } git_error_clear(); return error_code; }
int git_repository_open_bare( git_repository **repo_ptr, const char *bare_path) { int error; git_buf path = GIT_BUF_INIT; git_repository *repo = NULL; if ((error = git_path_prettify_dir(&path, bare_path, NULL)) < 0) return error; if (!valid_repository_path(&path)) { git_buf_free(&path); giterr_set(GITERR_REPOSITORY, "Path is not a repository: %s", bare_path); return GIT_ENOTFOUND; } repo = repository_alloc(); GITERR_CHECK_ALLOC(repo); repo->path_repository = git_buf_detach(&path); GITERR_CHECK_ALLOC(repo->path_repository); /* of course we're bare! */ repo->is_bare = 1; repo->workdir = NULL; *repo_ptr = repo; return 0; }
int git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir) { int error = 0; struct pack_backend *backend = NULL; git_buf path = GIT_BUF_INIT; if (git_mwindow_files_init() < 0) return -1; if (pack_backend__alloc(&backend, 8) < 0) return -1; if (!(error = git_buf_joinpath(&path, objects_dir, "pack")) && git_path_isdir(git_buf_cstr(&path))) { backend->pack_folder = git_buf_detach(&path); error = pack_backend__refresh((git_odb_backend *)backend); } if (error < 0) { pack_backend__free((git_odb_backend *)backend); backend = NULL; } *backend_out = (git_odb_backend *)backend; git_buf_free(&path); return error; }
/* what is the common non-wildcard prefix for all items in the pathspec */ char *git_pathspec_prefix(const git_strarray *pathspec) { git_buf prefix = GIT_BUF_INIT; const char *scan; if (!pathspec || !pathspec->count || git_buf_text_common_prefix(&prefix, pathspec) < 0) return NULL; /* diff prefix will only be leading non-wildcards */ for (scan = prefix.ptr; *scan; ++scan) { if (git__iswildcard(*scan) && (scan == prefix.ptr || (*(scan - 1) != '\\'))) break; } git_buf_truncate(&prefix, scan - prefix.ptr); if (prefix.size <= 0) { git_buf_free(&prefix); return NULL; } git_buf_text_unescape(&prefix); return git_buf_detach(&prefix); }
static char *get_filename(const char *in) { char *search_dirname, *search_filename, *filename = NULL; git_buf out = GIT_BUF_INIT; DIR *dir; struct dirent *de; cl_assert(search_dirname = git_path_dirname(in)); cl_assert(search_filename = git_path_basename(in)); cl_assert(dir = opendir(search_dirname)); while ((de = readdir(dir))) { if (strcasecmp(de->d_name, search_filename) == 0) { git_buf_join(&out, '/', search_dirname, de->d_name); filename = git_buf_detach(&out); break; } } closedir(dir); git__free(search_dirname); git__free(search_filename); git_buf_dispose(&out); return filename; }
static int rebase_state_type( git_rebase_type_t *type_out, char **path_out, git_repository *repo) { git_buf path = GIT_BUF_INIT; git_rebase_type_t type = GIT_REBASE_TYPE_NONE; if (git_buf_joinpath(&path, repo->path_repository, REBASE_APPLY_DIR) < 0) return -1; if (git_path_isdir(git_buf_cstr(&path))) { type = GIT_REBASE_TYPE_APPLY; goto done; } git_buf_clear(&path); if (git_buf_joinpath(&path, repo->path_repository, REBASE_MERGE_DIR) < 0) return -1; if (git_path_isdir(git_buf_cstr(&path))) { type = GIT_REBASE_TYPE_MERGE; goto done; } done: *type_out = type; if (type != GIT_REBASE_TYPE_NONE && path_out) *path_out = git_buf_detach(&path); git_buf_free(&path); return 0; }
static int rebase_open_merge(git_rebase *rebase) { git_buf state_path = GIT_BUF_INIT, buf = GIT_BUF_INIT, cmt = GIT_BUF_INIT; git_oid id; git_rebase_operation *operation; size_t i, msgnum = 0, end; int error; if ((error = git_buf_puts(&state_path, rebase->state_path)) < 0) goto done; /* Read 'msgnum' if it exists (otherwise, let msgnum = 0) */ if ((error = rebase_readint(&msgnum, &buf, &state_path, MSGNUM_FILE)) < 0 && error != GIT_ENOTFOUND) goto done; if (msgnum) { rebase->started = 1; rebase->current = msgnum - 1; } /* Read 'end' */ if ((error = rebase_readint(&end, &buf, &state_path, END_FILE)) < 0) goto done; /* Read 'current' if it exists */ if ((error = rebase_readoid(&id, &buf, &state_path, CURRENT_FILE)) < 0 && error != GIT_ENOTFOUND) goto done; /* Read cmt.* */ git_array_init_to_size(rebase->operations, end); GITERR_CHECK_ARRAY(rebase->operations); for (i = 0; i < end; i++) { git_buf_clear(&cmt); if ((error = git_buf_printf(&cmt, "cmt.%" PRIuZ, (i+1))) < 0 || (error = rebase_readoid(&id, &buf, &state_path, cmt.ptr)) < 0) goto done; operation = rebase_operation_alloc(rebase, GIT_REBASE_OPERATION_PICK, &id, NULL); GITERR_CHECK_ALLOC(operation); } /* Read 'onto_name' */ if ((error = rebase_readfile(&buf, &state_path, ONTO_NAME_FILE)) < 0) goto done; rebase->onto_name = git_buf_detach(&buf); done: git_buf_free(&cmt); git_buf_free(&state_path); git_buf_free(&buf); return error; }
static int diff_file_content_load_workdir_file( git_diff_file_content *fc, git_buf *path) { int error = 0; git_vector filters = GIT_VECTOR_INIT; git_buf raw = GIT_BUF_INIT, filtered = GIT_BUF_INIT; git_file fd = git_futils_open_ro(git_buf_cstr(path)); if (fd < 0) return fd; if (!fc->file->size && !(fc->file->size = git_futils_filesize(fd))) goto cleanup; if (diff_file_content_binary_by_size(fc)) goto cleanup; if ((error = git_filters_load( &filters, fc->repo, fc->file->path, GIT_FILTER_TO_ODB)) < 0) goto cleanup; /* error >= is a filter count */ if (error == 0) { if (!(error = git_futils_mmap_ro( &fc->map, fd, 0, (size_t)fc->file->size))) fc->flags |= GIT_DIFF_FLAG__UNMAP_DATA; else /* fall through to try readbuffer below */ giterr_clear(); } if (error != 0) { error = git_futils_readbuffer_fd(&raw, fd, (size_t)fc->file->size); if (error < 0) goto cleanup; if (!filters.length) git_buf_swap(&filtered, &raw); else error = git_filters_apply(&filtered, &raw, &filters); if (!error) { fc->map.len = git_buf_len(&filtered); fc->map.data = git_buf_detach(&filtered); fc->flags |= GIT_DIFF_FLAG__FREE_DATA; } git_buf_free(&raw); git_buf_free(&filtered); } cleanup: git_filters_free(&filters); p_close(fd); return error; }
static int parse_header_path(char **out, git_patch_parse_ctx *ctx) { git_buf path = GIT_BUF_INIT; int error = parse_header_path_buf(&path, ctx, header_path_len(ctx)); *out = git_buf_detach(&path); return error; }
static int rebase_init_merge( git_rebase *rebase, git_repository *repo, const git_annotated_commit *branch, const git_annotated_commit *upstream, const git_annotated_commit *onto) { git_reference *head_ref = NULL; git_commit *onto_commit = NULL; git_buf reflog = GIT_BUF_INIT; git_buf state_path = GIT_BUF_INIT; int error; GIT_UNUSED(upstream); if ((error = git_buf_joinpath(&state_path, repo->path_repository, REBASE_MERGE_DIR)) < 0) goto done; rebase->state_path = git_buf_detach(&state_path); GITERR_CHECK_ALLOC(rebase->state_path); if (branch->ref_name) { rebase->orig_head_name = git__strdup(branch->ref_name); GITERR_CHECK_ALLOC(rebase->orig_head_name); } else { rebase->head_detached = 1; } rebase->onto_name = git__strdup(rebase_onto_name(onto)); GITERR_CHECK_ALLOC(rebase->onto_name); rebase->quiet = rebase->options.quiet; git_oid_cpy(&rebase->orig_head_id, git_annotated_commit_id(branch)); git_oid_cpy(&rebase->onto_id, git_annotated_commit_id(onto)); if ((error = rebase_setupfiles(rebase)) < 0 || (error = git_buf_printf(&reflog, "rebase: checkout %s", rebase_onto_name(onto))) < 0 || (error = git_commit_lookup( &onto_commit, repo, git_annotated_commit_id(onto))) < 0 || (error = git_checkout_tree(repo, (git_object *)onto_commit, &rebase->options.checkout_options)) < 0 || (error = git_reference_create(&head_ref, repo, GIT_HEAD_FILE, git_annotated_commit_id(onto), 1, reflog.ptr)) < 0) goto done; done: git_reference_free(head_ref); git_commit_free(onto_commit); git_buf_free(&reflog); git_buf_free(&state_path); return error; }
char *git_path_basename(const char *path) { git_buf buf = GIT_BUF_INIT; char *basename; git_path_basename_r(&buf, path); basename = git_buf_detach(&buf); git_buf_free(&buf); /* avoid memleak if error occurs */ return basename; }
/* test detach/attach data */ void test_core_buffer__7(void) { const char *fun = "This is fun"; git_buf a = GIT_BUF_INIT; char *b = NULL; git_buf_sets(&a, "foo"); cl_assert(git_buf_oom(&a) == 0); cl_assert_equal_s("foo", git_buf_cstr(&a)); b = git_buf_detach(&a); cl_assert_equal_s("foo", b); cl_assert_equal_s("", a.ptr); git__free(b); b = git_buf_detach(&a); cl_assert_equal_s(NULL, b); cl_assert_equal_s("", a.ptr); git_buf_free(&a); b = git__strdup(fun); git_buf_attach(&a, b, 0); cl_assert_equal_s(fun, a.ptr); cl_assert(a.size == strlen(fun)); cl_assert(a.asize == strlen(fun) + 1); git_buf_free(&a); b = git__strdup(fun); git_buf_attach(&a, b, strlen(fun) + 1); cl_assert_equal_s(fun, a.ptr); cl_assert(a.size == strlen(fun)); cl_assert(a.asize == strlen(fun) + 1); git_buf_free(&a); }
static char *get_filename(const char *in) { #ifdef GIT_WIN32 HANDLE fh; HMODULE kerneldll; char *filename; typedef DWORD (__stdcall *getfinalpathname)(HANDLE, LPSTR, DWORD, DWORD); getfinalpathname getfinalpathfn; cl_assert(filename = malloc(MAX_PATH)); cl_assert(kerneldll = LoadLibrary("kernel32.dll")); cl_assert(getfinalpathfn = (getfinalpathname)GetProcAddress(kerneldll, "GetFinalPathNameByHandleA")); cl_assert(fh = CreateFileA(in, FILE_READ_ATTRIBUTES | STANDARD_RIGHTS_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL)); cl_win32_pass(getfinalpathfn(fh, filename, MAX_PATH, VOLUME_NAME_DOS)); CloseHandle(fh); git_path_mkposix(filename); return filename; #else char *search_dirname, *search_filename, *filename = NULL; git_buf out = GIT_BUF_INIT; DIR *dir; struct dirent *de; cl_assert(search_dirname = git_path_dirname(in)); cl_assert(search_filename = git_path_basename(in)); cl_assert(dir = opendir(search_dirname)); while ((de = readdir(dir))) { if (strcasecmp(de->d_name, search_filename) == 0) { git_buf_join(&out, '/', search_dirname, de->d_name); filename = git_buf_detach(&out); break; } } closedir(dir); git__free(search_dirname); git__free(search_filename); git_buf_free(&out); return filename; #endif }
static int diff_file_content_commit_to_str( git_diff_file_content *fc, bool check_status) { char oid[GIT_OID_HEXSZ+1]; git_buf content = GIT_BUF_INIT; const char *status = ""; if (check_status) { int error = 0; git_submodule *sm = NULL; unsigned int sm_status = 0; const git_oid *sm_head; if ((error = git_submodule_lookup(&sm, fc->repo, fc->file->path)) < 0) { /* GIT_EEXISTS means a "submodule" that has not been git added */ if (error == GIT_EEXISTS) { giterr_clear(); error = 0; } return error; } if ((error = git_submodule_status(&sm_status, fc->repo, fc->file->path, GIT_SUBMODULE_IGNORE_UNSPECIFIED)) < 0) { git_submodule_free(sm); return error; } /* update OID if we didn't have it previously */ if ((fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0 && ((sm_head = git_submodule_wd_id(sm)) != NULL || (sm_head = git_submodule_head_id(sm)) != NULL)) { git_oid_cpy(&fc->file->id, sm_head); fc->file->flags |= GIT_DIFF_FLAG_VALID_ID; } if (GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status)) status = "-dirty"; git_submodule_free(sm); } git_oid_tostr(oid, sizeof(oid), &fc->file->id); if (git_buf_printf(&content, "Subproject commit %s%s\n", oid, status) < 0) return -1; fc->map.len = git_buf_len(&content); fc->map.data = git_buf_detach(&content); fc->flags |= GIT_DIFF_FLAG__FREE_DATA; return 0; }
static int parse_variable(git_config_parser *reader, char **var_name, char **var_value) { const char *value_start = NULL; char *line; int quote_count; bool multiline; git_parse_advance_ws(&reader->ctx); line = git__strndup(reader->ctx.line, reader->ctx.line_len); if (line == NULL) return -1; quote_count = strip_comments(line, 0); /* If there is no value, boolean true is assumed */ *var_value = NULL; if (parse_name(var_name, &value_start, reader, line) < 0) goto on_error; /* * Now, let's try to parse the value */ if (value_start != NULL) { while (git__isspace(value_start[0])) value_start++; if (unescape_line(var_value, &multiline, value_start, 0) < 0) goto on_error; if (multiline) { git_buf multi_value = GIT_BUF_INIT; git_buf_attach(&multi_value, *var_value, 0); if (parse_multiline_variable(reader, &multi_value, quote_count) < 0 || git_buf_oom(&multi_value)) { git_buf_free(&multi_value); goto on_error; } *var_value = git_buf_detach(&multi_value); } } git__free(line); return 0; on_error: git__free(*var_name); git__free(line); return -1; }
static int parse_header_git_oldpath( git_patch_parsed *patch, git_patch_parse_ctx *ctx) { git_buf old_path = GIT_BUF_INIT; int error; if ((error = parse_header_path_buf(&old_path, ctx, ctx->parse_ctx.line_len - 1)) < 0) goto out; patch->old_path = git_buf_detach(&old_path); out: git_buf_dispose(&old_path); return error; }
int git_repository_set_workdir(git_repository *repo, const char *workdir) { git_buf path = GIT_BUF_INIT; assert(repo && workdir); if (git_path_prettify_dir(&path, workdir, NULL) < 0) return -1; git__free(repo->workdir); repo->workdir = git_buf_detach(&path); repo->is_bare = 0; return 0; }
static int parse_header_rename( char **out, git_patch_parse_ctx *ctx) { git_buf path = GIT_BUF_INIT; if (parse_header_path_buf(&path, ctx, header_path_len(ctx)) < 0) return -1; /* Note: the `rename from` and `rename to` lines include the literal * filename. They do *not* include the prefix. (Who needs consistency?) */ *out = git_buf_detach(&path); return 0; }
static int diff_file_content_load_workdir_symlink_fake( git_diff_file_content *fc, git_buf *path) { git_buf target = GIT_BUF_INIT; int error; if ((error = git_futils_readbuffer(&target, path->ptr)) < 0) return error; fc->map.len = git_buf_len(&target); fc->map.data = git_buf_detach(&target); fc->flags |= GIT_DIFF_FLAG__FREE_DATA; git_buf_dispose(&target); return error; }
/* * At one point, there was a loose object format that was intended to * mimic the format used in pack-files. This was to allow easy copying * of loose object data into packs. This format is no longer used, but * we must still read it. */ static int read_loose_packlike(git_rawobj *out, git_buf *obj) { git_buf body = GIT_BUF_INIT; const unsigned char *obj_data; obj_hdr hdr; size_t obj_len, head_len, alloc_size; int error; obj_data = (unsigned char *)obj->ptr; obj_len = obj->size; /* * read the object header, which is an (uncompressed) * binary encoding of the object type and size. */ if ((error = parse_header_packlike(&hdr, &head_len, obj_data, obj_len)) < 0) goto done; if (!git_object_typeisloose(hdr.type) || head_len > obj_len) { giterr_set(GITERR_ODB, "failed to inflate loose object"); error = -1; goto done; } obj_data += head_len; obj_len -= head_len; /* * allocate a buffer and inflate the data into it */ if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, hdr.size, 1) || git_buf_init(&body, alloc_size) < 0) { error = -1; goto done; } if ((error = git_zstream_inflatebuf(&body, obj_data, obj_len)) < 0) goto done; out->len = hdr.size; out->type = hdr.type; out->data = git_buf_detach(&body); done: git_buf_dispose(&body); return error; }
void cl_fake_home(void) { git_buf path = GIT_BUF_INIT; cl_git_pass(git_libgit2_opts( GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &path)); _cl_restore_home = git_buf_detach(&path); cl_set_cleanup(cl_fake_home_cleanup, NULL); if (!git_path_exists("home")) cl_must_pass(p_mkdir("home", 0777)); cl_git_pass(git_path_prettify(&path, "home", NULL)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); git_buf_free(&path); }
static int read_variable_cb( git_config_parser *reader, const char *current_section, const char *var_name, const char *var_value, const char *line, size_t line_len, void *payload) { config_memory_parse_data *parse_data = (config_memory_parse_data *) payload; git_buf buf = GIT_BUF_INIT; git_config_entry *entry; const char *c; int result; GIT_UNUSED(reader); GIT_UNUSED(line); GIT_UNUSED(line_len); if (current_section) { /* TODO: Once warnings land, we should likely warn * here. Git appears to warn in most cases if it sees * un-namespaced config options. */ git_buf_puts(&buf, current_section); git_buf_putc(&buf, '.'); } for (c = var_name; *c; c++) git_buf_putc(&buf, git__tolower(*c)); if (git_buf_oom(&buf)) return -1; entry = git__calloc(1, sizeof(git_config_entry)); GITERR_CHECK_ALLOC(entry); entry->name = git_buf_detach(&buf); entry->value = var_value ? git__strdup(var_value) : NULL; entry->level = parse_data->level; entry->include_depth = 0; if ((result = git_config_entries_append(parse_data->entries, entry)) < 0) return result; return result; }
const char *git_commit_summary(git_commit *commit) { git_buf summary = GIT_BUF_INIT; const char *msg, *space; bool space_contains_newline = false; assert(commit); if (!commit->summary) { for (msg = git_commit_message(commit), space = NULL; *msg; ++msg) { char next_character = msg[0]; /* stop processing at the end of the first paragraph */ if (next_character == '\n' && (!msg[1] || msg[1] == '\n')) break; /* record the beginning of contiguous whitespace runs */ else if (git__isspace(next_character)) { if(space == NULL) { space = msg; space_contains_newline = false; } space_contains_newline |= next_character == '\n'; } /* the next character is non-space */ else { /* process any recorded whitespace */ if (space) { if(space_contains_newline) git_buf_putc(&summary, ' '); /* if the space contains a newline, collapse to ' ' */ else git_buf_put(&summary, space, (msg - space)); /* otherwise copy it */ space = NULL; } /* copy the next character */ git_buf_putc(&summary, next_character); } } commit->summary = git_buf_detach(&summary); if (!commit->summary) commit->summary = git__strdup(""); } return commit->summary; }