Beispiel #1
0
        /// <summary>
        /// Promotes to the staging area the latest modifications of a file in the working directory (addition, updation or removal).
        ///
        /// If this path is ignored by configuration then it will not be staged unless <see cref="StageOptions.IncludeIgnored"/> is unset.
        /// </summary>
        /// <param name="repository">The repository in which to act</param>
        /// <param name="path">The path of the file within the working directory.</param>
        /// <param name="stageOptions">Determines how paths will be staged.</param>
        public static void Stage(IRepository repository, string path, StageOptions stageOptions)
        {
            Ensure.ArgumentNotNull(repository, "repository");
            Ensure.ArgumentNotNull(path, "path");

            Stage(repository, new[] { path }, stageOptions);
        }
Beispiel #2
0
        /// <summary>
        /// Promotes to the staging area the latest modifications of a collection of files in the working directory (addition, updation or removal).
        ///
        /// Any paths (even those listed explicitly) that are ignored by configuration will not be staged unless <see cref="StageOptions.IncludeIgnored"/> is unset.
        /// </summary>
        /// <param name="paths">The collection of paths of the files within the working directory.</param>
        /// <param name="stageOptions">If set, determines how paths will be staged.</param>
        public virtual void Stage(IEnumerable <string> paths, StageOptions stageOptions = null)
        {
            Ensure.ArgumentNotNull(paths, "paths");

            DiffModifiers        diffModifiers        = DiffModifiers.IncludeUntracked;
            ExplicitPathsOptions explicitPathsOptions = stageOptions != null ? stageOptions.ExplicitPathsOptions : null;

            if (stageOptions != null && stageOptions.IncludeIgnored)
            {
                diffModifiers |= DiffModifiers.IncludeIgnored;
            }

            var changes = repo.Diff.Compare <TreeChanges>(diffModifiers, paths, explicitPathsOptions);

            foreach (var treeEntryChanges in changes)
            {
                switch (treeEntryChanges.Status)
                {
                case ChangeKind.Unmodified:
                    continue;

                case ChangeKind.Deleted:
                    RemoveFromIndex(treeEntryChanges.Path);
                    continue;

                case ChangeKind.Added:
                /* Fall through */
                case ChangeKind.Modified:
                    AddToIndex(treeEntryChanges.Path);
                    continue;

                default:
                    throw new InvalidOperationException(
                              string.Format(CultureInfo.InvariantCulture, "Entry '{0}' bears an unexpected ChangeKind '{1}'", treeEntryChanges.Path, treeEntryChanges.Status));
                }
            }

            UpdatePhysicalIndex();
        }
Beispiel #3
0
        /// <summary>
        /// Promotes to the staging area the latest modifications of a collection of files in the working directory (addition, updation or removal).
        ///
        /// Any paths (even those listed explicitly) that are ignored by configuration will not be staged unless <see cref="StageOptions.IncludeIgnored"/> is unset.
        /// </summary>
        /// <param name="repository">The repository in which to act</param>
        /// <param name="paths">The collection of paths of the files within the working directory.</param>
        /// <param name="stageOptions">Determines how paths will be staged.</param>
        public static void Stage(IRepository repository, IEnumerable <string> paths, StageOptions stageOptions)
        {
            Ensure.ArgumentNotNull(repository, "repository");
            Ensure.ArgumentNotNull(paths, "paths");

            DiffModifiers        diffModifiers        = DiffModifiers.IncludeUntracked;
            ExplicitPathsOptions explicitPathsOptions = stageOptions != null ? stageOptions.ExplicitPathsOptions : null;

            if (stageOptions != null && stageOptions.IncludeIgnored)
            {
                diffModifiers |= DiffModifiers.IncludeIgnored;
            }

            using (var changes = repository.Diff.Compare <TreeChanges>(diffModifiers, paths, explicitPathsOptions,
                                                                       new CompareOptions {
                Similarity = SimilarityOptions.None
            }))
            {
                var unexpectedTypesOfChanges = changes
                                               .Where(
                    tec => tec.Status != ChangeKind.Added &&
                    tec.Status != ChangeKind.Modified &&
                    tec.Status != ChangeKind.Conflicted &&
                    tec.Status != ChangeKind.Unmodified &&
                    tec.Status != ChangeKind.Deleted).ToList();

                if (unexpectedTypesOfChanges.Count > 0)
                {
                    throw new InvalidOperationException(
                              string.Format(CultureInfo.InvariantCulture,
                                            "Entry '{0}' bears an unexpected ChangeKind '{1}'",
                                            unexpectedTypesOfChanges[0].Path, unexpectedTypesOfChanges[0].Status));
                }

                /* Remove files from the index that don't exist on disk */
                foreach (TreeEntryChanges treeEntryChanges in changes)
                {
                    switch (treeEntryChanges.Status)
                    {
                    case ChangeKind.Conflicted:
                        if (!treeEntryChanges.Exists)
                        {
                            repository.Index.Remove(treeEntryChanges.Path);
                        }
                        break;

                    case ChangeKind.Deleted:
                        repository.Index.Remove(treeEntryChanges.Path);
                        break;

                    default:
                        continue;
                    }
                }

                foreach (TreeEntryChanges treeEntryChanges in changes)
                {
                    switch (treeEntryChanges.Status)
                    {
                    case ChangeKind.Added:
                    case ChangeKind.Modified:
                        repository.Index.Add(treeEntryChanges.Path);
                        break;

                    case ChangeKind.Conflicted:
                        if (treeEntryChanges.Exists)
                        {
                            repository.Index.Add(treeEntryChanges.Path);
                        }
                        break;

                    default:
                        continue;
                    }
                }

                repository.Index.Write();
            }
        }
Beispiel #4
0
 public virtual void Stage(IEnumerable <string> paths, StageOptions stageOptions = null)
 {
     repo.Stage(paths, stageOptions);
 }
Beispiel #5
0
 public virtual void Stage(string path, StageOptions stageOptions = null)
 {
     repo.Stage(path, stageOptions);
 }
Beispiel #6
0
        /// <summary>
        /// Promotes to the staging area the latest modifications of a file in the working directory (addition, updation or removal).
        ///
        /// If this path is ignored by configuration then it will not be staged unless <see cref="StageOptions.IncludeIgnored"/> is unset.
        /// </summary>
        /// <param name="path">The path of the file within the working directory.</param>
        /// <param name="stageOptions">If set, determines how paths will be staged.</param>
        public virtual void Stage(string path, StageOptions stageOptions = null)
        {
            Ensure.ArgumentNotNull(path, "path");

            Stage(new[] { path }, stageOptions);
        }
        private void ConfigureCommitCommands()
        {
            var canCommit = this.Files.WhenAny(vm => vm.Modified, vm => vm.Added, vm => vm.Removed, (mod, add, rem) =>
            {
                return mod.GetValue() > 0 || add.GetValue() > 0 || rem.GetValue() > 0;
            });

            this.commitUntrackedChanges = ReactiveCommand.CreateAsyncTask<CommitResultModel>(canCommit, async x =>
            {
                throw new NotImplementedException();
            });

            this.commitTrackedChanges = ReactiveCommand.CreateAsyncTask<CommitResultModel>(canCommit, async x =>
            {
                //Commit every modified file (tracked)
                StatusOptions status;
                StageOptions stage;
                IRepository repo;
                IList<string> committedFiles;
                IEnumerable<StatusEntry> modifiedFiles;

                committedFiles = new List<string>();
                stage = new StageOptions();
                status = new StatusOptions()
                {
                    DetectRenamesInIndex = true,
                    DetectRenamesInWorkDir = true
                };

                repo = this.currentRepositoryModel.Repository;

                modifiedFiles = await Task.Run<IEnumerable<StatusEntry>>(() => repo.RetrieveStatus(status).Modified);

                //Now stage all modified files
                foreach (var file in modifiedFiles)
                {
                    if (file.State.HasFlag(FileStatus.ModifiedInWorkdir) && file.State.HasFlag(FileStatus.ModifiedInIndex) == false)
                    {
                        await Task.Run(() => repo.Stage(file.FilePath, stage));
                        committedFiles.Add(file.FilePath);
                    }
                }

                return new CommitResultModel() { Files = committedFiles.AsEnumerable() };
            });
        }
Beispiel #8
0
        /// <summary>
        /// Promotes to the staging area the latest modifications of a collection of files in the working directory (addition, updation or removal).
        ///
        /// Any paths (even those listed explicitly) that are ignored by configuration will not be staged unless <see cref="StageOptions.IncludeIgnored"/> is unset.
        /// </summary>
        /// <param name="repository">The repository in which to act</param>
        /// <param name="paths">The collection of paths of the files within the working directory.</param>
        /// <param name="stageOptions">Determines how paths will be staged.</param>
        public static void Stage(IRepository repository, IEnumerable<string> paths, StageOptions stageOptions)
        {
            Ensure.ArgumentNotNull(repository, "repository");
            Ensure.ArgumentNotNull(paths, "paths");

            DiffModifiers diffModifiers = DiffModifiers.IncludeUntracked;
            ExplicitPathsOptions explicitPathsOptions = stageOptions != null ? stageOptions.ExplicitPathsOptions : null;

            if (stageOptions != null && stageOptions.IncludeIgnored)
            {
                diffModifiers |= DiffModifiers.IncludeIgnored;
            }

            using (var changes = repository.Diff.Compare<TreeChanges>(diffModifiers, paths, explicitPathsOptions,
                new CompareOptions { Similarity = SimilarityOptions.None }))
            {
                var unexpectedTypesOfChanges = changes
                    .Where(
                        tec => tec.Status != ChangeKind.Added &&
                        tec.Status != ChangeKind.Modified &&
                        tec.Status != ChangeKind.Conflicted &&
                        tec.Status != ChangeKind.Unmodified &&
                        tec.Status != ChangeKind.Deleted).ToList();

                if (unexpectedTypesOfChanges.Count > 0)
                {
                    throw new InvalidOperationException(
                        string.Format(CultureInfo.InvariantCulture,
                            "Entry '{0}' bears an unexpected ChangeKind '{1}'",
                            unexpectedTypesOfChanges[0].Path, unexpectedTypesOfChanges[0].Status));
                }

                /* Remove files from the index that don't exist on disk */
                foreach (TreeEntryChanges treeEntryChanges in changes)
                {
                    switch (treeEntryChanges.Status)
                    {
                        case ChangeKind.Conflicted:
                            if (!treeEntryChanges.Exists)
                            {
                                repository.Index.Remove(treeEntryChanges.Path);
                            }
                            break;

                        case ChangeKind.Deleted:
                            repository.Index.Remove(treeEntryChanges.Path);
                            break;

                        default:
                            continue;
                    }
                }

                foreach (TreeEntryChanges treeEntryChanges in changes)
                {
                    switch (treeEntryChanges.Status)
                    {
                        case ChangeKind.Added:
                        case ChangeKind.Modified:
                            repository.Index.Add(treeEntryChanges.Path);
                            break;

                        case ChangeKind.Conflicted:
                            if (treeEntryChanges.Exists)
                            {
                                repository.Index.Add(treeEntryChanges.Path);
                            }
                            break;

                        default:
                            continue;
                    }
                }

                repository.Index.Write();
            }
        }
Beispiel #9
0
        /// <summary>
        /// Promotes to the staging area the latest modifications of a file in the working directory (addition, updation or removal).
        ///
        /// If this path is ignored by configuration then it will not be staged unless <see cref="StageOptions.IncludeIgnored"/> is unset.
        /// </summary>
        /// <param name="repository">The repository in which to act</param>
        /// <param name="path">The path of the file within the working directory.</param>
        /// <param name="stageOptions">Determines how paths will be staged.</param>
        public static void Stage(IRepository repository, string path, StageOptions stageOptions)
        {
            Ensure.ArgumentNotNull(repository, "repository");
            Ensure.ArgumentNotNull(path, "path");

            Stage(repository, new[] { path }, stageOptions);
        }
        public GitBackupStatus InPlaceGitBackup(string repositoryFolder)
        {
            var isValidGitRepository = LibGit2Sharp.Repository.IsValid(repositoryFolder);
            var createdRepository = false;
            if (!isValidGitRepository)
            {
                _logger.InfoFormat("Initializing git repository in folder '{0}'...", repositoryFolder);
                repositoryFolder = LibGit2Sharp.Repository.Init(repositoryFolder, false);
                createdRepository = true;
            }
            var numberOfFilesAdded = 0;
            var numberOfFilesChanged = 0;
            var numberOfFilesRemoved = 0;
            using (var repository = new LibGit2Sharp.Repository(repositoryFolder))
            {
                var options = new StatusOptions();
                var status = repository.RetrieveStatus(options);
                _logger.Debug("Repository Status: " + JsonConvert.SerializeObject(status));
                if (status.IsDirty)
                {
                    var untractedFiles = status.Untracked;
                    foreach (var untrackedFile in untractedFiles)
                    {
                        _logger.Info("Added: " + untrackedFile.FilePath);
                        var stageOptions = new StageOptions();
                        repository.Stage(untrackedFile.FilePath, stageOptions);
                        numberOfFilesAdded++;
                    }

                    var modifiedFiles = status.Modified;
                    foreach (var modifiedFile in modifiedFiles)
                    {
                        _logger.Info("Modified: " + modifiedFile.FilePath);
                        var stageOptions = new StageOptions();
                        repository.Stage(modifiedFile.FilePath, stageOptions);
                        numberOfFilesChanged++;
                    }

                    var removedFiles = status.Missing;
                    foreach (var removedFile in removedFiles)
                    {
                        _logger.Info("Removed: " + removedFile.FilePath);
                        var stageOptions = new StageOptions();
                        repository.Stage(removedFile.FilePath, stageOptions);
                        numberOfFilesRemoved++;
                    }
                    var author = new Signature(Environment.UserName, "*****@*****.**", System.DateTimeOffset.Now);
                    var committer = new Signature(Environment.UserName, "*****@*****.**", System.DateTimeOffset.Now);
                    var commitOptions = new CommitOptions();
                    _logger.Info("Commiting...");
                    repository.Commit(DateTime.Now.ToString(CultureInfo.InvariantCulture), author, committer, commitOptions);
                }
            }
            var gitBackupStatus = new GitBackupStatus(createdRepository, numberOfFilesAdded, numberOfFilesChanged, numberOfFilesRemoved);
            return gitBackupStatus;
        }