package io.digdag.core.archive;

import org.eclipse.jgit.ignore.IgnoreNode;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Path;
import java.util.Optional;

public class DigdagIgnore implements DirectoryStream.Filter<Path> {
    private Path projectPath;
    private IgnoreNode ignoreNode;

    private DigdagIgnore(){}

    public static Optional<DigdagIgnore> ofProject(Path projectPath) throws IOException {
        File digdagIgnoreFile = projectPath.resolve(".digdagignore").toFile();
        if (!digdagIgnoreFile.exists()) {
            return Optional.empty();
        }

        DigdagIgnore instance = new DigdagIgnore();
        instance.projectPath = projectPath;
        instance.ignoreNode = new IgnoreNode();
        try (FileInputStream s = new FileInputStream(digdagIgnoreFile)) {
            instance.ignoreNode.parse(s);
            return Optional.of(instance);
        }
    }

    @Override
    public boolean accept(Path target) {
        Boolean ignored = this.ignoreNode.checkIgnored(
                this.projectPath.relativize(target).toString(),
                target.toFile().isDirectory()
        );
         return ignored == null || ignored == false;
    }
}

@@ -1097,16 +1097,76 @@
 gnored(%0A
+                // checkIgnored assumes separator to be %22/%22%0A
         
@@ -1219,16 +1219,49 @@
 String()
+.replace(File.separatorChar, '/')
 ,%0A      

replace path separator by '/'