long GetCommitSize(TfsGitCommit gitCommit, TeamFoundationRequestContext requestContext)
        {
            var tree = gitCommit.GetTree(requestContext);
            if (tree == null)
                return 0;

            long totalSize = tree.GetBlobs(requestContext).Aggregate(
                0L,
                (size, blob) => size + blob.GetLength(requestContext));
            return totalSize;
        }
Beispiel #2
0
        long GetCommitSize(TfsGitCommit gitCommit, TeamFoundationRequestContext requestContext)
        {
            var tree = gitCommit.GetTree(requestContext);

            if (tree == null)
            {
                return(0);
            }

            long totalSize = tree.GetBlobs(requestContext).Aggregate(
                0L,
                (size, blob) => size + blob.GetLength(requestContext));

            return(totalSize);
        }
Beispiel #3
0
        protected override PolicyEvaluationResult EvaluateInternal(IVssRequestContext requestContext, PushNotification push)
        {
            var repositoryService = requestContext.GetService <ITeamFoundationGitRepositoryService>();

            var newBranches = push.RefUpdateResults.Where(r => r.OldObjectId == Sha1Id.Empty && r.Name.StartsWith(Constants.BranchPrefix));
            var branch      = newBranches.SingleOrDefault(b => b.Name == Constants.BranchPrefix + "master");

            if (branch != null)
            {
                using (ITfsGitRepository repository = repositoryService.FindRepositoryById(requestContext, push.RepositoryId))
                {
                    TfsGitCommit commit = (TfsGitCommit)repository.LookupObject(branch.NewObjectId);
                    if (commit != null)
                    {
                        var tree        = commit.GetTree();
                        var treeEntries = tree.GetTreeEntries();
                        if (treeEntries.Any())
                        {
                            bool includesGitignore     = false;
                            bool includesGitattributes = false;
                            foreach (var entry in treeEntries)
                            {
                                if (entry.ObjectType == GitObjectType.Blob && entry.Name.Equals(".gitignore", StringComparison.OrdinalIgnoreCase))
                                {
                                    includesGitignore = true;
                                }

                                if (entry.ObjectType == GitObjectType.Blob && entry.Name.Equals(".gitattributes", StringComparison.OrdinalIgnoreCase))
                                {
                                    using (var reader = new StreamReader(entry.Object.GetContent()))
                                    {
                                        var gitattributesContents = reader.ReadToEnd();
                                        // Make sure .gitattributes file has a '* text=auto' line for eol normalization
                                        if (!Regex.IsMatch(gitattributesContents, @"^\*\s+text=auto\s*$", RegexOptions.Multiline))
                                        {
                                            Logger.Log("pushNotification:", push);
                                            var statusMessage = $".gitattributes is missing '* text=auto'. See {Settings.DocsBaseUrl}/guidelines/scm/git-conventions/#mandatory-files.";
                                            Logger.Log(statusMessage);
                                            return(new PolicyEvaluationResult(false, push.Pusher, statusMessage));
                                        }

                                        includesGitattributes = true;
                                    }
                                }
                            }

                            if (!includesGitignore || !includesGitattributes)
                            {
                                Logger.Log("pushNotification:", push);
                                var statusMessage = $"Mandatory files missing. See {Settings.DocsBaseUrl}/guidelines/scm/git-conventions/#mandatory-files.";
                                Logger.Log(statusMessage);
                                return(new PolicyEvaluationResult(false, push.Pusher, statusMessage));
                            }
                        }
                        else
                        {
                            Logger.Log("Commit without tree entries: " + branch.NewObjectId);
                        }
                    }
                    else
                    {
                        Logger.Log("Unable to find commit " + branch.NewObjectId);
                    }
                }
            }

            return(new PolicyEvaluationResult(true));
        }