Skip to content

Commit a07d37b

Browse files
edith007gitster
authored andcommitted
replay: add replay.defaultAction config option
Add a configuration option to control the default behavior of git replay for updating references. This allows users who prefer the traditional pipeline output to set it once in their config instead of passing --update-refs=print with every command. The config option uses enum string values for extensibility: * replay.defaultAction = update-refs (default): atomic ref updates * replay.defaultAction = show-commands: output commands for pipeline The command-line --update-refs option always overrides the config setting, allowing users to temporarily change behavior for a single invocation. Implementation details: In cmd_replay(), before parsing command-line options, we read the configuration using repo_config_get_string_tmp(). If the config variable is set, we validate the value and map it to an internal mode: Config value Internal mode Behavior ──────────────────────────────────────────────────────────────── "update-refs" "yes" Atomic ref updates (default) "show-commands" "print" Pipeline output (not set) "yes" Atomic ref updates (default) (invalid) error Die with helpful message If an invalid value is provided, we die() immediately with an error message explaining the valid options. This catches configuration errors early and provides clear guidance to users. The command-line --update-refs option, when provided, overrides the config value. This precedence allows users to set their preferred default while still having per-invocation control: git config replay.defaultAction show-commands # Set default git replay --update-refs=yes --onto main topic # Override once The config option uses different value names ('update-refs' vs 'show-commands') compared to the command-line option ('yes' vs 'print') for semantic clarity. The config values describe what action is being taken, while the command-line values are terse for typing convenience. The enum string design (rather than a boolean like 'replay.updateRefs') allows future expansion to additional modes without requiring new configuration variables. For example, if we later add custom format support (--update-refs=format), we can extend the config to support 'replay.defaultAction = format' without breaking existing configurations or requiring a second config variable. Helped-by: Junio C Hamano <gitster@pobox.com> Helped-by: Elijah Newren <newren@gmail.com> Helped-by: Phillip Wood <phillip.wood123@gmail.com> Signed-off-by: Siddharth Asthana <siddharthasthana31@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 1cbe82c commit a07d37b

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

Documentation/config/replay.adoc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
replay.defaultAction::
2+
Control the default behavior of `git replay` for updating references.
3+
Can be set to:
4+
+
5+
--
6+
* `update-refs` (default): Update refs directly using an atomic transaction.
7+
* `show-commands`: Output update-ref commands that can be piped to
8+
`git update-ref --stdin`.
9+
--
10+
+
11+
This can be overridden with the `--update-refs` command-line option.
12+
Note that the command-line option uses slightly different values
13+
(`yes` and `print`) for brevity, but they map to the same behavior
14+
as the config values.

builtin/replay.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "git-compat-util.h"
99

1010
#include "builtin.h"
11+
#include "config.h"
1112
#include "environment.h"
1213
#include "hex.h"
1314
#include "lockfile.h"
@@ -359,9 +360,22 @@ int cmd_replay(int argc,
359360
die_for_incompatible_opt2(!!advance_name_opt, "--advance",
360361
contained, "--contained");
361362

362-
/* Set default mode if not specified */
363-
if (!update_refs_mode)
364-
update_refs_mode = "yes";
363+
/* Set default mode from config if not specified on command line */
364+
if (!update_refs_mode) {
365+
const char *config_value = NULL;
366+
if (!repo_config_get_string_tmp(repo, "replay.defaultaction", &config_value)) {
367+
if (!strcmp(config_value, "update-refs"))
368+
update_refs_mode = "yes";
369+
else if (!strcmp(config_value, "show-commands"))
370+
update_refs_mode = "print";
371+
else
372+
die(_("invalid value for replay.defaultAction: '%s' "
373+
"(expected 'update-refs' or 'show-commands')"),
374+
config_value);
375+
} else {
376+
update_refs_mode = "yes";
377+
}
378+
}
365379

366380
/* Validate update-refs mode */
367381
if (strcmp(update_refs_mode, "yes") && strcmp(update_refs_mode, "print"))

t/t3650-replay-basics.sh

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ test_expect_success 'replay atomic guarantee: all refs updated or none' '
299299
# Store original states
300300
START_TOPIC1=$(git rev-parse topic1) &&
301301
START_TOPIC3=$(git rev-parse topic3) &&
302-
test_when_finished "git branch -f topic1 $START_TOPIC1 && git branch -f topic3 $START_TOPIC3 && rm -f .git/refs/heads/topic1.lock" &&
302+
test_when_finished "git branch -f topic1 $START_TOPIC1 && git branch -f topic3 $START_TOPIC3" &&
303303
304304
# Create a lock on topic1 to simulate a concurrent update
305305
>.git/refs/heads/topic1.lock &&
@@ -308,6 +308,9 @@ test_expect_success 'replay atomic guarantee: all refs updated or none' '
308308
# This should fail atomically - neither branch should be updated
309309
test_must_fail git replay --contained --onto main main..topic3 2>error &&
310310
311+
# Remove the lock before checking refs
312+
rm -f .git/refs/heads/topic1.lock &&
313+
311314
# Verify the transaction failed
312315
grep "failed to commit ref transaction" error &&
313316
@@ -354,4 +357,46 @@ test_expect_success 'replay validates --update-refs mode values' '
354357
grep "invalid value for --update-refs" error
355358
'
356359

360+
test_expect_success 'replay.defaultAction config option' '
361+
# Store original state
362+
START=$(git rev-parse topic2) &&
363+
test_when_finished "git branch -f topic2 $START && git config --unset replay.defaultAction" &&
364+
365+
# Set config to show-commands
366+
git config replay.defaultAction show-commands &&
367+
git replay --onto main topic1..topic2 >output &&
368+
test_line_count = 1 output &&
369+
grep "^update refs/heads/topic2 " output &&
370+
371+
# Reset and test update-refs mode
372+
git branch -f topic2 $START &&
373+
git config replay.defaultAction update-refs &&
374+
git replay --onto main topic1..topic2 >output &&
375+
test_must_be_empty output &&
376+
377+
# Verify ref was updated
378+
git log --format=%s topic2 >actual &&
379+
test_write_lines E D M L B A >expect &&
380+
test_cmp expect actual
381+
'
382+
383+
test_expect_success 'command-line --update-refs overrides config' '
384+
# Store original state
385+
START=$(git rev-parse topic2) &&
386+
test_when_finished "git branch -f topic2 $START && git config --unset replay.defaultAction" &&
387+
388+
# Set config to update-refs but use --update-refs=print
389+
git config replay.defaultAction update-refs &&
390+
git replay --update-refs=print --onto main topic1..topic2 >output &&
391+
test_line_count = 1 output &&
392+
grep "^update refs/heads/topic2 " output
393+
'
394+
395+
test_expect_success 'invalid replay.defaultAction value' '
396+
test_when_finished "git config --unset replay.defaultAction" &&
397+
git config replay.defaultAction invalid &&
398+
test_must_fail git replay --onto main topic1..topic2 2>error &&
399+
grep "invalid value for replay.defaultAction" error
400+
'
401+
357402
test_done

0 commit comments

Comments
 (0)