public void Iterate()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<FileChanges>));

            using (var repo = new Repository(repoRoot))
            {
                string patchDirectory = Path.Combine(repo.Info.Path, "patches");

                if (!Directory.Exists(patchDirectory))
                {
                    Directory.CreateDirectory(patchDirectory);
                }

                int commitCount = repo.Commits.Count();
                int currentCommitCount = 0;

                foreach (Commit c in repo.Commits)
                {
                    currentCommitCount++;
                    Console.Write("\rProcessing {0}/{1} ({2}%)    ", currentCommitCount, commitCount, currentCommitCount * 100 / commitCount);

                    ExecuteCommitAnalysis(c, commitAnalysis, patchAnalysis);

                    if (c.Parents.Count() == 0)
                    {
                        continue;
                    }

                    List<FileChanges> fileChanges = null;

                    string ext = GetExtension();
                    string patchFileName = patchDirectory + "/" + c.Id + ext;

                    if (File.Exists(patchFileName))
                    {
                        //Console.WriteLine("Reloading {0} from cache.", c.Id);
                        try
                        {
                            switch (currentSeerializer)
                            {
                                case SerializerType.XmlSerializer:
                                    fileChanges = (List<FileChanges>)serializer.Deserialize(File.OpenRead(patchFileName));
                                    break;
                                case SerializerType.ProtoBuf:
                                    fileChanges = Serializer.Deserialize<List<FileChanges>>(File.OpenRead(patchFileName));
                                    break;
                                default:
                                    break;
                            }

                        }
                        catch
                        {
                            // any exception to deserialize, we attempt to find the patch info again and serialize.
                        }
                    }

                    if (fileChanges == null)
                    {
                        fileChanges = new List<FileChanges>();
                        // TODO: need to handle multiple parents.
                        if (c.Parents.Count() > 1)
                        {
                            // ignore all merge commits
                            continue;
                        }

                        Patch changes = null;
                        changes = repo.Diff.Compare<Patch>(c.Parents.First().Tree, c.Tree);

                        foreach (var patchChanges in changes)
                        {
                            FileChanges change = new FileChanges()
                            {
                                LinesAdded = patchChanges.LinesAdded,
                                LinesDeleted = patchChanges.LinesDeleted,
                                Diff = patchChanges.Patch,
                                Path = patchChanges.Path
                            };

                            fileChanges.Add(change);
                        }

                        switch (currentSeerializer)
                        {
                            case SerializerType.XmlSerializer:
                                serializer.Serialize(new FileStream(patchFileName, FileMode.OpenOrCreate), fileChanges);
                                break;
                            case SerializerType.ProtoBuf:
                                Serializer.Serialize(new FileStream(patchFileName, FileMode.OpenOrCreate), fileChanges);
                                break;
                            default:
                                break;
                        }
                    }

                    ExecutePatchAnalysis(patchAnalysis, c, fileChanges);
                }
            }
        }
Exemple #2
0
        public void Iterate()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List <FileChanges>));

            using (var repo = new Repository(repoRoot))
            {
                string patchDirectory = Path.Combine(repo.Info.Path, "patches");

                if (!Directory.Exists(patchDirectory))
                {
                    Directory.CreateDirectory(patchDirectory);
                }

                int commitCount        = repo.Commits.Count();
                int currentCommitCount = 0;

                foreach (Commit c in repo.Commits)
                {
                    currentCommitCount++;
                    Console.Write("\rProcessing {0}/{1} ({2}%)    ", currentCommitCount, commitCount, currentCommitCount * 100 / commitCount);

                    ExecuteCommitAnalysis(c, commitAnalysis, patchAnalysis);

                    if (c.Parents.Count() == 0)
                    {
                        continue;
                    }

                    List <FileChanges> fileChanges = null;

                    string ext           = GetExtension();
                    string patchFileName = patchDirectory + "/" + c.Id + ext;

                    if (File.Exists(patchFileName))
                    {
                        //Console.WriteLine("Reloading {0} from cache.", c.Id);
                        try
                        {
                            switch (currentSeerializer)
                            {
                            case SerializerType.XmlSerializer:
                                fileChanges = (List <FileChanges>)serializer.Deserialize(File.OpenRead(patchFileName));
                                break;

                            case SerializerType.ProtoBuf:
                                fileChanges = Serializer.Deserialize <List <FileChanges> >(File.OpenRead(patchFileName));
                                break;

                            default:
                                break;
                            }
                        }
                        catch
                        {
                            // any exception to deserialize, we attempt to find the patch info again and serialize.
                        }
                    }

                    if (fileChanges == null)
                    {
                        fileChanges = new List <FileChanges>();
                        // TODO: need to handle multiple parents.
                        if (c.Parents.Count() > 1)
                        {
                            // ignore all merge commits
                            continue;
                        }

                        Patch changes = null;
                        changes = repo.Diff.Compare <Patch>(c.Parents.First().Tree, c.Tree);

                        foreach (var patchChanges in changes)
                        {
                            FileChanges change = new FileChanges()
                            {
                                LinesAdded   = patchChanges.LinesAdded,
                                LinesDeleted = patchChanges.LinesDeleted,
                                Diff         = patchChanges.Patch,
                                Path         = patchChanges.Path
                            };

                            fileChanges.Add(change);
                        }

                        switch (currentSeerializer)
                        {
                        case SerializerType.XmlSerializer:
                            serializer.Serialize(new FileStream(patchFileName, FileMode.OpenOrCreate), fileChanges);
                            break;

                        case SerializerType.ProtoBuf:
                            Serializer.Serialize(new FileStream(patchFileName, FileMode.OpenOrCreate), fileChanges);
                            break;

                        default:
                            break;
                        }
                    }

                    ExecutePatchAnalysis(patchAnalysis, c, fileChanges);
                }
            }
        }