Exemple #1
0
        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);
                }
            }
        }
Exemple #2
0
        /// <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);
        }