int git_reference__read_head( git_reference **out, git_repository *repo, const char *path) { git_buf reference = GIT_BUF_INIT; char *name = NULL; int error; if ((error = git_futils_readbuffer(&reference, path)) < 0) goto out; git_buf_rtrim(&reference); if (git__strncmp(reference.ptr, GIT_SYMREF, strlen(GIT_SYMREF)) == 0) { git_buf_consume(&reference, reference.ptr + strlen(GIT_SYMREF)); name = git_path_basename(path); if ((*out = git_reference__alloc_symbolic(name, reference.ptr)) == NULL) { error = -1; goto out; } } else { if ((error = git_reference_lookup(out, repo, reference.ptr)) < 0) goto out; } out: git__free(name); git_buf_free(&reference); return error; }
static int diff_context_line__pattern_match( git_diff_driver *driver, git_buf *line) { size_t i, maxi = git_array_size(driver->fn_patterns); regmatch_t pmatch[2]; for (i = 0; i < maxi; ++i) { git_diff_driver_pattern *pat = git_array_get(driver->fn_patterns, i); if (!regexec(&pat->re, line->ptr, 2, pmatch, 0)) { if (pat->flags & REG_NEGATE) return false; /* use pmatch data to trim line data */ i = (pmatch[1].rm_so >= 0) ? 1 : 0; git_buf_consume(line, git_buf_cstr(line) + pmatch[i].rm_so); git_buf_truncate(line, pmatch[i].rm_eo - pmatch[i].rm_so); git_buf_rtrim(line); return true; } } return false; }
static int on_body_parse_response(http_parser *parser, const char *str, size_t len) { transport_http *t = (transport_http *) parser->data; git_buf *buf = &t->buf; git_vector *common = &t->common; int error; const char *line_end, *ptr; if (len == 0) { /* EOF */ if (git_buf_len(buf) != 0) { giterr_set(GITERR_NET, "Unexpected EOF"); return t->error = -1; } else { return 0; } } git_buf_put(buf, str, len); ptr = buf->ptr; while (1) { git_pkt *pkt; if (git_buf_len(buf) == 0) return 0; error = git_pkt_parse_line(&pkt, ptr, &line_end, git_buf_len(buf)); if (error == GIT_EBUFS) { return 0; /* Ask for more */ } if (error < 0) return t->error = -1; git_buf_consume(buf, line_end); if (pkt->type == GIT_PKT_PACK) { git__free(pkt); t->pack_ready = 1; return 0; } if (pkt->type == GIT_PKT_NAK) { git__free(pkt); return 0; } if (pkt->type != GIT_PKT_ACK) { git__free(pkt); continue; } if (git_vector_insert(common, pkt) < 0) return -1; } return error; }
/* let's try some producer/consumer tests */ void test_core_buffer__4(void) { git_buf buf = GIT_BUF_INIT; int i; for (i = 0; i < 10; ++i) { git_buf_puts(&buf, "1234"); /* add 4 */ cl_assert(git_buf_oom(&buf) == 0); git_buf_consume(&buf, buf.ptr + 2); /* eat the first two */ cl_assert(strlen(git_buf_cstr(&buf)) == (size_t)((i + 1) * 2)); } /* we have appended 1234 10x and removed the first 20 letters */ cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf)); git_buf_consume(&buf, NULL); cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf)); git_buf_consume(&buf, "invalid pointer"); cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf)); git_buf_consume(&buf, buf.ptr); cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf)); git_buf_consume(&buf, buf.ptr + 1); cl_assert_equal_s("2341234123412341234", git_buf_cstr(&buf)); git_buf_consume(&buf, buf.ptr + buf.size); cl_assert_equal_s("", git_buf_cstr(&buf)); git_buf_free(&buf); }