Skip to content

Commit

Permalink
display git repo info and authors; refs #27
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysVuika committed Jun 12, 2023
1 parent 197f81c commit 5c4755d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
41 changes: 38 additions & 3 deletions src/assets/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,38 @@
}
}

class GitReport extends ReportPanel {
render({ data }, { activeTab }) {
const TabHeader = ({ index, title }) => html`
<a class="${ activeTab === index ? 'is-active' : null}" onClick="${() => this.setActiveTab(index)}">${title}</a>
`;

return html`
<nav class="panel is-link">
<p class="panel-heading">Git</p>
<p class="panel-tabs">
<${TabHeader} index=${0} title="Stats" />
<${TabHeader} index=${1} title="Authors" />
</p>
<div style="max-height: 500px; overflow-y: scroll">
${activeTab === 0 && html`
<${LinkRow} caption="Remote: ${data.remote}" url="${data.remote}"/>
<${LinkRow} caption="Branch: ${data.branch}"/>
<${LinkRow} caption="Head: ${data.target}"/>
`}
${activeTab === 1 && html`
${data.authors.map(author => html`
<${LinkRow} caption="${author.name} (${author.commits} commits)"/>
`)}
`}
</div>
</nav>
`;
}
}

function FileTypes({ data }) {
const rows = Object.keys(data)
.map(key => ({ key, value: data[key] }))
Expand All @@ -501,13 +533,16 @@

function App({ data }) {
const hasModule = (moduleName) => data.project.modules.includes(moduleName);
const hasGit = !!data.project.git;

return html`
<main>
<${Header} data=${data}></Header>
<section class="section">
${hasModule('packages') && html`<${Packages} data=${data}></Packages>`}
${hasModule('angular-entities') && html`<${Angular} data=${data}></Angular>`}
${hasModule('angular-tests') && html`<${Tests} data=${data}></Tests>`}
${hasGit && html`<${GitReport} data=${data.project.git}/>`}
${hasModule('packages') && html`<${Packages} data=${data}/>`}
${hasModule('angular-entities') && html`<${Angular} data=${data}/>`}
${hasModule('angular-tests') && html`<${Tests} data=${data}/>`}
${hasModule('file-types') && html`<${FileTypes} data=${data.stats.types}></FileTypes>`}
</section>
</main>
Expand Down
49 changes: 49 additions & 0 deletions src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use crate::models::PackageJsonFile;
use chrono::Utc;
use git2::Repository;
use ignore::WalkBuilder;
use serde::Serialize;
use serde_json::{json, Map, Value};
use std::collections::HashMap;
use std::error::Error;
use std::path::PathBuf;

Expand Down Expand Up @@ -81,10 +83,14 @@ impl Workspace {
Some(value) => value,
None => url,
};

let authors = get_commit_authors(&repo).unwrap();

project.entry("git").or_insert(json!({
"remote": remote_url,
"branch": repo.head()?.shorthand().unwrap(),
"target": repo.head()?.target().unwrap().to_string(),
"authors": authors
}));
}
}
Expand Down Expand Up @@ -141,3 +147,46 @@ impl Workspace {
}
}
}

#[derive(Debug, Serialize)]
struct AuthorInfo {
name: String,
commits: i64,
}

fn get_commit_authors(repo: &Repository) -> Result<Vec<AuthorInfo>, Box<dyn Error>> {
let mut rev_walker = repo.revwalk()?;
rev_walker.push_head()?;

let mut authors: Vec<AuthorInfo> = rev_walker
.map(|r| {
let oid = r?;
repo.find_commit(oid)
})
.filter_map(|c| match c {
Ok(commit) => Some(commit),
Err(e) => {
println!("Error walking the revisions {}, skipping", e);
None
}
})
.fold(
HashMap::new(),
|mut result: HashMap<String, AuthorInfo>, cur| {
if let Some(name) = cur.author().name() {
let author_name = name.to_string();
let mut author = result.entry(author_name).or_insert(AuthorInfo {
name: name.to_string(),
commits: 0,
});
author.commits += 1;
}
result
},
)
.into_values()
.collect();

authors.sort_by(|a, b| b.commits.cmp(&a.commits));
Ok(authors)
}

0 comments on commit 5c4755d

Please sign in to comment.