Skip to content

Commit

Permalink
fix: accidental file deletion resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
zleyyij committed Aug 8, 2024
1 parent c4f9edd commit 8997dbe
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 32 deletions.
29 changes: 4 additions & 25 deletions backend/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use color_eyre::eyre::{bail, ContextCompat};
use color_eyre::{eyre::Context, Result};
use git2::{AnnotatedCommit, FetchOptions, Index, Oid, Repository, Signature};
use git2::{AnnotatedCommit, FetchOptions, Oid, Repository, Signature};
use serde::{Deserialize, Serialize};
use std::fs;
use std::io::{Read, Write};
Expand Down Expand Up @@ -153,7 +153,7 @@ impl Interface {
);
// Standard practice is to stage commits by adding them to an index.
relative_path.push(path);
let _guard = Self::git_add(&repo, relative_path)?;
Self::git_add(&repo, relative_path)?;
let commit_id = Self::git_commit(&repo, msg, None)?;
debug!("New commit made with ID: {:?}", commit_id);
Self::git_push(&repo, token)?;
Expand Down Expand Up @@ -194,18 +194,11 @@ impl Interface {
}

/// A code level re-implementation of `git add`.
///
/// This function returns an RAII guard attached to the index, the file path specified
/// will only be in the index as long as that guard is in scope. As soon as the guard leaves scope,
/// the file path is removed from the index, and changes made will not be tracked.
fn git_add<P: AsRef<Path>>(repo: &Repository, path: P) -> Result<IndexPathGuard> {
fn git_add<P: AsRef<Path>>(repo: &Repository, path: P) -> Result<()> {
let mut index = repo.index()?;
index.add_path(path.as_ref())?;
index.write()?;
Ok(IndexPathGuard {
index,
path: path.as_ref().into(),
})
Ok(())
}

/// A code level re-implementation of `git commit`.
Expand Down Expand Up @@ -395,17 +388,3 @@ impl Interface {
.map_err(|_| git2::Error::from_str("Couldn't find commit"))
}
}

/// An RAII guard associated with a path currently added to the index.
/// When dropped, the file is removed from the index.
struct IndexPathGuard {
index: Index,
path: PathBuf,
}

impl Drop for IndexPathGuard {
fn drop(&mut self) {
self.index.remove_path(&self.path).unwrap();
self.index.write().unwrap();
}
}
1 change: 0 additions & 1 deletion backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ async fn main() -> Result<()> {
.extensions()
.get::<MatchedPath>()
.map(MatchedPath::as_str);
println!("Path: {:?}", matched_path);
info_span!(
"http_request",
method = ?request.method(),
Expand Down
13 changes: 7 additions & 6 deletions frontend/src/lib/components/FileNavigation.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!-- https://svelte.dev/repl/347b37e18b5d4a65bbacfd097536db02?version=4.2.17 -->
<script lang="ts">
import { createEventDispatcher, tick } from 'svelte';
import { createEventDispatcher, onMount, tick } from 'svelte';
import { currentFile } from '$lib/main';
import { cache } from '$lib/cache';
import { get } from 'svelte/store';

Check failure on line 6 in frontend/src/lib/components/FileNavigation.svelte

View workflow job for this annotation

GitHub Actions / frontend-build

'get' is defined but never used
Expand All @@ -20,10 +20,6 @@
let showNewFileInput = false;
let newFileInput: HTMLInputElement;
// Sort nodes alphabetically
// https://stackoverflow.com/questions/8900732/sort-objects-in-an-array-alphabetically-on-one-property-of-the-array
children = children.sort((a, b) => a.name.localeCompare(b.name))
const dispatch = createEventDispatcher();
function fileClickHandler() {
Expand Down Expand Up @@ -55,6 +51,12 @@
newFileInput.setSelectionRange(0, 0);
newFileInput.focus();
}
onMount(async () => {
// Sort nodes alphabetically
// https://stackoverflow.com/questions/8900732/sort-objects-in-an-array-alphabetically-on-one-property-of-the-array
children = children.sort((a, b) => a.name.localeCompare(b.name));
});
</script>

<span class={'container' + (selected ? ' selected' : '')}>
Expand Down Expand Up @@ -122,7 +124,6 @@ last_modified_date: ${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}
---\n\n`
);
currentFile.set(path + newFileInput.value);
console.log(cache.get(get(currentFile)));
}
if (e.key === 'Escape') {
showNewFileInput = false;
Expand Down

0 comments on commit 8997dbe

Please sign in to comment.