]> gitweb.ps.run Git - ps-cgit/blob - ui-blob.c
Makefile: re-use Git's Makefile where possible
[ps-cgit] / ui-blob.c
1 /* ui-blob.c: show blob content
2  *
3  * Copyright (C) 2008 Lars Hjemli
4  * Copyright (C) 2010 Jason A. Donenfeld <Jason@zx2c4.com>
5  *
6  * Licensed under GNU General Public License v2
7  *   (see COPYING for full license text)
8  */
9
10 #include "cgit.h"
11 #include "html.h"
12 #include "ui-shared.h"
13
14 struct walk_tree_context {
15         char *match_path;
16         unsigned char *matched_sha1;
17         int found_path;
18 };
19
20 static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
21         const char *pathname, unsigned mode, int stage, void *cbdata)
22 {
23         struct walk_tree_context *walk_tree_ctx = cbdata;
24
25         if (strncmp(base, walk_tree_ctx->match_path, baselen)
26                 || strcmp(walk_tree_ctx->match_path + baselen, pathname))
27                 return READ_TREE_RECURSIVE;
28         memmove(walk_tree_ctx->matched_sha1, sha1, 20);
29         walk_tree_ctx->found_path = 1;
30         return 0;
31 }
32
33 int cgit_print_file(char *path, const char *head)
34 {
35         unsigned char sha1[20];
36         enum object_type type;
37         char *buf;
38         unsigned long size;
39         struct commit *commit;
40         struct pathspec_item path_items = {
41                 .match = path,
42                 .len = strlen(path)
43         };
44         struct pathspec paths = {
45                 .nr = 1,
46                 .items = &path_items
47         };
48         struct walk_tree_context walk_tree_ctx = {
49                 .match_path = path,
50                 .matched_sha1 = sha1,
51                 .found_path = 0
52         };
53
54         if (get_sha1(head, sha1))
55                 return -1;
56         type = sha1_object_info(sha1, &size);
57         if (type == OBJ_COMMIT && path) {
58                 commit = lookup_commit_reference(sha1);
59                 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
60                 if (!walk_tree_ctx.found_path)
61                         return -1;
62                 type = sha1_object_info(sha1, &size);
63         }
64         if (type == OBJ_BAD)
65                 return -1;
66         buf = read_sha1_file(sha1, &type, &size);
67         if (!buf)
68                 return -1;
69         buf[size] = '\0';
70         html_raw(buf, size);
71         return 0;
72 }
73
74 void cgit_print_blob(const char *hex, char *path, const char *head)
75 {
76         unsigned char sha1[20];
77         enum object_type type;
78         char *buf;
79         unsigned long size;
80         struct commit *commit;
81         struct pathspec_item path_items = {
82                 .match = path,
83                 .len = strlen(path)
84         };
85         struct pathspec paths = {
86                 .nr = 1,
87                 .items = &path_items
88         };
89         struct walk_tree_context walk_tree_ctx = {
90                 .match_path = path,
91                 .matched_sha1 = sha1,
92         };
93
94         if (hex) {
95                 if (get_sha1_hex(hex, sha1)) {
96                         cgit_print_error(fmt("Bad hex value: %s", hex));
97                         return;
98                 }
99         } else {
100                 if (get_sha1(head, sha1)) {
101                         cgit_print_error(fmt("Bad ref: %s", head));
102                         return;
103                 }
104         }
105
106         type = sha1_object_info(sha1, &size);
107
108         if ((!hex) && type == OBJ_COMMIT && path) {
109                 commit = lookup_commit_reference(sha1);
110                 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
111                 type = sha1_object_info(sha1,&size);
112         }
113
114         if (type == OBJ_BAD) {
115                 cgit_print_error(fmt("Bad object name: %s", hex));
116                 return;
117         }
118
119         buf = read_sha1_file(sha1, &type, &size);
120         if (!buf) {
121                 cgit_print_error(fmt("Error reading object %s", hex));
122                 return;
123         }
124
125         buf[size] = '\0';
126         ctx.page.mimetype = ctx.qry.mimetype;
127         if (!ctx.page.mimetype) {
128                 if (buffer_is_binary(buf, size))
129                         ctx.page.mimetype = "application/octet-stream";
130                 else
131                         ctx.page.mimetype = "text/plain";
132         }
133         ctx.page.filename = path;
134         cgit_print_http_headers(&ctx);
135         html_raw(buf, size);
136 }