ui-clone.c (view raw)
1/* ui-clone.c: functions for http cloning, based on
2 * git's http-backend.c by Shawn O. Pearce
3 *
4 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.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 "ui-clone.h"
12#include "html.h"
13#include "ui-shared.h"
14
15static int print_ref_info(const char *refname, const unsigned char *sha1,
16 int flags, void *cb_data)
17{
18 struct object *obj;
19
20 if (!(obj = parse_object(sha1)))
21 return 0;
22
23 htmlf("%s\t%s\n", sha1_to_hex(sha1), refname);
24 if (obj->type == OBJ_TAG) {
25 if (!(obj = deref_tag(obj, refname, 0)))
26 return 0;
27 htmlf("%s\t%s^{}\n", sha1_to_hex(obj->sha1), refname);
28 }
29 return 0;
30}
31
32static void print_pack_info(void)
33{
34 struct packed_git *pack;
35 int ofs;
36
37 ctx.page.mimetype = "text/plain";
38 ctx.page.filename = "objects/info/packs";
39 cgit_print_http_headers();
40 ofs = strlen(ctx.repo->path) + strlen("/objects/pack/");
41 prepare_packed_git();
42 for (pack = packed_git; pack; pack = pack->next)
43 if (pack->pack_local)
44 htmlf("P %s\n", pack->pack_name + ofs);
45}
46
47static void send_file(char *path)
48{
49 struct stat st;
50
51 if (stat(path, &st)) {
52 switch (errno) {
53 case ENOENT:
54 html_status(404, "Not found", 0);
55 break;
56 case EACCES:
57 html_status(403, "Forbidden", 0);
58 break;
59 default:
60 html_status(400, "Bad request", 0);
61 }
62 return;
63 }
64 ctx.page.mimetype = "application/octet-stream";
65 ctx.page.filename = path;
66 if (prefixcmp(ctx.repo->path, path))
67 ctx.page.filename += strlen(ctx.repo->path) + 1;
68 cgit_print_http_headers();
69 html_include(path);
70}
71
72void cgit_clone_info(void)
73{
74 if (!ctx.qry.path || strcmp(ctx.qry.path, "refs"))
75 return;
76
77 ctx.page.mimetype = "text/plain";
78 ctx.page.filename = "info/refs";
79 cgit_print_http_headers();
80 for_each_ref(print_ref_info, NULL);
81}
82
83void cgit_clone_objects(void)
84{
85 if (!ctx.qry.path) {
86 html_status(400, "Bad request", 0);
87 return;
88 }
89
90 if (!strcmp(ctx.qry.path, "info/packs")) {
91 print_pack_info();
92 return;
93 }
94
95 send_file(git_path("objects/%s", ctx.qry.path));
96}
97
98void cgit_clone_head(void)
99{
100 send_file(git_path("%s", "HEAD"));
101}