From 90b4a71c493bf24f11b5edee8a519110624a6bea Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 9 Sep 2008 01:27:07 -0700 Subject: is_directory(): a generic helper function A simple "grep -e stat --and -e S_ISDIR" revealed there are many open-coded implementations of this function. Signed-off-by: Junio C Hamano diff --git a/abspath.c b/abspath.c index 0d56124..8194ce1 100644 --- a/abspath.c +++ b/abspath.c @@ -1,5 +1,16 @@ #include "cache.h" +/* + * Do not use this for inspecting *tracked* content. When path is a + * symlink to a directory, we do not want to say it is a directory when + * dealing with tracked content in the working tree. + */ +int is_directory(const char *path) +{ + struct stat st; + return (!stat(path, &st) && S_ISDIR(st.st_mode)); +} + /* We allow "recursive" symbolic links. Only within reason, though. */ #define MAXDEPTH 5 @@ -17,7 +28,7 @@ const char *make_absolute_path(const char *path) die ("Too long path: %.*s", 60, path); while (depth--) { - if (stat(buf, &st) || !S_ISDIR(st.st_mode)) { + if (!is_directory(buf)) { char *last_slash = strrchr(buf, '/'); if (last_slash) { *last_slash = '\0'; diff --git a/builtin-clone.c b/builtin-clone.c index c843529..a4b8790 100644 --- a/builtin-clone.c +++ b/builtin-clone.c @@ -77,7 +77,7 @@ static char *get_repo_path(const char *repo, int *is_bundle) for (i = 0; i < ARRAY_SIZE(suffix); i++) { const char *path; path = mkpath("%s%s", repo, suffix[i]); - if (!stat(path, &st) && S_ISDIR(st.st_mode)) { + if (is_directory(path)) { *is_bundle = 0; return xstrdup(make_nonrelative_path(path)); } @@ -140,13 +140,6 @@ static char *guess_dir_name(const char *repo, int is_bundle, int is_bare) return xstrndup(start, end - start); } -static int is_directory(const char *path) -{ - struct stat buf; - - return !stat(path, &buf) && S_ISDIR(buf.st_mode); -} - static void strip_trailing_slashes(char *dir) { char *end = dir + strlen(dir); diff --git a/cache.h b/cache.h index de8c2b6..ce1f630 100644 --- a/cache.h +++ b/cache.h @@ -533,6 +533,7 @@ static inline int is_absolute_path(const char *path) { return path[0] == '/' || has_dos_drive_prefix(path); } +int is_directory(const char *); const char *make_absolute_path(const char *path); const char *make_nonrelative_path(const char *path); const char *make_relative_path(const char *abs, const char *base); diff --git a/daemon.c b/daemon.c index c315932..ab7a273 100644 --- a/daemon.c +++ b/daemon.c @@ -1115,13 +1115,9 @@ int main(int argc, char **argv) if (strict_paths && (!ok_paths || !*ok_paths)) die("option --strict-paths requires a whitelist"); - if (base_path) { - struct stat st; - - if (stat(base_path, &st) || !S_ISDIR(st.st_mode)) - die("base-path '%s' does not exist or " - "is not a directory", base_path); - } + if (base_path && !is_directory(base_path)) + die("base-path '%s' does not exist or is not a directory", + base_path); if (inetd_mode) { struct sockaddr_storage ss; diff --git a/rerere.c b/rerere.c index 323e493..c38886b 100644 --- a/rerere.c +++ b/rerere.c @@ -319,7 +319,6 @@ static int git_rerere_config(const char *var, const char *value, void *cb) static int is_rerere_enabled(void) { - struct stat st; const char *rr_cache; int rr_cache_exists; @@ -327,7 +326,7 @@ static int is_rerere_enabled(void) return 0; rr_cache = git_path("rr-cache"); - rr_cache_exists = !stat(rr_cache, &st) && S_ISDIR(st.st_mode); + rr_cache_exists = is_directory(rr_cache); if (rerere_enabled < 0) return rr_cache_exists; diff --git a/sha1_file.c b/sha1_file.c index 9ee1ed1..ae550e8 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -250,7 +250,6 @@ static void read_info_alternates(const char * alternates, int depth); */ static int link_alt_odb_entry(const char * entry, int len, const char * relative_base, int depth) { - struct stat st; const char *objdir = get_object_directory(); struct alternate_object_database *ent; struct alternate_object_database *alt; @@ -281,7 +280,7 @@ static int link_alt_odb_entry(const char * entry, int len, const char * relative ent->base[pfxlen] = ent->base[entlen-1] = 0; /* Detect cases where alternate disappeared */ - if (stat(ent->base, &st) || !S_ISDIR(st.st_mode)) { + if (!is_directory(ent->base)) { error("object directory %s does not exist; " "check .git/objects/info/alternates.", ent->base); -- cgit v0.10.2-6-g49f6 From be5908aed35d1c129f1ea38913b230ee1927361b Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 9 Sep 2008 01:27:08 -0700 Subject: receive-pack: make it a builtin It is a good thing to do in general, but more importantly, transport routines can only be used by built-ins, which is what I'll be adding next. Signed-off-by: Junio C Hamano diff --git a/Makefile b/Makefile index f4c31c8..f9c54ff 100644 --- a/Makefile +++ b/Makefile @@ -294,7 +294,6 @@ PROGRAMS += git-mktag$X PROGRAMS += git-mktree$X PROGRAMS += git-pack-redundant$X PROGRAMS += git-patch-id$X -PROGRAMS += git-receive-pack$X PROGRAMS += git-send-pack$X PROGRAMS += git-shell$X PROGRAMS += git-show-index$X @@ -546,6 +545,7 @@ BUILTIN_OBJS += builtin-prune-packed.o BUILTIN_OBJS += builtin-prune.o BUILTIN_OBJS += builtin-push.o BUILTIN_OBJS += builtin-read-tree.o +BUILTIN_OBJS += builtin-receive-pack.o BUILTIN_OBJS += builtin-reflog.o BUILTIN_OBJS += builtin-remote.o BUILTIN_OBJS += builtin-rerere.o diff --git a/builtin-receive-pack.c b/builtin-receive-pack.c new file mode 100644 index 0000000..6d6027e --- /dev/null +++ b/builtin-receive-pack.c @@ -0,0 +1,520 @@ +#include "cache.h" +#include "pack.h" +#include "refs.h" +#include "pkt-line.h" +#include "run-command.h" +#include "exec_cmd.h" +#include "commit.h" +#include "object.h" + +static const char receive_pack_usage[] = "git-receive-pack "; + +static int deny_non_fast_forwards = 0; +static int receive_fsck_objects; +static int receive_unpack_limit = -1; +static int transfer_unpack_limit = -1; +static int unpack_limit = 100; +static int report_status; + +static char capabilities[] = " report-status delete-refs "; +static int capabilities_sent; + +static int receive_pack_config(const char *var, const char *value, void *cb) +{ + if (strcmp(var, "receive.denynonfastforwards") == 0) { + deny_non_fast_forwards = git_config_bool(var, value); + return 0; + } + + if (strcmp(var, "receive.unpacklimit") == 0) { + receive_unpack_limit = git_config_int(var, value); + return 0; + } + + if (strcmp(var, "transfer.unpacklimit") == 0) { + transfer_unpack_limit = git_config_int(var, value); + return 0; + } + + if (strcmp(var, "receive.fsckobjects") == 0) { + receive_fsck_objects = git_config_bool(var, value); + return 0; + } + + return git_default_config(var, value, cb); +} + +static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data) +{ + if (capabilities_sent) + packet_write(1, "%s %s\n", sha1_to_hex(sha1), path); + else + packet_write(1, "%s %s%c%s\n", + sha1_to_hex(sha1), path, 0, capabilities); + capabilities_sent = 1; + return 0; +} + +static void write_head_info(void) +{ + for_each_ref(show_ref, NULL); + if (!capabilities_sent) + show_ref("capabilities^{}", null_sha1, 0, NULL); + +} + +struct command { + struct command *next; + const char *error_string; + unsigned char old_sha1[20]; + unsigned char new_sha1[20]; + char ref_name[FLEX_ARRAY]; /* more */ +}; + +static struct command *commands; + +static const char pre_receive_hook[] = "hooks/pre-receive"; +static const char post_receive_hook[] = "hooks/post-receive"; + +static int hook_status(int code, const char *hook_name) +{ + switch (code) { + case 0: + return 0; + case -ERR_RUN_COMMAND_FORK: + return error("hook fork failed"); + case -ERR_RUN_COMMAND_EXEC: + return error("hook execute failed"); + case -ERR_RUN_COMMAND_PIPE: + return error("hook pipe failed"); + case -ERR_RUN_COMMAND_WAITPID: + return error("waitpid failed"); + case -ERR_RUN_COMMAND_WAITPID_WRONG_PID: + return error("waitpid is confused"); + case -ERR_RUN_COMMAND_WAITPID_SIGNAL: + return error("%s died of signal", hook_name); + case -ERR_RUN_COMMAND_WAITPID_NOEXIT: + return error("%s died strangely", hook_name); + default: + error("%s exited with error code %d", hook_name, -code); + return -code; + } +} + +static int run_hook(const char *hook_name) +{ + static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4]; + struct command *cmd; + struct child_process proc; + const char *argv[2]; + int have_input = 0, code; + + for (cmd = commands; !have_input && cmd; cmd = cmd->next) { + if (!cmd->error_string) + have_input = 1; + } + + if (!have_input || access(hook_name, X_OK) < 0) + return 0; + + argv[0] = hook_name; + argv[1] = NULL; + + memset(&proc, 0, sizeof(proc)); + proc.argv = argv; + proc.in = -1; + proc.stdout_to_stderr = 1; + + code = start_command(&proc); + if (code) + return hook_status(code, hook_name); + for (cmd = commands; cmd; cmd = cmd->next) { + if (!cmd->error_string) { + size_t n = snprintf(buf, sizeof(buf), "%s %s %s\n", + sha1_to_hex(cmd->old_sha1), + sha1_to_hex(cmd->new_sha1), + cmd->ref_name); + if (write_in_full(proc.in, buf, n) != n) + break; + } + } + close(proc.in); + return hook_status(finish_command(&proc), hook_name); +} + +static int run_update_hook(struct command *cmd) +{ + static const char update_hook[] = "hooks/update"; + struct child_process proc; + const char *argv[5]; + + if (access(update_hook, X_OK) < 0) + return 0; + + argv[0] = update_hook; + argv[1] = cmd->ref_name; + argv[2] = sha1_to_hex(cmd->old_sha1); + argv[3] = sha1_to_hex(cmd->new_sha1); + argv[4] = NULL; + + memset(&proc, 0, sizeof(proc)); + proc.argv = argv; + proc.no_stdin = 1; + proc.stdout_to_stderr = 1; + + return hook_status(run_command(&proc), update_hook); +} + +static const char *update(struct command *cmd) +{ + const char *name = cmd->ref_name; + unsigned char *old_sha1 = cmd->old_sha1; + unsigned char *new_sha1 = cmd->new_sha1; + struct ref_lock *lock; + + /* only refs/... are allowed */ + if (prefixcmp(name, "refs/") || check_ref_format(name + 5)) { + error("refusing to create funny ref '%s' remotely", name); + return "funny refname"; + } + + if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) { + error("unpack should have generated %s, " + "but I can't find it!", sha1_to_hex(new_sha1)); + return "bad pack"; + } + if (deny_non_fast_forwards && !is_null_sha1(new_sha1) && + !is_null_sha1(old_sha1) && + !prefixcmp(name, "refs/heads/")) { + struct object *old_object, *new_object; + struct commit *old_commit, *new_commit; + struct commit_list *bases, *ent; + + old_object = parse_object(old_sha1); + new_object = parse_object(new_sha1); + + if (!old_object || !new_object || + old_object->type != OBJ_COMMIT || + new_object->type != OBJ_COMMIT) { + error("bad sha1 objects for %s", name); + return "bad ref"; + } + old_commit = (struct commit *)old_object; + new_commit = (struct commit *)new_object; + bases = get_merge_bases(old_commit, new_commit, 1); + for (ent = bases; ent; ent = ent->next) + if (!hashcmp(old_sha1, ent->item->object.sha1)) + break; + free_commit_list(bases); + if (!ent) { + error("denying non-fast forward %s" + " (you should pull first)", name); + return "non-fast forward"; + } + } + if (run_update_hook(cmd)) { + error("hook declined to update %s", name); + return "hook declined"; + } + + if (is_null_sha1(new_sha1)) { + if (!parse_object(old_sha1)) { + warning ("Allowing deletion of corrupt ref."); + old_sha1 = NULL; + } + if (delete_ref(name, old_sha1)) { + error("failed to delete %s", name); + return "failed to delete"; + } + return NULL; /* good */ + } + else { + lock = lock_any_ref_for_update(name, old_sha1, 0); + if (!lock) { + error("failed to lock %s", name); + return "failed to lock"; + } + if (write_ref_sha1(lock, new_sha1, "push")) { + return "failed to write"; /* error() already called */ + } + return NULL; /* good */ + } +} + +static char update_post_hook[] = "hooks/post-update"; + +static void run_update_post_hook(struct command *cmd) +{ + struct command *cmd_p; + int argc; + const char **argv; + + for (argc = 0, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) { + if (cmd_p->error_string) + continue; + argc++; + } + if (!argc || access(update_post_hook, X_OK) < 0) + return; + argv = xmalloc(sizeof(*argv) * (2 + argc)); + argv[0] = update_post_hook; + + for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) { + char *p; + if (cmd_p->error_string) + continue; + p = xmalloc(strlen(cmd_p->ref_name) + 1); + strcpy(p, cmd_p->ref_name); + argv[argc] = p; + argc++; + } + argv[argc] = NULL; + run_command_v_opt(argv, RUN_COMMAND_NO_STDIN + | RUN_COMMAND_STDOUT_TO_STDERR); +} + +static void execute_commands(const char *unpacker_error) +{ + struct command *cmd = commands; + + if (unpacker_error) { + while (cmd) { + cmd->error_string = "n/a (unpacker error)"; + cmd = cmd->next; + } + return; + } + + if (run_hook(pre_receive_hook)) { + while (cmd) { + cmd->error_string = "pre-receive hook declined"; + cmd = cmd->next; + } + return; + } + + while (cmd) { + cmd->error_string = update(cmd); + cmd = cmd->next; + } +} + +static void read_head_info(void) +{ + struct command **p = &commands; + for (;;) { + static char line[1000]; + unsigned char old_sha1[20], new_sha1[20]; + struct command *cmd; + char *refname; + int len, reflen; + + len = packet_read_line(0, line, sizeof(line)); + if (!len) + break; + if (line[len-1] == '\n') + line[--len] = 0; + if (len < 83 || + line[40] != ' ' || + line[81] != ' ' || + get_sha1_hex(line, old_sha1) || + get_sha1_hex(line + 41, new_sha1)) + die("protocol error: expected old/new/ref, got '%s'", + line); + + refname = line + 82; + reflen = strlen(refname); + if (reflen + 82 < len) { + if (strstr(refname + reflen + 1, "report-status")) + report_status = 1; + } + cmd = xmalloc(sizeof(struct command) + len - 80); + hashcpy(cmd->old_sha1, old_sha1); + hashcpy(cmd->new_sha1, new_sha1); + memcpy(cmd->ref_name, line + 82, len - 81); + cmd->error_string = NULL; + cmd->next = NULL; + *p = cmd; + p = &cmd->next; + } +} + +static const char *parse_pack_header(struct pack_header *hdr) +{ + switch (read_pack_header(0, hdr)) { + case PH_ERROR_EOF: + return "eof before pack header was fully read"; + + case PH_ERROR_PACK_SIGNATURE: + return "protocol error (pack signature mismatch detected)"; + + case PH_ERROR_PROTOCOL: + return "protocol error (pack version unsupported)"; + + default: + return "unknown error in parse_pack_header"; + + case 0: + return NULL; + } +} + +static const char *pack_lockfile; + +static const char *unpack(void) +{ + struct pack_header hdr; + const char *hdr_err; + char hdr_arg[38]; + + hdr_err = parse_pack_header(&hdr); + if (hdr_err) + return hdr_err; + snprintf(hdr_arg, sizeof(hdr_arg), + "--pack_header=%"PRIu32",%"PRIu32, + ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries)); + + if (ntohl(hdr.hdr_entries) < unpack_limit) { + int code, i = 0; + const char *unpacker[4]; + unpacker[i++] = "unpack-objects"; + if (receive_fsck_objects) + unpacker[i++] = "--strict"; + unpacker[i++] = hdr_arg; + unpacker[i++] = NULL; + code = run_command_v_opt(unpacker, RUN_GIT_CMD); + switch (code) { + case 0: + return NULL; + case -ERR_RUN_COMMAND_FORK: + return "unpack fork failed"; + case -ERR_RUN_COMMAND_EXEC: + return "unpack execute failed"; + case -ERR_RUN_COMMAND_WAITPID: + return "waitpid failed"; + case -ERR_RUN_COMMAND_WAITPID_WRONG_PID: + return "waitpid is confused"; + case -ERR_RUN_COMMAND_WAITPID_SIGNAL: + return "unpacker died of signal"; + case -ERR_RUN_COMMAND_WAITPID_NOEXIT: + return "unpacker died strangely"; + default: + return "unpacker exited with error code"; + } + } else { + const char *keeper[7]; + int s, status, i = 0; + char keep_arg[256]; + struct child_process ip; + + s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid()); + if (gethostname(keep_arg + s, sizeof(keep_arg) - s)) + strcpy(keep_arg + s, "localhost"); + + keeper[i++] = "index-pack"; + keeper[i++] = "--stdin"; + if (receive_fsck_objects) + keeper[i++] = "--strict"; + keeper[i++] = "--fix-thin"; + keeper[i++] = hdr_arg; + keeper[i++] = keep_arg; + keeper[i++] = NULL; + memset(&ip, 0, sizeof(ip)); + ip.argv = keeper; + ip.out = -1; + ip.git_cmd = 1; + if (start_command(&ip)) + return "index-pack fork failed"; + pack_lockfile = index_pack_lockfile(ip.out); + close(ip.out); + status = finish_command(&ip); + if (!status) { + reprepare_packed_git(); + return NULL; + } + return "index-pack abnormal exit"; + } +} + +static void report(const char *unpack_status) +{ + struct command *cmd; + packet_write(1, "unpack %s\n", + unpack_status ? unpack_status : "ok"); + for (cmd = commands; cmd; cmd = cmd->next) { + if (!cmd->error_string) + packet_write(1, "ok %s\n", + cmd->ref_name); + else + packet_write(1, "ng %s %s\n", + cmd->ref_name, cmd->error_string); + } + packet_flush(1); +} + +static int delete_only(struct command *cmd) +{ + while (cmd) { + if (!is_null_sha1(cmd->new_sha1)) + return 0; + cmd = cmd->next; + } + return 1; +} + +int cmd_receive_pack(int argc, const char **argv, const char *prefix) +{ + int i; + char *dir = NULL; + + argv++; + for (i = 1; i < argc; i++) { + const char *arg = *argv++; + + if (*arg == '-') { + /* Do flag handling here */ + usage(receive_pack_usage); + } + if (dir) + usage(receive_pack_usage); + dir = xstrdup(arg); + } + if (!dir) + usage(receive_pack_usage); + + setup_path(); + + if (!enter_repo(dir, 0)) + die("'%s': unable to chdir or not a git archive", dir); + + if (is_repository_shallow()) + die("attempt to push into a shallow repository"); + + git_config(receive_pack_config, NULL); + + if (0 <= transfer_unpack_limit) + unpack_limit = transfer_unpack_limit; + else if (0 <= receive_unpack_limit) + unpack_limit = receive_unpack_limit; + + write_head_info(); + + /* EOF */ + packet_flush(1); + + read_head_info(); + if (commands) { + const char *unpack_status = NULL; + + if (!delete_only(commands)) + unpack_status = unpack(); + execute_commands(unpack_status); + if (pack_lockfile) + unlink(pack_lockfile); + if (report_status) + report(unpack_status); + run_hook(post_receive_hook); + run_update_post_hook(commands); + } + return 0; +} diff --git a/builtin.h b/builtin.h index e67cb20..5d7cdca 100644 --- a/builtin.h +++ b/builtin.h @@ -78,6 +78,7 @@ extern int cmd_prune(int argc, const char **argv, const char *prefix); extern int cmd_prune_packed(int argc, const char **argv, const char *prefix); extern int cmd_push(int argc, const char **argv, const char *prefix); extern int cmd_read_tree(int argc, const char **argv, const char *prefix); +extern int cmd_receive_pack(int argc, const char **argv, const char *prefix); extern int cmd_reflog(int argc, const char **argv, const char *prefix); extern int cmd_remote(int argc, const char **argv, const char *prefix); extern int cmd_config(int argc, const char **argv, const char *prefix); diff --git a/git.c b/git.c index adf7352..2f5b4d7 100644 --- a/git.c +++ b/git.c @@ -328,6 +328,7 @@ static void handle_internal_command(int argc, const char **argv) { "prune-packed", cmd_prune_packed, RUN_SETUP }, { "push", cmd_push, RUN_SETUP }, { "read-tree", cmd_read_tree, RUN_SETUP }, + { "receive-pack", cmd_receive_pack }, { "reflog", cmd_reflog, RUN_SETUP }, { "remote", cmd_remote, RUN_SETUP }, { "repo-config", cmd_config }, diff --git a/receive-pack.c b/receive-pack.c deleted file mode 100644 index b81678a..0000000 --- a/receive-pack.c +++ /dev/null @@ -1,520 +0,0 @@ -#include "cache.h" -#include "pack.h" -#include "refs.h" -#include "pkt-line.h" -#include "run-command.h" -#include "exec_cmd.h" -#include "commit.h" -#include "object.h" - -static const char receive_pack_usage[] = "git-receive-pack "; - -static int deny_non_fast_forwards = 0; -static int receive_fsck_objects; -static int receive_unpack_limit = -1; -static int transfer_unpack_limit = -1; -static int unpack_limit = 100; -static int report_status; - -static char capabilities[] = " report-status delete-refs "; -static int capabilities_sent; - -static int receive_pack_config(const char *var, const char *value, void *cb) -{ - if (strcmp(var, "receive.denynonfastforwards") == 0) { - deny_non_fast_forwards = git_config_bool(var, value); - return 0; - } - - if (strcmp(var, "receive.unpacklimit") == 0) { - receive_unpack_limit = git_config_int(var, value); - return 0; - } - - if (strcmp(var, "transfer.unpacklimit") == 0) { - transfer_unpack_limit = git_config_int(var, value); - return 0; - } - - if (strcmp(var, "receive.fsckobjects") == 0) { - receive_fsck_objects = git_config_bool(var, value); - return 0; - } - - return git_default_config(var, value, cb); -} - -static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data) -{ - if (capabilities_sent) - packet_write(1, "%s %s\n", sha1_to_hex(sha1), path); - else - packet_write(1, "%s %s%c%s\n", - sha1_to_hex(sha1), path, 0, capabilities); - capabilities_sent = 1; - return 0; -} - -static void write_head_info(void) -{ - for_each_ref(show_ref, NULL); - if (!capabilities_sent) - show_ref("capabilities^{}", null_sha1, 0, NULL); - -} - -struct command { - struct command *next; - const char *error_string; - unsigned char old_sha1[20]; - unsigned char new_sha1[20]; - char ref_name[FLEX_ARRAY]; /* more */ -}; - -static struct command *commands; - -static const char pre_receive_hook[] = "hooks/pre-receive"; -static const char post_receive_hook[] = "hooks/post-receive"; - -static int hook_status(int code, const char *hook_name) -{ - switch (code) { - case 0: - return 0; - case -ERR_RUN_COMMAND_FORK: - return error("hook fork failed"); - case -ERR_RUN_COMMAND_EXEC: - return error("hook execute failed"); - case -ERR_RUN_COMMAND_PIPE: - return error("hook pipe failed"); - case -ERR_RUN_COMMAND_WAITPID: - return error("waitpid failed"); - case -ERR_RUN_COMMAND_WAITPID_WRONG_PID: - return error("waitpid is confused"); - case -ERR_RUN_COMMAND_WAITPID_SIGNAL: - return error("%s died of signal", hook_name); - case -ERR_RUN_COMMAND_WAITPID_NOEXIT: - return error("%s died strangely", hook_name); - default: - error("%s exited with error code %d", hook_name, -code); - return -code; - } -} - -static int run_hook(const char *hook_name) -{ - static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4]; - struct command *cmd; - struct child_process proc; - const char *argv[2]; - int have_input = 0, code; - - for (cmd = commands; !have_input && cmd; cmd = cmd->next) { - if (!cmd->error_string) - have_input = 1; - } - - if (!have_input || access(hook_name, X_OK) < 0) - return 0; - - argv[0] = hook_name; - argv[1] = NULL; - - memset(&proc, 0, sizeof(proc)); - proc.argv = argv; - proc.in = -1; - proc.stdout_to_stderr = 1; - - code = start_command(&proc); - if (code) - return hook_status(code, hook_name); - for (cmd = commands; cmd; cmd = cmd->next) { - if (!cmd->error_string) { - size_t n = snprintf(buf, sizeof(buf), "%s %s %s\n", - sha1_to_hex(cmd->old_sha1), - sha1_to_hex(cmd->new_sha1), - cmd->ref_name); - if (write_in_full(proc.in, buf, n) != n) - break; - } - } - close(proc.in); - return hook_status(finish_command(&proc), hook_name); -} - -static int run_update_hook(struct command *cmd) -{ - static const char update_hook[] = "hooks/update"; - struct child_process proc; - const char *argv[5]; - - if (access(update_hook, X_OK) < 0) - return 0; - - argv[0] = update_hook; - argv[1] = cmd->ref_name; - argv[2] = sha1_to_hex(cmd->old_sha1); - argv[3] = sha1_to_hex(cmd->new_sha1); - argv[4] = NULL; - - memset(&proc, 0, sizeof(proc)); - proc.argv = argv; - proc.no_stdin = 1; - proc.stdout_to_stderr = 1; - - return hook_status(run_command(&proc), update_hook); -} - -static const char *update(struct command *cmd) -{ - const char *name = cmd->ref_name; - unsigned char *old_sha1 = cmd->old_sha1; - unsigned char *new_sha1 = cmd->new_sha1; - struct ref_lock *lock; - - /* only refs/... are allowed */ - if (prefixcmp(name, "refs/") || check_ref_format(name + 5)) { - error("refusing to create funny ref '%s' remotely", name); - return "funny refname"; - } - - if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) { - error("unpack should have generated %s, " - "but I can't find it!", sha1_to_hex(new_sha1)); - return "bad pack"; - } - if (deny_non_fast_forwards && !is_null_sha1(new_sha1) && - !is_null_sha1(old_sha1) && - !prefixcmp(name, "refs/heads/")) { - struct object *old_object, *new_object; - struct commit *old_commit, *new_commit; - struct commit_list *bases, *ent; - - old_object = parse_object(old_sha1); - new_object = parse_object(new_sha1); - - if (!old_object || !new_object || - old_object->type != OBJ_COMMIT || - new_object->type != OBJ_COMMIT) { - error("bad sha1 objects for %s", name); - return "bad ref"; - } - old_commit = (struct commit *)old_object; - new_commit = (struct commit *)new_object; - bases = get_merge_bases(old_commit, new_commit, 1); - for (ent = bases; ent; ent = ent->next) - if (!hashcmp(old_sha1, ent->item->object.sha1)) - break; - free_commit_list(bases); - if (!ent) { - error("denying non-fast forward %s" - " (you should pull first)", name); - return "non-fast forward"; - } - } - if (run_update_hook(cmd)) { - error("hook declined to update %s", name); - return "hook declined"; - } - - if (is_null_sha1(new_sha1)) { - if (!parse_object(old_sha1)) { - warning ("Allowing deletion of corrupt ref."); - old_sha1 = NULL; - } - if (delete_ref(name, old_sha1)) { - error("failed to delete %s", name); - return "failed to delete"; - } - return NULL; /* good */ - } - else { - lock = lock_any_ref_for_update(name, old_sha1, 0); - if (!lock) { - error("failed to lock %s", name); - return "failed to lock"; - } - if (write_ref_sha1(lock, new_sha1, "push")) { - return "failed to write"; /* error() already called */ - } - return NULL; /* good */ - } -} - -static char update_post_hook[] = "hooks/post-update"; - -static void run_update_post_hook(struct command *cmd) -{ - struct command *cmd_p; - int argc; - const char **argv; - - for (argc = 0, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) { - if (cmd_p->error_string) - continue; - argc++; - } - if (!argc || access(update_post_hook, X_OK) < 0) - return; - argv = xmalloc(sizeof(*argv) * (2 + argc)); - argv[0] = update_post_hook; - - for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) { - char *p; - if (cmd_p->error_string) - continue; - p = xmalloc(strlen(cmd_p->ref_name) + 1); - strcpy(p, cmd_p->ref_name); - argv[argc] = p; - argc++; - } - argv[argc] = NULL; - run_command_v_opt(argv, RUN_COMMAND_NO_STDIN - | RUN_COMMAND_STDOUT_TO_STDERR); -} - -static void execute_commands(const char *unpacker_error) -{ - struct command *cmd = commands; - - if (unpacker_error) { - while (cmd) { - cmd->error_string = "n/a (unpacker error)"; - cmd = cmd->next; - } - return; - } - - if (run_hook(pre_receive_hook)) { - while (cmd) { - cmd->error_string = "pre-receive hook declined"; - cmd = cmd->next; - } - return; - } - - while (cmd) { - cmd->error_string = update(cmd); - cmd = cmd->next; - } -} - -static void read_head_info(void) -{ - struct command **p = &commands; - for (;;) { - static char line[1000]; - unsigned char old_sha1[20], new_sha1[20]; - struct command *cmd; - char *refname; - int len, reflen; - - len = packet_read_line(0, line, sizeof(line)); - if (!len) - break; - if (line[len-1] == '\n') - line[--len] = 0; - if (len < 83 || - line[40] != ' ' || - line[81] != ' ' || - get_sha1_hex(line, old_sha1) || - get_sha1_hex(line + 41, new_sha1)) - die("protocol error: expected old/new/ref, got '%s'", - line); - - refname = line + 82; - reflen = strlen(refname); - if (reflen + 82 < len) { - if (strstr(refname + reflen + 1, "report-status")) - report_status = 1; - } - cmd = xmalloc(sizeof(struct command) + len - 80); - hashcpy(cmd->old_sha1, old_sha1); - hashcpy(cmd->new_sha1, new_sha1); - memcpy(cmd->ref_name, line + 82, len - 81); - cmd->error_string = NULL; - cmd->next = NULL; - *p = cmd; - p = &cmd->next; - } -} - -static const char *parse_pack_header(struct pack_header *hdr) -{ - switch (read_pack_header(0, hdr)) { - case PH_ERROR_EOF: - return "eof before pack header was fully read"; - - case PH_ERROR_PACK_SIGNATURE: - return "protocol error (pack signature mismatch detected)"; - - case PH_ERROR_PROTOCOL: - return "protocol error (pack version unsupported)"; - - default: - return "unknown error in parse_pack_header"; - - case 0: - return NULL; - } -} - -static const char *pack_lockfile; - -static const char *unpack(void) -{ - struct pack_header hdr; - const char *hdr_err; - char hdr_arg[38]; - - hdr_err = parse_pack_header(&hdr); - if (hdr_err) - return hdr_err; - snprintf(hdr_arg, sizeof(hdr_arg), - "--pack_header=%"PRIu32",%"PRIu32, - ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries)); - - if (ntohl(hdr.hdr_entries) < unpack_limit) { - int code, i = 0; - const char *unpacker[4]; - unpacker[i++] = "unpack-objects"; - if (receive_fsck_objects) - unpacker[i++] = "--strict"; - unpacker[i++] = hdr_arg; - unpacker[i++] = NULL; - code = run_command_v_opt(unpacker, RUN_GIT_CMD); - switch (code) { - case 0: - return NULL; - case -ERR_RUN_COMMAND_FORK: - return "unpack fork failed"; - case -ERR_RUN_COMMAND_EXEC: - return "unpack execute failed"; - case -ERR_RUN_COMMAND_WAITPID: - return "waitpid failed"; - case -ERR_RUN_COMMAND_WAITPID_WRONG_PID: - return "waitpid is confused"; - case -ERR_RUN_COMMAND_WAITPID_SIGNAL: - return "unpacker died of signal"; - case -ERR_RUN_COMMAND_WAITPID_NOEXIT: - return "unpacker died strangely"; - default: - return "unpacker exited with error code"; - } - } else { - const char *keeper[7]; - int s, status, i = 0; - char keep_arg[256]; - struct child_process ip; - - s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid()); - if (gethostname(keep_arg + s, sizeof(keep_arg) - s)) - strcpy(keep_arg + s, "localhost"); - - keeper[i++] = "index-pack"; - keeper[i++] = "--stdin"; - if (receive_fsck_objects) - keeper[i++] = "--strict"; - keeper[i++] = "--fix-thin"; - keeper[i++] = hdr_arg; - keeper[i++] = keep_arg; - keeper[i++] = NULL; - memset(&ip, 0, sizeof(ip)); - ip.argv = keeper; - ip.out = -1; - ip.git_cmd = 1; - if (start_command(&ip)) - return "index-pack fork failed"; - pack_lockfile = index_pack_lockfile(ip.out); - close(ip.out); - status = finish_command(&ip); - if (!status) { - reprepare_packed_git(); - return NULL; - } - return "index-pack abnormal exit"; - } -} - -static void report(const char *unpack_status) -{ - struct command *cmd; - packet_write(1, "unpack %s\n", - unpack_status ? unpack_status : "ok"); - for (cmd = commands; cmd; cmd = cmd->next) { - if (!cmd->error_string) - packet_write(1, "ok %s\n", - cmd->ref_name); - else - packet_write(1, "ng %s %s\n", - cmd->ref_name, cmd->error_string); - } - packet_flush(1); -} - -static int delete_only(struct command *cmd) -{ - while (cmd) { - if (!is_null_sha1(cmd->new_sha1)) - return 0; - cmd = cmd->next; - } - return 1; -} - -int main(int argc, char **argv) -{ - int i; - char *dir = NULL; - - argv++; - for (i = 1; i < argc; i++) { - char *arg = *argv++; - - if (*arg == '-') { - /* Do flag handling here */ - usage(receive_pack_usage); - } - if (dir) - usage(receive_pack_usage); - dir = arg; - } - if (!dir) - usage(receive_pack_usage); - - setup_path(); - - if (!enter_repo(dir, 0)) - die("'%s': unable to chdir or not a git archive", dir); - - if (is_repository_shallow()) - die("attempt to push into a shallow repository"); - - git_config(receive_pack_config, NULL); - - if (0 <= transfer_unpack_limit) - unpack_limit = transfer_unpack_limit; - else if (0 <= receive_unpack_limit) - unpack_limit = receive_unpack_limit; - - write_head_info(); - - /* EOF */ - packet_flush(1); - - read_head_info(); - if (commands) { - const char *unpack_status = NULL; - - if (!delete_only(commands)) - unpack_status = unpack(); - execute_commands(unpack_status); - if (pack_lockfile) - unlink(pack_lockfile); - if (report_status) - report(unpack_status); - run_hook(post_receive_hook); - run_update_post_hook(commands); - } - return 0; -} -- cgit v0.10.2-6-g49f6 From 40c155ff14c8b313d408f2e51a55c881ce082e4e Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 9 Sep 2008 01:27:09 -0700 Subject: push: prepare sender to receive extended ref information from the receiver "git push" enhancement allows the receiving end to report not only its own refs but refs in repositories it borrows from via the alternate object store mechanism. By telling the sender that objects reachable from these extra refs are already complete in the receiving end, the number of objects that need to be transfered can be cut down. These entries are sent over the wire with string ".have", instead of the actual names of the refs. This string was chosen so that they are ignored by older programs at the sending end. If we sent some random but valid looking refnames for these entries, "matching refs" rule (triggered when running "git push" without explicit refspecs, where the sender learns what refs the receiver has, and updates only the ones with the names of the refs the sender also has) and "delete missing" rule (triggered when "git push --mirror" is used, where the sender tells the receiver to delete the refs it itself does not have) would try to update/delete them, which is not what we want. This prepares the send-pack (and "push" that runs native protocol) to accept extended existing ref information and make use of it. The ".have" entries are excluded from ref matching rules, and are exempt from deletion rule while pushing with --mirror option, but are still used for pack generation purposes by providing more "bottom" range commits. Signed-off-by: Junio C Hamano diff --git a/builtin-fetch-pack.c b/builtin-fetch-pack.c index 459c6f0..e0c2561 100644 --- a/builtin-fetch-pack.c +++ b/builtin-fetch-pack.c @@ -735,7 +735,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix) conn = git_connect(fd, (char *)dest, args.uploadpack, args.verbose ? CONNECT_VERBOSE : 0); if (conn) { - get_remote_heads(fd[0], &ref, 0, NULL, 0); + get_remote_heads(fd[0], &ref, 0, NULL, 0, NULL); ref = fetch_pack(&args, fd, conn, ref, dest, nr_heads, heads, NULL); close(fd[0]); diff --git a/builtin-send-pack.c b/builtin-send-pack.c index 7588d22..b3c22f6 100644 --- a/builtin-send-pack.c +++ b/builtin-send-pack.c @@ -18,7 +18,7 @@ static struct send_pack_args args = { /* * Make a pack stream and spit it out into file descriptor fd */ -static int pack_objects(int fd, struct ref *refs) +static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra) { /* * The child becomes pack-objects --revs; we feed @@ -34,6 +34,8 @@ static int pack_objects(int fd, struct ref *refs) NULL, }; struct child_process po; + int i; + char buf[42]; if (args.use_thin_pack) argv[4] = "--thin"; @@ -49,9 +51,15 @@ static int pack_objects(int fd, struct ref *refs) * We feed the pack-objects we just spawned with revision * parameters by writing to the pipe. */ - while (refs) { - char buf[42]; + for (i = 0; i < extra->nr; i++) { + memcpy(buf + 1, sha1_to_hex(&extra->array[i][0]), 40); + buf[0] = '^'; + buf[41] = '\n'; + if (!write_or_whine(po.in, buf, 42, "send-pack: send refs")) + break; + } + while (refs) { if (!is_null_sha1(refs->old_sha1) && has_sha1_file(refs->old_sha1)) { memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40); @@ -381,14 +389,17 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest int expect_status_report = 0; int flags = MATCH_REFS_NONE; int ret; + struct extra_have_objects extra_have; + memset(&extra_have, 0, sizeof(extra_have)); if (args.send_all) flags |= MATCH_REFS_ALL; if (args.send_mirror) flags |= MATCH_REFS_MIRROR; /* No funny business with the matcher */ - remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL); + remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL, + &extra_have); get_local_heads(); /* Does the other end support the reporting? */ @@ -496,7 +507,7 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest packet_flush(out); if (new_refs && !args.dry_run) { - if (pack_objects(out, remote_refs) < 0) + if (pack_objects(out, remote_refs, &extra_have) < 0) return -1; } else diff --git a/cache.h b/cache.h index ce1f630..98a7421 100644 --- a/cache.h +++ b/cache.h @@ -709,7 +709,11 @@ extern struct child_process *git_connect(int fd[2], const char *url, const char extern int finish_connect(struct child_process *conn); extern int path_match(const char *path, int nr, char **match); extern int get_ack(int fd, unsigned char *result_sha1); -extern struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match, unsigned int flags); +struct extra_have_objects { + int nr, alloc; + unsigned char (*array)[20]; +}; +extern struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match, unsigned int flags, struct extra_have_objects *); extern int server_supports(const char *feature); extern struct packed_git *parse_pack_index(unsigned char *sha1); diff --git a/connect.c b/connect.c index dd96f8e..67d2cd8 100644 --- a/connect.c +++ b/connect.c @@ -41,12 +41,20 @@ int check_ref_type(const struct ref *ref, int flags) return check_ref(ref->name, strlen(ref->name), flags); } +static void add_extra_have(struct extra_have_objects *extra, unsigned char *sha1) +{ + ALLOC_GROW(extra->array, extra->nr + 1, extra->alloc); + hashcpy(&(extra->array[extra->nr][0]), sha1); + extra->nr++; +} + /* * Read all the refs from the other end */ struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match, - unsigned int flags) + unsigned int flags, + struct extra_have_objects *extra_have) { *list = NULL; for (;;) { @@ -72,6 +80,12 @@ struct ref **get_remote_heads(int in, struct ref **list, server_capabilities = xstrdup(name + name_len + 1); } + if (extra_have && + name_len == 5 && !memcmp(".have", name, 5)) { + add_extra_have(extra_have, old_sha1); + continue; + } + if (!check_ref(name, name_len, flags)) continue; if (nr_match && !path_match(name, nr_match, match)) diff --git a/transport.c b/transport.c index 71433d9..f7db5d9 100644 --- a/transport.c +++ b/transport.c @@ -619,7 +619,7 @@ static struct ref *get_refs_via_connect(struct transport *transport) struct ref *refs; connect_setup(transport); - get_remote_heads(data->fd[0], &refs, 0, NULL, 0); + get_remote_heads(data->fd[0], &refs, 0, NULL, 0, NULL); return refs; } @@ -652,7 +652,7 @@ static int fetch_refs_via_pack(struct transport *transport, if (!data->conn) { connect_setup(transport); - get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0); + get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0, NULL); } refs = fetch_pack(&args, data->fd, data->conn, -- cgit v0.10.2-6-g49f6 From d79796bcf05b89774671a75b3285000c43129823 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 9 Sep 2008 01:27:10 -0700 Subject: push: receiver end advertises refs from alternate repositories Earlier, when pushing into a repository that borrows from alternate object stores, we followed the longstanding design decision not to trust refs in the alternate repository that houses the object store we are borrowing from. If your public repository is borrowing from Linus's public repository, you pushed into it long time ago, and now when you try to push your updated history that is in sync with more recent history from Linus, you will end up sending not just your own development, but also the changes you acquired through Linus's tree, even though the objects needed for the latter already exists at the receiving end. This is because the receiving end does not advertise that the objects only reachable from the borrowed repository (i.e. Linus's) are already available there. This solves the issue by making the receiving end advertise refs from borrowed repositories. They are not sent with their true names but with a phoney name ".have" to make sure that the old senders will safely ignore them (otherwise, the old senders will misbehave, trying to push matching refs, and mirror push that deletes refs that only exist at the receiving end). Signed-off-by: Junio C Hamano diff --git a/builtin-receive-pack.c b/builtin-receive-pack.c index 6d6027e..45e3cd9 100644 --- a/builtin-receive-pack.c +++ b/builtin-receive-pack.c @@ -6,6 +6,8 @@ #include "exec_cmd.h" #include "commit.h" #include "object.h" +#include "remote.h" +#include "transport.h" static const char receive_pack_usage[] = "git-receive-pack "; @@ -462,6 +464,40 @@ static int delete_only(struct command *cmd) return 1; } +static int add_refs_from_alternate(struct alternate_object_database *e, void *unused) +{ + char *other = xstrdup(make_absolute_path(e->base)); + size_t len = strlen(other); + struct remote *remote; + struct transport *transport; + const struct ref *extra; + + while (other[len-1] == '/') + other[--len] = '\0'; + if (len < 8 || memcmp(other + len - 8, "/objects", 8)) + return 0; + /* Is this a git repository with refs? */ + memcpy(other + len - 8, "/refs", 6); + if (!is_directory(other)) + return 0; + other[len - 8] = '\0'; + remote = remote_get(other); + transport = transport_get(remote, other); + for (extra = transport_get_remote_refs(transport); + extra; + extra = extra->next) { + add_extra_ref(".have", extra->old_sha1, 0); + } + transport_disconnect(transport); + free(other); + return 0; +} + +static void add_alternate_refs(void) +{ + foreach_alt_odb(add_refs_from_alternate, NULL); +} + int cmd_receive_pack(int argc, const char **argv, const char *prefix) { int i; @@ -497,7 +533,9 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix) else if (0 <= receive_unpack_limit) unpack_limit = receive_unpack_limit; + add_alternate_refs(); write_head_info(); + clear_extra_refs(); /* EOF */ packet_flush(1); diff --git a/cache.h b/cache.h index 98a7421..99af83a 100644 --- a/cache.h +++ b/cache.h @@ -641,6 +641,8 @@ extern struct alternate_object_database { } *alt_odb_list; extern void prepare_alt_odb(void); extern void add_to_alternates_file(const char *reference); +typedef int alt_odb_fn(struct alternate_object_database *, void *); +extern void foreach_alt_odb(alt_odb_fn, void*); struct pack_window { struct pack_window *next; diff --git a/sha1_file.c b/sha1_file.c index ae550e8..12be17b 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -393,6 +393,16 @@ void add_to_alternates_file(const char *reference) link_alt_odb_entries(alt, alt + strlen(alt), '\n', NULL, 0); } +void foreach_alt_odb(alt_odb_fn fn, void *cb) +{ + struct alternate_object_database *ent; + + prepare_alt_odb(); + for (ent = alt_odb_list; ent; ent = ent->next) + if (fn(ent, cb)) + return; +} + void prepare_alt_odb(void) { const char *alt; -- cgit v0.10.2-6-g49f6