Skip to content

Commit 4635371

Browse files
KarthikNayakgitster
authored andcommitted
builtin/refs: add '--skip-reflog' flag to bypass reflog migration
The 'git-refs(1)' migrate subcommand, which transfers repositories between reference backends, currently migrates reflogs by default as of 246cebe (refs: add support for migrating reflogs, 2024-12-16). While this behavior is desirable for most client-side repositories, server-side repositories are not expected to contain reflogs. However, due to historical reasons, some may still have them. This could be caused, for example, by bugs, misconfiguration, or an administrator enabling reflogs on the server for debugging purposes. To address this, introduce the --skip-reflog flag, allowing users to bypass reflog migration. This ensures that the repository ends up in the expected state after migration. Helped-by: Patrick Steinhardt <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e2067b4 commit 4635371

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

builtin/refs.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ static int cmd_refs_migrate(int argc, const char **argv, const char *prefix,
3030
OPT_BIT(0, "dry-run", &flags,
3131
N_("perform a non-destructive dry-run"),
3232
REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN),
33+
OPT_BIT(0, "skip-reflog", &flags,
34+
N_("skip migrating reflogs"),
35+
REPO_MIGRATE_REF_STORAGE_FORMAT_SKIP_REFLOG),
3336
OPT_END(),
3437
};
3538
struct strbuf errbuf = STRBUF_INIT;

refs.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3035,9 +3035,11 @@ int repo_migrate_ref_storage_format(struct repository *repo,
30353035
if (ret < 0)
30363036
goto done;
30373037

3038-
ret = refs_for_each_reflog(old_refs, migrate_one_reflog, &data);
3039-
if (ret < 0)
3040-
goto done;
3038+
if (!(flags & REPO_MIGRATE_REF_STORAGE_FORMAT_SKIP_REFLOG)) {
3039+
ret = refs_for_each_reflog(old_refs, migrate_one_reflog, &data);
3040+
if (ret < 0)
3041+
goto done;
3042+
}
30413043

30423044
ret = ref_transaction_commit(transaction, errbuf);
30433045
if (ret < 0)

refs.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,8 +1157,11 @@ int is_pseudo_ref(const char *refname);
11571157
* - REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN: perform a dry-run migration
11581158
* without touching the main repository. The result will be written into a
11591159
* temporary ref storage directory.
1160+
*
1161+
* - REPO_MIGRATE_REF_STORAGE_FORMAT_SKIP_REFLOG: skip migration of reflogs.
11601162
*/
1161-
#define REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN (1 << 0)
1163+
#define REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN (1 << 0)
1164+
#define REPO_MIGRATE_REF_STORAGE_FORMAT_SKIP_REFLOG (1 << 1)
11621165

11631166
/*
11641167
* Migrate the ref storage format used by the repository to the

t/t1460-refs-migrate.sh

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,21 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
99

1010
# Migrate the provided repository from one format to the other and
1111
# verify that the references and logs are migrated over correctly.
12-
# Usage: test_migration <repo> <format> <skip_reflog_verify>
12+
# Usage: test_migration <repo> <format> [<skip_reflog_verify> [<options...>]]
1313
# <repo> is the relative path to the repo to be migrated.
1414
# <format> is the ref format to be migrated to.
15-
# <skip_reflog_verify> (true or false) whether to skip reflog verification.
15+
# <skip_reflog_verify> (default: false) whether to skip reflog verification.
16+
# <options...> are other options be passed directly to 'git refs migrate'.
1617
test_migration () {
1718
repo=$1 &&
1819
format=$2 &&
19-
skip_reflog_verify=${3:-false} &&
20+
shift 2 &&
21+
skip_reflog_verify=false &&
22+
if test $# -ge 1
23+
then
24+
skip_reflog_verify=$1
25+
shift
26+
fi &&
2027
git -C "$repo" for-each-ref --include-root-refs \
2128
--format='%(refname) %(objectname) %(symref)' >expect &&
2229
if ! $skip_reflog_verify
@@ -25,7 +32,7 @@ test_migration () {
2532
git -C "$repo" reflog list >expect_log_list
2633
fi &&
2734

28-
git -C "$repo" refs migrate --ref-format="$2" &&
35+
git -C "$repo" refs migrate --ref-format="$format" "$@" &&
2936

3037
git -C "$repo" for-each-ref --include-root-refs \
3138
--format='%(refname) %(objectname) %(symref)' >actual &&
@@ -241,6 +248,19 @@ do
241248
test_cmp expect.reflog actual.reflog
242249
)
243250
'
251+
252+
test_expect_success "$from_format -> $to_format: skip reflog with --skip-reflog" '
253+
test_when_finished "rm -rf repo" &&
254+
git init --ref-format=$from_format repo &&
255+
test_commit -C repo initial &&
256+
# we see that the repository contains reflogs.
257+
git -C repo reflog --all >reflogs &&
258+
test_line_count = 2 reflogs &&
259+
test_migration repo "$to_format" true --skip-reflog &&
260+
# there should be no reflogs post migration.
261+
git -C repo reflog --all >reflogs &&
262+
test_must_be_empty reflogs
263+
'
244264
done
245265
done
246266

0 commit comments

Comments
 (0)