#include "config.h"
#include "gettext.h"
#include "hash.h"
+#include "hex.h"
#include "refs.h"
#include "object-name.h"
#include "parse-options.h"
static const char * const git_update_ref_usage[] = {
N_("git update-ref [<options>] -d <refname> [<old-oid>]"),
N_("git update-ref [<options>] <refname> <new-oid> [<old-oid>]"),
- N_("git update-ref [<options>] --stdin [-z]"),
+ N_("git update-ref [<options>] --stdin [-z] [--batch-updates]"),
NULL
};
report_ok("abort");
}
+static void print_rejected_refs(const char *refname,
+ const struct object_id *old_oid,
+ const struct object_id *new_oid,
+ const char *old_target,
+ const char *new_target,
+ enum ref_transaction_error err,
+ void *cb_data UNUSED)
+{
+ struct strbuf sb = STRBUF_INIT;
+ const char *reason = "";
+
+ switch (err) {
+ case REF_TRANSACTION_ERROR_NAME_CONFLICT:
+ reason = "refname conflict";
+ break;
+ case REF_TRANSACTION_ERROR_CREATE_EXISTS:
+ reason = "reference already exists";
+ break;
+ case REF_TRANSACTION_ERROR_NONEXISTENT_REF:
+ reason = "reference does not exist";
+ break;
+ case REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE:
+ reason = "incorrect old value provided";
+ break;
+ case REF_TRANSACTION_ERROR_INVALID_NEW_VALUE:
+ reason = "invalid new value provided";
+ break;
+ case REF_TRANSACTION_ERROR_EXPECTED_SYMREF:
+ reason = "expected symref but found regular ref";
+ break;
+ default:
+ reason = "unkown failure";
+ }
+
+ strbuf_addf(&sb, "rejected %s %s %s %s\n", refname,
+ new_oid ? oid_to_hex(new_oid) : new_target,
+ old_oid ? oid_to_hex(old_oid) : old_target,
+ reason);
+
+ fwrite(sb.buf, sb.len, 1, stdout);
+ strbuf_release(&sb);
+}
+
static void parse_cmd_commit(struct ref_transaction *transaction,
const char *next, const char *end UNUSED)
{
die("commit: extra input: %s", next);
if (ref_transaction_commit(transaction, &error))
die("commit: %s", error.buf);
+
+ ref_transaction_for_each_rejected_update(transaction,
+ print_rejected_refs, NULL);
+
report_ok("commit");
ref_transaction_free(transaction);
}
{ "commit", parse_cmd_commit, 0, UPDATE_REFS_CLOSED },
};
-static void update_refs_stdin(void)
+static void update_refs_stdin(unsigned int flags)
{
struct strbuf input = STRBUF_INIT, err = STRBUF_INIT;
enum update_refs_state state = UPDATE_REFS_OPEN;
int i, j;
transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
- 0, &err);
+ flags, &err);
if (!transaction)
die("%s", err.buf);
*/
state = cmd->state;
transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
- 0, &err);
+ flags, &err);
if (!transaction)
die("%s", err.buf);
/* Commit by default if no transaction was requested. */
if (ref_transaction_commit(transaction, &err))
die("%s", err.buf);
+ ref_transaction_for_each_rejected_update(transaction,
+ print_rejected_refs, NULL);
ref_transaction_free(transaction);
break;
case UPDATE_REFS_STARTED:
struct object_id oid, oldoid;
int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0;
int create_reflog = 0;
+ unsigned int flags = 0;
+
struct option options[] = {
OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")),
OPT_BOOL('d', NULL, &delete, N_("delete the reference")),
OPT_BOOL('z', NULL, &end_null, N_("stdin has NUL-terminated arguments")),
OPT_BOOL( 0 , "stdin", &read_stdin, N_("read updates from stdin")),
OPT_BOOL( 0 , "create-reflog", &create_reflog, N_("create a reflog")),
+ OPT_BIT('0', "batch-updates", &flags, N_("batch reference updates"),
+ REF_TRANSACTION_ALLOW_FAILURE),
OPT_END(),
};
usage_with_options(git_update_ref_usage, options);
if (end_null)
line_termination = '\0';
- update_refs_stdin();
+ update_refs_stdin(flags);
return 0;
+ } else if (flags & REF_TRANSACTION_ALLOW_FAILURE) {
+ die("--batch-updates can only be used with --stdin");
}
if (end_null)
grep "$(git rev-parse $a) $(git rev-parse $a)" actual
'
+ test_expect_success "stdin $type batch-updates" '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ test_commit commit &&
+ head=$(git rev-parse HEAD) &&
+
+ format_command $type "update refs/heads/ref1" "$head" "$Z" >stdin &&
+ format_command $type "update refs/heads/ref2" "$head" "$Z" >>stdin &&
+ git update-ref $type --stdin --batch-updates <stdin &&
+ echo $head >expect &&
+ git rev-parse refs/heads/ref1 >actual &&
+ test_cmp expect actual &&
+ git rev-parse refs/heads/ref2 >actual &&
+ test_cmp expect actual
+ )
+ '
+
+ test_expect_success "stdin $type batch-updates with invalid new_oid" '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ test_commit one &&
+ old_head=$(git rev-parse HEAD) &&
+ test_commit two &&
+ head=$(git rev-parse HEAD) &&
+ git update-ref refs/heads/ref1 $head &&
+ git update-ref refs/heads/ref2 $head &&
+
+ format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
+ format_command $type "update refs/heads/ref2" "$(test_oid 001)" "$head" >>stdin &&
+ git update-ref $type --stdin --batch-updates <stdin >stdout &&
+ echo $old_head >expect &&
+ git rev-parse refs/heads/ref1 >actual &&
+ test_cmp expect actual &&
+ echo $head >expect &&
+ git rev-parse refs/heads/ref2 >actual &&
+ test_cmp expect actual &&
+ test_grep -q "invalid new value provided" stdout
+ )
+ '
+
+ test_expect_success "stdin $type batch-updates with non-commit new_oid" '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ test_commit one &&
+ old_head=$(git rev-parse HEAD) &&
+ test_commit two &&
+ head=$(git rev-parse HEAD) &&
+ head_tree=$(git rev-parse HEAD^{tree}) &&
+ git update-ref refs/heads/ref1 $head &&
+ git update-ref refs/heads/ref2 $head &&
+
+ format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
+ format_command $type "update refs/heads/ref2" "$head_tree" "$head" >>stdin &&
+ git update-ref $type --stdin --batch-updates <stdin >stdout &&
+ echo $old_head >expect &&
+ git rev-parse refs/heads/ref1 >actual &&
+ test_cmp expect actual &&
+ echo $head >expect &&
+ git rev-parse refs/heads/ref2 >actual &&
+ test_cmp expect actual &&
+ test_grep -q "invalid new value provided" stdout
+ )
+ '
+
+ test_expect_success "stdin $type batch-updates with non-existent ref" '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ test_commit one &&
+ old_head=$(git rev-parse HEAD) &&
+ test_commit two &&
+ head=$(git rev-parse HEAD) &&
+ git update-ref refs/heads/ref1 $head &&
+
+ format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
+ format_command $type "update refs/heads/ref2" "$old_head" "$head" >>stdin &&
+ git update-ref $type --stdin --batch-updates <stdin >stdout &&
+ echo $old_head >expect &&
+ git rev-parse refs/heads/ref1 >actual &&
+ test_cmp expect actual &&
+ test_must_fail git rev-parse refs/heads/ref2 &&
+ test_grep -q "reference does not exist" stdout
+ )
+ '
+
+ test_expect_success "stdin $type batch-updates with dangling symref" '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ test_commit one &&
+ old_head=$(git rev-parse HEAD) &&
+ test_commit two &&
+ head=$(git rev-parse HEAD) &&
+ git update-ref refs/heads/ref1 $head &&
+ git symbolic-ref refs/heads/ref2 refs/heads/nonexistent &&
+
+ format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
+ format_command $type "update refs/heads/ref2" "$old_head" "$head" >>stdin &&
+ git update-ref $type --no-deref --stdin --batch-updates <stdin >stdout &&
+ echo $old_head >expect &&
+ git rev-parse refs/heads/ref1 >actual &&
+ test_cmp expect actual &&
+ echo $head >expect &&
+ test_must_fail git rev-parse refs/heads/ref2 &&
+ test_grep -q "reference does not exist" stdout
+ )
+ '
+
+ test_expect_success "stdin $type batch-updates with regular ref as symref" '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ test_commit one &&
+ old_head=$(git rev-parse HEAD) &&
+ test_commit two &&
+ head=$(git rev-parse HEAD) &&
+ git update-ref refs/heads/ref1 $head &&
+ git update-ref refs/heads/ref2 $head &&
+
+ format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
+ format_command $type "symref-update refs/heads/ref2" "$old_head" "ref" "refs/heads/nonexistent" >>stdin &&
+ git update-ref $type --no-deref --stdin --batch-updates <stdin >stdout &&
+ echo $old_head >expect &&
+ git rev-parse refs/heads/ref1 >actual &&
+ test_cmp expect actual &&
+ echo $head >expect &&
+ echo $head >expect &&
+ git rev-parse refs/heads/ref2 >actual &&
+ test_cmp expect actual &&
+ test_grep -q "expected symref but found regular ref" stdout
+ )
+ '
+
+ test_expect_success "stdin $type batch-updates with invalid old_oid" '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ test_commit one &&
+ old_head=$(git rev-parse HEAD) &&
+ test_commit two &&
+ head=$(git rev-parse HEAD) &&
+ git update-ref refs/heads/ref1 $head &&
+ git update-ref refs/heads/ref2 $head &&
+
+ format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
+ format_command $type "update refs/heads/ref2" "$old_head" "$Z" >>stdin &&
+ git update-ref $type --stdin --batch-updates <stdin >stdout &&
+ echo $old_head >expect &&
+ git rev-parse refs/heads/ref1 >actual &&
+ test_cmp expect actual &&
+ echo $head >expect &&
+ git rev-parse refs/heads/ref2 >actual &&
+ test_cmp expect actual &&
+ test_grep -q "reference already exists" stdout
+ )
+ '
+
+ test_expect_success "stdin $type batch-updates with incorrect old oid" '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ test_commit one &&
+ old_head=$(git rev-parse HEAD) &&
+ test_commit two &&
+ head=$(git rev-parse HEAD) &&
+ git update-ref refs/heads/ref1 $head &&
+ git update-ref refs/heads/ref2 $head &&
+
+ format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
+ format_command $type "update refs/heads/ref2" "$head" "$old_head" >>stdin &&
+ git update-ref $type --stdin --batch-updates <stdin >stdout &&
+ echo $old_head >expect &&
+ git rev-parse refs/heads/ref1 >actual &&
+ test_cmp expect actual &&
+ echo $head >expect &&
+ git rev-parse refs/heads/ref2 >actual &&
+ test_cmp expect actual &&
+ test_grep -q "incorrect old value provided" stdout
+ )
+ '
+
+ test_expect_success "stdin $type batch-updates refname conflict" '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ test_commit one &&
+ old_head=$(git rev-parse HEAD) &&
+ test_commit two &&
+ head=$(git rev-parse HEAD) &&
+ git update-ref refs/heads/ref/foo $head &&
+
+ format_command $type "update refs/heads/ref/foo" "$old_head" "$head" >stdin &&
+ format_command $type "update refs/heads/ref" "$old_head" "" >>stdin &&
+ git update-ref $type --stdin --batch-updates <stdin >stdout &&
+ echo $old_head >expect &&
+ git rev-parse refs/heads/ref/foo >actual &&
+ test_cmp expect actual &&
+ test_grep -q "refname conflict" stdout
+ )
+ '
+
+ test_expect_success "stdin $type batch-updates refname conflict new ref" '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ test_commit one &&
+ old_head=$(git rev-parse HEAD) &&
+ test_commit two &&
+ head=$(git rev-parse HEAD) &&
+ git update-ref refs/heads/ref/foo $head &&
+
+ format_command $type "update refs/heads/foo" "$old_head" "" >stdin &&
+ format_command $type "update refs/heads/ref" "$old_head" "" >>stdin &&
+ git update-ref $type --stdin --batch-updates <stdin >stdout &&
+ echo $old_head >expect &&
+ git rev-parse refs/heads/foo >actual &&
+ test_cmp expect actual &&
+ test_grep -q "refname conflict" stdout
+ )
+ '
done
test_expect_success 'update-ref should also create reflog for HEAD' '