Formats exceptions expected in this application with type-specific details.
Example #1
0
        private void ShowException(Exception exception)
        {
            var message = ExceptionFormatter.Format(exception);

            logger.WriteLine("ERROR: {0}", message);
            logger.WriteLine(exception);

            MessageBox.Show(message, "Unhandled Exception",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
Example #2
0
        private void ProcessItem(VssItem item, string path, PathMatcher exclusionMatcher)
        {
            try
            {
                foreach (VssRevision vssRevision in item.Revisions)
                {
                    var actionType  = vssRevision.Action.Type;
                    var namedAction = vssRevision.Action as VssNamedAction;
                    if (namedAction != null)
                    {
                        if (actionType == VssActionType.Destroy)
                        {
                            // track destroyed files so missing history can be anticipated
                            // (note that Destroy actions on shared files simply delete
                            // that copy, so destroyed files can't be completely ignored)
                            destroyedFiles.Add(namedAction.Name.PhysicalName);
                        }

                        var targetPath = path + VssDatabase.ProjectSeparator + namedAction.Name.LogicalName;
                        if (exclusionMatcher != null && exclusionMatcher.Matches(targetPath))
                        {
                            // project action targets an excluded file
                            continue;
                        }
                    }

                    Revision revision = new Revision(vssRevision.DateTime,
                                                     vssRevision.User, item.ItemName, vssRevision.Version,
                                                     vssRevision.Comment, vssRevision.Action);

                    ICollection <Revision> revisionSet;
                    if (!sortedRevisions.TryGetValue(vssRevision.DateTime, out revisionSet))
                    {
                        revisionSet = new LinkedList <Revision>();
                        sortedRevisions[vssRevision.DateTime] = revisionSet;
                    }
                    revisionSet.Add(revision);
                    ++revisionCount;
                }
            }
            catch (RecordException e)
            {
                bool isknown;
                var  message = string.Format("Failed to read revisions for {0} ({1}): {2}",
                                             path, item.PhysicalName, ExceptionFormatter.Format(e, out isknown));
                message = isknown ? message : e.ToString();
                LogException(e);
                ReportError(message);
            }
        }
Example #3
0
        private void ShowException(Exception exception)
        {
            bool   isKnown;
            string message = ExceptionFormatter.Format(exception, out isKnown);

            //logger.WriteLine("ERROR: {0}", message);
            //logger.WriteLine(exception);

            if (!isKnown)
            {
                message = exception.ToString();
            }
            MessageBox.Show(message, "Unhandled Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
Example #4
0
        private bool WriteRevisionTo(string physical, int version, string destPath)
        {
            VssFile         item;
            VssFileRevision revision;
            Stream          contents;

            try
            {
                item     = (VssFile)database.GetItemPhysical(physical);
                revision = item.GetRevision(version);
                contents = revision.GetContents();
            }
            catch (Exception e)
            {
                // log an error for missing data files or versions, but keep processing
                var message = ExceptionFormatter.Format(e);
                logger.WriteLine("ERROR: {0}", message);
                logger.WriteLine(e);
                return(false);
            }

            // propagate exceptions here (e.g. disk full) to abort/retry/ignore
            using (contents)
            {
                WriteStream(contents, destPath);
            }

            // try to use the first revision (for this branch) as the create time,
            // since the item creation time doesn't seem to be meaningful
            var createDateTime = item.Created;

            using (var revEnum = item.Revisions.GetEnumerator())
            {
                if (revEnum.MoveNext())
                {
                    createDateTime = revEnum.Current.DateTime;
                }
            }

            // set file creation and update timestamps
            File.SetCreationTimeUtc(destPath, TimeZoneInfo.ConvertTimeToUtc(createDateTime));
            File.SetLastWriteTimeUtc(destPath, TimeZoneInfo.ConvertTimeToUtc(revision.DateTime));

            return(true);
        }
Example #5
0
        private void ProcessProject(VssProject project, string path, PathMatcher exclusionMatcher)
        {
            try
            {
                ProjectLocation projectLocation;
                if (sortedProjectLocations.TryGetValue(project.PhysicalName, out projectLocation))
                {
                    logger.WriteLine("Unexpected: ProjectAnalyzer.ProcessProject: sortedProjectLocations already contains project: {0}", project.PhysicalName);
                }
                else
                {
                    projectLocation = new ProjectLocation(project.PhysicalName, path);
                    sortedProjectLocations[project.PhysicalName] = projectLocation;
                };

                foreach (VssRevision vssRevision in project.Revisions)
                {
                    var actionType  = vssRevision.Action.Type;
                    var namedAction = vssRevision.Action as VssNamedAction;
                    if (namedAction != null)
                    {
                        var targetPath = path + VssDatabase.ProjectSeparator + namedAction.Name.LogicalName;
                        if (exclusionMatcher != null && exclusionMatcher.Matches(targetPath))
                        {
                            // project action targets an excluded file
                            continue;
                        }

                        if (namedAction.Name.IsProject)
                        {
                            if (
                                (actionType == VssActionType.Delete) ||
                                (actionType == VssActionType.Destroy)
                                )
                            {
                                deletedProjects.Add(namedAction.Name.PhysicalName);
                            }
                            else if (
                                (actionType == VssActionType.Recover) ||
                                (actionType == VssActionType.Share)
                                )
                            {
                                deletedProjects.Remove(namedAction.Name.PhysicalName);
                            }
                        }
                        else
                        {
                            if (
                                (actionType == VssActionType.Delete) ||
                                (actionType == VssActionType.Destroy)
                                )
                            {
                                projectLocation.DeletedFiles.Add(namedAction.Name.PhysicalName);
                            }
                            else if (
                                (actionType == VssActionType.Recover) ||
                                (actionType == VssActionType.Share)
                                )
                            {
                                projectLocation.DeletedFiles.Remove(namedAction.Name.PhysicalName);
                            }
                        }
                    }
                }
            }
            catch (RecordException e)
            {
                var message = string.Format("ProjectAnalyzer.ProcessProject: Failed to process project for {0} ({1}): {2}",
                                            path, project.PhysicalName, ExceptionFormatter.Format(e));
                LogException(e, message);
                ReportError(message);
            }
        }
Example #6
0
        private static void RunFromCommandLine(ProgramOptions options)
        {
            var logger            = string.IsNullOrEmpty(options.LogFile) ? Logger.Null : new Logger(options.LogFile);
            var workQueue         = new WorkQueue(1);
            var messageDispatcher = new ConsoleLoggerMessageDispatcher(logger, options.IgnoreErrors);

            messageDispatcher.Dispatch(MessageType.Info, workQueue.LastStatus ?? "Idle", MessageChoice.Ok);

            var encoding = Encoding.GetEncoding(options.CodePage);
            var df       = new VssDatabaseFactory(options.VssDirectory)
            {
                Encoding = encoding
            };
            var db   = df.Open();
            var item = db.GetItem(options.VssProject);

            if (!(item is VssProject project))
            {
                logger.WriteLine("Error: Not a vss project.");
                return;
            }

            var revisionAnalyzer = new RevisionAnalyzer(workQueue, logger, db, messageDispatcher);

            var changesetBuilder = new ChangesetBuilder(workQueue, logger, revisionAnalyzer, messageDispatcher)
            {
                AnyCommentThreshold  = TimeSpan.FromSeconds(options.AnyCommentSeconds),
                SameCommentThreshold = TimeSpan.FromSeconds(options.SameCommentSeconds)
            };

            void OnTimer(object state)
            {
                messageDispatcher.Dispatch(MessageType.Info, workQueue.LastStatus ?? "Idle", MessageChoice.Ok);
                messageDispatcher.Dispatch(MessageType.Info, $"Files: {revisionAnalyzer.FileCount}, Revisions: {revisionAnalyzer.RevisionCount}", MessageChoice.Ok);
                messageDispatcher.Dispatch(MessageType.Info, $"Changesets: {changesetBuilder.Changesets.Count}", MessageChoice.Ok);

                var exceptions = workQueue.FetchExceptions();

                if (exceptions != null)
                {
                    foreach (var exception in exceptions)
                    {
                        var message = ExceptionFormatter.Format(exception);
                        logger.WriteLine("ERROR: {0}", message);
                        logger.WriteLine(exception);
                    }
                }
            }

            var timer = new Timer(OnTimer, null, TimeSpan.Zero, TimeSpan.FromSeconds(1));

            if (!string.IsNullOrWhiteSpace(options.VssExcludePaths))
            {
                revisionAnalyzer.ExcludeFiles = options.VssExcludePaths;
            }
            revisionAnalyzer.AddItem(project);

            changesetBuilder.BuildChangesets();

            if (!string.IsNullOrEmpty(options.GitDirectory))
            {
                var gitExporter = new GitExporter(workQueue, logger,
                                                  revisionAnalyzer, changesetBuilder, messageDispatcher);
                if (!string.IsNullOrEmpty(options.DefaultEmailDomain))
                {
                    gitExporter.EmailDomain = options.DefaultEmailDomain;
                }
                if (!string.IsNullOrEmpty(options.EmailMapFile))
                {
                    if (File.Exists(options.EmailMapFile))
                    {
                        gitExporter.EmailMapFile = options.EmailMapFile;
                    }
                    else
                    {
                        logger.WriteLine($"Warn: {options.EmailMapFile} does not exist.");
                    }
                }
                if (!string.IsNullOrEmpty(options.DefaultComment))
                {
                    gitExporter.DefaultComment = options.DefaultComment;
                }
                if (!options.TranscodeComments)
                {
                    gitExporter.CommitEncoding = encoding;
                }
                gitExporter.IgnoreErrors = options.IgnoreErrors;
                gitExporter.ExportToGit(options.GitDirectory);
            }

            workQueue.WaitIdle();
            timer.Dispose();

            messageDispatcher.Dispatch(MessageType.Info, "Done", MessageChoice.Ok);

            Application.Exit();
        }