コード例 #1
0
ファイル: GitService.cs プロジェクト: NewLifeX/GitCandy
        public BlameModel GetBlame(String path)
        {
            var cfg = UserConfiguration.Current;

            if (!cfg.AllowBlame)
            {
                return(null);
            }

            var commit = GetCommitByPath(ref path, out var referenceName);

            if (commit == null)
            {
                return(null);
            }

            var entry = commit[path];

            if (entry == null || entry.TargetType != TreeEntryTargetType.Blob)
            {
                return(null);
            }

            var blob = (Blob)entry.Target;

            var accessor = GitCacheAccessor.Singleton(new BlameAccessor(_repoId, _repository, commit, path, CpToEncoding(commit.Encoding), _i18n.Value));
            var hunks    = accessor.Result.Value;

            var model = new BlameModel
            {
                ReferenceName  = referenceName,
                Sha            = commit.Sha,
                Path           = String.IsNullOrEmpty(path) ? "" : path,
                SizeString     = FileHelper.GetSizeString(blob.Size),
                Brush          = FileHelper.GetBrush(path),
                Hunks          = hunks,
                BranchSelector = GetBranchSelectorModel(referenceName, commit.Sha, path),
                PathBar        = new PathBarModel
                {
                    Name          = Name,
                    Action        = "Tree",
                    Path          = path,
                    ReferenceName = referenceName,
                    ReferenceSha  = commit.Sha,
                    HideLastSlash = true,
                },
            };

            return(model);
        }
コード例 #2
0
        public BlameModel GetBlame(string path)
        {
            string referenceName;
            var    commit = GetCommitByPath(ref path, out referenceName);

            if (commit == null)
            {
                return(null);
            }

            var entry = commit[path];

            if (entry == null || entry.TargetType != TreeEntryTargetType.Blob)
            {
                return(null);
            }

            var blob     = (Blob)entry.Target;
            var bytes    = blob.GetContentStream().ToBytes();
            var encoding = FileHelper.DetectEncoding(bytes, CpToEncoding(commit.Encoding), _i18n.Value);

            if (encoding == null)
            {
                return(null);
            }

            var code   = FileHelper.ReadToEnd(bytes, encoding);
            var reader = new StringReader(code);

            var hunks = GitCache.Get <BlameHunkModel[]>(blob.Sha, "blame");

            if (hunks == null)
            {
                var blame = _repository.Blame(path, new BlameOptions {
                    StartingAt = commit
                });
                hunks = blame.Select(s => new BlameHunkModel
                {
                    Code         = reader.ReadLines(s.LineCount),
                    StartLine    = s.FinalStartLineNumber,
                    EndLine      = s.LineCount,
                    MessageShort = s.FinalCommit.MessageShort.RepetitionIfEmpty(NoCommitMessage),
                    Sha          = s.FinalCommit.Sha,
                    Author       = s.FinalCommit.Author.Name,
                    AuthorEmail  = s.FinalCommit.Author.Email,
                    AuthorDate   = s.FinalCommit.Author.When,
                })
                        .ToArray();
                GitCache.Set(blob.Sha, "blame", hunks);
            }

            var model = new BlameModel
            {
                ReferenceName  = referenceName,
                Sha            = commit.Sha,
                Path           = string.IsNullOrEmpty(path) ? "" : path,
                SizeString     = FileHelper.GetSizeString(blob.Size),
                Brush          = FileHelper.GetBrush(path),
                Hunks          = hunks,
                BranchSelector = GetBranchSelectorModel(referenceName, commit.Sha, path),
                PathBar        = new PathBarModel
                {
                    Name          = Name,
                    Action        = "Tree",
                    Path          = path,
                    ReferenceName = referenceName,
                    ReferenceSha  = commit.Sha,
                    HideLastSlash = true,
                },
            };

            return(model);
        }