2008-08-05 23:20:24 +00:00
|
|
|
/* ui-clone.c: functions for http cloning, based on
|
|
|
|
* git's http-backend.c by Shawn O. Pearce
|
|
|
|
*
|
2014-01-08 14:10:49 +00:00
|
|
|
* Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
|
2008-08-05 23:20:24 +00:00
|
|
|
*
|
|
|
|
* Licensed under GNU General Public License v2
|
|
|
|
* (see COPYING for full license text)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cgit.h"
|
2013-04-06 10:37:59 +00:00
|
|
|
#include "ui-clone.h"
|
2008-08-05 23:20:24 +00:00
|
|
|
#include "html.h"
|
|
|
|
#include "ui-shared.h"
|
|
|
|
|
|
|
|
static int print_ref_info(const char *refname, const unsigned char *sha1,
|
|
|
|
int flags, void *cb_data)
|
|
|
|
{
|
|
|
|
struct object *obj;
|
|
|
|
|
|
|
|
if (!(obj = parse_object(sha1)))
|
|
|
|
return 0;
|
|
|
|
|
2013-02-01 20:08:51 +00:00
|
|
|
htmlf("%s\t%s\n", sha1_to_hex(sha1), refname);
|
|
|
|
if (obj->type == OBJ_TAG) {
|
2008-08-05 23:20:24 +00:00
|
|
|
if (!(obj = deref_tag(obj, refname, 0)))
|
|
|
|
return 0;
|
|
|
|
htmlf("%s\t%s^{}\n", sha1_to_hex(obj->sha1), refname);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
static void print_pack_info(void)
|
2008-08-05 23:20:24 +00:00
|
|
|
{
|
|
|
|
struct packed_git *pack;
|
2015-02-09 11:27:44 +00:00
|
|
|
char *offset;
|
2008-08-05 23:20:24 +00:00
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
ctx.page.mimetype = "text/plain";
|
|
|
|
ctx.page.filename = "objects/info/packs";
|
|
|
|
cgit_print_http_headers();
|
2008-08-05 23:20:24 +00:00
|
|
|
prepare_packed_git();
|
2015-02-09 11:27:44 +00:00
|
|
|
for (pack = packed_git; pack; pack = pack->next) {
|
|
|
|
if (pack->pack_local) {
|
|
|
|
offset = strrchr(pack->pack_name, '/');
|
|
|
|
if (offset && offset[1] != '\0')
|
|
|
|
++offset;
|
|
|
|
else
|
|
|
|
offset = pack->pack_name;
|
|
|
|
htmlf("P %s\n", offset);
|
|
|
|
}
|
|
|
|
}
|
2008-08-05 23:20:24 +00:00
|
|
|
}
|
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
static void send_file(char *path)
|
2008-08-05 23:20:24 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(path, &st)) {
|
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
2008-08-06 20:57:44 +00:00
|
|
|
html_status(404, "Not found", 0);
|
2008-08-05 23:20:24 +00:00
|
|
|
break;
|
|
|
|
case EACCES:
|
2008-08-06 20:57:44 +00:00
|
|
|
html_status(403, "Forbidden", 0);
|
2008-08-05 23:20:24 +00:00
|
|
|
break;
|
|
|
|
default:
|
2008-08-06 20:57:44 +00:00
|
|
|
html_status(400, "Bad request", 0);
|
2008-08-05 23:20:24 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2014-01-15 20:53:15 +00:00
|
|
|
ctx.page.mimetype = "application/octet-stream";
|
|
|
|
ctx.page.filename = path;
|
2015-02-09 06:25:00 +00:00
|
|
|
skip_prefix(path, ctx.repo->path, &ctx.page.filename);
|
|
|
|
skip_prefix(ctx.page.filename, "/", &ctx.page.filename);
|
2014-01-15 20:53:15 +00:00
|
|
|
cgit_print_http_headers();
|
2008-08-05 23:20:24 +00:00
|
|
|
html_include(path);
|
|
|
|
}
|
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
void cgit_clone_info(void)
|
2008-08-05 23:20:24 +00:00
|
|
|
{
|
2015-01-15 18:47:42 +00:00
|
|
|
if (!ctx.qry.path || strcmp(ctx.qry.path, "refs")) {
|
|
|
|
html_status(400, "Bad request", 0);
|
2008-08-05 23:20:24 +00:00
|
|
|
return;
|
2015-01-15 18:47:42 +00:00
|
|
|
}
|
2008-08-05 23:20:24 +00:00
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
ctx.page.mimetype = "text/plain";
|
|
|
|
ctx.page.filename = "info/refs";
|
|
|
|
cgit_print_http_headers();
|
|
|
|
for_each_ref(print_ref_info, NULL);
|
2008-08-05 23:20:24 +00:00
|
|
|
}
|
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
void cgit_clone_objects(void)
|
2008-08-05 23:20:24 +00:00
|
|
|
{
|
2014-01-15 20:53:15 +00:00
|
|
|
if (!ctx.qry.path) {
|
2008-08-06 20:57:44 +00:00
|
|
|
html_status(400, "Bad request", 0);
|
2008-08-05 23:20:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
if (!strcmp(ctx.qry.path, "info/packs")) {
|
|
|
|
print_pack_info();
|
2008-08-05 23:20:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
send_file(git_path("objects/%s", ctx.qry.path));
|
2008-08-05 23:20:24 +00:00
|
|
|
}
|
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
void cgit_clone_head(void)
|
2008-08-05 23:20:24 +00:00
|
|
|
{
|
2014-01-15 20:53:15 +00:00
|
|
|
send_file(git_path("%s", "HEAD"));
|
2008-08-05 23:20:24 +00:00
|
|
|
}
|