Skip to content

Commit

Permalink
Merge branch 'rs/ref-transaction-1'
Browse files Browse the repository at this point in the history
The second batch of the transactional ref update series.

* rs/ref-transaction-1: (22 commits)
  update-ref --stdin: pass transaction around explicitly
  update-ref --stdin: narrow scope of err strbuf
  refs.c: make delete_ref use a transaction
  refs.c: make prune_ref use a transaction to delete the ref
  refs.c: remove lock_ref_sha1
  refs.c: remove the update_ref_write function
  refs.c: remove the update_ref_lock function
  refs.c: make lock_ref_sha1 static
  walker.c: use ref transaction for ref updates
  fast-import.c: use a ref transaction when dumping tags
  receive-pack.c: use a reference transaction for updating the refs
  refs.c: change update_ref to use a transaction
  branch.c: use ref transaction for all ref updates
  fast-import.c: change update_branch to use ref transactions
  sequencer.c: use ref transactions for all ref updates
  commit.c: use ref transactions for updates
  replace.c: use the ref transaction functions for updates
  tag.c: use ref transactions when doing updates
  refs.c: add transaction.status and track OPEN/CLOSED
  refs.c: make ref_transaction_begin take an err argument
  ...
  • Loading branch information
Junio C Hamano committed Sep 11, 2014
2 parents 5e1dc48 + 88499b2 commit 01d678a
Show file tree
Hide file tree
Showing 11 changed files with 398 additions and 248 deletions.
31 changes: 17 additions & 14 deletions branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ void create_branch(const char *head,
int force, int reflog, int clobber_head,
int quiet, enum branch_track track)
{
struct ref_lock *lock = NULL;
struct commit *commit;
unsigned char sha1[20];
char *real_ref, msg[PATH_MAX + 20];
Expand Down Expand Up @@ -269,29 +268,33 @@ void create_branch(const char *head,
die(_("Not a valid branch point: '%s'."), start_name);
hashcpy(sha1, commit->object.sha1);

if (!dont_change_ref) {
lock = lock_any_ref_for_update(ref.buf, NULL, 0, NULL);
if (!lock)
die_errno(_("Failed to lock ref for update"));
}

if (reflog)
log_all_ref_updates = 1;

if (forcing)
snprintf(msg, sizeof msg, "branch: Reset to %s",
start_name);
else if (!dont_change_ref)
snprintf(msg, sizeof msg, "branch: Created from %s",
start_name);

if (reflog)
log_all_ref_updates = 1;

if (!dont_change_ref) {
struct ref_transaction *transaction;
struct strbuf err = STRBUF_INIT;

transaction = ref_transaction_begin(&err);
if (!transaction ||
ref_transaction_update(transaction, ref.buf, sha1,
null_sha1, 0, !forcing, &err) ||
ref_transaction_commit(transaction, msg, &err))
die("%s", err.buf);
ref_transaction_free(transaction);
strbuf_release(&err);
}

if (real_ref && track)
setup_tracking(ref.buf + 11, real_ref, track, quiet);

if (!dont_change_ref)
if (write_ref_sha1(lock, sha1, msg) < 0)
die_errno(_("Failed to write ref"));

strbuf_release(&ref);
free(real_ref);
}
Expand Down
25 changes: 12 additions & 13 deletions builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1651,11 +1651,12 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
const char *index_file, *reflog_msg;
char *nl;
unsigned char sha1[20];
struct ref_lock *ref_lock;
struct commit_list *parents = NULL, **pptr = &parents;
struct stat statbuf;
struct commit *current_head = NULL;
struct commit_extra_header *extra = NULL;
struct ref_transaction *transaction;
struct strbuf err = STRBUF_INIT;

if (argc == 2 && !strcmp(argv[1], "-h"))
usage_with_options(builtin_commit_usage, builtin_commit_options);
Expand Down Expand Up @@ -1777,16 +1778,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
strbuf_release(&author_ident);
free_commit_extra_headers(extra);

ref_lock = lock_any_ref_for_update("HEAD",
!current_head
? NULL
: current_head->object.sha1,
0, NULL);
if (!ref_lock) {
rollback_index_files();
die(_("cannot lock HEAD ref"));
}

nl = strchr(sb.buf, '\n');
if (nl)
strbuf_setlen(&sb, nl + 1 - sb.buf);
Expand All @@ -1795,10 +1786,17 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);

if (write_ref_sha1(ref_lock, sha1, sb.buf) < 0) {
transaction = ref_transaction_begin(&err);
if (!transaction ||
ref_transaction_update(transaction, "HEAD", sha1,
current_head
? current_head->object.sha1 : NULL,
0, !!current_head, &err) ||
ref_transaction_commit(transaction, sb.buf, &err)) {
rollback_index_files();
die(_("cannot update HEAD ref"));
die("%s", err.buf);
}
ref_transaction_free(transaction);

unlink(git_path("CHERRY_PICK_HEAD"));
unlink(git_path("REVERT_HEAD"));
Expand Down Expand Up @@ -1827,5 +1825,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
if (!quiet)
print_summary(prefix, sha1, !current_head);

strbuf_release(&err);
return 0;
}
25 changes: 16 additions & 9 deletions builtin/receive-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,6 @@ static const char *update(struct command *cmd, struct shallow_info *si)
const char *namespaced_name;
unsigned char *old_sha1 = cmd->old_sha1;
unsigned char *new_sha1 = cmd->new_sha1;
struct ref_lock *lock;

/* only refs/... are allowed */
if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
Expand Down Expand Up @@ -574,19 +573,27 @@ static const char *update(struct command *cmd, struct shallow_info *si)
return NULL; /* good */
}
else {
struct strbuf err = STRBUF_INIT;
struct ref_transaction *transaction;

if (shallow_update && si->shallow_ref[cmd->index] &&
update_shallow_ref(cmd, si))
return "shallow error";

lock = lock_any_ref_for_update(namespaced_name, old_sha1,
0, NULL);
if (!lock) {
rp_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 */
transaction = ref_transaction_begin(&err);
if (!transaction ||
ref_transaction_update(transaction, namespaced_name,
new_sha1, old_sha1, 0, 1, &err) ||
ref_transaction_commit(transaction, "push", &err)) {
ref_transaction_free(transaction);

rp_error("%s", err.buf);
strbuf_release(&err);
return "failed to update ref";
}

ref_transaction_free(transaction);
strbuf_release(&err);
return NULL; /* good */
}
}
Expand Down
14 changes: 8 additions & 6 deletions builtin/replace.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ static int replace_object_sha1(const char *object_ref,
unsigned char prev[20];
enum object_type obj_type, repl_type;
char ref[PATH_MAX];
struct ref_lock *lock;
struct ref_transaction *transaction;
struct strbuf err = STRBUF_INIT;

obj_type = sha1_object_info(object, NULL);
repl_type = sha1_object_info(repl, NULL);
Expand All @@ -168,12 +169,13 @@ static int replace_object_sha1(const char *object_ref,

check_ref_valid(object, prev, ref, sizeof(ref), force);

lock = lock_any_ref_for_update(ref, prev, 0, NULL);
if (!lock)
die("%s: cannot lock the ref", ref);
if (write_ref_sha1(lock, repl, NULL) < 0)
die("%s: cannot update the ref", ref);
transaction = ref_transaction_begin(&err);
if (!transaction ||
ref_transaction_update(transaction, ref, repl, prev, 0, 1, &err) ||
ref_transaction_commit(transaction, NULL, &err))
die("%s", err.buf);

ref_transaction_free(transaction);
return 0;
}

Expand Down
16 changes: 10 additions & 6 deletions builtin/tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -576,14 +576,15 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
struct strbuf ref = STRBUF_INIT;
unsigned char object[20], prev[20];
const char *object_ref, *tag;
struct ref_lock *lock;
struct create_tag_options opt;
char *cleanup_arg = NULL;
int annotate = 0, force = 0, lines = -1;
int cmdmode = 0;
const char *msgfile = NULL, *keyid = NULL;
struct msg_arg msg = { 0, STRBUF_INIT };
struct commit_list *with_commit = NULL;
struct ref_transaction *transaction;
struct strbuf err = STRBUF_INIT;
struct option options[] = {
OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
{ OPTION_INTEGER, 'n', NULL, &lines, N_("n"),
Expand Down Expand Up @@ -729,14 +730,17 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
if (annotate)
create_tag(object, tag, &buf, &opt, prev, object);

lock = lock_any_ref_for_update(ref.buf, prev, 0, NULL);
if (!lock)
die(_("%s: cannot lock the ref"), ref.buf);
if (write_ref_sha1(lock, object, NULL) < 0)
die(_("%s: cannot update the ref"), ref.buf);
transaction = ref_transaction_begin(&err);
if (!transaction ||
ref_transaction_update(transaction, ref.buf, object, prev,
0, 1, &err) ||
ref_transaction_commit(transaction, NULL, &err))
die("%s", err.buf);
ref_transaction_free(transaction);
if (force && !is_null_sha1(prev) && hashcmp(prev, object))
printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));

strbuf_release(&err);
strbuf_release(&buf);
strbuf_release(&ref);
return 0;
Expand Down
52 changes: 35 additions & 17 deletions builtin/update-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ static const char * const git_update_ref_usage[] = {
NULL
};

static struct ref_transaction *transaction;

static char line_termination = '\n';
static int update_flags;
static struct strbuf err = STRBUF_INIT;

/*
* Parse one whitespace- or NUL-terminated, possibly C-quoted argument
Expand Down Expand Up @@ -177,8 +174,10 @@ static int parse_next_sha1(struct strbuf *input, const char **next,
* depending on how line_termination is set.
*/

static const char *parse_cmd_update(struct strbuf *input, const char *next)
static const char *parse_cmd_update(struct ref_transaction *transaction,
struct strbuf *input, const char *next)
{
struct strbuf err = STRBUF_INIT;
char *refname;
unsigned char new_sha1[20];
unsigned char old_sha1[20];
Expand All @@ -204,12 +203,15 @@ static const char *parse_cmd_update(struct strbuf *input, const char *next)

update_flags = 0;
free(refname);
strbuf_release(&err);

return next;
}

static const char *parse_cmd_create(struct strbuf *input, const char *next)
static const char *parse_cmd_create(struct ref_transaction *transaction,
struct strbuf *input, const char *next)
{
struct strbuf err = STRBUF_INIT;
char *refname;
unsigned char new_sha1[20];

Expand All @@ -226,16 +228,21 @@ static const char *parse_cmd_create(struct strbuf *input, const char *next)
if (*next != line_termination)
die("create %s: extra input: %s", refname, next);

ref_transaction_create(transaction, refname, new_sha1, update_flags);
if (ref_transaction_create(transaction, refname, new_sha1,
update_flags, &err))
die("%s", err.buf);

update_flags = 0;
free(refname);
strbuf_release(&err);

return next;
}

static const char *parse_cmd_delete(struct strbuf *input, const char *next)
static const char *parse_cmd_delete(struct ref_transaction *transaction,
struct strbuf *input, const char *next)
{
struct strbuf err = STRBUF_INIT;
char *refname;
unsigned char old_sha1[20];
int have_old;
Expand All @@ -256,17 +263,21 @@ static const char *parse_cmd_delete(struct strbuf *input, const char *next)
if (*next != line_termination)
die("delete %s: extra input: %s", refname, next);

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

update_flags = 0;
free(refname);
strbuf_release(&err);

return next;
}

static const char *parse_cmd_verify(struct strbuf *input, const char *next)
static const char *parse_cmd_verify(struct ref_transaction *transaction,
struct strbuf *input, const char *next)
{
struct strbuf err = STRBUF_INIT;
char *refname;
unsigned char new_sha1[20];
unsigned char old_sha1[20];
Expand Down Expand Up @@ -294,6 +305,7 @@ static const char *parse_cmd_verify(struct strbuf *input, const char *next)

update_flags = 0;
free(refname);
strbuf_release(&err);

return next;
}
Expand All @@ -307,7 +319,7 @@ static const char *parse_cmd_option(struct strbuf *input, const char *next)
return next + 8;
}

static void update_refs_stdin(void)
static void update_refs_stdin(struct ref_transaction *transaction)
{
struct strbuf input = STRBUF_INIT;
const char *next;
Expand All @@ -322,13 +334,13 @@ static void update_refs_stdin(void)
else if (isspace(*next))
die("whitespace before command: %s", next);
else if (starts_with(next, "update "))
next = parse_cmd_update(&input, next + 7);
next = parse_cmd_update(transaction, &input, next + 7);
else if (starts_with(next, "create "))
next = parse_cmd_create(&input, next + 7);
next = parse_cmd_create(transaction, &input, next + 7);
else if (starts_with(next, "delete "))
next = parse_cmd_delete(&input, next + 7);
next = parse_cmd_delete(transaction, &input, next + 7);
else if (starts_with(next, "verify "))
next = parse_cmd_verify(&input, next + 7);
next = parse_cmd_verify(transaction, &input, next + 7);
else if (starts_with(next, "option "))
next = parse_cmd_option(&input, next + 7);
else
Expand Down Expand Up @@ -362,15 +374,21 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
die("Refusing to perform update with empty message.");

if (read_stdin) {
transaction = ref_transaction_begin();
struct strbuf err = STRBUF_INIT;
struct ref_transaction *transaction;

transaction = ref_transaction_begin(&err);
if (!transaction)
die("%s", err.buf);
if (delete || no_deref || argc > 0)
usage_with_options(git_update_ref_usage, options);
if (end_null)
line_termination = '\0';
update_refs_stdin();
update_refs_stdin(transaction);
if (ref_transaction_commit(transaction, msg, &err))
die("%s", err.buf);
ref_transaction_free(transaction);
strbuf_release(&err);
return 0;
}

Expand Down
Loading

0 comments on commit 01d678a

Please sign in to comment.