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));
            }
        }
Esempio n. 2
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 string[] GetRepositories(string basePath)
        {
            var baseUri        = new Uri(basePath);
            var repositoryPath = baseUri.LocalPath;
            var branchCommand  = new GitCommand(repositoryPath, "branch")
            {
                new GitCommandItem("list")
            };
            var lines    = branchCommand.ReadLines();
            var itemList = new List <string>(lines.Length);

            foreach (var line in lines)
            {
                var match = Regex.Match(line, "^[*]*\\s*(\\S+)");
                if (match.Success)
                {
                    var branchName = match.Groups[1].Value;
                    itemList.Add(branchName);
                }
            }

            return(itemList.ToArray());
        }
Esempio n. 4
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);
            }
        }
Esempio n. 5
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();
        }
Esempio n. 6
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();
        }
Esempio n. 7
0
        public GitAddCommand(string basePath, GitPath[] items)
        {
            var itemList      = new Queue <string>(items.Select(item => $"{item}"));
            var commandList   = new List <GitCommand>();
            var command       = new GitCommand(basePath, "add");
            var commandLength = command.ToString().Length;

            while (itemList.Any())
            {
                var item = itemList.Dequeue();
                if (commandLength + item.Length + 1 > 1024)
                {
                    commandList.Add(command);
                    command       = new GitCommand(basePath, "add");
                    commandLength = command.ToString().Length;
                }
                commandLength += item.Length + 1;
                command.Add(item);
            }

            commandList.Add(command);
            this.commandList = commandList;
        }
        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());
        }
Esempio n. 9
0
        private void Push()
        {
            var pushCommand = new GitCommand(this.BasePath, "push");

            pushCommand.Run(this.logService);
        }
Esempio n. 10
0
        private void Pull()
        {
            var pullCommand = new GitCommand(this.BasePath, "pull");

            pullCommand.Run(this.logService);
        }