Skip to content

Commit

Permalink
Merge branch 'mh/refs-have-new'
Browse files Browse the repository at this point in the history
Simplify the ref transaction API around how "the ref should be
pointing at this object" is specified.

* mh/refs-have-new:
  refs.h: remove duplication in function docstrings
  update_ref(): improve documentation
  ref_transaction_verify(): new function to check a reference's value
  ref_transaction_delete(): check that old_sha1 is not null_sha1
  ref_transaction_create(): check that new_sha1 is valid
  commit: avoid race when creating orphan commits
  commit: add tests of commit races
  ref_transaction_delete(): remove "have_old" parameter
  ref_transaction_update(): remove "have_old" parameter
  struct ref_update: move "have_old" into "flags"
  refs.c: change some "flags" to "unsigned int"
  refs: remove the gap in the REF_* constant values
  refs: move REF_DELETING to refs.c
  • Loading branch information
Junio C Hamano committed Mar 5, 2015
2 parents 97c12a8 + d1dd721 commit fd9de86
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 106 deletions.
5 changes: 3 additions & 2 deletions branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,9 @@ void create_branch(const char *head,

transaction = ref_transaction_begin(&err);
if (!transaction ||
ref_transaction_update(transaction, ref.buf, sha1,
null_sha1, 0, !forcing, msg, &err) ||
ref_transaction_update(transaction, ref.buf,
sha1, forcing ? NULL : null_sha1,
0, msg, &err) ||
ref_transaction_commit(transaction, &err))
die("%s", err.buf);
ref_transaction_free(transaction);
Expand Down
4 changes: 2 additions & 2 deletions builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1766,8 +1766,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
if (!transaction ||
ref_transaction_update(transaction, "HEAD", sha1,
current_head
? current_head->object.sha1 : NULL,
0, !!current_head, sb.buf, &err) ||
? current_head->object.sha1 : null_sha1,
0, sb.buf, &err) ||
ref_transaction_commit(transaction, &err)) {
rollback_index_files();
die("%s", err.buf);
Expand Down
6 changes: 4 additions & 2 deletions builtin/fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,10 @@ static int s_update_ref(const char *action,

transaction = ref_transaction_begin(&err);
if (!transaction ||
ref_transaction_update(transaction, ref->name, ref->new_sha1,
ref->old_sha1, 0, check_old, msg, &err))
ref_transaction_update(transaction, ref->name,
ref->new_sha1,
check_old ? ref->old_sha1 : NULL,
0, msg, &err))
goto fail;

ret = ref_transaction_commit(transaction, &err);
Expand Down
5 changes: 2 additions & 3 deletions builtin/receive-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -953,8 +953,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
if (ref_transaction_delete(transaction,
namespaced_name,
old_sha1,
0, old_sha1 != NULL,
"push", &err)) {
0, "push", &err)) {
rp_error("%s", err.buf);
strbuf_release(&err);
return "failed to delete";
Expand All @@ -971,7 +970,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
if (ref_transaction_update(transaction,
namespaced_name,
new_sha1, old_sha1,
0, 1, "push",
0, "push",
&err)) {
rp_error("%s", err.buf);
strbuf_release(&err);
Expand Down
2 changes: 1 addition & 1 deletion builtin/replace.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ static int replace_object_sha1(const char *object_ref,
transaction = ref_transaction_begin(&err);
if (!transaction ||
ref_transaction_update(transaction, ref, repl, prev,
0, 1, NULL, &err) ||
0, NULL, &err) ||
ref_transaction_commit(transaction, &err))
die("%s", err.buf);

Expand Down
2 changes: 1 addition & 1 deletion builtin/tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
transaction = ref_transaction_begin(&err);
if (!transaction ||
ref_transaction_update(transaction, ref.buf, object, prev,
0, 1, NULL, &err) ||
0, NULL, &err) ||
ref_transaction_commit(transaction, &err))
die("%s", err.buf);
ref_transaction_free(transaction);
Expand Down
20 changes: 10 additions & 10 deletions builtin/update-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,9 @@ static const char *parse_cmd_update(struct ref_transaction *transaction,
if (*next != line_termination)
die("update %s: extra input: %s", refname, next);

if (ref_transaction_update(transaction, refname, new_sha1, old_sha1,
update_flags, have_old, msg, &err))
if (ref_transaction_update(transaction, refname,
new_sha1, have_old ? old_sha1 : NULL,
update_flags, msg, &err))
die("%s", err.buf);

update_flags = 0;
Expand Down Expand Up @@ -264,8 +265,9 @@ static const char *parse_cmd_delete(struct ref_transaction *transaction,
if (*next != line_termination)
die("delete %s: extra input: %s", refname, next);

if (ref_transaction_delete(transaction, refname, old_sha1,
update_flags, have_old, msg, &err))
if (ref_transaction_delete(transaction, refname,
have_old ? old_sha1 : NULL,
update_flags, msg, &err))
die("%s", err.buf);

update_flags = 0;
Expand All @@ -280,7 +282,6 @@ static const char *parse_cmd_verify(struct ref_transaction *transaction,
{
struct strbuf err = STRBUF_INIT;
char *refname;
unsigned char new_sha1[20];
unsigned char old_sha1[20];

refname = parse_refname(input, &next);
Expand All @@ -291,13 +292,11 @@ static const char *parse_cmd_verify(struct ref_transaction *transaction,
PARSE_SHA1_OLD))
hashclr(old_sha1);

hashcpy(new_sha1, old_sha1);

if (*next != line_termination)
die("verify %s: extra input: %s", refname, next);

if (ref_transaction_update(transaction, refname, new_sha1, old_sha1,
update_flags, 1, msg, &err))
if (ref_transaction_verify(transaction, refname, old_sha1,
update_flags, &err))
die("%s", err.buf);

update_flags = 0;
Expand Down Expand Up @@ -353,7 +352,8 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
{
const char *refname, *oldval;
unsigned char sha1[20], oldsha1[20];
int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0, flags = 0;
int delete = 0, no_deref = 0, read_stdin = 0, end_null = 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")),
Expand Down
2 changes: 1 addition & 1 deletion cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ extern void update_index_if_able(struct index_state *, struct lock_file *);
extern int hold_locked_index(struct lock_file *, int);
extern void set_alternate_index_output(const char *);

extern int delete_ref(const char *, const unsigned char *sha1, int delopt);
extern int delete_ref(const char *, const unsigned char *sha1, unsigned int flags);

/* Environment bits from configuration mechanism */
extern int trust_executable_bit;
Expand Down
6 changes: 3 additions & 3 deletions fast-import.c
Original file line number Diff line number Diff line change
Expand Up @@ -1720,7 +1720,7 @@ static int update_branch(struct branch *b)
transaction = ref_transaction_begin(&err);
if (!transaction ||
ref_transaction_update(transaction, b->name, b->sha1, old_sha1,
0, 1, msg, &err) ||
0, msg, &err) ||
ref_transaction_commit(transaction, &err)) {
ref_transaction_free(transaction);
error("%s", err.buf);
Expand Down Expand Up @@ -1760,8 +1760,8 @@ static void dump_tags(void)
strbuf_reset(&ref_name);
strbuf_addf(&ref_name, "refs/tags/%s", t->name);

if (ref_transaction_update(transaction, ref_name.buf, t->sha1,
NULL, 0, 0, msg, &err)) {
if (ref_transaction_update(transaction, ref_name.buf,
t->sha1, NULL, 0, msg, &err)) {
failure |= error("%s", err.buf);
goto cleanup;
}
Expand Down
Loading

0 comments on commit fd9de86

Please sign in to comment.