Skip to content

Fix .gitignore handling in root directory #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/svn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1099,14 +1099,20 @@ int SvnRevision::addGitIgnore(apr_pool_t *pool, const char *key, QString path,
}

// Add gitignore-File
QString gitIgnorePath = path + ".gitignore";
QString gitIgnorePath = path == "/" ? ".gitignore" : path + ".gitignore";
if (content) {
QIODevice *io = txn->addFile(gitIgnorePath, 33188, strlen(content));
if (!CommandLineParser::instance()->contains("dry-run")) {
io->write(content);
io->putChar('\n');
}
} else {
// no empty placeholder .gitignore for repository root
// this should be handled previously already, just a
// security measure here.
if (path == "/") {
return EXIT_FAILURE;
}
QIODevice *io = txn->addFile(gitIgnorePath, 33188, 0);
if (!CommandLineParser::instance()->contains("dry-run")) {
io->putChar('\n');
Expand Down Expand Up @@ -1145,6 +1151,11 @@ int SvnRevision::checkParentNotEmpty(apr_pool_t *pool, const char *key, QString
index = cleanPath.lastIndexOf(slash);
QString parentPath = cleanPath.left(index);

// we are in the root directory, do not add a .gitignore here
if (index == -1) {
return EXIT_FAILURE;
}

// if svn-ignore should have added a .gitignore file, do not overwrite it with an empty one
// if svn:ignore could not be determined, stay safe and do not overwrite the .gitignore file
// even if then an empty directory might be missing
Expand Down
90 changes: 90 additions & 0 deletions test/empty-dirs.bats
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,52 @@ load 'common'
assert_equal "$(git -C git-repo show master:dir-a/.gitignore)" '/ignore-a'
}

@test 'deleting last file from root should not add empty .gitignore with empty-dirs-parameter' {
touch file-a
svn add file-a
svn commit -m 'add file-a'
svn rm file-a
svn commit -m 'delete file-a'

cd "$TEST_TEMP_DIR"
svn2git "$SVN_REPO" --empty-dirs --rules <(echo "
create repository git-repo
end repository

match /
repository git-repo
branch master
end match
")

refute git -C git-repo show master:.gitignore
refute git -C git-repo show master:file-a/.gitignore
}

@test 'deleting last file from root should not add empty .gitignore with empty-dirs-parameter (nested)' {
svn mkdir project-a
cd project-a
touch file-a
svn add file-a
svn commit -m 'add file-a'
svn rm file-a
svn commit -m 'delete file-a'

cd "$TEST_TEMP_DIR"
svn2git "$SVN_REPO" --empty-dirs --rules <(echo "
create repository git-repo
end repository

match /project-a/
repository git-repo
branch master
end match
")

refute git -C git-repo show master:.gitignore
refute git -C git-repo show master:file-a/.gitignore
}

@test 'deleting last directory from a directory should add empty .gitignore with empty-dirs-parameter' {
svn mkdir --parents dir-a/subdir-a
svn commit -m 'add dir-a/subdir-a'
Expand Down Expand Up @@ -414,6 +460,50 @@ load 'common'
assert_equal "$(git -C git-repo show master:dir-a/.gitignore)" '/ignore-a'
}

@test 'deleting last directory from root should not add empty .gitignore with empty-dirs-parameter' {
svn mkdir dir-a
svn commit -m 'add dir-a'
svn rm dir-a
svn commit -m 'delete dir-a'

cd "$TEST_TEMP_DIR"
svn2git "$SVN_REPO" --empty-dirs --rules <(echo "
create repository git-repo
end repository

match /
repository git-repo
branch master
end match
")

refute git -C git-repo show master:.gitignore
refute git -C git-repo show master:dir-a/.gitignore
}

@test 'deleting last directory from root should not add empty .gitignore with empty-dirs-parameter (nested)' {
svn mkdir project-a
cd project-a
svn mkdir dir-a
svn commit -m 'add dir-a'
svn rm dir-a
svn commit -m 'delete dir-a'

cd "$TEST_TEMP_DIR"
svn2git "$SVN_REPO" --empty-dirs --rules <(echo "
create repository git-repo
end repository

match /project-a/
repository git-repo
branch master
end match
")

refute git -C git-repo show master:.gitignore
refute git -C git-repo show master:dir-a/.gitignore
}

@test 'copying an empty directory should put empty .gitignore file to copy with empty-dirs parameter' {
svn mkdir dir-a
svn commit -m 'add dir-a'
Expand Down
52 changes: 52 additions & 0 deletions test/svn-ignore.bats
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,58 @@ load 'common'
assert git -C git-repo show master:dir-a/file-a
}

@test 'svn-ignore translation should be done properly on the root directory' {
svn propset svn:ignore $'ignore-a\nignore-b' .
svn commit -m 'ignore ignore-a and ignore-b on root'
svn propset svn:global-ignores 'ignore-c' .
svn commit -m 'ignore ignore-c on root and descendents'

cd "$TEST_TEMP_DIR"
svn2git "$SVN_REPO" --svn-ignore --rules <(echo "
create repository git-repo
end repository

match /
repository git-repo
branch master
end match
")

assert_equal "$(git -C git-repo show master:.gitignore)" "$(cat <<-EOF
/ignore-a
/ignore-b
ignore-c
EOF
)"
}

@test 'svn-ignore translation should be done properly on the root directory (nested)' {
svn mkdir project-a
cd project-a
svn propset svn:ignore $'ignore-a\nignore-b' .
svn commit -m 'ignore ignore-a and ignore-b on root'
svn propset svn:global-ignores 'ignore-c' .
svn commit -m 'ignore ignore-c on root and descendents'

cd "$TEST_TEMP_DIR"
svn2git "$SVN_REPO" --svn-ignore --rules <(echo "
create repository git-repo
end repository

match /project-a/
repository git-repo
branch master
end match
")

assert_equal "$(git -C git-repo show master:.gitignore)" "$(cat <<-EOF
/ignore-a
/ignore-b
ignore-c
EOF
)"
}

@test 'gitignore file should be removed if all svn-ignores are removed' {
svn mkdir dir-a
svn propset svn:ignore 'ignore-a' dir-a
Expand Down