all repos — cgit @ b66b16ed026c6f57ad8ae892c821d835113e7fb2

a hyperfast web frontend for git written in c

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	skip_prefix(path, ctx.repo->path, &ctx.page.filename);
 67	skip_prefix(ctx.page.filename, "/", &ctx.page.filename);
 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		html_status(400, "Bad request", 0);
 76		return;
 77	}
 78
 79	ctx.page.mimetype = "text/plain";
 80	ctx.page.filename = "info/refs";
 81	cgit_print_http_headers();
 82	for_each_ref(print_ref_info, NULL);
 83}
 84
 85void cgit_clone_objects(void)
 86{
 87	if (!ctx.qry.path) {
 88		html_status(400, "Bad request", 0);
 89		return;
 90	}
 91
 92	if (!strcmp(ctx.qry.path, "info/packs")) {
 93		print_pack_info();
 94		return;
 95	}
 96
 97	send_file(git_path("objects/%s", ctx.qry.path));
 98}
 99
100void cgit_clone_head(void)
101{
102	send_file(git_path("%s", "HEAD"));
103}