// @} IGitRepository /// <summary> /// Construct GitRepository with a path that either does not exist or it is empty /// or points to a valid git repository /// Throws ArgumentException if requirements on `path` argument are not met /// </summary> internal GitRepository(string parentFolder, ProjectKey projectKey, ISynchronizeInvoke synchronizeInvoke, LocalCommitStorageType type, Action <IGitRepository> onClonedRepo) { Path = LocalCommitStoragePathFinder.FindPath(parentFolder, projectKey, type); if (!GitTools.IsSingleCommitFetchSupported(Path)) //-V3022 { throw new ArgumentException("Cannot work with such repositories"); } bool isShallowCloneAllowed = type == LocalCommitStorageType.ShallowGitRepository; UpdateMode mode = isShallowCloneAllowed ? UpdateMode.ShallowClone : UpdateMode.FullCloneWithSingleCommitFetches; // PathFinder must guarantee the following Debug.Assert(isEmptyFolder(Path) || (GitTools.GetRepositoryProjectKey(Path).HasValue && GitTools.GetRepositoryProjectKey(Path).Value.Equals(projectKey))); _processManager = new GitProcessManager(synchronizeInvoke, Path); _updater = new GitRepositoryUpdater(synchronizeInvoke, this, _processManager, mode, onCloned, onFetched); _onClonedRepo = onClonedRepo; _commandService = new NativeGitCommandService(_processManager, Path); ExpectingClone = isEmptyFolder(Path); ProjectKey = projectKey; Trace.TraceInformation(String.Format( "[GitRepository] Created GitRepository at Path {0} for host {1} and project {2}, " + "expecting clone = {3}", Path, ProjectKey.HostName, ProjectKey.ProjectName, ExpectingClone.ToString())); }
/// <summary> /// Splits passed url in parts and stores in object properties /// <summary> public static ParsedNewMergeRequestUrl Parse(string url) { if (url.Length > MaxUrlLength) { throw new UriFormatException("Too long URL"); } Match m = url_re.Match(url); if (!m.Success) { throw new UriFormatException("Failed to parse URL"); } Group path = m.Groups["Repository"]; Group sourceBranch = m.Groups["SourceBranch"]; if (!path.Success || !sourceBranch.Success) { throw new UriFormatException("Unsupported URL format"); } ProjectKey?projectKey = GitTools.GetRepositoryProjectKey(path.Value); if (!projectKey.HasValue) { throw new UriFormatException(String.Format("\"{0}\" is not a git repository", path.Value)); } // sourceBranch can be one of the following: // - br_foo // - origin/br_foo // - 53ff02a // Resolve all these cases to origin/br_foo here. string remoteSourceBranch = getRemoteSourceBranch(path.Value, sourceBranch.Value); if (String.IsNullOrEmpty(remoteSourceBranch)) { throw new UriFormatException(String.Format("\"{0}\" does not point to a remote branch", sourceBranch.Value)); } string sourceBranchName = trimRemoteOrigin(remoteSourceBranch); IEnumerable <string> targetBranchName = findTargetBranch(path.Value, remoteSourceBranch) .Select(fullName => trimRemoteOrigin(fullName)); return(new ParsedNewMergeRequestUrl(projectKey.Value, sourceBranchName, targetBranchName)); }
private static ProjectKey?getRepositoryProjectKey(string path, LocalCommitStorageType type) { if (type == LocalCommitStorageType.FileStorage) { return(FileStorageUtils.GetFileStorageProjectKey(path)); } ProjectKey?key = GitTools.GetRepositoryProjectKey(path); if (key == null) { return(null); } bool isShallowRepository = File.Exists(Path.Combine(path, ".git", "shallow")); bool isAskingForShallowRepository = type == LocalCommitStorageType.ShallowGitRepository; return(isShallowRepository == isAskingForShallowRepository ? key : null); }