public static void Copy(string sourceDirectoryName, string destinationDirectoryName, bool copySubDirectories) { DirectoryInfo sourceDirectory = new DirectoryInfo(sourceDirectoryName); if (!sourceDirectory.Exists) { throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + sourceDirectoryName); } DirectoryInfo[] subDirectories = sourceDirectory.GetDirectories(); if (!Directory.Exists(destinationDirectoryName)) { Directory.CreateDirectory(destinationDirectoryName); } FileInfo[] files = sourceDirectory.GetFiles(); foreach (FileInfo file in files) { string newFilePath = Path.Combine(destinationDirectoryName, file.Name); file.CopyTo(newFilePath, false); } if (copySubDirectories) { foreach (DirectoryInfo subDirectory in subDirectories) { string newDirectoryPath = Path.Combine(destinationDirectoryName, subDirectory.Name); DirectoryExtensions.Copy(subDirectory.FullName, newDirectoryPath, copySubDirectories); } } }
/// <summary> /// Deletes the working copy of this repository. It also releases any native handle of the repository. /// </summary> public void DeleteWorkingCopy() { if (!this.IsRepositoryInitialized) { throw new ModuniException("The repository is not initialized."); } string repositoryURL = this.RepositoryURL; this.Dispose(); DirectoryExtensions.DeleteCompletely(repositoryURL); }
/// <summary> /// Removes the submodule whose relative path is specified as argument. /// </summary> /// <param name="relativePath">The relative path of the submodule to be removed.</param> /// <param name="forceRemoval">If set to <c>true</c>, it deletes the submodule even if it has local modifications.</param> public void RemoveSubmodule(string relativePath, bool forceRemoval) { if (!this.IsRepositoryInitialized) { throw new ModuniException("The repository is not initialized."); } if (forceRemoval) { DirectoryExtensions.DeleteCompletely(System.IO.Path.Combine(this.RepositoryURL, relativePath)); } this.RemoveFile(relativePath, true); this.DeleteSubmoduleFromConfiguration(relativePath); this.StageFile(".gitmodules"); DirectoryExtensions.DeleteCompletely(System.IO.Path.Combine(System.IO.Path.Combine(this.RepositoryURL, ".git/modules"), relativePath)); }
/// <summary> /// Moves the submodule situated at <paramref name="oldRelativePath"/> to <paramref name="newRelativePath"/>. /// It also changes the file ".gitmodules" with the new path and commit the modifications. /// </summary> /// <param name="oldRelativePath">The relative path to the current location of the submodule.</param> /// <param name="newRelativePath">The relative path to the new location of the submodule.</param> public void MoveSubmodule(string oldRelativePath, string newRelativePath) { if (!this.IsRepositoryInitialized) { throw new ModuniException("The repository is not initialized."); } string oldAbsolutePath = System.IO.Path.Combine(this.RepositoryURL, oldRelativePath); string newAbsolutePath = System.IO.Path.Combine(this.RepositoryURL, newRelativePath); string submoduleURL = this.gitRepositoryHandle.Submodules[oldRelativePath].Url; string oldGitDirectoryPath = System.IO.Path.Combine(System.IO.Path.Combine(this.RepositoryURL, ".git/Modules"), oldRelativePath); string gitDirectoryPath = System.IO.Path.Combine(System.IO.Path.Combine(this.RepositoryURL, ".git/Modules"), newRelativePath); DirectoryExtensions.Copy(oldAbsolutePath, newAbsolutePath, true); DirectoryExtensions.Copy(oldGitDirectoryPath, gitDirectoryPath, true); this.RemoveSubmodule(oldRelativePath, true); Configuration gitModulesConfiguration = LibGit2Sharp.Configuration.BuildFrom(System.IO.Path.Combine(this.RepositoryURL, ".gitmodules")); gitModulesConfiguration.Set <string>(@"submodule." + newRelativePath + ".url", submoduleURL); gitModulesConfiguration.Set <string>(@"submodule." + newRelativePath + ".path", newRelativePath); string gitLinkPath = System.IO.Path.Combine(newAbsolutePath, ".git"); string gitDirFilePath = System.IO.Path.Combine(gitDirectoryPath, "gitdir"); string submoduleConfigPath = System.IO.Path.Combine(gitDirectoryPath, "config"); if (System.IO.File.Exists(gitLinkPath)) { System.IO.File.WriteAllText(gitLinkPath, "gitdir: " + new Uri(gitLinkPath, UriKind.Absolute).MakeRelativeUri(new Uri(gitDirectoryPath, UriKind.Absolute))); } if (System.IO.File.Exists(gitDirFilePath)) { System.IO.File.WriteAllText(gitDirFilePath, new Uri(this.RepositoryURL, UriKind.Absolute).MakeRelativeUri(new Uri(gitLinkPath, UriKind.Absolute)).ToString()); } if (System.IO.File.Exists(submoduleConfigPath)) { Configuration submoduleConfiguration = LibGit2Sharp.Configuration.BuildFrom(submoduleConfigPath); submoduleConfiguration.Set <string>("core.worktree", new Uri(submoduleConfigPath, UriKind.Absolute).MakeRelativeUri(new Uri(newAbsolutePath, UriKind.Absolute)).ToString()); } this.StageFile(".gitmodules"); this.StageFile(newRelativePath); }
/// <summary> /// Moves the repository to the path specified as argument. /// </summary> /// <param name="pathToMoveTo">The new path of the repository.</param> public void MoveTo(string pathToMoveTo) { if (!this.IsRepositoryInitialized) { throw new ModuniException("The repository is not initialized."); } // If it is a submodule, we can't move it by our own so we do nothing if (System.IO.File.Exists(System.IO.Path.Combine(this.RepositoryURL, ".git"))) { return; } if (System.IO.Directory.Exists(pathToMoveTo)) { DirectoryExtensions.DeleteCompletely(pathToMoveTo); } string repositoryURL = this.RepositoryURL; this.Dispose(); System.IO.Directory.Move(repositoryURL, pathToMoveTo); this.gitRepositoryHandle = new Repository(pathToMoveTo); }