ce48a8804b4a7fd6ed4f7781f569a06a5738636f
[webgit] / src / object_blob.c
1 /*
2     cehtehs git web frontend
3
4   Copyright (C)
5     2007, 2008,         Christian Thaeter <ct@pipapo.org>
6
7   This program is free software: you can redistribute it and/or modify
8   it under the terms of the GNU Affero General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU Affero General Public License for more details.
16
17   You should have received a copy of the GNU Affero General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "object.h"
22 #include "actions.h"
23 #include "repo.h"
24 #include "summary.h"
25 #include "worktree.h"
26
27 /*
28   Display blobs
29 */
30 static Html
31 webgit_object_blob_menu_action (struct webgit_repo_info* repo, unsigned char* sha1, void* buf, unsigned long size)
32 {
33   (void) sha1;
34   (void) buf;
35   (void) size;
36
37   struct webgit_query* query = repo->query;
38
39   return html (html_tag ("div"),
40                webgit_repo_logo (repo), "<br />",
41                // TODO: "diff against.. <br />",
42                // TODO: "log <br />",
43                // TODO: "history <br />",
44                // TODO: "blame <br />",
45                // TODO: "Back to tree <br />",
46                // TODO: ,
47                repo->worktree
48                ? ( webgit_worktree_blob_eq (repo, query->path)
49                    ?  webgit_object_link (query,
50                                           html ("Edit<br />"),
51                                           "repo", query->repo,
52                                           "ref", query->head,
53                                           "commit", query->commit,
54                                           "path", query->path,
55                                           "action", "edit")
56                    : html ("diff<br />")
57                    )
58                : html (),
59                webgit_object_tree_parent_link (query, "<<%s"),
60                webgit_summary_link (query, repo, html("Summary")), "<br />",
61                webgit_main_link (query, html ("Main"))
62                );
63 }
64
65 static Html
66 webgit_object_blob_content_action (struct webgit_repo_info* repo, unsigned char* sha1, void* buf, unsigned long size)
67 {
68   (void) sha1;
69
70   if (!memchr(buf, 0, size>8192 ? 8192 : size))
71     {
72       return html (
73                    html_tag ("textarea",
74                              html_attr ("id", "blob"),
75                              html_attr ("name", "blob"),
76                              html_attr ("class", "codepress generic readonly-on"),
77                              html_attr ("wrap", "off"),
78                              html_attr ("style", "width:100%;height:600px")
79                              ),
80                    html_strndup (buf, size)
81                    );
82     }
83   else
84     {
85       struct webgit_query* query = repo->query;
86       const char* mimetype = webgit_mimetype (query->path);
87
88       Html ret;
89
90       if (mimetype && !strncmp(mimetype, "image/", 6))
91         {
92           /* inline image */
93           ret = html (
94                       html_tag ("img",
95                                 html_attr ("src",
96                                            html_fmt ("%s?repo=%s&action=raw&object=%s&path=%s",
97                                                      query->request->script_name,
98                                                      query->repo, query->object, query->path)
99                                            ),
100                                 html_attr ("alt", query->path ? query->path : query->object)
101                                 )
102                       );
103         }
104       else
105         {
106           /* link to raw data */
107           ret = html ("binary blob");
108         }
109       free ((char*)mimetype);
110
111       return ret;
112     }
113 }
114
115
116 Html
117 webgit_object_blob_action (struct webgit_repo_info* repo, unsigned char* sha1)
118 {
119   void* buf;
120   unsigned long size;
121
122   buf = read_object_with_reference (sha1, "blob", &size, NULL);
123
124   return html(
125               html(html_tag("div",
126                             html_attr("id", "sub-menu")),
127                    webgit_object_blob_menu_action (repo, sha1, buf, size)), html_nl (),
128               html(html_tag("div",
129                             html_attr("id", "content")),
130                    webgit_object_blob_content_action (repo, sha1, buf, size)), html_nl ()
131               );
132 }
133
134 /*
135 //      Local Variables:
136 //      mode: C
137 //      c-file-style: "gnu"
138 //      indent-tabs-mode: nil
139 //      End:
140 */