public void ExportRepository(string basePath, string repositoryName, string revision, string exportPath)
        {
            var tempPath = PathUtility.GetTempFileName();

            try
            {
                if (Directory.Exists(exportPath) == false)
                {
                    Directory.CreateDirectory(exportPath);
                }
                if (DirectoryUtility.IsEmpty(exportPath) == true)
                {
                    new CremaDataSet().WriteToDirectory(exportPath);
                }
                var revisionValue  = revision == string.Empty ? repositoryName : revision;
                var archiveCommand = new GitCommand(basePath, "archive")
                {
                    new GitCommandItem($"output={(GitPath)tempPath}"),
                    new GitCommandItem("format=zip"),
                    revisionValue
                };
                archiveCommand.Run();
                ZipFile.ExtractToDirectory(tempPath, exportPath);
            }
            finally
            {
                FileUtility.Delete(tempPath);
            }
        }
        public void DeleteRepository(string author, string basePath, string repositoryName, string comment, params LogPropertyInfo[] properties)
        {
            var baseUri          = new Uri(basePath);
            var repositoryPath   = baseUri.LocalPath;
            var branchName       = repositoryName;
            var branchCollection = GitBranchCollection.Run(repositoryPath);

            if (branchCollection.Count <= 1)
            {
                throw new InvalidOperationException();
            }

            if (branchCollection.CurrentBranch == branchName)
            {
                var nextBranchName = branchCollection.First(item => item != branchCollection.CurrentBranch);
                this.CheckoutBranch(repositoryPath, nextBranchName);
            }

            var deleteCommand = new GitCommand(repositoryPath, "branch")
            {
                new GitCommandItem('D'),
                branchName
            };

            deleteCommand.Run();
            this.UnsetID(repositoryPath, repositoryName);
        }
        public void RevertRepository(string author, string basePath, string repositoryName, string revision, string comment)
        {
            var baseUri        = new Uri(basePath);
            var repositoryPath = baseUri.LocalPath;

            this.CheckoutBranch(repositoryPath, repositoryName);

            try
            {
                var revisionsCommand = new GitCommand(repositoryPath, "log")
                {
                    GitCommandItem.FromPretty("format:%H"),
                };
                var revisions = revisionsCommand.ReadLines();
                if (revisions.Contains(revision) == false)
                {
                    throw new ArgumentException($"'{revision}' is invalid revision.", nameof(revision));
                }
                foreach (var item in revisions)
                {
                    if (item == revision)
                    {
                        break;
                    }
                    var revertCommand = new GitCommand(repositoryPath, "revert")
                    {
                        new GitCommandItem('n'),
                        item,
                    };
                    revertCommand.Run();
                }
                var statusCommand = new GitCommand(repositoryPath, "status")
                {
                    new GitCommandItem('s')
                };
                var items = statusCommand.ReadLines(true);
                if (items.Length != 0)
                {
                    var commitCommand = new GitCommitCommand(basePath, author, comment);
                    commitCommand.Run();
                }
                else
                {
                    throw new InvalidOperationException("nothing to revert.");
                }
            }
            catch
            {
                var abortCommand = new GitCommand(repositoryPath, "revert")
                {
                    new GitCommandItem("abort"),
                };
                abortCommand.TryRun();
                throw;
            }
        }
Beispiel #4
0
        private void PushNotes()
        {
            var pushNotesCommand = new GitCommand(this.BasePath, "push")
            {
                "origin",
                "refs/notes/commits",
            };

            pushNotesCommand.Run(this.logService);
        }
Beispiel #5
0
        public void Move(string srcPath, string toPath)
        {
            var moveCommand = new GitCommand(this.BasePath, "mv")
            {
                (GitPath)srcPath,
                (GitPath)toPath,
            };

            moveCommand.Run(this.logService);
        }
Beispiel #6
0
        private void SetNotes(params LogPropertyInfo[] properties)
        {
            var props        = properties.Select(item => (GitPropertyValue)item).ToArray();
            var propText     = propertySerializer.Serialize(props);
            var notesCommand = new GitCommand(this.BasePath, "notes")
            {
                "add",
                GitCommandItem.FromMessage(propText),
            };

            notesCommand.Run(this.logService);
        }
        public void CreateRepository(string author, string basePath, string initPath, string comment, params LogPropertyInfo[] properties)
        {
            var baseUri         = new Uri(basePath);
            var repositoryPath  = baseUri.LocalPath;
            var repositoryName  = Path.GetFileName(initPath);
            var checkoutCommand = new GitCommand(repositoryPath, "checkout")
            {
                new GitCommandItem("orphan"), repositoryName
            };

            checkoutCommand.Run();
            var removeCommand = new GitCommand(repositoryPath, "rm")
            {
                "-rf", "."
            };

            removeCommand.Run();

            DirectoryUtility.Copy(initPath, repositoryPath);
            foreach (var item in GetEmptyDirectories(repositoryPath))
            {
                File.WriteAllText(Path.Combine(item, KeepExtension), string.Empty);
            }

            var statusItems = GitItemStatusInfo.Run(repositoryPath);
            var addCommand  = new GitCommand(repositoryPath, "add");

            foreach (var item in statusItems)
            {
                if (item.Status == RepositoryItemStatus.Untracked)
                {
                    addCommand.Add((GitPath)item.Path);
                }
            }
            addCommand.Run();

            var commitCommand = new GitCommitCommand(repositoryPath, author, comment);

            commitCommand.Run();

            var props           = properties.Select(item => (GitPropertyValue)item).ToArray();
            var propText        = propertySerializer.Serialize(props);
            var addNotesCommand = new GitCommand(basePath, "notes")
            {
                "add",
                GitCommandItem.FromMessage(propText)
            };

            addNotesCommand.Run();
            this.SetID(repositoryPath, repositoryName, Guid.NewGuid());
            this.SetDescription(repositoryPath, repositoryName, comment);
        }
Beispiel #8
0
        public void Delete(string path)
        {
            var removeCommand = new GitCommand(this.BasePath, "rm")
            {
                (GitPath)path,
            };

            if (Directory.Exists(path) == true)
            {
                removeCommand.Add(new GitCommandItem('r'));
            }
            removeCommand.Run(this.logService);
        }
        public static               GitItemStatusInfo[] Run(string repositoryPath, params string[] paths)
        {
            var statusCommand = new GitCommand(repositoryPath, "status")
            {
                new GitCommandItem('s'),
                new GitCommandItem(string.Empty)
            };

            foreach (var item in paths)
            {
                statusCommand.Add((GitPath)item);
            }
            return(Parse(statusCommand.Run()));
        }
Beispiel #10
0
        public void Revert()
        {
            this.resetCommand.Run(this.logService);
            this.cleanCommand.Run(this.logService);

            if (File.Exists(this.transactionPatchPath) == true)
            {
                var applyCommand = new GitCommand(this.BasePath, "apply")
                {
                    (GitPath)transactionPatchPath,
                    new GitCommandItem("index")
                };
                applyCommand.Run(this.logService);
            }
        }
        public void CloneRepository(string author, string basePath, string repositoryName, string newRepositoryName, string comment, string revision, params LogPropertyInfo[] properties)
        {
            var baseUri        = new Uri(basePath);
            var repositoryPath = baseUri.LocalPath;
            var branchCommand  = new GitCommand(repositoryPath, "branch")
            {
                newRepositoryName,
                revision == string.Empty ? repositoryName : revision,
            };

            branchCommand.Run();
            this.SetID(repositoryPath, newRepositoryName, Guid.NewGuid());
            this.SetDescription(repositoryPath, newRepositoryName, comment);
            this.SetCreationInfo(repositoryPath, newRepositoryName, new SignatureDate(author));
        }
        private void CheckoutBranch(string repositoryPath, string branchName)
        {
            var resetCommand = new GitCommand(repositoryPath, "reset")
            {
                new GitCommandItem("hard")
            };

            resetCommand.Run();
            var checkoutCommand = new GitCommand(repositoryPath, "checkout")
            {
                branchName
            };

            checkoutCommand.Run();
        }
        public string Migrate(string sourcePath)
        {
            var repositoryPath2 = PathUtility.GetTempPath(false);
            var repositoryUri   = new Uri(sourcePath).ToString();

            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                repositoryUri = Regex.Replace(repositoryUri, "(file:///\\w):(.+)", "$1$2");
            }

            var cloneCommand = new GitCommand(null, "svn clone")
            {
                (GitPath)repositoryUri,
                (GitPath)repositoryPath2,
                new GitCommandItem('T', "trunk"),
                new GitCommandItem('b', "branches"),
                new GitCommandItem('b', "tags")
            };

            cloneCommand.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
            cloneCommand.Run();

            var remoteBranches = GitBranchCollection.GetRemoteBranches(repositoryPath2);
            var branches       = GitBranchCollection.Run(repositoryPath2);

            foreach (var item in remoteBranches)
            {
                if (item != "trunk" && branches.Contains(item) == false)
                {
                    var checkoutCommand = new GitCommand(repositoryPath2, "checkout")
                    {
                        new GitCommandItem('b'),
                        item,
                        $"remotes/origin/{item}"
                    };
                    checkoutCommand.Run();
                }
            }

            var configCommand = new GitCommand(repositoryPath2, "config")
            {
                "receive.denyCurrentBranch",
                "ignore"
            };

            configCommand.Run();
            return(repositoryPath2);
        }
Beispiel #14
0
 public void Copy(string srcPath, string toPath)
 {
     if (DirectoryUtility.IsDirectory(srcPath) == true)
     {
         throw new NotImplementedException();
     }
     else
     {
         var copyCommand = new GitCommand(this.BasePath, "add")
         {
             (GitPath)toPath
         };
         File.Copy(srcPath, toPath);
         copyCommand.Run(this.logService);
     }
 }
Beispiel #15
0
        public static string GetValue(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            var configCommand = new GitCommand(null, "config")
            {
                GitCommandItem.Global,
                new GitCommandItem("get"),
                name,
            };

            return(configCommand.Run());
        }
        public void RenameRepository(string author, string basePath, string repositoryName, string newRepositoryName, string comment, params LogPropertyInfo[] properties)
        {
            var baseUri        = new Uri(basePath);
            var repositoryPath = baseUri.LocalPath;
            var renameCommand  = new GitCommand(repositoryPath, "branch")
            {
                new GitCommandItem('m'),
                repositoryName,
                newRepositoryName
            };

            var repositoryID = this.GetID(repositoryPath, repositoryName);

            renameCommand.Run();
            this.SetID(repositoryPath, newRepositoryName, repositoryID);
        }
Beispiel #17
0
        public static string GetValue(string repositoryPath, string name)
        {
            if (repositoryPath == null)
            {
                throw new ArgumentNullException(nameof(repositoryPath));
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            var configCommand = new GitCommand(repositoryPath, "config")
            {
                new GitCommandItem("get"),
                name,
            };

            return(configCommand.Run());
        }
Beispiel #18
0
        private void PullNotes()
        {
            var fetchCommand = new GitCommand(this.BasePath, "fetch")
            {
                "origin",
                "refs/notes/commits:refs/notes/origin/commits",
            };

            fetchCommand.Run();

            var mergeCommand = new GitCommand(this.BasePath, "notes")
            {
                "merge",
                new GitCommandItem('v'),
                "origin/commits",
            };

            mergeCommand.Run();
        }
Beispiel #19
0
        public static void UnsetValue(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (HasValue(name) == false)
            {
                return;
            }
            var configCommand = new GitCommand(null, "config")
            {
                GitCommandItem.Global,
                new GitCommandItem("unset"),
                name,
            };

            configCommand.Run();
        }
Beispiel #20
0
        public static void SetValue(string name, string value)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            var configCommand = new GitCommand(null, "config")
            {
                GitCommandItem.Global,
                name,
                (GitString)value
            };

            configCommand.Run();
        }
Beispiel #21
0
        public string Export(Uri uri, string exportPath)
        {
            var match    = Regex.Match(uri.LocalPath, "(?<path>.+)@(?<keep>.*)(?<revision>[a-f0-9]{40})", RegexOptions.ExplicitCapture);
            var path     = match.Groups["path"].Value;
            var keep     = match.Groups["keep"].Value;
            var revision = match.Groups["revision"].Value;

            var tempPath = PathUtility.GetTempFileName();

            try
            {
                if (Directory.Exists(exportPath) == false)
                {
                    Directory.CreateDirectory(exportPath);
                }
                if (DirectoryUtility.IsEmpty(exportPath) == true)
                {
                    new CremaDataSet().WriteToDirectory(exportPath);
                }
                var relativePath   = UriUtility.MakeRelativeOfDirectory(this.BasePath, path);
                var archiveCommand = new GitCommand(this.BasePath, "archive")
                {
                    new GitCommandItem($"output={(GitPath)tempPath}"),
                    new GitCommandItem("format=zip"),
                    revision,
                    GitCommandItem.Separator,
                    (GitPath)path,
                };
                archiveCommand.Run(this.logService);
                ZipFile.ExtractToDirectory(tempPath, exportPath);
                var exportUri = new Uri(UriUtility.Combine(exportPath, relativePath));
                return(exportUri.LocalPath);
            }
            finally
            {
                FileUtility.Delete(tempPath);
            }
        }
        public IRepository CreateInstance(RepositorySettings settings)
        {
            var remoteUri      = new Uri(settings.RemotePath);
            var baseUri        = new Uri(settings.BasePath);
            var originUri      = UriUtility.MakeRelativeOfDirectory(baseUri, remoteUri);
            var repositoryName = settings.RepositoryName == string.Empty ? "master" : settings.RepositoryName;

            if (Directory.Exists(settings.BasePath) == false)
            {
                var cloneCommand = new GitCommand(null, "clone")
                {
                    (GitPath)remoteUri,
                    new GitCommandItem('b'),
                    repositoryName,
                    (GitPath)settings.BasePath,
                    new GitCommandItem("single-branch")
                };
                cloneCommand.Run();

                var fetchCommand = new GitCommand(settings.BasePath, "fetch")
                {
                    "origin",
                    "refs/notes/commits:refs/notes/commits",
                };
                fetchCommand.Run();

                var id = this.GetID(settings.RemotePath, repositoryName);
                this.SetID(settings.BasePath, repositoryName, id);
                GitConfig.SetValue(settings.BasePath, "remote.origin.url", originUri);
                var repositoryInfo = this.GetRepositoryInfo(settings.RemotePath, repositoryName);
                return(new GitRepository(this, settings, repositoryInfo));
            }
            else
            {
                var repositoryInfo = this.GetRepositoryInfo(settings.BasePath, repositoryName);
                return(new GitRepository(this, settings, repositoryInfo));
            }
        }
Beispiel #23
0
        public static void SetValue(string repositoryPath, string name, string value)
        {
            if (repositoryPath == null)
            {
                throw new ArgumentNullException(nameof(repositoryPath));
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            var configCommand = new GitCommand(repositoryPath, "config")
            {
                name,
                (GitString)value
            };

            configCommand.Run();
        }
Beispiel #24
0
        public static void UnsetValue(string repositoryPath, string name)
        {
            if (repositoryPath == null)
            {
                throw new ArgumentNullException(nameof(repositoryPath));
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (HasValue(repositoryPath, name) == false)
            {
                return;
            }
            var configCommand = new GitCommand(repositoryPath, "config")
            {
                new GitCommandItem("unset"),
                name,
            };

            configCommand.Run();
        }
Beispiel #25
0
        public void Add(string path)
        {
            var addCommand = new GitCommand(this.BasePath, "add");

            if (DirectoryUtility.IsDirectory(path) == true)
            {
                var keepPath = Path.Combine(path, GitRepositoryProvider.KeepExtension);
                if (File.Exists(keepPath) == false)
                {
                    File.WriteAllText(keepPath, string.Empty);
                    addCommand.Add((GitPath)keepPath);
                }
            }
            else
            {
                addCommand.Add((GitPath)path);
            }

            if (addCommand.Items.Any() == true)
            {
                addCommand.Run(this.logService);
            }
        }
Beispiel #26
0
        private void Push()
        {
            var pushCommand = new GitCommand(this.BasePath, "push");

            pushCommand.Run(this.logService);
        }
        public void InitializeRepository(string basePath, string repositoryPath, params LogPropertyInfo[] properties)
        {
            var cloneCommand = new GitCommand(null, "clone")
            {
                (GitPath)repositoryPath,
                (GitPath)basePath,
            };

            if (cloneCommand.TryRun() == true)
            {
                var fetchCommand = new GitCommand(basePath, "fetch")
                {
                    "origin",
                    "refs/notes/commits:refs/notes/commits",
                };
                fetchCommand.Run();
                GitConfig.SetValue(basePath, "receive.denyCurrentBranch", "ignore");
                return;
            }

            var initCommand = new GitCommand(null, "init")
            {
                (GitPath)basePath
            };

            initCommand.Run();

            var configCommand = new GitCommand(basePath, "config")
            {
                "receive.denyCurrentBranch",
                "ignore"
            };

            configCommand.Run();

            DirectoryUtility.Copy(repositoryPath, basePath);
            foreach (var item in GetEmptyDirectories(basePath))
            {
                File.WriteAllText(Path.Combine(item, KeepExtension), string.Empty);
            }

            var query = from item in DirectoryUtility.GetAllFiles(basePath, "*", true)
                        select(GitPath) PathUtility.GetFullPath(item);
            var itemList = query.ToList();

            var addCommand = new GitAddCommand(basePath, query.ToArray());

            addCommand.Run();

            var commitCommand = new GitCommitCommand(basePath, Environment.UserName, "first commit");

            commitCommand.Run();

            var props           = properties.Select(item => (GitPropertyValue)item).ToArray();
            var propText        = propertySerializer.Serialize(props);
            var addNotesCommand = new GitCommand(basePath, "notes")
            {
                "add",
                GitCommandItem.FromMessage(propText)
            };

            addNotesCommand.Run();

            this.SetID(basePath, "master", Guid.NewGuid());
        }
Beispiel #28
0
        private void Pull()
        {
            var pullCommand = new GitCommand(this.BasePath, "pull");

            pullCommand.Run(this.logService);
        }