You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
855 lines
21 KiB
855 lines
21 KiB
20 years ago
|
#include "cache.h"
|
||
18 years ago
|
#include "pack.h"
|
||
20 years ago
|
#include "refs.h"
|
||
20 years ago
|
#include "pkt-line.h"
|
||
15 years ago
|
#include "sideband.h"
|
||
20 years ago
|
#include "run-command.h"
|
||
18 years ago
|
#include "exec_cmd.h"
|
||
19 years ago
|
#include "commit.h"
|
||
|
#include "object.h"
|
||
17 years ago
|
#include "remote.h"
|
||
|
#include "transport.h"
|
||
15 years ago
|
#include "string-list.h"
|
||
20 years ago
|
|
||
16 years ago
|
static const char receive_pack_usage[] = "git receive-pack <git-dir>";
|
||
20 years ago
|
|
||
16 years ago
|
enum deny_action {
|
||
16 years ago
|
DENY_UNCONFIGURED,
|
||
16 years ago
|
DENY_IGNORE,
|
||
|
DENY_WARN,
|
||
|
DENY_REFUSE,
|
||
|
};
|
||
|
|
||
16 years ago
|
static int deny_deletes;
|
||
|
static int deny_non_fast_forwards;
|
||
16 years ago
|
static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
|
||
16 years ago
|
static enum deny_action deny_delete_current = DENY_UNCONFIGURED;
|
||
17 years ago
|
static int receive_fsck_objects;
|
||
18 years ago
|
static int receive_unpack_limit = -1;
|
||
|
static int transfer_unpack_limit = -1;
|
||
18 years ago
|
static int unpack_limit = 100;
|
||
19 years ago
|
static int report_status;
|
||
15 years ago
|
static int use_sideband;
|
||
16 years ago
|
static int prefer_ofs_delta = 1;
|
||
15 years ago
|
static int auto_update_server_info;
|
||
|
static int auto_gc = 1;
|
||
16 years ago
|
static const char *head_name;
|
||
15 years ago
|
static int sent_capabilities;
|
||
19 years ago
|
|
||
16 years ago
|
static enum deny_action parse_deny_action(const char *var, const char *value)
|
||
|
{
|
||
|
if (value) {
|
||
|
if (!strcasecmp(value, "ignore"))
|
||
|
return DENY_IGNORE;
|
||
|
if (!strcasecmp(value, "warn"))
|
||
|
return DENY_WARN;
|
||
|
if (!strcasecmp(value, "refuse"))
|
||
|
return DENY_REFUSE;
|
||
|
}
|
||
|
if (git_config_bool(var, value))
|
||
|
return DENY_REFUSE;
|
||
|
return DENY_IGNORE;
|
||
|
}
|
||
|
|
||
17 years ago
|
static int receive_pack_config(const char *var, const char *value, void *cb)
|
||
18 years ago
|
{
|
||
16 years ago
|
if (strcmp(var, "receive.denydeletes") == 0) {
|
||
|
deny_deletes = git_config_bool(var, value);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
18 years ago
|
if (strcmp(var, "receive.denynonfastforwards") == 0) {
|
||
18 years ago
|
deny_non_fast_forwards = git_config_bool(var, value);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
18 years ago
|
if (strcmp(var, "receive.unpacklimit") == 0) {
|
||
|
receive_unpack_limit = git_config_int(var, value);
|
||
18 years ago
|
return 0;
|
||
|
}
|
||
|
|
||
18 years ago
|
if (strcmp(var, "transfer.unpacklimit") == 0) {
|
||
|
transfer_unpack_limit = git_config_int(var, value);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
17 years ago
|
if (strcmp(var, "receive.fsckobjects") == 0) {
|
||
|
receive_fsck_objects = git_config_bool(var, value);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
16 years ago
|
if (!strcmp(var, "receive.denycurrentbranch")) {
|
||
|
deny_current_branch = parse_deny_action(var, value);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
16 years ago
|
if (strcmp(var, "receive.denydeletecurrent") == 0) {
|
||
|
deny_delete_current = parse_deny_action(var, value);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
16 years ago
|
if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
|
||
|
prefer_ofs_delta = git_config_bool(var, value);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
15 years ago
|
if (strcmp(var, "receive.updateserverinfo") == 0) {
|
||
|
auto_update_server_info = git_config_bool(var, value);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
if (strcmp(var, "receive.autogc") == 0) {
|
||
|
auto_gc = git_config_bool(var, value);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
17 years ago
|
return git_default_config(var, value, cb);
|
||
18 years ago
|
}
|
||
|
|
||
19 years ago
|
static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
|
||
20 years ago
|
{
|
||
15 years ago
|
if (sent_capabilities)
|
||
19 years ago
|
packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
|
||
|
else
|
||
15 years ago
|
packet_write(1, "%s %s%c%s%s\n",
|
||
|
sha1_to_hex(sha1), path, 0,
|
||
15 years ago
|
" report-status delete-refs side-band-64k",
|
||
15 years ago
|
prefer_ofs_delta ? " ofs-delta" : "");
|
||
|
sent_capabilities = 1;
|
||
20 years ago
|
return 0;
|
||
20 years ago
|
}
|
||
|
|
||
20 years ago
|
static void write_head_info(void)
|
||
20 years ago
|
{
|
||
19 years ago
|
for_each_ref(show_ref, NULL);
|
||
15 years ago
|
if (!sent_capabilities)
|
||
19 years ago
|
show_ref("capabilities^{}", null_sha1, 0, NULL);
|
||
19 years ago
|
|
||
20 years ago
|
}
|
||
|
|
||
20 years ago
|
struct command {
|
||
|
struct command *next;
|
||
19 years ago
|
const char *error_string;
|
||
15 years ago
|
unsigned int skip_update;
|
||
20 years ago
|
unsigned char old_sha1[20];
|
||
|
unsigned char new_sha1[20];
|
||
19 years ago
|
char ref_name[FLEX_ARRAY]; /* more */
|
||
20 years ago
|
};
|
||
|
|
||
18 years ago
|
static const char pre_receive_hook[] = "hooks/pre-receive";
|
||
|
static const char post_receive_hook[] = "hooks/post-receive";
|
||
20 years ago
|
|
||
15 years ago
|
static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
|
||
|
static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
|
||
|
|
||
|
static void report_message(const char *prefix, const char *err, va_list params)
|
||
|
{
|
||
|
int sz = strlen(prefix);
|
||
|
char msg[4096];
|
||
|
|
||
|
strncpy(msg, prefix, sz);
|
||
|
sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
|
||
|
if (sz > (sizeof(msg) - 1))
|
||
|
sz = sizeof(msg) - 1;
|
||
|
msg[sz++] = '\n';
|
||
|
|
||
|
if (use_sideband)
|
||
|
send_sideband(1, 2, msg, sz, use_sideband);
|
||
|
else
|
||
|
xwrite(2, msg, sz);
|
||
|
}
|
||
|
|
||
|
static void rp_warning(const char *err, ...)
|
||
|
{
|
||
|
va_list params;
|
||
|
va_start(params, err);
|
||
|
report_message("warning: ", err, params);
|
||
|
va_end(params);
|
||
|
}
|
||
|
|
||
|
static void rp_error(const char *err, ...)
|
||
|
{
|
||
|
va_list params;
|
||
|
va_start(params, err);
|
||
|
report_message("error: ", err, params);
|
||
|
va_end(params);
|
||
|
}
|
||
|
|
||
15 years ago
|
static int copy_to_sideband(int in, int out, void *arg)
|
||
|
{
|
||
|
char data[128];
|
||
|
while (1) {
|
||
|
ssize_t sz = xread(in, data, sizeof(data));
|
||
|
if (sz <= 0)
|
||
|
break;
|
||
|
send_sideband(1, 2, data, sz, use_sideband);
|
||
|
}
|
||
|
close(in);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
15 years ago
|
static int run_receive_hook(struct command *commands, const char *hook_name)
|
||
20 years ago
|
{
|
||
18 years ago
|
static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
|
||
18 years ago
|
struct command *cmd;
|
||
18 years ago
|
struct child_process proc;
|
||
15 years ago
|
struct async muxer;
|
||
18 years ago
|
const char *argv[2];
|
||
|
int have_input = 0, code;
|
||
18 years ago
|
|
||
18 years ago
|
for (cmd = commands; !have_input && cmd; cmd = cmd->next) {
|
||
18 years ago
|
if (!cmd->error_string)
|
||
18 years ago
|
have_input = 1;
|
||
18 years ago
|
}
|
||
20 years ago
|
|
||
18 years ago
|
if (!have_input || access(hook_name, X_OK) < 0)
|
||
20 years ago
|
return 0;
|
||
18 years ago
|
|
||
|
argv[0] = hook_name;
|
||
18 years ago
|
argv[1] = NULL;
|
||
|
|
||
|
memset(&proc, 0, sizeof(proc));
|
||
|
proc.argv = argv;
|
||
|
proc.in = -1;
|
||
|
proc.stdout_to_stderr = 1;
|
||
|
|
||
15 years ago
|
if (use_sideband) {
|
||
|
memset(&muxer, 0, sizeof(muxer));
|
||
|
muxer.proc = copy_to_sideband;
|
||
|
muxer.in = -1;
|
||
|
code = start_async(&muxer);
|
||
|
if (code)
|
||
|
return code;
|
||
|
proc.err = muxer.in;
|
||
|
}
|
||
|
|
||
18 years ago
|
code = start_command(&proc);
|
||
15 years ago
|
if (code) {
|
||
|
if (use_sideband)
|
||
|
finish_async(&muxer);
|
||
16 years ago
|
return code;
|
||
15 years ago
|
}
|
||
|
|
||
18 years ago
|
for (cmd = commands; cmd; cmd = cmd->next) {
|
||
18 years ago
|
if (!cmd->error_string) {
|
||
18 years ago
|
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;
|
||
18 years ago
|
}
|
||
|
}
|
||
17 years ago
|
close(proc.in);
|
||
15 years ago
|
if (use_sideband)
|
||
|
finish_async(&muxer);
|
||
16 years ago
|
return finish_command(&proc);
|
||
20 years ago
|
}
|
||
|
|
||
18 years ago
|
static int run_update_hook(struct command *cmd)
|
||
|
{
|
||
|
static const char update_hook[] = "hooks/update";
|
||
|
const char *argv[5];
|
||
15 years ago
|
struct child_process proc;
|
||
|
int code;
|
||
18 years ago
|
|
||
|
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;
|
||
|
|
||
15 years ago
|
memset(&proc, 0, sizeof(proc));
|
||
|
proc.no_stdin = 1;
|
||
|
proc.stdout_to_stderr = 1;
|
||
|
proc.err = use_sideband ? -1 : 0;
|
||
|
proc.argv = argv;
|
||
|
|
||
|
code = start_command(&proc);
|
||
|
if (code)
|
||
|
return code;
|
||
|
if (use_sideband)
|
||
|
copy_to_sideband(proc.err, -1, NULL);
|
||
|
return finish_command(&proc);
|
||
18 years ago
|
}
|
||
|
|
||
16 years ago
|
static int is_ref_checked_out(const char *ref)
|
||
|
{
|
||
|
if (is_bare_repository())
|
||
|
return 0;
|
||
|
|
||
16 years ago
|
if (!head_name)
|
||
16 years ago
|
return 0;
|
||
16 years ago
|
return !strcmp(head_name, ref);
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
static char *refuse_unconfigured_deny_msg[] = {
|
||
|
"By default, updating the current branch in a non-bare repository",
|
||
|
"is denied, because it will make the index and work tree inconsistent",
|
||
|
"with what you pushed, and will require 'git reset --hard' to match",
|
||
|
"the work tree to HEAD.",
|
||
16 years ago
|
"",
|
||
|
"You can set 'receive.denyCurrentBranch' configuration variable to",
|
||
16 years ago
|
"'ignore' or 'warn' in the remote repository to allow pushing into",
|
||
|
"its current branch; however, this is not recommended unless you",
|
||
|
"arranged to update its work tree to match what you pushed in some",
|
||
|
"other way.",
|
||
16 years ago
|
"",
|
||
16 years ago
|
"To squelch this message and still keep the default behaviour, set",
|
||
|
"'receive.denyCurrentBranch' configuration variable to 'refuse'."
|
||
16 years ago
|
};
|
||
|
|
||
16 years ago
|
static void refuse_unconfigured_deny(void)
|
||
16 years ago
|
{
|
||
|
int i;
|
||
16 years ago
|
for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
|
||
15 years ago
|
rp_error("%s", refuse_unconfigured_deny_msg[i]);
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
static char *refuse_unconfigured_deny_delete_current_msg[] = {
|
||
|
"By default, deleting the current branch is denied, because the next",
|
||
|
"'git clone' won't result in any file checked out, causing confusion.",
|
||
16 years ago
|
"",
|
||
|
"You can set 'receive.denyDeleteCurrent' configuration variable to",
|
||
16 years ago
|
"'warn' or 'ignore' in the remote repository to allow deleting the",
|
||
|
"current branch, with or without a warning message.",
|
||
16 years ago
|
"",
|
||
16 years ago
|
"To squelch this message, you can set it to 'refuse'."
|
||
16 years ago
|
};
|
||
|
|
||
16 years ago
|
static void refuse_unconfigured_deny_delete_current(void)
|
||
16 years ago
|
{
|
||
|
int i;
|
||
|
for (i = 0;
|
||
16 years ago
|
i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg);
|
||
16 years ago
|
i++)
|
||
15 years ago
|
rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
|
||
16 years ago
|
}
|
||
|
|
||
18 years ago
|
static const char *update(struct command *cmd)
|
||
20 years ago
|
{
|
||
19 years ago
|
const char *name = cmd->ref_name;
|
||
|
unsigned char *old_sha1 = cmd->old_sha1;
|
||
|
unsigned char *new_sha1 = cmd->new_sha1;
|
||
19 years ago
|
struct ref_lock *lock;
|
||
20 years ago
|
|
||
17 years ago
|
/* only refs/... are allowed */
|
||
|
if (prefixcmp(name, "refs/") || check_ref_format(name + 5)) {
|
||
15 years ago
|
rp_error("refusing to create funny ref '%s' remotely", name);
|
||
18 years ago
|
return "funny refname";
|
||
19 years ago
|
}
|
||
19 years ago
|
|
||
16 years ago
|
if (is_ref_checked_out(name)) {
|
||
|
switch (deny_current_branch) {
|
||
|
case DENY_IGNORE:
|
||
16 years ago
|
break;
|
||
16 years ago
|
case DENY_WARN:
|
||
15 years ago
|
rp_warning("updating the current branch");
|
||
16 years ago
|
break;
|
||
16 years ago
|
case DENY_REFUSE:
|
||
16 years ago
|
case DENY_UNCONFIGURED:
|
||
15 years ago
|
rp_error("refusing to update checked out branch: %s", name);
|
||
16 years ago
|
if (deny_current_branch == DENY_UNCONFIGURED)
|
||
|
refuse_unconfigured_deny();
|
||
16 years ago
|
return "branch is currently checked out";
|
||
|
}
|
||
16 years ago
|
}
|
||
|
|
||
18 years ago
|
if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
|
||
18 years ago
|
error("unpack should have generated %s, "
|
||
|
"but I can't find it!", sha1_to_hex(new_sha1));
|
||
|
return "bad pack";
|
||
19 years ago
|
}
|
||
16 years ago
|
|
||
|
if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
|
||
|
if (deny_deletes && !prefixcmp(name, "refs/heads/")) {
|
||
15 years ago
|
rp_error("denying ref deletion for %s", name);
|
||
16 years ago
|
return "deletion prohibited";
|
||
|
}
|
||
|
|
||
|
if (!strcmp(name, head_name)) {
|
||
|
switch (deny_delete_current) {
|
||
|
case DENY_IGNORE:
|
||
|
break;
|
||
|
case DENY_WARN:
|
||
15 years ago
|
rp_warning("deleting the current branch");
|
||
16 years ago
|
break;
|
||
|
case DENY_REFUSE:
|
||
16 years ago
|
case DENY_UNCONFIGURED:
|
||
|
if (deny_delete_current == DENY_UNCONFIGURED)
|
||
|
refuse_unconfigured_deny_delete_current();
|
||
15 years ago
|
rp_error("refusing to delete the current branch: %s", name);
|
||
16 years ago
|
return "deletion of the current branch prohibited";
|
||
|
}
|
||
|
}
|
||
16 years ago
|
}
|
||
16 years ago
|
|
||
18 years ago
|
if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
|
||
18 years ago
|
!is_null_sha1(old_sha1) &&
|
||
18 years ago
|
!prefixcmp(name, "refs/heads/")) {
|
||
17 years ago
|
struct object *old_object, *new_object;
|
||
19 years ago
|
struct commit *old_commit, *new_commit;
|
||
19 years ago
|
struct commit_list *bases, *ent;
|
||
19 years ago
|
|
||
17 years ago
|
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;
|
||
19 years ago
|
bases = get_merge_bases(old_commit, new_commit, 1);
|
||
|
for (ent = bases; ent; ent = ent->next)
|
||
|
if (!hashcmp(old_sha1, ent->item->object.sha1))
|
||
19 years ago
|
break;
|
||
19 years ago
|
free_commit_list(bases);
|
||
18 years ago
|
if (!ent) {
|
||
15 years ago
|
rp_error("denying non-fast-forward %s"
|
||
|
" (you should pull first)", name);
|
||
15 years ago
|
return "non-fast-forward";
|
||
18 years ago
|
}
|
||
19 years ago
|
}
|
||
18 years ago
|
if (run_update_hook(cmd)) {
|
||
15 years ago
|
rp_error("hook declined to update %s", name);
|
||
18 years ago
|
return "hook declined";
|
||
20 years ago
|
}
|
||
19 years ago
|
|
||
18 years ago
|
if (is_null_sha1(new_sha1)) {
|
||
17 years ago
|
if (!parse_object(old_sha1)) {
|
||
15 years ago
|
rp_warning("Allowing deletion of corrupt ref.");
|
||
17 years ago
|
old_sha1 = NULL;
|
||
|
}
|
||
16 years ago
|
if (delete_ref(name, old_sha1, 0)) {
|
||
15 years ago
|
rp_error("failed to delete %s", name);
|
||
18 years ago
|
return "failed to delete";
|
||
18 years ago
|
}
|
||
18 years ago
|
return NULL; /* good */
|
||
18 years ago
|
}
|
||
|
else {
|
||
18 years ago
|
lock = lock_any_ref_for_update(name, old_sha1, 0);
|
||
18 years ago
|
if (!lock) {
|
||
15 years ago
|
rp_error("failed to lock %s", name);
|
||
18 years ago
|
return "failed to lock";
|
||
18 years ago
|
}
|
||
18 years ago
|
if (write_ref_sha1(lock, new_sha1, "push")) {
|
||
18 years ago
|
return "failed to write"; /* error() already called */
|
||
18 years ago
|
}
|
||
18 years ago
|
return NULL; /* good */
|
||
20 years ago
|
}
|
||
20 years ago
|
}
|
||
|
|
||
20 years ago
|
static char update_post_hook[] = "hooks/post-update";
|
||
|
|
||
15 years ago
|
static void run_update_post_hook(struct command *commands)
|
||
20 years ago
|
{
|
||
15 years ago
|
struct command *cmd;
|
||
15 years ago
|
int argc;
|
||
19 years ago
|
const char **argv;
|
||
15 years ago
|
struct child_process proc;
|
||
20 years ago
|
|
||
15 years ago
|
for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
|
||
|
if (cmd->error_string)
|
||
20 years ago
|
continue;
|
||
|
argc++;
|
||
|
}
|
||
18 years ago
|
if (!argc || access(update_post_hook, X_OK) < 0)
|
||
|
return;
|
||
|
argv = xmalloc(sizeof(*argv) * (2 + argc));
|
||
20 years ago
|
argv[0] = update_post_hook;
|
||
|
|
||
15 years ago
|
for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
|
||
19 years ago
|
char *p;
|
||
15 years ago
|
if (cmd->error_string)
|
||
20 years ago
|
continue;
|
||
15 years ago
|
p = xmalloc(strlen(cmd->ref_name) + 1);
|
||
|
strcpy(p, cmd->ref_name);
|
||
19 years ago
|
argv[argc] = p;
|
||
20 years ago
|
argc++;
|
||
|
}
|
||
|
argv[argc] = NULL;
|
||
15 years ago
|
|
||
|
memset(&proc, 0, sizeof(proc));
|
||
|
proc.no_stdin = 1;
|
||
|
proc.stdout_to_stderr = 1;
|
||
|
proc.err = use_sideband ? -1 : 0;
|
||
|
proc.argv = argv;
|
||
|
|
||
|
if (!start_command(&proc)) {
|
||
|
if (use_sideband)
|
||
|
copy_to_sideband(proc.err, -1, NULL);
|
||
|
finish_command(&proc);
|
||
|
}
|
||
20 years ago
|
}
|
||
20 years ago
|
|
||
15 years ago
|
static void check_aliased_update(struct command *cmd, struct string_list *list)
|
||
|
{
|
||
|
struct string_list_item *item;
|
||
|
struct command *dst_cmd;
|
||
|
unsigned char sha1[20];
|
||
|
char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41];
|
||
|
int flag;
|
||
|
|
||
|
const char *dst_name = resolve_ref(cmd->ref_name, sha1, 0, &flag);
|
||
|
|
||
|
if (!(flag & REF_ISSYMREF))
|
||
|
return;
|
||
|
|
||
|
if ((item = string_list_lookup(dst_name, list)) == NULL)
|
||
|
return;
|
||
|
|
||
|
cmd->skip_update = 1;
|
||
|
|
||
|
dst_cmd = (struct command *) item->util;
|
||
|
|
||
|
if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) &&
|
||
|
!hashcmp(cmd->new_sha1, dst_cmd->new_sha1))
|
||
|
return;
|
||
|
|
||
|
dst_cmd->skip_update = 1;
|
||
|
|
||
|
strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV));
|
||
15 years ago
|
strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
|
||
15 years ago
|
strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
|
||
15 years ago
|
strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
|
||
15 years ago
|
rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
|
||
|
" its target '%s' (%s..%s)",
|
||
|
cmd->ref_name, cmd_oldh, cmd_newh,
|
||
|
dst_cmd->ref_name, dst_oldh, dst_newh);
|
||
|
|
||
|
cmd->error_string = dst_cmd->error_string =
|
||
|
"inconsistent aliased update";
|
||
|
}
|
||
|
|
||
|
static void check_aliased_updates(struct command *commands)
|
||
|
{
|
||
|
struct command *cmd;
|
||
|
struct string_list ref_list = { NULL, 0, 0, 0 };
|
||
|
|
||
|
for (cmd = commands; cmd; cmd = cmd->next) {
|
||
|
struct string_list_item *item =
|
||
|
string_list_append(cmd->ref_name, &ref_list);
|
||
|
item->util = (void *)cmd;
|
||
|
}
|
||
|
sort_string_list(&ref_list);
|
||
|
|
||
|
for (cmd = commands; cmd; cmd = cmd->next)
|
||
|
check_aliased_update(cmd, &ref_list);
|
||
|
|
||
|
string_list_clear(&ref_list, 0);
|
||
|
}
|
||
|
|
||
15 years ago
|
static void execute_commands(struct command *commands, const char *unpacker_error)
|
||
20 years ago
|
{
|
||
15 years ago
|
struct command *cmd;
|
||
16 years ago
|
unsigned char sha1[20];
|
||
18 years ago
|
|
||
|
if (unpacker_error) {
|
||
15 years ago
|
for (cmd = commands; cmd; cmd = cmd->next)
|
||
18 years ago
|
cmd->error_string = "n/a (unpacker error)";
|
||
|
return;
|
||
|
}
|
||
|
|
||
15 years ago
|
if (run_receive_hook(commands, pre_receive_hook)) {
|
||
|
for (cmd = commands; cmd; cmd = cmd->next)
|
||
18 years ago
|
cmd->error_string = "pre-receive hook declined";
|
||
|
return;
|
||
|
}
|
||
|
|
||
15 years ago
|
check_aliased_updates(commands);
|
||
|
|
||
16 years ago
|
head_name = resolve_ref("HEAD", sha1, 0, NULL);
|
||
|
|
||
15 years ago
|
for (cmd = commands; cmd; cmd = cmd->next)
|
||
15 years ago
|
if (!cmd->skip_update)
|
||
|
cmd->error_string = update(cmd);
|
||
20 years ago
|
}
|
||
|
|
||
15 years ago
|
static struct command *read_head_info(void)
|
||
20 years ago
|
{
|
||
15 years ago
|
struct command *commands = NULL;
|
||
20 years ago
|
struct command **p = &commands;
|
||
20 years ago
|
for (;;) {
|
||
|
static char line[1000];
|
||
20 years ago
|
unsigned char old_sha1[20], new_sha1[20];
|
||
|
struct command *cmd;
|
||
19 years ago
|
char *refname;
|
||
|
int len, reflen;
|
||
20 years ago
|
|
||
|
len = packet_read_line(0, line, sizeof(line));
|
||
20 years ago
|
if (!len)
|
||
|
break;
|
||
20 years ago
|
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))
|
||
19 years ago
|
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;
|
||
15 years ago
|
if (strstr(refname + reflen + 1, "side-band-64k"))
|
||
|
use_sideband = LARGE_PACKET_MAX;
|
||
19 years ago
|
}
|
||
15 years ago
|
cmd = xcalloc(1, sizeof(struct command) + len - 80);
|
||
19 years ago
|
hashcpy(cmd->old_sha1, old_sha1);
|
||
|
hashcpy(cmd->new_sha1, new_sha1);
|
||
20 years ago
|
memcpy(cmd->ref_name, line + 82, len - 81);
|
||
|
*p = cmd;
|
||
|
p = &cmd->next;
|
||
20 years ago
|
}
|
||
15 years ago
|
return commands;
|
||
20 years ago
|
}
|
||
|
|
||
18 years ago
|
static const char *parse_pack_header(struct pack_header *hdr)
|
||
|
{
|
||
18 years ago
|
switch (read_pack_header(0, hdr)) {
|
||
|
case PH_ERROR_EOF:
|
||
|
return "eof before pack header was fully read";
|
||
|
|
||
|
case PH_ERROR_PACK_SIGNATURE:
|
||
18 years ago
|
return "protocol error (pack signature mismatch detected)";
|
||
18 years ago
|
|
||
|
case PH_ERROR_PROTOCOL:
|
||
18 years ago
|
return "protocol error (pack version unsupported)";
|
||
18 years ago
|
|
||
|
default:
|
||
|
return "unknown error in parse_pack_header";
|
||
|
|
||
|
case 0:
|
||
|
return NULL;
|
||
|
}
|
||
18 years ago
|
}
|
||
|
|
||
18 years ago
|
static const char *pack_lockfile;
|
||
|
|
||
18 years ago
|
static const char *unpack(void)
|
||
20 years ago
|
{
|
||
18 years ago
|
struct pack_header hdr;
|
||
|
const char *hdr_err;
|
||
|
char hdr_arg[38];
|
||
|
|
||
|
hdr_err = parse_pack_header(&hdr);
|
||
|
if (hdr_err)
|
||
|
return hdr_err;
|
||
17 years ago
|
snprintf(hdr_arg, sizeof(hdr_arg),
|
||
|
"--pack_header=%"PRIu32",%"PRIu32,
|
||
18 years ago
|
ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
|
||
|
|
||
|
if (ntohl(hdr.hdr_entries) < unpack_limit) {
|
||
17 years ago
|
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;
|
||
18 years ago
|
code = run_command_v_opt(unpacker, RUN_GIT_CMD);
|
||
16 years ago
|
if (!code)
|
||
18 years ago
|
return NULL;
|
||
16 years ago
|
return "unpack-objects abnormal exit";
|
||
18 years ago
|
} else {
|
||
17 years ago
|
const char *keeper[7];
|
||
|
int s, status, i = 0;
|
||
18 years ago
|
char keep_arg[256];
|
||
18 years ago
|
struct child_process ip;
|
||
18 years ago
|
|
||
17 years ago
|
s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
|
||
18 years ago
|
if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
|
||
|
strcpy(keep_arg + s, "localhost");
|
||
|
|
||
17 years ago
|
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;
|
||
18 years ago
|
memset(&ip, 0, sizeof(ip));
|
||
|
ip.argv = keeper;
|
||
|
ip.out = -1;
|
||
|
ip.git_cmd = 1;
|
||
16 years ago
|
status = start_command(&ip);
|
||
|
if (status) {
|
||
18 years ago
|
return "index-pack fork failed";
|
||
16 years ago
|
}
|
||
18 years ago
|
pack_lockfile = index_pack_lockfile(ip.out);
|
||
17 years ago
|
close(ip.out);
|
||
18 years ago
|
status = finish_command(&ip);
|
||
|
if (!status) {
|
||
18 years ago
|
reprepare_packed_git();
|
||
|
return NULL;
|
||
|
}
|
||
|
return "index-pack abnormal exit";
|
||
19 years ago
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
static void report(struct command *commands, const char *unpack_status)
|
||
19 years ago
|
{
|
||
|
struct command *cmd;
|
||
15 years ago
|
struct strbuf buf = STRBUF_INIT;
|
||
|
|
||
|
packet_buf_write(&buf, "unpack %s\n",
|
||
|
unpack_status ? unpack_status : "ok");
|
||
19 years ago
|
for (cmd = commands; cmd; cmd = cmd->next) {
|
||
|
if (!cmd->error_string)
|
||
15 years ago
|
packet_buf_write(&buf, "ok %s\n",
|
||
|
cmd->ref_name);
|
||
19 years ago
|
else
|
||
15 years ago
|
packet_buf_write(&buf, "ng %s %s\n",
|
||
|
cmd->ref_name, cmd->error_string);
|
||
20 years ago
|
}
|
||
15 years ago
|
packet_buf_flush(&buf);
|
||
|
|
||
|
if (use_sideband)
|
||
|
send_sideband(1, 1, buf.buf, buf.len, use_sideband);
|
||
|
else
|
||
|
safe_write(1, buf.buf, buf.len);
|
||
|
strbuf_release(&buf);
|
||
20 years ago
|
}
|
||
|
|
||
15 years ago
|
static int delete_only(struct command *commands)
|
||
18 years ago
|
{
|
||
15 years ago
|
struct command *cmd;
|
||
|
for (cmd = commands; cmd; cmd = cmd->next) {
|
||
18 years ago
|
if (!is_null_sha1(cmd->new_sha1))
|
||
|
return 0;
|
||
|
}
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
17 years ago
|
static int add_refs_from_alternate(struct alternate_object_database *e, void *unused)
|
||
|
{
|
||
16 years ago
|
char *other;
|
||
|
size_t len;
|
||
17 years ago
|
struct remote *remote;
|
||
|
struct transport *transport;
|
||
|
const struct ref *extra;
|
||
|
|
||
16 years ago
|
e->name[-1] = '\0';
|
||
|
other = xstrdup(make_absolute_path(e->base));
|
||
|
e->name[-1] = '/';
|
||
|
len = strlen(other);
|
||
|
|
||
17 years ago
|
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);
|
||
|
}
|
||
|
|
||
17 years ago
|
int cmd_receive_pack(int argc, const char **argv, const char *prefix)
|
||
20 years ago
|
{
|
||
15 years ago
|
int advertise_refs = 0;
|
||
|
int stateless_rpc = 0;
|
||
20 years ago
|
int i;
|
||
19 years ago
|
char *dir = NULL;
|
||
15 years ago
|
struct command *commands;
|
||
20 years ago
|
|
||
|
argv++;
|
||
|
for (i = 1; i < argc; i++) {
|
||
17 years ago
|
const char *arg = *argv++;
|
||
20 years ago
|
|
||
|
if (*arg == '-') {
|
||
15 years ago
|
if (!strcmp(arg, "--advertise-refs")) {
|
||
|
advertise_refs = 1;
|
||
|
continue;
|
||
|
}
|
||
|
if (!strcmp(arg, "--stateless-rpc")) {
|
||
|
stateless_rpc = 1;
|
||
|
continue;
|
||
|
}
|
||
|
|
||
20 years ago
|
usage(receive_pack_usage);
|
||
|
}
|
||
20 years ago
|
if (dir)
|
||
|
usage(receive_pack_usage);
|
||
17 years ago
|
dir = xstrdup(arg);
|
||
20 years ago
|
}
|
||
|
if (!dir)
|
||
|
usage(receive_pack_usage);
|
||
|
|
||
17 years ago
|
setup_path();
|
||
17 years ago
|
|
||
19 years ago
|
if (!enter_repo(dir, 0))
|
||
16 years ago
|
die("'%s' does not appear to be a git repository", dir);
|
||
20 years ago
|
|
||
18 years ago
|
if (is_repository_shallow())
|
||
|
die("attempt to push into a shallow repository");
|
||
|
|
||
17 years ago
|
git_config(receive_pack_config, NULL);
|
||
18 years ago
|
|
||
18 years ago
|
if (0 <= transfer_unpack_limit)
|
||
|
unpack_limit = transfer_unpack_limit;
|
||
|
else if (0 <= receive_unpack_limit)
|
||
|
unpack_limit = receive_unpack_limit;
|
||
|
|
||
15 years ago
|
if (advertise_refs || !stateless_rpc) {
|
||
|
add_alternate_refs();
|
||
|
write_head_info();
|
||
|
clear_extra_refs();
|
||
20 years ago
|
|
||
15 years ago
|
/* EOF */
|
||
|
packet_flush(1);
|
||
|
}
|
||
|
if (advertise_refs)
|
||
|
return 0;
|
||
20 years ago
|
|
||
15 years ago
|
if ((commands = read_head_info()) != NULL) {
|
||
18 years ago
|
const char *unpack_status = NULL;
|
||
|
|
||
|
if (!delete_only(commands))
|
||
|
unpack_status = unpack();
|
||
15 years ago
|
execute_commands(commands, unpack_status);
|
||
18 years ago
|
if (pack_lockfile)
|
||
16 years ago
|
unlink_or_warn(pack_lockfile);
|
||
19 years ago
|
if (report_status)
|
||
15 years ago
|
report(commands, unpack_status);
|
||
|
run_receive_hook(commands, post_receive_hook);
|
||
18 years ago
|
run_update_post_hook(commands);
|
||
15 years ago
|
if (auto_gc) {
|
||
|
const char *argv_gc_auto[] = {
|
||
|
"gc", "--auto", "--quiet", NULL,
|
||
|
};
|
||
|
run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
|
||
|
}
|
||
|
if (auto_update_server_info)
|
||
|
update_server_info(0);
|
||
20 years ago
|
}
|
||
15 years ago
|
if (use_sideband)
|
||
|
packet_flush(1);
|
||
20 years ago
|
return 0;
|
||
|
}
|