Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e50e81ffb70a8134bee669f3f5892f077388feea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*******************************************************************************
 * Copyright (c) 2016, Matthias Sohn and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.egit.core.internal.util;

import java.io.File;
import java.net.URISyntaxException;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.egit.core.internal.CoreText;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.osgi.util.NLS;

/**
 * Checker for path suitable for storing a git repository
 */
public class RepositoryPathChecker {

	private boolean hasContent;

	private String errorMessage;

	/**
	 * Check if the given directory path is a valid local absolute repository
	 * path
	 *
	 * @param dir
	 *            the directory path to check
	 * @return {@code true} if the repository path is valid
	 */
	public boolean check(String dir) {
		hasContent = false;
		errorMessage = null;

		if (dir.length() == 0) {
			errorMessage = CoreText.RepositoryPathChecker_errAbsoluteRepoPath;
			return false;
		}

		if (dir.startsWith("git clone")) { //$NON-NLS-1$
			errorMessage = CoreText.RepositoryPathChecker_errNoCloneCommand;
			return false;
		}
		if (isURL(dir)) {
			errorMessage = CoreText.RepositoryPathChecker_errNoURL;
			return false;
		}
		File testFile = new File(dir);
		IPath path = Path.fromOSString(dir);
		for (String segment : path.segments()) {
			IStatus status = ResourcesPlugin.getWorkspace()
					.validateName(segment, IResource.FOLDER);
			if (!status.isOK()) {
				errorMessage = status.getMessage();
				return false;
			}
		}
		if (!path.isAbsolute()) {
			errorMessage = NLS.bind(CoreText.RepositoryPathChecker_errNotAbsoluteRepoPath,
					dir);
			return false;
		}
		if (testFile.exists() && !testFile.isDirectory()) {
			errorMessage = NLS.bind(CoreText.RepositoryPathChecker_errNoDirectory, dir);
			return false;
		}
		hasContent = testFile.exists() && testFile.list().length > 0;
		return true;
	}

	private boolean isURL(String dir) {
		try {
			URIish u = new URIish(dir);
			return u.isRemote() || u.getScheme() != null;
		} catch (URISyntaxException e) {
			// skip
		}
		return false;
	}

	/**
	 * @return {@code true} if the directory exists and already has content
	 */
	public boolean hasContent() {
		return hasContent;
	}

	/**
	 * @return the error message if the path is invalid
	 */
	public @Nullable String getErrorMessage() {
		return errorMessage;
	}
}

Back to the top