static String convertToRelativePath()

in src/main/java/org/apache/maven/plugins/shade/mojo/RelativizePath.java [41:81]


    static String convertToRelativePath(File thing, File relativeTo) {
        StringBuilder relativePath;

        if (thing.getParentFile().equals(relativeTo.getParentFile())) {
            return thing.getName(); // a very simple relative path.
        }

        List<String> thingDirectories = RelativizePath.parentDirs(thing);
        List<String> relativeToDirectories = RelativizePath.parentDirs(relativeTo);

        // Get the shortest of the two paths
        int length = Math.min(thingDirectories.size(), relativeToDirectories.size());

        int lastCommonRoot = -1; // index of the lowest directory down from the root that the two have in common.
        int index;

        // Find common root
        for (index = 0; index < length; index++) {
            if (thingDirectories.get(index).equals(relativeToDirectories.get(index))) {
                lastCommonRoot = index;
            } else {
                break;
            }
        }
        if (lastCommonRoot != -1) { // possible on Windows or other multi-root cases.
            // Build up the relative path
            relativePath = new StringBuilder();
            // add ..'s to get from the base up to the common point
            for (index = lastCommonRoot + 1; index < relativeToDirectories.size(); index++) {
                relativePath.append("../");
            }

            // now add down from the common point to the actual 'thing' item.
            for (index = lastCommonRoot + 1; index < thingDirectories.size(); index++) {
                relativePath.append(thingDirectories.get(index)).append('/');
            }
            relativePath.append(thing.getName());
            return relativePath.toString();
        }
        return null;
    }