@@ -1305,6 +1305,43 @@ static int refdb_fs_backend__write_tail(
13051305 return error ;
13061306}
13071307
1308+ static void refdb_fs_backend__try_delete_empty_ref_hierarchie (
1309+ refdb_fs_backend * backend ,
1310+ const char * ref_name ,
1311+ bool reflog )
1312+ {
1313+ git_buf relative_path = GIT_BUF_INIT ;
1314+ git_buf base_path = GIT_BUF_INIT ;
1315+ size_t commonlen ;
1316+
1317+ assert (backend && ref_name );
1318+
1319+ if (git_buf_sets (& relative_path , ref_name ) < 0 )
1320+ goto cleanup ;
1321+
1322+ git_path_squash_slashes (& relative_path );
1323+ if ((commonlen = git_path_common_dirlen ("refs/heads/" , git_buf_cstr (& relative_path ))) == strlen ("refs/heads/" ) ||
1324+ (commonlen = git_path_common_dirlen ("refs/tags/" , git_buf_cstr (& relative_path ))) == strlen ("refs/tags/" ) ||
1325+ (commonlen = git_path_common_dirlen ("refs/remotes/" , git_buf_cstr (& relative_path ))) == strlen ("refs/remotes/" )) {
1326+
1327+ git_buf_truncate (& relative_path , commonlen );
1328+
1329+ if (reflog ) {
1330+ if (git_buf_join3 (& base_path , '/' , backend -> commonpath , GIT_REFLOG_DIR , git_buf_cstr (& relative_path )) < 0 )
1331+ goto cleanup ;
1332+ } else {
1333+ if (git_buf_joinpath (& base_path , backend -> commonpath , git_buf_cstr (& relative_path )) < 0 )
1334+ goto cleanup ;
1335+ }
1336+
1337+ git_futils_rmdir_r (ref_name + commonlen , git_buf_cstr (& base_path ), GIT_RMDIR_EMPTY_PARENTS | GIT_RMDIR_SKIP_ROOT );
1338+ }
1339+
1340+ cleanup :
1341+ git_buf_dispose (& relative_path );
1342+ git_buf_dispose (& base_path );
1343+ }
1344+
13081345static int refdb_fs_backend__delete (
13091346 git_refdb_backend * _backend ,
13101347 const char * ref_name ,
@@ -1385,7 +1422,8 @@ static int refdb_fs_backend__delete_tail(
13851422cleanup :
13861423 git_buf_dispose (& loose_path );
13871424 git_filebuf_cleanup (file );
1388-
1425+ if (loose_deleted )
1426+ refdb_fs_backend__try_delete_empty_ref_hierarchie (backend , ref_name , false);
13891427 return error ;
13901428}
13911429
@@ -2013,8 +2051,10 @@ static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name
20132051
20142052 error = retrieve_reflog_path (& path , repo , name );
20152053
2016- if (!error && git_path_exists (path .ptr ))
2054+ if (!error && git_path_exists (path .ptr )) {
20172055 error = p_unlink (path .ptr );
2056+ refdb_fs_backend__try_delete_empty_ref_hierarchie (backend , name , true);
2057+ }
20182058
20192059 git_buf_dispose (& path );
20202060
0 commit comments