コード例 #1
0
        public void ResetFiles(string pathspec = null)
        {
            var localChanges    = GetLocalState();
            var treeFilesByName = ParseTreeFile().ToDictionary(r => r.Path);

            void ResetFile(string path)
            {
                if (localChanges.AddedFiles.Contains(path))
                {
                    UI.PrintLine($"Deleting {path} ...");
                    Directory.DeleteFile(path);
                }
                else if (localChanges.ModifiedFiles.Contains(path) || localChanges.DeletedFiles.Contains(path))
                {
                    UI.PrintLine($"Restoring {path} ...");
                    var blobSha      = treeFilesByName[path].BlobSha;
                    var fileContents = Api.GetBlob(blobSha);
                    Directory.CreateFile(fileContents, path);
                    Directory.SetUnmodified(path);
                }
            }

            if (pathspec == null)
            {
                if (!localChanges.HasChanges)
                {
                    UI.PrintLine("*** No changes match the supplied pathspec, nothing to do");
                    return;
                }

                treeFilesByName = ParseTreeFile().ToDictionary(r => r.Path);
                foreach (var file in localChanges.AllChangedFiles)
                {
                    ResetFile(file);
                }
            }
            else
            {
                var matchedFiles   = Directory.FindFiles(pathspec).Select(f => Directory.RelativePathOf(f));
                var filesToProcess = matchedFiles.Intersect(localChanges.AllChangedFiles).ToArray();
                if (filesToProcess.Length == 0)
                {
                    var deletedFile = localChanges.DeletedFiles.FirstOrDefault(f => f.Equals(pathspec, StringComparison.InvariantCultureIgnoreCase));
                    if (deletedFile == null)
                    {
                        UI.PrintLine("*** No changes match the supplied pathspec, nothing to do");
                        return;
                    }
                    filesToProcess = new[] { deletedFile };
                }

                foreach (var file in filesToProcess)
                {
                    ResetFile(file);
                }
            }
        }