コード例 #1
0
        //public static String GetUrl(this PathBarModel model, String action, String path)
        //{
        //    return $"/{model.Owner}/{model.Name}/{action}/{model.ReferenceName ?? model.ReferenceSha}/{path}";
        //}

        public static void FixMarkdown(this TreeEntryModel model, String baseurl)
        {
            var txt = model.TextContent;

            // 预处理Markdown链接和图片
            var p  = 0;
            var ps = new Int32[] { 0, 0 };

            while (true)
            {
                var url = txt.Substring("](", ")", p, ps);
                if (url.IsNullOrEmpty() || ps[0] < 0)
                {
                    break;
                }

                if (!url.IsNullOrEmpty() && !url.StartsWithIgnoreCase("http://", "https://", "/"))
                {
                    // 处理Url。找到当前文件路径
                    var path = model.Path;
                    var p2   = path.LastIndexOf("/");
                    if (p2 >= 0)
                    {
                        path = path.Substring(0, p2 + 1);
                    }
                    else
                    {
                        path = "";
                    }

                    var name = model.ReferenceName;
                    if (name.IsNullOrEmpty())
                    {
                        name = "master";
                    }
                    url = $"{baseurl}/Blob/{name}/{path}{url}";

                    // 重新拼接
                    txt = txt.Substring(0, ps[0]) + url + txt.Substring(ps[1]);
                }

                // 下一次
                p = ps[0];
            }

            model.TextContent = txt;
        }
コード例 #2
0
ファイル: GitService.cs プロジェクト: zmking888/GitCandy
        public TreeEntryModel GetBlob(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 cacheAccessor = GitCacheAccessor.Singleton(new LastCommitAccessor(_repoId, _repository, commit, path));
            var lastCommitSha = cacheAccessor.Result.Value;

            if (lastCommitSha != commit.Sha)
            {
                commit = _repository.Lookup <Commit>(lastCommitSha);
            }

            var data      = blob.GetContentStream().ToBytes();
            var encoding  = FileHelper.DetectEncoding(data, CpToEncoding(commit.Encoding), _i18n.Value);
            var extension = Path.GetExtension(entry.Name).ToLower();
            var model     = new TreeEntryModel
            {
                Name          = entry.Name,
                ReferenceName = referenceName,
                Sha           = commit.Sha,
                Path          = string.IsNullOrEmpty(path) ? "" : path,
                Commit        = new CommitModel
                {
                    Sha                = commit.Sha,
                    Author             = commit.Author,
                    Committer          = commit.Committer,
                    CommitMessage      = commit.Message.RepetitionIfEmpty(UnknowString),
                    CommitMessageShort = commit.MessageShort.RepetitionIfEmpty(UnknowString),
                    Parents            = commit.Parents.Select(s => s.Sha).ToArray()
                },
                EntryType   = entry.TargetType,
                RawData     = data,
                SizeString  = FileHelper.GetSizeString(data.Length),
                TextContent = encoding == null
                    ? null
                    : FileHelper.ReadToEnd(data, encoding),
                TextBrush = FileHelper.BrushMapping.ContainsKey(extension)
                    ? FileHelper.BrushMapping[extension]
                    : "no-highlight",
                BlobType = encoding == null
                    ? FileHelper.ImageSet.Contains(extension)
                        ? BlobType.Image
                        : BlobType.Binary
                    : extension == ".md"
                        ? BlobType.MarkDown
                        : BlobType.Text,
                BlobEncoding   = encoding,
                BranchSelector = GetBranchSelectorModel(referenceName, commit.Sha, path),
                PathBar        = new PathBarModel
                {
                    Name          = Name,
                    Action        = "Tree",
                    Path          = path,
                    ReferenceName = referenceName,
                    ReferenceSha  = commit.Sha,
                    HideLastSlash = true,
                },
            };

            return(model);
        }
コード例 #3
0
        public TreeEntryModel GetBlob(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 lastCommitSha = GitCache.Get <string>(blob.Sha, "affectedcommit");

            if (lastCommitSha == null)
            {
                var hs    = new HashSet <string>();
                var queue = new Queue <Commit>();
                queue.Enqueue(commit);
                hs.Add(commit.Sha);
                while (queue.Count > 0)
                {
                    commit = queue.Dequeue();
                    var has = false;
                    foreach (var parent in commit.Parents)
                    {
                        var tree = parent[path];
                        if (tree == null)
                        {
                            continue;
                        }
                        var eq = tree.Target.Sha == blob.Sha;
                        if (eq && hs.Add(parent.Sha))
                        {
                            queue.Enqueue(parent);
                        }
                        has = has || eq;
                    }
                    if (!has)
                    {
                        break;
                    }
                }
                lastCommitSha = commit.Sha;
                GitCache.Set(blob.Sha, "affectedcommit", lastCommitSha);
            }
            if (lastCommitSha != commit.Sha)
            {
                commit = _repository.Lookup <Commit>(lastCommitSha);
            }

            var data      = blob.GetContentStream().ToBytes();
            var encoding  = FileHelper.DetectEncoding(data, CpToEncoding(commit.Encoding), _i18n.Value);
            var extension = Path.GetExtension(entry.Name).ToLower();
            var model     = new TreeEntryModel
            {
                Name          = entry.Name,
                ReferenceName = referenceName,
                Sha           = commit.Sha,
                Path          = string.IsNullOrEmpty(path) ? "" : path,
                Commit        = new CommitModel
                {
                    Sha                = commit.Sha,
                    Author             = commit.Author,
                    Committer          = commit.Committer,
                    CommitMessage      = commit.Message.RepetitionIfEmpty(NoCommitMessage),
                    CommitMessageShort = commit.MessageShort.RepetitionIfEmpty(NoCommitMessage),
                    Parents            = commit.Parents.Select(s => s.Sha).ToArray()
                },
                EntryType   = entry.TargetType,
                RawData     = data,
                SizeString  = FileHelper.GetSizeString(data.Length),
                TextContent = encoding == null
                    ? null
                    : FileHelper.ReadToEnd(data, encoding),
                TextBrush = FileHelper.BrushMapping.ContainsKey(extension)
                    ? FileHelper.BrushMapping[extension]
                    : "no-highlight",
                BlobType = encoding == null
                    ? FileHelper.ImageSet.Contains(extension)
                        ? BlobType.Image
                        : BlobType.Binary
                    : extension == ".md"
                        ? BlobType.MarkDown
                        : BlobType.Text,
                BlobEncoding   = encoding,
                BranchSelector = GetBranchSelectorModel(referenceName, commit.Sha, path),
                PathBar        = new PathBarModel
                {
                    Name          = Name,
                    Action        = "Tree",
                    Path          = path,
                    ReferenceName = referenceName,
                    ReferenceSha  = commit.Sha,
                    HideLastSlash = true,
                },
            };

            return(model);
        }