示例#1
0
        public ActionResult History(Guid id)
        {
            using (var db = ApplicationDbContext.Create())
            {
                //var record = GetRecord(id, db);
                var file = GetFile(id, db);

                EnsureUserIsAllowed(file.CatalogRecord, db);

                var events = db.Events
                             .Where(x => x.RelatedManagedFiles.Any(f => f.Id == id))
                             .OrderByDescending(x => x.Timestamp)
                             .Include(x => x.RelatedCatalogRecord)
                             .Include(x => x.RelatedManagedFiles)
                             .Include(x => x.User);

                var model = new ManagedFileHistoryModel();
                model.File           = file;
                model.IsUserCurator  = true;
                model.IsUserApprover = true;

                var logs = new List <HistoryEventModel>();

                foreach (var log in events)
                {
                    var eventModel = HistoryEventModel.FromEvent(log, log.User);
                    logs.Add(eventModel);
                }

                // Sort all the events.
                var sorted = logs.OrderByDescending(x => x.Timestamp);
                foreach (var log in sorted)
                {
                    model.Events.Add(log);
                }

                return(View(model));
            }
        }
示例#2
0
        public ActionResult Revisions(Guid id)
        {
            using (var db = ApplicationDbContext.Create())
            {
                var file = GetFile(id, db);

                EnsureUserIsAllowed(file.CatalogRecord, db);

                var model = new ManagedFileHistoryModel();
                model.File = file;

                model.IsUserCurator  = file.CatalogRecord.Curators.Any(x => x.UserName == User.Identity.Name);
                model.IsUserApprover = file.CatalogRecord.Approvers.Any(x => x.UserName == User.Identity.Name) ||
                                       OrganizationHelper.DoesUserHaveRight(db, User, file.CatalogRecord.Organization.Id, Right.CanApprove);

                string processingDirectory = SettingsHelper.GetProcessingDirectory(file.CatalogRecord.Organization, db);
                string path = Path.Combine(processingDirectory, file.CatalogRecord.Id.ToString());
                using (var repo = new LibGit2Sharp.Repository(path))
                {
                    List <Tuple <Commit, TreeEntry> > modificationCommits = new List <Tuple <Commit, TreeEntry> >();

                    string currentSha  = null;            // startingItemSha;
                    string currentPath = model.File.Name; //startingItemPath;
                    Tuple <Commit, TreeEntry> current = null;

                    foreach (Commit c in repo.Commits)
                    {
                        if (c.Tree.Any <TreeEntry>(entry => entry.Name == currentPath))
                        {
                            // If file with given name was found, check its SHA
                            TreeEntry te = c.Tree.First <TreeEntry>(entry => entry.Name == currentPath);

                            if (te.Target.Sha == currentSha)
                            {
                                // In case if file's SHA matches
                                // file was not changed in this commit
                                // and temporary commit need to be updated to current one
                                current = new Tuple <Commit, TreeEntry>(c, te);
                            }
                            else
                            {
                                // file's SHA doesn't match
                                // file was changed during commit (or is first one)
                                // current commit needs to be added to the commits collection
                                // The file's SHA updated to current one
                                modificationCommits.Add(new Tuple <Commit, TreeEntry>(c, te));
                                currentSha = te.Target.Sha;
                                current    = null;
                            }
                        }
                        else
                        {
                            // File with given name not found. this means it was renamed.
                            // SHA should still be the same
                            if (c.Tree.Any <TreeEntry>(entry => entry.Target.Sha == currentSha))
                            {
                                TreeEntry te = c.Tree.First <TreeEntry>(entry => entry.Target.Sha == currentSha);
                                currentPath = te.Name;
                                modificationCommits.Add(new Tuple <Commit, TreeEntry>(c, te));
                                current = null;
                            }
                        }
                    }

                    if (current != null)
                    {
                        modificationCommits.Add(current);
                    }

                    foreach (var m in modificationCommits)
                    {
                        RevisionModel h = RevisionModel.FromCommit(m.Item1, m.Item2, file);

                        // replace uuid with real user name
                        ApplicationUser user = db.Users
                                               .Where(x => x.Id == h.CommitterName)
                                               .FirstOrDefault();
                        if (user != null)
                        {
                            h.CommitterName  = user.FullName;
                            h.CommitterEmail = user.Email;
                            h.CommitterId    = user.Id;
                        }

                        model.Revisions.Add(h);
                    }
                }

                return(View(model));
            }
        }