Skip to content

Commit

Permalink
Merge pull request #11 from lsk569937453/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
lsk569937453 authored Oct 17, 2024
2 parents 623881e + cbdd945 commit dd6dcf4
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 97 deletions.
182 changes: 91 additions & 91 deletions src-tauri/src/common_tools/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,15 +451,15 @@ fn init_git_tasks(connection: &Connection, repo_path: String) -> Result<(), anyh

revwalk.push(head_ref)?;
let commit_task_count = revwalk.collect::<Result<Vec<_>, _>>()?.len();
let mut tag_set = HashSet::new();
let mut tag_set = vec![];
let refs = repo.references()?;
for r in refs {
let r = r?;
if r.shorthand().is_some() {
if let Some(target) = r.target() {
// Filter tags
if r.is_tag() {
tag_set.insert(target);
tag_set.push(target);
}
}
}
Expand Down Expand Up @@ -673,10 +673,7 @@ fn analyze_base_info(
}

fn get_commit_count(repo_path: String) -> Result<i32, anyhow::Error> {
// Open the repository at the current path
let repo = Repository::open(repo_path)?;

// Create a revwalker to traverse commits starting from HEAD
let mut revwalk = repo.revwalk()?;
revwalk.push_head()?;
revwalk.set_sorting(git2::Sort::NONE)?;
Expand All @@ -698,7 +695,6 @@ fn analyze_files(repo: &Repository, total_lines: i32) -> Result<FileStatisticInf
.ok()
.and_then(|o| o.as_blob().cloned())
{
// let blob_id = blob.id().to_string();
let size = blob.size();
let fullpath = entry.name().unwrap_or_default();

Expand Down Expand Up @@ -756,112 +752,116 @@ fn analyze_files(repo: &Repository, total_lines: i32) -> Result<FileStatisticInf
};
Ok(file_statisctic_info)
}

fn analyze_tag(state: AppState, repo: &Repository) -> Result<TagStatisticInfo, anyhow::Error> {
#[derive(Clone)]
pub struct TagItem {
pub tag_name: String,
pub tag_oid: Oid,
pub date_time: DateTime<Utc>,
pub pre_oid: Option<Oid>,
}
let refs = repo.references()?;
let mut tag_refs: Vec<(Oid, String)> = vec![];
let mut tag_refs = vec![];
for r in refs {
let r = r?;
if let Some(name) = r.shorthand() {
if let Some(target) = r.target() {
// Filter tags
if r.is_tag() {
tag_refs.push((target, name.to_string()));
let tag_time = if let Ok(tag) = repo
.find_tag(target)
.map_err(|e| anyhow!("Can not find tag {}", e))
{
tag.tagger().ok_or(anyhow!(""))?.when()
} else {
let tag = repo
.find_commit(target)
.map_err(|e| anyhow!("Can not find commit {}", e))?;
tag.time()
};

let date_time = Utc
.timestamp_opt(tag_time.seconds(), 0)
.single()
.ok_or(anyhow!(""))?;
// let date_time_str = date_time.format("%Y-%m-%d %H:%M:%S.%3f").to_string();
tag_refs.push(TagItem {
tag_name: name.to_string(),
tag_oid: target,
date_time,
pre_oid: None,
});
}
}
}
}
let mut map = HashMap::new();
tag_refs.sort_by(|a, b| a.date_time.cmp(&b.date_time));

let mut map = vec![];
let mut prev: Option<Oid> = None;

for (hash, tag) in tag_refs {
let (time, prev_oid) = if repo
.find_commit(hash)
.map_err(|e| anyhow!("Can not find commit {}", e))
.is_ok()
{
let commit = repo
.find_commit(hash)
.map_err(|e| anyhow!("Can not find commit {}", e))?;
for item in tag_refs.iter_mut() {
info!("item is {},date:{}", item.tag_name, item.date_time);
let tag = &item.tag_name;

(
Utc.timestamp_opt(commit.time().seconds(), 0)
.single()
.ok_or(anyhow!(""))?,
Some(hash),
)
} else if let Ok(tag_obj) = repo.find_tag(hash) {
(
Utc.timestamp_opt(
tag_obj
.tagger()
.ok_or(anyhow!("Invalid tag time"))?
.when()
.seconds(),
0,
)
.single()
.ok_or(anyhow!("Invalid tag time"))?,
prev,
)
} else {
Err(anyhow!(""))?
};
let converted: DateTime<Local> = DateTime::from(time);
let year_and_month = converted.format("%Y-%m-%d").to_string();

map.insert(
hash,
(
TagStatisticMainInfoItem::new(tag.clone(), year_and_month, 0, vec![]),
prev,
),
);
prev = prev_oid;
let year_and_month = item
.date_time
.clone()
.format("%Y-%m-%d %H:%M:%S.%3f")
.to_string();

item.pre_oid = prev;
let cloned_item = item.clone();
map.push((
TagStatisticMainInfoItem::new(tag.clone(), year_and_month, 0, vec![]),
cloned_item,
));
prev = Some(item.tag_oid);
}
let tag_count = map.len() as i32;
info!("real tag count is {}", tag_count);
let repo_path = repo.path();
let total_commit = map
.par_iter_mut()
.map(
|(tag_oid, (tag_info, prevs))| -> Result<i32, anyhow::Error> {
let repo = Repository::open(repo_path)?;
let connections = state.pool.get()?;
connections.execute(
"UPDATE git_init_status SET current_tasks = current_tasks + 1",
params![],
)?;
let cancel_flag = state.cancel_flag.clone();
if cancel_flag.load(std::sync::atomic::Ordering::SeqCst) {
cancel_flag.store(false, std::sync::atomic::Ordering::SeqCst);
return Err(anyhow!("The task has been cancelled."));
.map(|(tag_info, tag_item)| -> Result<i32, anyhow::Error> {
let tag_oid = tag_item.tag_oid;
let prevs = tag_item.pre_oid;
let repo = Repository::open(repo_path)?;
let connections = state.pool.get()?;
connections.execute(
"UPDATE git_init_status SET current_tasks = current_tasks + 1",
params![],
)?;
let cancel_flag = state.cancel_flag.clone();
if cancel_flag.load(std::sync::atomic::Ordering::SeqCst) {
cancel_flag.store(false, std::sync::atomic::Ordering::SeqCst);
return Err(anyhow!("The task has been cancelled."));
}
let mut commit_count = 0;

{
let mut author_count: HashMap<String, usize> = HashMap::new();

let mut revwalk = repo.revwalk()?;
revwalk.push(tag_oid)?;

if let Some(prev_oid) = prevs {
revwalk.hide(prev_oid)?;
}
let mut commit_count = 0;

if repo.find_commit(*tag_oid).is_ok() {
let mut author_count: HashMap<String, usize> = HashMap::new();

let mut revwalk = repo.revwalk()?;
revwalk.push(*tag_oid)?;

for oid in revwalk {
let commit_oid = oid?;
if let Some(prev_oid) = prevs {
if commit_oid == *prev_oid {
break; // Stop if we reach the previous tag
}
}
let commit = repo.find_commit(commit_oid)?;
let author_name = commit.author().name().unwrap_or("Unknown").to_string();
commit_count += 1;
*author_count.entry(author_name).or_insert(0) += 1;
}
tag_info.commit_count = commit_count;
tag_info.authors = author_count.into_iter().collect();

for oid in revwalk {
let commit_oid = oid?;

let commit = repo.find_commit(commit_oid)?;
let author_name = commit.author().name().unwrap_or("Unknown").to_string();
commit_count += 1;
*author_count.entry(author_name).or_insert(0) += 1;
}
Ok(commit_count)
},
)
tag_info.commit_count = commit_count;
tag_info.authors = author_count.into_iter().collect();
}
Ok(commit_count)
})
.filter_map(Result::ok) // Filter out errors
.reduce(|| 0, |acc, count| acc + count);

Expand All @@ -878,7 +878,7 @@ fn analyze_tag(state: AppState, repo: &Repository) -> Result<TagStatisticInfo, a
};
let mut tag_statistic_main_info_list = map
.into_iter()
.map(|(_, (item, _))| item)
.map(|(item, _)| item)
.collect::<Vec<TagStatisticMainInfoItem>>();
tag_statistic_main_info_list.sort_by(|a, b| b.date.cmp(&a.date));
for item in tag_statistic_main_info_list.iter_mut() {
Expand Down
12 changes: 6 additions & 6 deletions src/dashboard/page/lineInfoPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,17 @@ export function LineInfoPage() {

xAxis: {
type: 'category',
data: [] // Empty x-axis data
data: []
},
yAxis: {
// name: '同步sql条数',
type: 'value',
},
series: [] // No series data
series: []
};
}
return {
title: {
text: 'Lines Of Code'
text: t("linePage.linesOfCodeText"),
},
tooltip: {
trigger: 'axis',
Expand All @@ -95,7 +94,8 @@ export function LineInfoPage() {
},
xAxis: [
{
type: 'time', boundaryGap: false,
type: 'time',
// boundaryGap: false,

}
],
Expand All @@ -117,7 +117,7 @@ export function LineInfoPage() {
<Card className="pt-4">
<CardContent className="flex flex-col gap-5 text-right">
<div className="flex flex-row gap-10">
<p className="basis-2/12 text-lg font-bold">Total Lines:</p>
<p className="basis-2/12 text-lg font-bold">{t("linePage.totalLinesText")}</p>
<p className="text-lg">{totalLines}</p>
</div>

Expand Down
4 changes: 4 additions & 0 deletions src/locales/en/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
"commits": "Commit",
"authors": "Authors"
},
"linePage": {
"totalLinesText": "Total Lines",
"linesOfCodeText": "Lines Of Code"
},
"menu": {
"gitPathInvalidMessageBody": "Please select the valid git repository."
},
Expand Down
4 changes: 4 additions & 0 deletions src/locales/zh/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@
"commits": "提交",
"authors": "作者列表"
},
"linePage": {
"totalLinesText": "总代码行数",
"linesOfCodeText": "代码行数"
},
"formatToolsPage": {
"buttonText": "格式化",
"formatInputPlaceHolder": "请输入需要格式化的文本"
Expand Down

0 comments on commit dd6dcf4

Please sign in to comment.