blob: 0d116721cad04dec7da6002dbcd284ecce566f2f [file] [log] [blame]
Lars Hjemli02a545e2008-08-06 01:20:24 +02001/* ui-clone.c: functions for http cloning, based on
2 * git's http-backend.c by Shawn O. Pearce
3 *
Lukas Fleischerf7f26f82014-01-08 15:10:49 +01004 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemli02a545e2008-08-06 01:20:24 +02005 *
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
8 */
9
10#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +010011#include "ui-clone.h"
Lars Hjemli02a545e2008-08-06 01:20:24 +020012#include "html.h"
13#include "ui-shared.h"
14
Christian Hessede83de22015-07-28 10:42:01 +020015static int print_ref_info(const char *refname, const struct object_id *oid,
Lars Hjemli02a545e2008-08-06 01:20:24 +020016 int flags, void *cb_data)
17{
18 struct object *obj;
19
Jeff Smith86a6d352017-08-09 19:02:56 -050020 if (!(obj = parse_object(oid)))
Lars Hjemli02a545e2008-08-06 01:20:24 +020021 return 0;
22
Christian Hessede83de22015-07-28 10:42:01 +020023 htmlf("%s\t%s\n", oid_to_hex(oid), refname);
Jason A. Donenfeld05da4c22013-02-01 21:08:51 +010024 if (obj->type == OBJ_TAG) {
Lars Hjemli02a545e2008-08-06 01:20:24 +020025 if (!(obj = deref_tag(obj, refname, 0)))
26 return 0;
Christian Hesse559ab5e2016-01-05 07:38:53 +010027 htmlf("%s\t%s^{}\n", oid_to_hex(&obj->oid), refname);
Lars Hjemli02a545e2008-08-06 01:20:24 +020028 }
29 return 0;
30}
31
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010032static void print_pack_info(void)
Lars Hjemli02a545e2008-08-06 01:20:24 +020033{
34 struct packed_git *pack;
Jason A. Donenfeld6e498de2015-02-09 12:27:44 +010035 char *offset;
Lars Hjemli02a545e2008-08-06 01:20:24 +020036
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010037 ctx.page.mimetype = "text/plain";
38 ctx.page.filename = "objects/info/packs";
39 cgit_print_http_headers();
Lars Hjemli02a545e2008-08-06 01:20:24 +020040 prepare_packed_git();
Jason A. Donenfeld6e498de2015-02-09 12:27:44 +010041 for (pack = packed_git; pack; pack = pack->next) {
42 if (pack->pack_local) {
43 offset = strrchr(pack->pack_name, '/');
44 if (offset && offset[1] != '\0')
45 ++offset;
46 else
47 offset = pack->pack_name;
48 htmlf("P %s\n", offset);
49 }
50 }
Lars Hjemli02a545e2008-08-06 01:20:24 +020051}
52
Christian Hessede83de22015-07-28 10:42:01 +020053static void send_file(const char *path)
Lars Hjemli02a545e2008-08-06 01:20:24 +020054{
55 struct stat st;
Lars Hjemli02a545e2008-08-06 01:20:24 +020056
57 if (stat(path, &st)) {
58 switch (errno) {
59 case ENOENT:
John Keeping329381d2015-08-14 12:47:03 +010060 cgit_print_error_page(404, "Not found", "Not found");
Lars Hjemli02a545e2008-08-06 01:20:24 +020061 break;
62 case EACCES:
John Keeping329381d2015-08-14 12:47:03 +010063 cgit_print_error_page(403, "Forbidden", "Forbidden");
Lars Hjemli02a545e2008-08-06 01:20:24 +020064 break;
65 default:
John Keeping329381d2015-08-14 12:47:03 +010066 cgit_print_error_page(400, "Bad request", "Bad request");
Lars Hjemli02a545e2008-08-06 01:20:24 +020067 }
Lars Hjemli02a545e2008-08-06 01:20:24 +020068 return;
69 }
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010070 ctx.page.mimetype = "application/octet-stream";
71 ctx.page.filename = path;
Lukas Fleischerb66b16e2015-02-09 07:25:00 +010072 skip_prefix(path, ctx.repo->path, &ctx.page.filename);
73 skip_prefix(ctx.page.filename, "/", &ctx.page.filename);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010074 cgit_print_http_headers();
Lars Hjemli02a545e2008-08-06 01:20:24 +020075 html_include(path);
76}
77
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010078void cgit_clone_info(void)
Lars Hjemli02a545e2008-08-06 01:20:24 +020079{
Lukas Fleischered7e3bc2015-01-15 19:47:42 +010080 if (!ctx.qry.path || strcmp(ctx.qry.path, "refs")) {
John Keeping329381d2015-08-14 12:47:03 +010081 cgit_print_error_page(400, "Bad request", "Bad request");
Lars Hjemli02a545e2008-08-06 01:20:24 +020082 return;
Lukas Fleischered7e3bc2015-01-15 19:47:42 +010083 }
Lars Hjemli02a545e2008-08-06 01:20:24 +020084
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010085 ctx.page.mimetype = "text/plain";
86 ctx.page.filename = "info/refs";
87 cgit_print_http_headers();
88 for_each_ref(print_ref_info, NULL);
Lars Hjemli02a545e2008-08-06 01:20:24 +020089}
90
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010091void cgit_clone_objects(void)
Lars Hjemli02a545e2008-08-06 01:20:24 +020092{
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010093 if (!ctx.qry.path) {
John Keeping329381d2015-08-14 12:47:03 +010094 cgit_print_error_page(400, "Bad request", "Bad request");
Lars Hjemli02a545e2008-08-06 01:20:24 +020095 return;
96 }
97
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010098 if (!strcmp(ctx.qry.path, "info/packs")) {
99 print_pack_info();
Lars Hjemli02a545e2008-08-06 01:20:24 +0200100 return;
101 }
102
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100103 send_file(git_path("objects/%s", ctx.qry.path));
Lars Hjemli02a545e2008-08-06 01:20:24 +0200104}
105
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100106void cgit_clone_head(void)
Lars Hjemli02a545e2008-08-06 01:20:24 +0200107{
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100108 send_file(git_path("%s", "HEAD"));
Lars Hjemli02a545e2008-08-06 01:20:24 +0200109}