Merge pull request #481 from gitbutlerapp/sc-vbranch-commits

Send mtime with hunk
This commit is contained in:
Scott Chacon 2023-06-21 10:55:21 +02:00 committed by GitHub
commit c109a91dc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,6 +5,7 @@ pub mod target;
use std::{collections::HashMap, time, vec}; use std::{collections::HashMap, time, vec};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use filetime::FileTime;
use serde::Serialize; use serde::Serialize;
pub use branch::Branch; pub use branch::Branch;
@ -256,6 +257,7 @@ pub fn get_status_by_branch(
let mut last_path = String::new(); let mut last_path = String::new();
let mut last_hunk_id = String::new(); let mut last_hunk_id = String::new();
let mut hunk_numbers = String::new(); let mut hunk_numbers = String::new();
let mut mtimes = HashMap::new();
diff.print(git2::DiffFormat::Patch, |delta, hunk, line| { diff.print(git2::DiffFormat::Patch, |delta, hunk, line| {
if let Some(hunk) = hunk { if let Some(hunk) = hunk {
@ -269,13 +271,33 @@ pub fn get_status_by_branch(
.unwrap() .unwrap()
.to_string(); .to_string();
let mtime = match mtimes.get(&new_path) {
Some(mtime) => *mtime,
None => {
let file_path = project_repository
.git_repository
.workdir()
.unwrap()
.join(new_path.clone());
let metadata = file_path.metadata().unwrap();
let mtime = FileTime::from_last_modification_time(&metadata);
println!("mtime: {:?}", mtime);
// convert seconds and nanoseconds to milliseconds
let mtime = (mtime.seconds() as u128 * 1000) as u128;
println!("mtime: {:?}", mtime);
mtimes.insert(new_path.clone(), mtime);
mtime
}
};
let hunk_id = format!("{}:{}", new_path, hunk_numbers); let hunk_id = format!("{}:{}", new_path, hunk_numbers);
if hunk_id != last_hunk_id { if hunk_id != last_hunk_id {
let hunk = VirtualBranchHunk { let hunk = VirtualBranchHunk {
id: last_hunk_id.clone(), id: last_hunk_id.clone(),
name: "".to_string(), name: "".to_string(),
diff: results.clone(), diff: results.clone(),
modified_at: 0, modified_at: mtime,
file_path: last_path.clone(), file_path: last_path.clone(),
}; };
hunks.push(hunk); hunks.push(hunk);