Skip to content

Commit 01d678a

Browse files
committed
Merge branch 'rs/ref-transaction-1'
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 ...
2 parents 5e1dc48 + 88499b2 commit 01d678a

File tree

11 files changed

+398
-248
lines changed

11 files changed

+398
-248
lines changed

branch.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ void create_branch(const char *head,
210210
int force, int reflog, int clobber_head,
211211
int quiet, enum branch_track track)
212212
{
213-
struct ref_lock *lock = NULL;
214213
struct commit *commit;
215214
unsigned char sha1[20];
216215
char *real_ref, msg[PATH_MAX + 20];
@@ -269,29 +268,33 @@ void create_branch(const char *head,
269268
die(_("Not a valid branch point: '%s'."), start_name);
270269
hashcpy(sha1, commit->object.sha1);
271270

272-
if (!dont_change_ref) {
273-
lock = lock_any_ref_for_update(ref.buf, NULL, 0, NULL);
274-
if (!lock)
275-
die_errno(_("Failed to lock ref for update"));
276-
}
277-
278-
if (reflog)
279-
log_all_ref_updates = 1;
280-
281271
if (forcing)
282272
snprintf(msg, sizeof msg, "branch: Reset to %s",
283273
start_name);
284274
else if (!dont_change_ref)
285275
snprintf(msg, sizeof msg, "branch: Created from %s",
286276
start_name);
287277

278+
if (reflog)
279+
log_all_ref_updates = 1;
280+
281+
if (!dont_change_ref) {
282+
struct ref_transaction *transaction;
283+
struct strbuf err = STRBUF_INIT;
284+
285+
transaction = ref_transaction_begin(&err);
286+
if (!transaction ||
287+
ref_transaction_update(transaction, ref.buf, sha1,
288+
null_sha1, 0, !forcing, &err) ||
289+
ref_transaction_commit(transaction, msg, &err))
290+
die("%s", err.buf);
291+
ref_transaction_free(transaction);
292+
strbuf_release(&err);
293+
}
294+
288295
if (real_ref && track)
289296
setup_tracking(ref.buf + 11, real_ref, track, quiet);
290297

291-
if (!dont_change_ref)
292-
if (write_ref_sha1(lock, sha1, msg) < 0)
293-
die_errno(_("Failed to write ref"));
294-
295298
strbuf_release(&ref);
296299
free(real_ref);
297300
}

builtin/commit.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,11 +1651,12 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
16511651
const char *index_file, *reflog_msg;
16521652
char *nl;
16531653
unsigned char sha1[20];
1654-
struct ref_lock *ref_lock;
16551654
struct commit_list *parents = NULL, **pptr = &parents;
16561655
struct stat statbuf;
16571656
struct commit *current_head = NULL;
16581657
struct commit_extra_header *extra = NULL;
1658+
struct ref_transaction *transaction;
1659+
struct strbuf err = STRBUF_INIT;
16591660

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

1780-
ref_lock = lock_any_ref_for_update("HEAD",
1781-
!current_head
1782-
? NULL
1783-
: current_head->object.sha1,
1784-
0, NULL);
1785-
if (!ref_lock) {
1786-
rollback_index_files();
1787-
die(_("cannot lock HEAD ref"));
1788-
}
1789-
17901781
nl = strchr(sb.buf, '\n');
17911782
if (nl)
17921783
strbuf_setlen(&sb, nl + 1 - sb.buf);
@@ -1795,10 +1786,17 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
17951786
strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
17961787
strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
17971788

1798-
if (write_ref_sha1(ref_lock, sha1, sb.buf) < 0) {
1789+
transaction = ref_transaction_begin(&err);
1790+
if (!transaction ||
1791+
ref_transaction_update(transaction, "HEAD", sha1,
1792+
current_head
1793+
? current_head->object.sha1 : NULL,
1794+
0, !!current_head, &err) ||
1795+
ref_transaction_commit(transaction, sb.buf, &err)) {
17991796
rollback_index_files();
1800-
die(_("cannot update HEAD ref"));
1797+
die("%s", err.buf);
18011798
}
1799+
ref_transaction_free(transaction);
18021800

18031801
unlink(git_path("CHERRY_PICK_HEAD"));
18041802
unlink(git_path("REVERT_HEAD"));
@@ -1827,5 +1825,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
18271825
if (!quiet)
18281826
print_summary(prefix, sha1, !current_head);
18291827

1828+
strbuf_release(&err);
18301829
return 0;
18311830
}

builtin/receive-pack.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,6 @@ static const char *update(struct command *cmd, struct shallow_info *si)
473473
const char *namespaced_name;
474474
unsigned char *old_sha1 = cmd->old_sha1;
475475
unsigned char *new_sha1 = cmd->new_sha1;
476-
struct ref_lock *lock;
477476

478477
/* only refs/... are allowed */
479478
if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
@@ -574,19 +573,27 @@ static const char *update(struct command *cmd, struct shallow_info *si)
574573
return NULL; /* good */
575574
}
576575
else {
576+
struct strbuf err = STRBUF_INIT;
577+
struct ref_transaction *transaction;
578+
577579
if (shallow_update && si->shallow_ref[cmd->index] &&
578580
update_shallow_ref(cmd, si))
579581
return "shallow error";
580582

581-
lock = lock_any_ref_for_update(namespaced_name, old_sha1,
582-
0, NULL);
583-
if (!lock) {
584-
rp_error("failed to lock %s", name);
585-
return "failed to lock";
586-
}
587-
if (write_ref_sha1(lock, new_sha1, "push")) {
588-
return "failed to write"; /* error() already called */
583+
transaction = ref_transaction_begin(&err);
584+
if (!transaction ||
585+
ref_transaction_update(transaction, namespaced_name,
586+
new_sha1, old_sha1, 0, 1, &err) ||
587+
ref_transaction_commit(transaction, "push", &err)) {
588+
ref_transaction_free(transaction);
589+
590+
rp_error("%s", err.buf);
591+
strbuf_release(&err);
592+
return "failed to update ref";
589593
}
594+
595+
ref_transaction_free(transaction);
596+
strbuf_release(&err);
590597
return NULL; /* good */
591598
}
592599
}

builtin/replace.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ static int replace_object_sha1(const char *object_ref,
155155
unsigned char prev[20];
156156
enum object_type obj_type, repl_type;
157157
char ref[PATH_MAX];
158-
struct ref_lock *lock;
158+
struct ref_transaction *transaction;
159+
struct strbuf err = STRBUF_INIT;
159160

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

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

171-
lock = lock_any_ref_for_update(ref, prev, 0, NULL);
172-
if (!lock)
173-
die("%s: cannot lock the ref", ref);
174-
if (write_ref_sha1(lock, repl, NULL) < 0)
175-
die("%s: cannot update the ref", ref);
172+
transaction = ref_transaction_begin(&err);
173+
if (!transaction ||
174+
ref_transaction_update(transaction, ref, repl, prev, 0, 1, &err) ||
175+
ref_transaction_commit(transaction, NULL, &err))
176+
die("%s", err.buf);
176177

178+
ref_transaction_free(transaction);
177179
return 0;
178180
}
179181

builtin/tag.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -576,14 +576,15 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
576576
struct strbuf ref = STRBUF_INIT;
577577
unsigned char object[20], prev[20];
578578
const char *object_ref, *tag;
579-
struct ref_lock *lock;
580579
struct create_tag_options opt;
581580
char *cleanup_arg = NULL;
582581
int annotate = 0, force = 0, lines = -1;
583582
int cmdmode = 0;
584583
const char *msgfile = NULL, *keyid = NULL;
585584
struct msg_arg msg = { 0, STRBUF_INIT };
586585
struct commit_list *with_commit = NULL;
586+
struct ref_transaction *transaction;
587+
struct strbuf err = STRBUF_INIT;
587588
struct option options[] = {
588589
OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
589590
{ OPTION_INTEGER, 'n', NULL, &lines, N_("n"),
@@ -729,14 +730,17 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
729730
if (annotate)
730731
create_tag(object, tag, &buf, &opt, prev, object);
731732

732-
lock = lock_any_ref_for_update(ref.buf, prev, 0, NULL);
733-
if (!lock)
734-
die(_("%s: cannot lock the ref"), ref.buf);
735-
if (write_ref_sha1(lock, object, NULL) < 0)
736-
die(_("%s: cannot update the ref"), ref.buf);
733+
transaction = ref_transaction_begin(&err);
734+
if (!transaction ||
735+
ref_transaction_update(transaction, ref.buf, object, prev,
736+
0, 1, &err) ||
737+
ref_transaction_commit(transaction, NULL, &err))
738+
die("%s", err.buf);
739+
ref_transaction_free(transaction);
737740
if (force && !is_null_sha1(prev) && hashcmp(prev, object))
738741
printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
739742

743+
strbuf_release(&err);
740744
strbuf_release(&buf);
741745
strbuf_release(&ref);
742746
return 0;

builtin/update-ref.c

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ static const char * const git_update_ref_usage[] = {
1212
NULL
1313
};
1414

15-
static struct ref_transaction *transaction;
16-
1715
static char line_termination = '\n';
1816
static int update_flags;
19-
static struct strbuf err = STRBUF_INIT;
2017

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

180-
static const char *parse_cmd_update(struct strbuf *input, const char *next)
177+
static const char *parse_cmd_update(struct ref_transaction *transaction,
178+
struct strbuf *input, const char *next)
181179
{
180+
struct strbuf err = STRBUF_INIT;
182181
char *refname;
183182
unsigned char new_sha1[20];
184183
unsigned char old_sha1[20];
@@ -204,12 +203,15 @@ static const char *parse_cmd_update(struct strbuf *input, const char *next)
204203

205204
update_flags = 0;
206205
free(refname);
206+
strbuf_release(&err);
207207

208208
return next;
209209
}
210210

211-
static const char *parse_cmd_create(struct strbuf *input, const char *next)
211+
static const char *parse_cmd_create(struct ref_transaction *transaction,
212+
struct strbuf *input, const char *next)
212213
{
214+
struct strbuf err = STRBUF_INIT;
213215
char *refname;
214216
unsigned char new_sha1[20];
215217

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

229-
ref_transaction_create(transaction, refname, new_sha1, update_flags);
231+
if (ref_transaction_create(transaction, refname, new_sha1,
232+
update_flags, &err))
233+
die("%s", err.buf);
230234

231235
update_flags = 0;
232236
free(refname);
237+
strbuf_release(&err);
233238

234239
return next;
235240
}
236241

237-
static const char *parse_cmd_delete(struct strbuf *input, const char *next)
242+
static const char *parse_cmd_delete(struct ref_transaction *transaction,
243+
struct strbuf *input, const char *next)
238244
{
245+
struct strbuf err = STRBUF_INIT;
239246
char *refname;
240247
unsigned char old_sha1[20];
241248
int have_old;
@@ -256,17 +263,21 @@ static const char *parse_cmd_delete(struct strbuf *input, const char *next)
256263
if (*next != line_termination)
257264
die("delete %s: extra input: %s", refname, next);
258265

259-
ref_transaction_delete(transaction, refname, old_sha1,
260-
update_flags, have_old);
266+
if (ref_transaction_delete(transaction, refname, old_sha1,
267+
update_flags, have_old, &err))
268+
die("%s", err.buf);
261269

262270
update_flags = 0;
263271
free(refname);
272+
strbuf_release(&err);
264273

265274
return next;
266275
}
267276

268-
static const char *parse_cmd_verify(struct strbuf *input, const char *next)
277+
static const char *parse_cmd_verify(struct ref_transaction *transaction,
278+
struct strbuf *input, const char *next)
269279
{
280+
struct strbuf err = STRBUF_INIT;
270281
char *refname;
271282
unsigned char new_sha1[20];
272283
unsigned char old_sha1[20];
@@ -294,6 +305,7 @@ static const char *parse_cmd_verify(struct strbuf *input, const char *next)
294305

295306
update_flags = 0;
296307
free(refname);
308+
strbuf_release(&err);
297309

298310
return next;
299311
}
@@ -307,7 +319,7 @@ static const char *parse_cmd_option(struct strbuf *input, const char *next)
307319
return next + 8;
308320
}
309321

310-
static void update_refs_stdin(void)
322+
static void update_refs_stdin(struct ref_transaction *transaction)
311323
{
312324
struct strbuf input = STRBUF_INIT;
313325
const char *next;
@@ -322,13 +334,13 @@ static void update_refs_stdin(void)
322334
else if (isspace(*next))
323335
die("whitespace before command: %s", next);
324336
else if (starts_with(next, "update "))
325-
next = parse_cmd_update(&input, next + 7);
337+
next = parse_cmd_update(transaction, &input, next + 7);
326338
else if (starts_with(next, "create "))
327-
next = parse_cmd_create(&input, next + 7);
339+
next = parse_cmd_create(transaction, &input, next + 7);
328340
else if (starts_with(next, "delete "))
329-
next = parse_cmd_delete(&input, next + 7);
341+
next = parse_cmd_delete(transaction, &input, next + 7);
330342
else if (starts_with(next, "verify "))
331-
next = parse_cmd_verify(&input, next + 7);
343+
next = parse_cmd_verify(transaction, &input, next + 7);
332344
else if (starts_with(next, "option "))
333345
next = parse_cmd_option(&input, next + 7);
334346
else
@@ -362,15 +374,21 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
362374
die("Refusing to perform update with empty message.");
363375

364376
if (read_stdin) {
365-
transaction = ref_transaction_begin();
377+
struct strbuf err = STRBUF_INIT;
378+
struct ref_transaction *transaction;
379+
380+
transaction = ref_transaction_begin(&err);
381+
if (!transaction)
382+
die("%s", err.buf);
366383
if (delete || no_deref || argc > 0)
367384
usage_with_options(git_update_ref_usage, options);
368385
if (end_null)
369386
line_termination = '\0';
370-
update_refs_stdin();
387+
update_refs_stdin(transaction);
371388
if (ref_transaction_commit(transaction, msg, &err))
372389
die("%s", err.buf);
373390
ref_transaction_free(transaction);
391+
strbuf_release(&err);
374392
return 0;
375393
}
376394

0 commit comments

Comments
 (0)