Exemple #1
0
        // GET: /<controller>/
        public async Task <IActionResult> Raw([FromServices] IContentProvider contentProvider, string path)
        {
            var cp       = ContentPath.FromUrlPath(path);
            var content  = contentProvider.Read(cp);
            var mimeType = cp.Name.Select(MimeTypes.GetMimeType).ValueOr(MimeTypes.FallbackMimeType);

            return(File(content.Value, mimeType));
        }
Exemple #2
0
        internal static GrepResult ParseGitGrepOutputLine(string line)
        {
            var p = line.Split(":", 3);

            return(new GrepResult
            {
                Path = ContentPath.FromUrlPath(p[0]),
                LineNumber = Int32.Parse(p[1]),
                Text = p[2]
            });
        }
Exemple #3
0
        public async Task <IActionResult> Grep(
            [FromServices] IContentProvider contentProvider,
            [FromServices] IProcessRunner runner,
            [FromServices] IMarkdownRenderer renderer,
            [FromServices] IContentGrep grep,
            [FromQuery] string q)
        {
            var result = await grep.Grep(q);

            var markdown = result.Select(_ => $"* [{_.Path}]({_.Path.AbsoluteHref}#{_.LineNumber})({_.LineNumber}): `{_.Text.Truncate(256)}`").JoinLines();

            return(await MarkdownView(renderer, ContentPath.FromUrlPath("/"), markdown));
        }
Exemple #4
0
        // GET: /<controller>/
        public async Task <IActionResult> Index(
            [FromServices] IContentProvider contentProvider,
            [FromServices] IMarkdownRenderer renderer,
            [FromServices] IHistory history,
            [FromServices] IGit git,
            [FromQuery] string log,
            [FromQuery] string q,
            string path)
        {
            var contentPath = ContentPath.FromUrlPath(path);

            if (this.HttpContext.WebSockets.IsWebSocketRequest)
            {
                await NotifyContentChanged(this.HttpContext.WebSockets, contentProvider, contentPath);

                return(Ok());
            }

            if (log != null)
            {
                var r = await git.Run(new[] { "log" }.Concat(Utils.SplitArguments(log)).Concat(new[] { "--", git.GetPath(contentPath) }));

                return(await MarkdownView(renderer, contentPath, AsCode(r.Output)));
            }

            if (q != null)
            {
                var pretty  = $"--pretty=format:* [%ar: %s]({git.GetPath(contentPath)}?log=--stat+-p+-1+-U+%H), by %an";
                var grepLog = await git.Run(new[] { "log", pretty, "-100", "-S", q, git.GetPath(contentPath) });

                var grepText = git.GrepOutputToMarkdown((await git.Run(new[] { "grep", "-I", "-n", q })).Output);
                return(await MarkdownView(renderer, contentPath, grepLog.Output + hr + grepText));
            }

            var children = contentProvider.GetChildren(contentPath);

            if (children.Any())
            {
                if (!this.HttpContext.Request.Path.Value.EndsWith("/"))
                {
                    var url        = Microsoft.AspNetCore.Http.Extensions.UriHelper.GetEncodedUrl(this.HttpContext.Request);
                    var redirectTo = url + "/";
                    return(this.Redirect(redirectTo));
                }
                return(await MarkdownView(renderer, contentPath, GetDirectoryMarkdown(contentProvider, history, contentPath, children)));
            }

            if (contentPath.IsExtension(new[] { ".md" }))
            {
                return(await MarkdownView(renderer, contentPath, GetMarkdown(contentProvider, history, contentPath)));
            }

            var mdFile = contentPath.CatName(".md");

            if (mdFile.HasValue && contentProvider.Exists(mdFile.Value))
            {
                return(await MarkdownView(renderer, contentPath, GetText(contentProvider, mdFile.Value)));
            }

            if (IsText(contentProvider, contentPath))
            {
                return(await MarkdownView(renderer, contentPath, GetSourceAsMarkdown(contentProvider, history, contentPath)));
            }

            contentProvider.Pull();

            // raw file
            return(await Raw(contentProvider, path));
        }
Exemple #5
0
        public async Task <IActionResult> Stats(
            [FromServices] IMarkdownRenderer renderer,
            [FromServices] IGit git,
            [FromQuery] string q,
            [FromQuery] string since,
            [FromQuery] string until,
            [FromQuery] string author
            )
        {
            var logArgs = Utils.SplitArguments(q).Concat(new[]
            {
                Param(nameof(since), since),
                Param(nameof(until), until),
                Param(nameof(author), author)
            }.WhereValue()).ToArray();

            var c = git.GetChanges(logArgs);

            var byName = c.GroupBy(_ => _.Commit.author.Email)
                         .Select(_ => new
            {
                User         = _.Key,
                AddedLines   = _.Sum(_c => _c.Stats.AddedLines),
                RemovedLines = _.Sum(_c => _c.Stats.RemovedLines)
            })
                         .OrderByDescending(_ => _.AddedLines + _.RemovedLines)
                         .ToList();

            var byPath = c
                         .SelectMany(_ => ContentPath.FromUrlPath(_.Stats.Path).Lineage
                                     .Select(dir => new { Path = dir, Change = _ }))
                         .GroupBy(_ => _.Path)
                         .Select(_ => new
            {
                Path         = _.Key,
                AddedLines   = _.Sum(_c => _c.Change.Stats.AddedLines),
                RemovedLines = _.Sum(_c => _c.Change.Stats.RemovedLines)
            })
                         .OrderByDescending(_ => _.AddedLines)
                         .Take(100)
                         .ToList();

            var markdown = $@"
<form action=""/stats"" method=""get"" >
<input name=""since"" placeholder=""since yyyy-mm-dd"" value={Attribute(since)} />
<input name=""until"" placeholder=""until yyyy-mm-dd"" value={Attribute(until)} />
<input name=""author"" placeholder=""author"" value={Attribute(author)} />
<input type=""submit"" />
</form>

## By User

{byName.MarkdownTable()
    .With("User", _ => $"[{_.User}](/stats?author={_.User})")
    .With("Added Lines", _ => _.AddedLines)
    .With("Removed Lines", _ => _.RemovedLines)
}

## By Path

{byPath.MarkdownTable()
    .With("Path", _ => $"[{_.Path}](/{_.Path})")
    .With("Added Lines", _ => _.AddedLines)
    .With("Removed Lines", _ => _.RemovedLines)
}

";

            return(await MarkdownView(renderer, new ContentPath(), markdown));
        }