3601650c3f1d5244c9e62d018ad6950757d5a813
[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
26 /*
27   Display blobs
28 */
29 static Html
30 webgit_object_blob_menu_action (struct webgit_repo_info* repo, unsigned char* sha1, void* buf, unsigned long size)
31 {
32   (void) sha1;
33   (void) buf;
34   (void) size;
35
36   struct webgit_query* query = repo->query;
37
38   return html (html_tag ("div"),
39                webgit_repo_logo (repo), "<br />",
40                // TODO: "diff against.. <br />",
41                // TODO: "log <br />",
42                // TODO: "history <br />",
43                // TODO: "blame <br />",
44                // TODO: "Back to tree <br />",
45                // TODO: ,
46                repo->worktree ?
47                 webgit_object_link (query, html ("Edit <br />"),
48                                    "repo", query->repo,
49                                    "ref", query->head,
50                                    "commit", query->commit,
51                                    "path", query->path,
52                                     /* "object", worktree_object_get () */
53                                     "action", "edit")
54                : html ("TODO difflink this/worktree <br />"),
55                webgit_object_tree_parent_link (query, "<<%s"),
56                webgit_summary_link (query, repo, html("Summary")), "<br />",
57                webgit_main_link (query, html ("Main"))
58                );
59 }
60
61 static Html
62 webgit_object_blob_content_action (struct webgit_repo_info* repo, unsigned char* sha1, void* buf, unsigned long size)
63 {
64   (void) sha1;
65
66   if (!memchr(buf, 0, size>8192 ? 8192 : size))
67     {
68       return html (
69                    html_tag ("textarea",
70                              html_attr ("id", "blob"),
71                              html_attr ("name", "blob"),
72                              html_attr ("class", "codepress generic readonly-on"),
73                              html_attr ("wrap", "off"),
74                              html_attr ("style", "width:100%;height:600px")
75                              ),
76                    html_strndup (buf, size)
77                    );
78     }
79   else
80     {
81       struct webgit_query* query = repo->query;
82       const char* mimetype = webgit_mimetype (query->path);
83
84       Html ret;
85
86       if (mimetype && !strncmp(mimetype, "image/", 6))
87         {
88           /* inline image */
89           ret = html (
90                       html_tag ("img",
91                                 html_attr ("src",
92                                            html_fmt ("%s?repo=%s&action=raw&object=%s&path=%s",
93                                                      query->request->script_name,
94                                                      query->repo, query->object, query->path)
95                                            ),
96                                 html_attr ("alt", query->path ? query->path : query->object)
97                                 )
98                       );
99         }
100       else
101         {
102           /* link to raw data */
103           ret = html ("binary blob");
104         }
105       free ((char*)mimetype);
106
107       return ret;
108     }
109 }
110
111
112 Html
113 webgit_object_blob_action (struct webgit_repo_info* repo, unsigned char* sha1)
114 {
115   void* buf;
116   unsigned long size;
117
118   buf = read_object_with_reference (sha1, "blob", &size, NULL);
119
120   return html(
121               html(html_tag("div",
122                             html_attr("id", "sub-menu")),
123                    webgit_object_blob_menu_action (repo, sha1, buf, size)), html_nl (),
124               html(html_tag("div",
125                             html_attr("id", "content")),
126                    webgit_object_blob_content_action (repo, sha1, buf, size)), html_nl ()
127               );
128 }
129
130 /*
131 //      Local Variables:
132 //      mode: C
133 //      c-file-style: "gnu"
134 //      indent-tabs-mode: nil
135 //      End:
136 */