Skip to content

Commit 7076784

Browse files
committed
maintenance: add prune-remote-refs task
Remote-tracking refs can accumulate in local repositories even as branches are deleted on remotes, impacting git performance. While git fetch --prune helps clean these up, it requires a full fetch operation which is expensive for large repositories. Add a new maintenance task 'prune-remote-refs' that runs 'git remote prune' for each configured remote. This provides an automated way to clean up stale remote-tracking refs without requiring full fetches or manual intervention. This task is disabled by default.
1 parent 063bceb commit 7076784

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

Documentation/git-maintenance.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ pack-refs::
158158
need to iterate across many references. See linkgit:git-pack-refs[1]
159159
for more information.
160160

161+
prune-remote-refs::
162+
The `prune-remote-refs` task runs `git remote prune` on each remote
163+
repository registered in the local repository. This task helps clean
164+
up deleted remote branches, improving the performance of operations
165+
that iterate through the refs. See linkgit:git-remote[1] for more
166+
information. This task is disabled by default and must be enabled
167+
manually.
168+
161169
OPTIONS
162170
-------
163171
--auto::

builtin/gc.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "lockfile.h"
2121
#include "parse-options.h"
2222
#include "run-command.h"
23+
#include "remote.h"
2324
#include "sigchain.h"
2425
#include "strvec.h"
2526
#include "commit.h"
@@ -913,6 +914,41 @@ static int maintenance_opt_schedule(const struct option *opt, const char *arg,
913914
return 0;
914915
}
915916

917+
static int collect_remote(struct remote *remote, void *cb_data)
918+
{
919+
struct string_list *list = cb_data;
920+
921+
if (!remote->url.nr)
922+
return 0;
923+
924+
string_list_append(list, remote->name);
925+
return 0;
926+
}
927+
928+
static int maintenance_task_prune_remote(struct maintenance_run_opts *opts,
929+
struct gc_config *cfg UNUSED)
930+
{
931+
struct string_list_item *item;
932+
struct string_list remotes_list = STRING_LIST_INIT_NODUP;
933+
const char *dest_prefix = "refs/remotes/";
934+
struct child_process child = CHILD_PROCESS_INIT;
935+
int result = 0;
936+
937+
for_each_remote(collect_remote, &remotes_list);
938+
939+
for_each_string_list_item (item, &remotes_list) {
940+
const char *remote_name = item->string;
941+
child.git_cmd = 1;
942+
strvec_pushl(&child.args, "remote", "prune", remote_name, NULL);
943+
944+
if (run_command(&child))
945+
result = error(_("failed to prune '%s'"), remote_name);
946+
}
947+
948+
string_list_clear(&remotes_list, 0);
949+
return result;
950+
}
951+
916952
/* Remember to update object flag allocation in object.h */
917953
#define SEEN (1u<<0)
918954

@@ -1375,6 +1411,7 @@ enum maintenance_task_label {
13751411
TASK_GC,
13761412
TASK_COMMIT_GRAPH,
13771413
TASK_PACK_REFS,
1414+
TASK_PRUNE_REMOTE_REFS,
13781415

13791416
/* Leave as final value */
13801417
TASK__COUNT
@@ -1411,6 +1448,10 @@ static struct maintenance_task tasks[] = {
14111448
maintenance_task_pack_refs,
14121449
pack_refs_condition,
14131450
},
1451+
[TASK_PRUNE_REMOTE_REFS] = {
1452+
"prune-remote-refs",
1453+
maintenance_task_prune_remote,
1454+
},
14141455
};
14151456

14161457
static int compare_tasks_by_selection(const void *a_, const void *b_)
@@ -1505,6 +1546,8 @@ static void initialize_maintenance_strategy(void)
15051546
tasks[TASK_LOOSE_OBJECTS].schedule = SCHEDULE_DAILY;
15061547
tasks[TASK_PACK_REFS].enabled = 1;
15071548
tasks[TASK_PACK_REFS].schedule = SCHEDULE_WEEKLY;
1549+
tasks[TASK_PRUNE_REMOTE_REFS].enabled = 0;
1550+
tasks[TASK_PRUNE_REMOTE_REFS].schedule = SCHEDULE_DAILY;
15081551
}
15091552
}
15101553

t/t7900-maintenance.sh

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,50 @@ test_expect_success 'pack-refs task' '
446446
test_subcommand git pack-refs --all --prune <pack-refs.txt
447447
'
448448

449+
test_expect_success 'prune-remote-refs task not enabled by default' '
450+
git clone . prune-test &&
451+
(
452+
cd prune-test &&
453+
GIT_TRACE2_EVENT="$(pwd)/prune.txt" git maintenance run 2>err &&
454+
test_subcommand ! git remote prune origin <prune.txt
455+
)
456+
'
457+
458+
test_expect_success 'prune-remote-refs task cleans stale remote refs' '
459+
test_commit initial &&
460+
461+
# Create two separate remote repos
462+
git clone . remote1 &&
463+
git clone . remote2 &&
464+
465+
git clone . prune-test-clean &&
466+
(
467+
cd prune-test-clean &&
468+
git config maintenance.prune-remote-refs.enabled true &&
469+
470+
# Add both remotes
471+
git remote add remote1 "../remote1" &&
472+
git remote add remote2 "../remote2" &&
473+
474+
# Create and push branches to both remotes
475+
git branch -f side2 HEAD &&
476+
git push remote1 side2 &&
477+
git push remote2 side2 &&
478+
479+
# Rename branches in each remote to simulate a stale branch
480+
git -C ../remote1 branch -m side2 side3 &&
481+
git -C ../remote2 branch -m side2 side4 &&
482+
483+
GIT_TRACE2_EVENT="$(pwd)/prune.txt" git maintenance run --task=prune-remote-refs &&
484+
485+
# Verify pruning happened for both remotes
486+
test_subcommand git remote prune remote1 <prune.txt &&
487+
test_subcommand git remote prune remote2 <prune.txt &&
488+
test_must_fail git rev-parse refs/remotes/remote1/side2 &&
489+
test_must_fail git rev-parse refs/remotes/remote2/side2
490+
)
491+
'
492+
449493
test_expect_success '--auto and --schedule incompatible' '
450494
test_must_fail git maintenance run --auto --schedule=daily 2>err &&
451495
test_grep "at most one" err
@@ -942,7 +986,7 @@ test_expect_success 'register and unregister bare repo' '
942986
test_expect_success 'failed schedule prevents config change' '
943987
git init --bare failcase &&
944988
945-
for scheduler in crontab launchctl schtasks systemctl
989+
for scheduler in crontab launchctl schtasks
946990
do
947991
GIT_TEST_MAINT_SCHEDULER="$scheduler:false" &&
948992
export GIT_TEST_MAINT_SCHEDULER &&

0 commit comments

Comments
 (0)