@@ -143,30 +143,71 @@ int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);
143
143
int dwim_log (const char * str , int len , unsigned char * sha1 , char * * ref );
144
144
145
145
/*
146
- * A ref_transaction represents a collection of ref updates
147
- * that should succeed or fail together.
146
+ * A ref_transaction represents a collection of reference updates that
147
+ * should succeed or fail together.
148
148
*
149
149
* Calling sequence
150
150
* ----------------
151
+ *
151
152
* - Allocate and initialize a `struct ref_transaction` by calling
152
153
* `ref_transaction_begin()`.
153
154
*
154
- * - List intended ref updates by calling functions like
155
- * `ref_transaction_update()` and `ref_transaction_create()`.
156
- *
157
- * - Call `ref_transaction_commit()` to execute the transaction.
158
- * If this succeeds, the ref updates will have taken place and
159
- * the transaction cannot be rolled back.
160
- *
161
- * - Instead of `ref_transaction_commit`, use
162
- * `initial_ref_transaction_commit()` if the ref database is known
163
- * to be empty (e.g. during clone). This is likely to be much
164
- * faster.
165
- *
166
- * - At any time call `ref_transaction_free()` to discard the
167
- * transaction and free associated resources. In particular,
168
- * this rolls back the transaction if it has not been
169
- * successfully committed.
155
+ * - Specify the intended ref updates by calling one or more of the
156
+ * following functions:
157
+ * - `ref_transaction_update()`
158
+ * - `ref_transaction_create()`
159
+ * - `ref_transaction_delete()`
160
+ * - `ref_transaction_verify()`
161
+ *
162
+ * - Then either:
163
+ *
164
+ * - Optionally call `ref_transaction_prepare()` to prepare the
165
+ * transaction. This locks all references, checks preconditions,
166
+ * etc. but doesn't finalize anything. If this step fails, the
167
+ * transaction has been closed and can only be freed. If this step
168
+ * succeeds, then `ref_transaction_commit()` is almost certain to
169
+ * succeed. However, you can still call `ref_transaction_abort()`
170
+ * if you decide not to commit the transaction after all.
171
+ *
172
+ * - Call `ref_transaction_commit()` to execute the transaction,
173
+ * make the changes permanent, and release all locks. If you
174
+ * haven't already called `ref_transaction_prepare()`, then
175
+ * `ref_transaction_commit()` calls it for you.
176
+ *
177
+ * Or
178
+ *
179
+ * - Call `initial_ref_transaction_commit()` if the ref database is
180
+ * known to be empty and have no other writers (e.g. during
181
+ * clone). This is likely to be much faster than
182
+ * `ref_transaction_commit()`. `ref_transaction_prepare()` should
183
+ * *not* be called before `initial_ref_transaction_commit()`.
184
+ *
185
+ * - Then finally, call `ref_transaction_free()` to free the
186
+ * `ref_transaction` data structure.
187
+ *
188
+ * At any time before calling `ref_transaction_commit()`, you can call
189
+ * `ref_transaction_abort()` to abort the transaction, rollback any
190
+ * locks, and free any associated resources (including the
191
+ * `ref_transaction` data structure).
192
+ *
193
+ * Putting it all together, a complete reference update looks like
194
+ *
195
+ * struct ref_transaction *transaction;
196
+ * struct strbuf err = STRBUF_INIT;
197
+ * int ret = 0;
198
+ *
199
+ * transaction = ref_store_transaction_begin(refs, &err);
200
+ * if (!transaction ||
201
+ * ref_transaction_update(...) ||
202
+ * ref_transaction_create(...) ||
203
+ * ...etc... ||
204
+ * ref_transaction_commit(transaction, &err)) {
205
+ * error("%s", err.buf);
206
+ * ret = -1;
207
+ * }
208
+ * ref_transaction_free(transaction);
209
+ * strbuf_release(&err);
210
+ * return ret;
170
211
*
171
212
* Error handling
172
213
* --------------
@@ -183,8 +224,9 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **ref);
183
224
* -------
184
225
*
185
226
* Note that no locks are taken, and no refs are read, until
186
- * `ref_transaction_commit` is called. So `ref_transaction_verify`
187
- * won't report a verification failure until the commit is attempted.
227
+ * `ref_transaction_prepare()` or `ref_transaction_commit()` is
228
+ * called. So, for example, `ref_transaction_verify()` won't report a
229
+ * verification failure until the commit is attempted.
188
230
*/
189
231
struct ref_transaction ;
190
232
@@ -523,19 +565,47 @@ int ref_transaction_verify(struct ref_transaction *transaction,
523
565
unsigned int flags ,
524
566
struct strbuf * err );
525
567
526
- /*
527
- * Commit all of the changes that have been queued in transaction, as
528
- * atomically as possible.
529
- *
530
- * Returns 0 for success, or one of the below error codes for errors.
531
- */
532
568
/* Naming conflict (for example, the ref names A and A/B conflict). */
533
569
#define TRANSACTION_NAME_CONFLICT -1
534
570
/* All other errors. */
535
571
#define TRANSACTION_GENERIC_ERROR -2
572
+
573
+ /*
574
+ * Perform the preparatory stages of commiting `transaction`. Acquire
575
+ * any needed locks, check preconditions, etc.; basically, do as much
576
+ * as possible to ensure that the transaction will be able to go
577
+ * through, stopping just short of making any irrevocable or
578
+ * user-visible changes. The updates that this function prepares can
579
+ * be finished up by calling `ref_transaction_commit()` or rolled back
580
+ * by calling `ref_transaction_abort()`.
581
+ *
582
+ * On success, return 0 and leave the transaction in "prepared" state.
583
+ * On failure, abort the transaction, write an error message to `err`,
584
+ * and return one of the `TRANSACTION_*` constants.
585
+ *
586
+ * Callers who don't need such fine-grained control over commiting
587
+ * reference transactions should just call `ref_transaction_commit()`.
588
+ */
589
+ int ref_transaction_prepare (struct ref_transaction * transaction ,
590
+ struct strbuf * err );
591
+
592
+ /*
593
+ * Commit all of the changes that have been queued in transaction, as
594
+ * atomically as possible. On success, return 0 and leave the
595
+ * transaction in "closed" state. On failure, roll back the
596
+ * transaction, write an error message to `err`, and return one of the
597
+ * `TRANSACTION_*` constants
598
+ */
536
599
int ref_transaction_commit (struct ref_transaction * transaction ,
537
600
struct strbuf * err );
538
601
602
+ /*
603
+ * Abort `transaction`, which has been begun and possibly prepared,
604
+ * but not yet committed.
605
+ */
606
+ int ref_transaction_abort (struct ref_transaction * transaction ,
607
+ struct strbuf * err );
608
+
539
609
/*
540
610
* Like ref_transaction_commit(), but optimized for creating
541
611
* references when originally initializing a repository (e.g., by "git
@@ -551,7 +621,7 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,
551
621
struct strbuf * err );
552
622
553
623
/*
554
- * Free an existing transaction and all associated data.
624
+ * Free `* transaction` and all associated data.
555
625
*/
556
626
void ref_transaction_free (struct ref_transaction * transaction );
557
627
0 commit comments