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