blob: bc989804df9b3090110efe01e21427099d98b0f7 [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"
Christian Hesse5d947ba2017-11-29 22:25:42 +010014#include "packfile.h"
Lars Hjemli02a545e2008-08-06 01:20:24 +020015
Christian Hessede83de22015-07-28 10:42:01 +020016static int print_ref_info(const char *refname, const struct object_id *oid,
Lars Hjemli02a545e2008-08-06 01:20:24 +020017 int flags, void *cb_data)
18{
19 struct object *obj;
20
Jeff Smith86a6d352017-08-09 19:02:56 -050021 if (!(obj = parse_object(oid)))
Lars Hjemli02a545e2008-08-06 01:20:24 +020022 return 0;
23
Christian Hessede83de22015-07-28 10:42:01 +020024 htmlf("%s\t%s\n", oid_to_hex(oid), refname);
Jason A. Donenfeld05da4c22013-02-01 21:08:51 +010025 if (obj->type == OBJ_TAG) {
Lars Hjemli02a545e2008-08-06 01:20:24 +020026 if (!(obj = deref_tag(obj, refname, 0)))
27 return 0;
Christian Hesse559ab5e2016-01-05 07:38:53 +010028 htmlf("%s\t%s^{}\n", oid_to_hex(&obj->oid), refname);
Lars Hjemli02a545e2008-08-06 01:20:24 +020029 }
30 return 0;
31}
32
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010033static void print_pack_info(void)
Lars Hjemli02a545e2008-08-06 01:20:24 +020034{
35 struct packed_git *pack;
Jason A. Donenfeld6e498de2015-02-09 12:27:44 +010036 char *offset;
Lars Hjemli02a545e2008-08-06 01:20:24 +020037
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010038 ctx.page.mimetype = "text/plain";
39 ctx.page.filename = "objects/info/packs";
40 cgit_print_http_headers();
Lars Hjemli02a545e2008-08-06 01:20:24 +020041 prepare_packed_git();
Jason A. Donenfeld6e498de2015-02-09 12:27:44 +010042 for (pack = packed_git; pack; pack = pack->next) {
43 if (pack->pack_local) {
44 offset = strrchr(pack->pack_name, '/');
45 if (offset && offset[1] != '\0')
46 ++offset;
47 else
48 offset = pack->pack_name;
49 htmlf("P %s\n", offset);
50 }
51 }
Lars Hjemli02a545e2008-08-06 01:20:24 +020052}
53
Christian Hessede83de22015-07-28 10:42:01 +020054static void send_file(const char *path)
Lars Hjemli02a545e2008-08-06 01:20:24 +020055{
56 struct stat st;
Lars Hjemli02a545e2008-08-06 01:20:24 +020057
58 if (stat(path, &st)) {
59 switch (errno) {
60 case ENOENT:
John Keeping329381d2015-08-14 12:47:03 +010061 cgit_print_error_page(404, "Not found", "Not found");
Lars Hjemli02a545e2008-08-06 01:20:24 +020062 break;
63 case EACCES:
John Keeping329381d2015-08-14 12:47:03 +010064 cgit_print_error_page(403, "Forbidden", "Forbidden");
Lars Hjemli02a545e2008-08-06 01:20:24 +020065 break;
66 default:
John Keeping329381d2015-08-14 12:47:03 +010067 cgit_print_error_page(400, "Bad request", "Bad request");
Lars Hjemli02a545e2008-08-06 01:20:24 +020068 }
Lars Hjemli02a545e2008-08-06 01:20:24 +020069 return;
70 }
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010071 ctx.page.mimetype = "application/octet-stream";
72 ctx.page.filename = path;
Lukas Fleischerb66b16e2015-02-09 07:25:00 +010073 skip_prefix(path, ctx.repo->path, &ctx.page.filename);
74 skip_prefix(ctx.page.filename, "/", &ctx.page.filename);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010075 cgit_print_http_headers();
Lars Hjemli02a545e2008-08-06 01:20:24 +020076 html_include(path);
77}
78
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010079void cgit_clone_info(void)
Lars Hjemli02a545e2008-08-06 01:20:24 +020080{
Lukas Fleischered7e3bc2015-01-15 19:47:42 +010081 if (!ctx.qry.path || strcmp(ctx.qry.path, "refs")) {
John Keeping329381d2015-08-14 12:47:03 +010082 cgit_print_error_page(400, "Bad request", "Bad request");
Lars Hjemli02a545e2008-08-06 01:20:24 +020083 return;
Lukas Fleischered7e3bc2015-01-15 19:47:42 +010084 }
Lars Hjemli02a545e2008-08-06 01:20:24 +020085
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010086 ctx.page.mimetype = "text/plain";
87 ctx.page.filename = "info/refs";
88 cgit_print_http_headers();
89 for_each_ref(print_ref_info, NULL);
Lars Hjemli02a545e2008-08-06 01:20:24 +020090}
91
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010092void cgit_clone_objects(void)
Lars Hjemli02a545e2008-08-06 01:20:24 +020093{
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010094 if (!ctx.qry.path) {
John Keeping329381d2015-08-14 12:47:03 +010095 cgit_print_error_page(400, "Bad request", "Bad request");
Lars Hjemli02a545e2008-08-06 01:20:24 +020096 return;
97 }
98
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010099 if (!strcmp(ctx.qry.path, "info/packs")) {
100 print_pack_info();
Lars Hjemli02a545e2008-08-06 01:20:24 +0200101 return;
102 }
103
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100104 send_file(git_path("objects/%s", ctx.qry.path));
Lars Hjemli02a545e2008-08-06 01:20:24 +0200105}
106
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100107void cgit_clone_head(void)
Lars Hjemli02a545e2008-08-06 01:20:24 +0200108{
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100109 send_file(git_path("%s", "HEAD"));
Lars Hjemli02a545e2008-08-06 01:20:24 +0200110}