Skip to content

Commit 1b50b04

Browse files
committed
Remove an empty directory .gitignore placeholder once the directory becomes non-empty
1 parent f8bb129 commit 1b50b04

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;
@@ -1168,6 +1171,67 @@ int SvnRevision::checkParentNotEmpty(apr_pool_t *pool, const char *key, QString
11681171
return EXIT_SUCCESS;
11691172
}
11701173

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

0 commit comments

Comments
 (0)