Diff options
Diffstat (limited to '')
| -rw-r--r-- | source/ui-blob.c | 56 |
1 file changed, 16 insertions, 40 deletions
diff --git a/source/ui-blob.c b/source/ui-blob.c index 4690bb5..4ed9b3b 100644 --- a/source/ui-blob.c +++ b/source/ui-blob.c @@ -34,10 +34,6 @@ static void peel_to_commit(struct object_id *oid) oidcpy(oid, &commit->object.oid); } -/* - * read_tree reads the return value as a direction rather than a status, so - * READ_TREE_RECURSIVE means step into this entry and zero means step over it. - */ static int walk_tree(const struct object_id *oid, struct strbuf *base, const char *pathname, unsigned mode, void *context) { @@ -59,24 +55,14 @@ static int walk_tree(const struct object_id *oid, struct strbuf *base, /* * oid comes in naming a commit and goes out naming the blob found at path, - * which every caller relies on. The path has to be writable because a pathspec - * item does not hold a const string. + * which every caller relies on. */ -static int find_path_oid(struct object_id *oid, char *path, int file_only) +static int find_path_oid(struct object_id *oid, const char *path, int file_only) { struct commit *commit = lookup_commit_reference(the_repository, oid); struct tree *tree; - // nowildcard_len matching len makes git treat the path as literal - // rather than as a glob. - struct pathspec_item item = { - .match = path, - .len = strlen(path), - .nowildcard_len = strlen(path) - }; - struct pathspec paths = { - .nr = 1, - .items = &item - }; + struct pathspec_item item; + struct pathspec paths = { 0 }; struct walk_tree_context walk = { .match_path = path, .matched_oid = oid, @@ -91,30 +77,25 @@ static int find_path_oid(struct object_id *oid, char *path, int file_only) tree = repo_get_commit_tree(the_repository, commit); if (!tree) return 0; + cgit_literal_pathspec(&paths, &item, path); read_tree(the_repository, tree, &paths, walk_tree, &walk); return walk.found_path; } -int cgit_ref_path_exists(const char *path, const char *ref, int file_only) +int cgit_ref_path_exists(const char *path, const char *ref) { struct object_id oid; size_t size; - char *path_copy = xstrdup(path); - int found = 0; if (repo_get_oid(the_repository, ref, &oid)) - goto done; + return 0; peel_to_commit(&oid); if (odb_read_object_info(the_repository->objects, &oid, &size) != OBJ_COMMIT) - goto done; - found = find_path_oid(&oid, path_copy, file_only); - -done: - free(path_copy); - return found; + return 0; + return find_path_oid(&oid, path, 1); } -int cgit_print_file(char *path, const char *head, int file_only, int html_escape) +int cgit_print_file(char *path, const char *head, int html_escape) { struct object_id oid; enum object_type type; @@ -126,7 +107,7 @@ int cgit_print_file(char *path, const char *head, int file_only, int html_escape peel_to_commit(&oid); type = odb_read_object_info(the_repository->objects, &oid, &size); if (type == OBJ_COMMIT) { - if (!find_path_oid(&oid, path, file_only)) + if (!find_path_oid(&oid, path, 1)) return -1; type = odb_read_object_info(the_repository->objects, &oid, &size); } @@ -138,9 +119,8 @@ int cgit_print_file(char *path, const char *head, int file_only, int html_escape if (!buf) return -1; - // html_txt wants a terminated string, and git leaves a spare byte - // past every object it reads, so this write stays in the allocation. - buf[size] = '\0'; + // html_txt wants a terminated string, which git leaves past every + // object it reads. if (html_escape) { // html_txt stops at a NUL, so a blob holding one is written // segment by segment with a question mark standing in for @@ -162,7 +142,7 @@ int cgit_print_file(char *path, const char *head, int file_only, int html_escape return 0; } -void cgit_print_blob(const char *hex, char *path, const char *head, int file_only) +void cgit_print_blob(const char *hex, char *path, const char *head) { struct object_id oid; enum object_type type; @@ -191,7 +171,7 @@ void cgit_print_blob(const char *hex, char *path, const char *head, int file_onl } if (type == OBJ_COMMIT && path) { - if (!find_path_oid(&oid, path, file_only)) { + if (!find_path_oid(&oid, path, 0)) { cgit_print_error_page(404, "Not Found", "Path not found: %s", path); return; } @@ -215,11 +195,7 @@ void cgit_print_blob(const char *hex, char *path, const char *head, int file_onl return; } - buf[size] = '\0'; - if (buffer_is_binary(buf, size)) - ctx.page.mimetype = "application/octet-stream"; - else - ctx.page.mimetype = "text/plain"; + ctx.page.mimetype = buffer_is_binary(buf, size) ? "application/octet-stream" : "text/plain"; ctx.page.filename = path; ctx.page.untrusted = 1; cgit_print_http_headers(); |
