Example #1
0
        public void EndTransaction()
        {
            var transactionMessage = string.Join(Environment.NewLine, this.transactionMessageList);
            var statusCommand      = new GitCommand(this.BasePath, "status")
            {
                new GitCommandItem('s')
            };
            var items = statusCommand.ReadLines(true);

            if (items.Length != 0)
            {
                var commitCommand = new GitCommitCommand(this.BasePath, this.transactionAuthor, transactionMessage);
                var result        = commitCommand.Run(this.logService);
                this.logService?.Debug(result);
                var log = GitLogInfo.GetLatestLog(this.BasePath);
                this.repositoryInfo.Revision         = log.CommitID;
                this.repositoryInfo.ModificationInfo = new SignatureDate(this.transactionAuthor, log.CommitDate);
                this.SetNotes(this.transactionPropertyList.ToArray());
                FileUtility.Delete(this.transactionPatchPath);
                this.transactionAuthor       = null;
                this.transactionName         = null;
                this.transactionMessageList  = null;
                this.transactionPropertyList = null;
                this.transactionPatchPath    = null;
                this.Pull();
                this.Push();
                this.PushNotes();
            }
            else
            {
                this.logService?.Debug("repository has no changes.");
            }
        }
        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;
            }
        }
Example #3
0
        public void Commit(string author, string comment, params LogPropertyInfo[] properties)
        {
            if (this.transactionName != null)
            {
                var diffCommand = new GitCommand(this.BasePath, "diff")
                {
                    "HEAD",
                    new GitCommandItem("stat"),
                    new GitCommandItem("binary")
                };
                var output = diffCommand.ReadLine();
                FileUtility.Prepare(this.transactionPatchPath);
                File.WriteAllText(this.transactionPatchPath, output);
                this.transactionMessageList.Add(comment);
                this.transactionPropertyList.AddRange(properties);
                return;
            }

            try
            {
                var statusCommand = new GitCommand(this.BasePath, "status")
                {
                    new GitCommandItem('s')
                };
                var items = statusCommand.ReadLines(true);
                if (items.Length != 0)
                {
                    var authorValue = new GitAuthor(author);
                    GitConfig.SetValue(this.BasePath, "user.email", authorValue.Email == string.Empty ? "<>" : authorValue.Email);
                    GitConfig.SetValue(this.BasePath, "user.name", authorValue.Name);

                    var commitCommand = new GitCommitCommand(this.BasePath, author, comment);
                    var result        = commitCommand.Run(this.logService);
                    this.logService?.Debug(result);
                    var log = GitLogInfo.GetLatestLog(this.BasePath);
                    this.repositoryInfo.Revision         = log.CommitID;
                    this.repositoryInfo.ModificationInfo = new SignatureDate(author, log.CommitDate);

                    this.SetNotes(properties);
                    //this.isModified = true;
                    //this.Pull();
                    //this.Push();
                    //this.PushNotes();
                }
                else
                {
                    this.logService?.Debug("repository no changes. \"{0}\"", this.BasePath);
                }
            }
            catch (Exception e)
            {
                this.logService?.Warn(e);
                throw;
            }
        }
        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);
        }
        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());
        }