Add get_file_contents API to Rust and display added file content in DiffViewer

This commit adds a new feature to display the contents of a file when it's in the "added" status in the commits view. A new `get_file_contents` function is implemented in the `Repository` struct, and a corresponding command is added to the main Tauri app. The frontend is updated to handle added files by invoking this command and rendering the contents in the DiffViewer component.

Changes include:
- Add `get_file_contents` function to `repository.rs` and command in main.rs
- Update the viewer to handle added files
This commit is contained in:
Scott Chacon 2023-03-22 14:31:20 +01:00
parent 501983f9bb
commit 258ed370f7
3 changed files with 47 additions and 2 deletions

View File

@ -423,6 +423,20 @@ async fn git_match_paths(
Ok(files)
}
#[tauri::command(async)]
async fn get_file_contents(
handle: tauri::AppHandle,
project_id: &str,
path: &str,
) -> Result<String, Error> {
let repo = repo_for_project(handle, project_id)?;
let file = repo
.get_file_contents(path)
.with_context(|| "Failed to get file contents")?;
Ok(file)
}
fn repo_for_project(
handle: tauri::AppHandle,
project_id: &str,
@ -614,7 +628,8 @@ fn main() {
git_branch,
git_switch_branch,
git_commit,
git_wd_diff
git_wd_diff,
get_file_contents
]);
let tauri_context = generate_context!();

View File

@ -3,7 +3,7 @@ use anyhow::{Context, Result};
use git2::{BranchType, Cred, DiffOptions, Signature};
use std::{
collections::HashMap,
env,
env, fs as std_fs,
path::Path,
sync::{Arc, Mutex},
};
@ -151,6 +151,20 @@ impl Repository {
Ok(branch.to_string())
}
// return file contents for path in the working directory
pub fn get_file_contents(&self, path: &str) -> Result<String> {
let repo = self.git_repository.lock().unwrap();
let workdir = repo
.workdir()
.with_context(|| "failed to get working directory")?;
let file_path = workdir.join(path);
// read the file contents
let content =
std_fs::read_to_string(file_path).with_context(|| "failed to read file contents")?;
Ok(content)
}
pub fn wd_diff(&self, max_lines: usize) -> Result<HashMap<String, String>> {
let repo = self.git_repository.lock().unwrap();
let head = repo.head()?;

View File

@ -66,6 +66,8 @@
const getDiff = (params: { projectId: string }) =>
invoke<Record<string, string>>('git_wd_diff', params);
const getBranch = (params: { projectId: string }) => invoke<string>('git_branch', params);
const getFile = (params: { projectId: string; path: string }) =>
invoke<string>('get_file_contents', params);
let gitBranch = <string | undefined>undefined;
let gitDiff = <Record<string, string> | undefined>undefined;
@ -74,11 +76,23 @@
let currentPath = '';
let currentDiff = '';
let addedContents = '';
function selectPath(path) {
currentDiff = '';
addedContents = '';
if (gitDiff[path]) {
currentPath = path;
currentDiff = gitDiff[path];
} else {
let file = $filesStatus.filter((file) => file.path === path)[0];
if (file && file.status === 'added') {
getFile({ projectId: $project?.id, path: path }).then((contents) => {
currentPath = path;
addedContents = contents;
});
}
}
}
@ -264,6 +278,8 @@
<div class="h-full max-h-screen flex-grow overflow-auto p-2 h-100">
{#if currentDiff}
<DiffViewer diff={currentDiff} path={currentPath} />
{:else if addedContents}
<pre class="bg-green-900">{addedContents}</pre>
{:else}
<div class="text-zinc-400 text-center text-lg p-20">Select a file to view changes.</div>
{/if}