IDEA-141883 .gitignore in the repository that tracks the settings is not respected

1) support .gitignore only in the repository root
2) changes is not tracked — to apply changed file IDE must be restarted.
This commit is contained in:
Vladimir Krivosheev
2015-08-26 18:49:45 +02:00
parent b7144c656a
commit 6420bd963e
5 changed files with 57 additions and 8 deletions
@@ -103,7 +103,7 @@ public abstract class BaseRepositoryManager(protected val dir: File) : Repositor
return null
}
override fun write(path: String, content: ByteArray, size: Int) {
override fun write(path: String, content: ByteArray, size: Int): Boolean {
if (LOG.isDebugEnabled()) {
LOG.debug("Write $path")
}
@@ -118,7 +118,9 @@ public abstract class BaseRepositoryManager(protected val dir: File) : Repositor
}
catch (e: Exception) {
LOG.error(e)
return false
}
return true
}
/**
@@ -215,16 +215,12 @@ class IcsManager(dir: File) {
throw IllegalStateException("Save is prohibited now")
}
doSave(fileSpec, content, size, roamingType)
if (isAutoCommit(fileSpec, roamingType)) {
if (doSave(fileSpec, content, size, roamingType) && isAutoCommit(fileSpec, roamingType)) {
scheduleCommit()
}
}
fun doSave(fileSpec: String, content: ByteArray, size: Int, roamingType: RoamingType) {
repositoryManager.write(buildPath(fileSpec, roamingType, projectId), content, size)
}
fun doSave(fileSpec: String, content: ByteArray, size: Int, roamingType: RoamingType) = repositoryManager.write(buildPath(fileSpec, roamingType, projectId), content, size)
protected open fun isAutoCommit(fileSpec: String, roamingType: RoamingType): Boolean = true
@@ -41,7 +41,10 @@ public interface RepositoryManager {
public fun read(path: String): InputStream?
public fun write(path: String, content: ByteArray, size: Int)
/**
* Returns false if file is not written (for example, due to ignore rules).
*/
public fun write(path: String, content: ByteArray, size: Int): Boolean
public fun delete(path: String)
@@ -26,6 +26,7 @@ import com.intellij.util.SmartList
import org.eclipse.jgit.api.AddCommand
import org.eclipse.jgit.api.errors.UnmergedPathsException
import org.eclipse.jgit.errors.TransportException
import org.eclipse.jgit.ignore.IgnoreNode
import org.eclipse.jgit.lib.ConfigConstants
import org.eclipse.jgit.lib.Constants
import org.eclipse.jgit.lib.Repository
@@ -62,6 +63,8 @@ class GitRepositoryManager(private val credentialsStore: NotNullLazyValue<Creden
JGitCredentialsProvider(credentialsStore, repository)
}
private var ignoreRules: IgnoreNode? = null
init {
if (ApplicationManager.getApplication()?.isUnitTestMode() != true) {
ShutDownTracker.getInstance().registerShutdownTask(object: Runnable {
@@ -73,6 +76,8 @@ class GitRepositoryManager(private val credentialsStore: NotNullLazyValue<Creden
}
override fun createRepositoryIfNeed(): Boolean {
ignoreRules = null
if (isRepositoryExists()) {
return false
}
@@ -83,6 +88,8 @@ class GitRepositoryManager(private val credentialsStore: NotNullLazyValue<Creden
}
override fun deleteRepository() {
ignoreRules = null
super.deleteRepository()
val r = _repository
@@ -276,6 +283,28 @@ class GitRepositoryManager(private val credentialsStore: NotNullLazyValue<Creden
repository.commit(with(IdeaCommitMessageFormatter()) { StringBuilder().appendCommitOwnerInfo(true) } .append("Get rid of \$ROOT_CONFIG$ and \$APP_CONFIG").toString())
return true
}
private fun getIgnoreRules(): IgnoreNode? {
var node = ignoreRules
if (node == null) {
val file = File(dir, Constants.DOT_GIT_IGNORE)
if (file.exists()) {
node = IgnoreNode()
file.inputStream().use { node!!.parse(it) }
ignoreRules = node
}
}
return node
}
override fun write(path: String, content: ByteArray, size: Int): Boolean {
val ignoreRules = getIgnoreRules()
// add first slash as WorkingTreeIterator does "The ignore code wants path to start with a '/' if possible."
if (ignoreRules != null && ignoreRules.isIgnored("/$path", false) == IgnoreNode.MatchResult.IGNORED) {
return false
}
return super.write(path, content, size)
}
}
fun printMessages(fetchResult: OperationResult) {
@@ -343,6 +343,25 @@ class GitTest : IcsTestCase() {
doSyncWithUninitializedUpstream(SyncType.OVERWRITE_LOCAL)
}
Test fun gitignore() {
createLocalRepository()
repository.add(".gitignore", "*.html")
sync(SyncType.MERGE)
provider.write("bar.html", "<data />")
provider.write("i/am/a/long/path/to/file/foo.html", "<data />")
val diff = repository.computeIndexDiff()
assertThat(diff.diff()).isFalse()
assertThat(diff.getAdded()).isEmpty()
assertThat(diff.getChanged()).isEmpty()
assertThat(diff.getRemoved()).isEmpty()
assertThat(diff.getModified()).isEmpty()
assertThat(diff.getUntracked()).isEmpty()
assertThat(diff.getUntrackedFolders()).isEmpty()
}
private fun createRemoteRepository(branchName: String? = null, initialCommit: Boolean = true) {
val repository = tempDirManager.createRepository("upstream")
if (initialCommit) {