@@ -1313,6 +1313,43 @@ static int refdb_fs_backend__write_tail(
13131313 return error ;
13141314}
13151315
1316+ static void refdb_fs_backend__try_delete_empty_ref_hierarchie (
1317+ refdb_fs_backend * backend ,
1318+ const char * ref_name ,
1319+ bool reflog )
1320+ {
1321+ git_buf relative_path = GIT_BUF_INIT ;
1322+ git_buf base_path = GIT_BUF_INIT ;
1323+ size_t commonlen ;
1324+
1325+ assert (backend && ref_name );
1326+
1327+ if (git_buf_sets (& relative_path , ref_name ) < 0 )
1328+ goto cleanup ;
1329+
1330+ git_path_squash_slashes (& relative_path );
1331+ if ((commonlen = git_path_common_dirlen ("refs/heads/" , git_buf_cstr (& relative_path ))) == strlen ("refs/heads/" ) ||
1332+ (commonlen = git_path_common_dirlen ("refs/tags/" , git_buf_cstr (& relative_path ))) == strlen ("refs/tags/" ) ||
1333+ (commonlen = git_path_common_dirlen ("refs/remotes/" , git_buf_cstr (& relative_path ))) == strlen ("refs/remotes/" )) {
1334+
1335+ git_buf_truncate (& relative_path , commonlen );
1336+
1337+ if (reflog ) {
1338+ if (git_buf_join3 (& base_path , '/' , backend -> commonpath , GIT_REFLOG_DIR , git_buf_cstr (& relative_path )) < 0 )
1339+ goto cleanup ;
1340+ } else {
1341+ if (git_buf_joinpath (& base_path , backend -> commonpath , git_buf_cstr (& relative_path )) < 0 )
1342+ goto cleanup ;
1343+ }
1344+
1345+ git_futils_rmdir_r (ref_name + commonlen , git_buf_cstr (& base_path ), GIT_RMDIR_EMPTY_PARENTS | GIT_RMDIR_SKIP_ROOT );
1346+ }
1347+
1348+ cleanup :
1349+ git_buf_dispose (& relative_path );
1350+ git_buf_dispose (& base_path );
1351+ }
1352+
13161353static int refdb_fs_backend__delete (
13171354 git_refdb_backend * _backend ,
13181355 const char * ref_name ,
@@ -1393,7 +1430,8 @@ static int refdb_fs_backend__delete_tail(
13931430cleanup :
13941431 git_buf_dispose (& loose_path );
13951432 git_filebuf_cleanup (file );
1396-
1433+ if (loose_deleted )
1434+ refdb_fs_backend__try_delete_empty_ref_hierarchie (backend , ref_name , false);
13971435 return error ;
13981436}
13991437
@@ -2021,8 +2059,10 @@ static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name
20212059
20222060 error = retrieve_reflog_path (& path , repo , name );
20232061
2024- if (!error && git_path_exists (path .ptr ))
2062+ if (!error && git_path_exists (path .ptr )) {
20252063 error = p_unlink (path .ptr );
2064+ refdb_fs_backend__try_delete_empty_ref_hierarchie (backend , name , true);
2065+ }
20262066
20272067 git_buf_dispose (& path );
20282068
0 commit comments