001package co.codewizards.cloudstore.core.repo.local;
002
003import static co.codewizards.cloudstore.core.util.Util.*;
004
005import java.io.File;
006
007public final class LocalRepoHelper {
008
009        private LocalRepoHelper() { }
010
011        /**
012         * Gets the local root containing the given {@code file}.
013         * <p>
014         * If {@code file} itself is the root of a repository, it is returned directly.
015         * <p>
016         * If {@code file} is a directory or file inside the repository, the parent-directory
017         * being the repository's root is returned.
018         * <p>
019         * If {@code file} is not contained in any repository, <code>null</code> is returned.
020         *
021         * @param file the directory or file for which to search the repository's local root. Must not be <code>null</code>.
022         * @return the repository's local root. Is <code>null</code>, if {@code file} is not located inside a repository.
023         */
024        public static File getLocalRootContainingFile(final File file) {
025                File parentFile = assertNotNull("file", file);
026                while (parentFile != null) {
027                        File parentMetaDir = new File(parentFile, LocalRepoManager.META_DIR_NAME);
028                        if (parentMetaDir.exists())
029                                return parentFile;
030
031                        parentFile = parentFile.getParentFile();
032                }
033                return null;
034        }
035
036}