Skip to content

Commit 42cb1dc

Browse files
committed
Remove an empty directory .gitignore placeholder once the directory becomes non-empty
1 parent 0833cc2 commit 42cb1dc

File tree

2 files changed

+424
-0
lines changed

2 files changed

+424
-0
lines changed

src/svn.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ class SvnRevision
386386
int fetchIgnoreProps(QString *ignore, apr_pool_t *pool, const char *key, svn_fs_root_t *fs_root);
387387
int fetchUnknownProps(apr_pool_t *pool, const char *key, svn_fs_root_t *fs_root);
388388
private:
389+
int checkParentNoLongerEmpty(apr_pool_t *pool, const char *key, QString path, Repository::Transaction *txn);
389390
void splitPathName(const Rules::Match &rule, const QString &pathName, QString *svnprefix_p,
390391
QString *repository_p, QString *effectiveRepository_p, QString *branch_p, QString *path_p);
391392
int recursiveDumpDir(Repository::Transaction *txn, svn_fs_t *fs, svn_fs_root_t *fs_root,
@@ -868,6 +869,7 @@ int SvnRevision::exportInternal(const char *key, const svn_fs_path_change2_t *ch
868869
if(ruledebug)
869870
qDebug() << "add/change file (" << key << "->" << branch << path << ")";
870871
dumpBlob(txn, fs_root, key, path, pool);
872+
checkParentNoLongerEmpty(pool, key, path, txn);
871873
} else {
872874
if(ruledebug)
873875
qDebug() << "add/change dir (" << key << "->" << branch << path << ")";
@@ -897,6 +899,7 @@ int SvnRevision::exportInternal(const char *key, const svn_fs_path_change2_t *ch
897899
txn->deleteFile(path);
898900
checkParentNotEmpty(pool, key, path, fs_root, txn);
899901
}
902+
checkParentNoLongerEmpty(pool, key, path, txn);
900903

901904
// Add GitIgnore with svn:ignore
902905
int ignoreSet = false;
@@ -1151,6 +1154,67 @@ int SvnRevision::checkParentNotEmpty(apr_pool_t *pool, const char *key, QString
11511154
return EXIT_SUCCESS;
11521155
}
11531156

1157+
int SvnRevision::checkParentNoLongerEmpty(apr_pool_t *pool, const char *key, QString path, Repository::Transaction *txn) {
1158+
if (!CommandLineParser::instance()->contains("empty-dirs")) {
1159+
return EXIT_FAILURE;
1160+
}
1161+
QString slash = "/";
1162+
QString qkey = QString::fromUtf8(key);
1163+
while (qkey.endsWith('/'))
1164+
qkey = qkey.mid(0, qkey.length()-1);
1165+
int index = qkey.lastIndexOf(slash);
1166+
if (index == -1) {
1167+
return EXIT_FAILURE;
1168+
}
1169+
QString parentKey = qkey.left(index);
1170+
1171+
AprAutoPool subpool(pool);
1172+
svn_fs_root_t *previous_fs_root;
1173+
if (svn_fs_revision_root(&previous_fs_root, fs, revnum - 1, subpool) != SVN_NO_ERROR) {
1174+
return EXIT_FAILURE;
1175+
}
1176+
1177+
apr_hash_t *entries;
1178+
// directory did not exist
1179+
if (svn_fs_dir_entries(&entries, previous_fs_root, parentKey.toStdString().c_str(), pool) != SVN_NO_ERROR) {
1180+
return EXIT_FAILURE;
1181+
}
1182+
1183+
// directory was not empty
1184+
if (apr_hash_count(entries)!=0) {
1185+
return EXIT_FAILURE;
1186+
}
1187+
1188+
QString cleanPath = path;
1189+
while (cleanPath.endsWith('/'))
1190+
cleanPath = cleanPath.mid(0, cleanPath.length()-1);
1191+
index = cleanPath.lastIndexOf(slash);
1192+
QString parentPath = cleanPath.left(index);
1193+
1194+
// we are in the root directory, we did not add a .gitignore here
1195+
if (index == -1) {
1196+
return EXIT_FAILURE;
1197+
}
1198+
1199+
// if svn-ignore should have added a .gitignore file, do not consider the directory empty
1200+
// if svn:ignore could not be determined, stay safe and do not consider the directory empty
1201+
// even if then an empty .gitignore might be left over
1202+
QString svnignore;
1203+
if (CommandLineParser::instance()->contains("svn-ignore")) {
1204+
if (fetchIgnoreProps(&svnignore, pool, parentKey.toStdString().c_str(), previous_fs_root) != EXIT_SUCCESS) {
1205+
qWarning() << "Error fetching svn-properties (" << parentKey << ")";
1206+
return EXIT_FAILURE;
1207+
} else if (!svnignore.isNull()) {
1208+
return EXIT_FAILURE;
1209+
}
1210+
}
1211+
1212+
// Delete gitignore-File
1213+
QString gitIgnorePath = parentPath + "/.gitignore";
1214+
txn->deleteFile(gitIgnorePath);
1215+
return EXIT_SUCCESS;
1216+
}
1217+
11541218
int SvnRevision::addGitIgnoreOnBranch(apr_pool_t *pool, QString key, QString path,
11551219
svn_fs_root_t *fs_root, Repository::Transaction *txn)
11561220
{

0 commit comments

Comments
 (0)