Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/commands/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ impl Command for Status {
self.scan_workspace();
self.detect_workspace_changes();

let head_id = self.repo.refs.read_head();
dbg!(&head_id);

head_id.map(|id| {
self.repo.database.load(&id);
});

self.repo.index.write_updates()?;

// TODO: -clone
Expand Down
60 changes: 57 additions & 3 deletions src/database.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{id, objects};
use libflate::zlib::Encoder;
use libflate::zlib::{Encoder, Decoder};
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use std::{
fs::{File, OpenOptions},
Expand Down Expand Up @@ -27,9 +27,56 @@ impl Database {
Ok(id)
}

pub fn load(&self, id: &str) {
self.read_object(id);
}

fn read_object(&self, id: &str) {
let (_, object_path) = self.get_object_paths(id);

let bytes = std::fs::read(object_path).unwrap();

let mut decoder = Decoder::new(&bytes[..]).unwrap();

let mut buf = Vec::new();

decoder.read_to_end(&mut buf).unwrap();

let mut decoded_iter = buf.iter();

let mut object_type: Vec<u8> = vec![];
let mut size: Vec<u8> = vec![];

while let Some(byte) = decoded_iter.next() {
if byte != &b' ' {
object_type.push(*byte);
} else {
break;
}
}

while let Some(byte) = decoded_iter.next() {
if byte != &b'\0' {
size.push(*byte);
} else {
break;
}
}

let object_type = String::from_utf8_lossy(&object_type);
dbg!(&object_type);
dbg!(&size);

match &object_type[..] {
"commit" => {
let obj = objects::Commit::from(decoded_iter);
}
_ => panic!()
}
}

fn write_object<C: Read>(&self, id: &str, mut content: C) -> Result<(), io::Error> {
let dir_path = self.path.join(&id[0..2]);
let object_path = dir_path.join(&id[2..]);
let (dir_path, object_path) = self.get_object_paths(id);

if object_path.exists() {
return Ok(());
Expand Down Expand Up @@ -64,4 +111,11 @@ impl Database {

format!("tmp_obj_{}", s)
}

fn get_object_paths(&self, id: &str) -> (PathBuf, PathBuf) {
let dir_path = self.path.join(&id[0..2]);
let object_path = dir_path.join(&id[2..]);

(dir_path, object_path)
}
}
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ repository earlier: remove the file manually to continue.",
fn handle_ok(execution: rit::Execution) -> i32 {
match execution {
rit::Execution::Status(res) => {
for untracked in res.untracked {
println!("?? {}", untracked);
}

for modified in res.modified {
println!(" M {}", modified);
}

for untracked in res.untracked {
println!("?? {}", untracked);
}

for deleted in res.deleted {
println!(" D {}", deleted);
}
Expand Down
11 changes: 10 additions & 1 deletion src/objects/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
objects::{Author, Object, Storable},
};
use bytes::{BufMut, Bytes, BytesMut};
use std::fmt;
use std::{fmt, slice::Iter};

pub struct Commit<'a> {
parent: &'a Option<String>,
Expand Down Expand Up @@ -67,3 +67,12 @@ impl<'a> Object for Commit<'a> {
}

impl<'a> Storable for Commit<'a> {}

impl<'a> From<Iter<'_, u8>> for Commit<'a> {
fn from(iter: Iter<u8>) -> Self {
let parsed = String::from_utf8_lossy(iter.as_ref());
dbg!(&parsed);

unimplemented!()
}
}
15 changes: 6 additions & 9 deletions src/refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ use crate::{
id::Id,
lockfile::{LockError, Lockfile},
};
use std::{
fmt, fs,
io::{self, prelude::*},
path::PathBuf,
};
use std::{fmt, fs, io::{self, BufReader, prelude::*}, path::PathBuf};

#[derive(Debug)]
pub struct RefsError;
Expand Down Expand Up @@ -47,12 +43,13 @@ impl Refs {
Ok(())
}

// TODO:
pub fn read_head(&self) -> Option<String> {
let file = fs::File::open(self.head_path()).ok();
let mut head = String::new();
let file = fs::File::open(self.head_path()).unwrap();

let reader = BufReader::new(file);

file.and_then(|mut f| f.read_to_string(&mut head).ok())
.map(|_| head)
reader.lines().next().unwrap().ok()
}

fn head_path(&self) -> PathBuf {
Expand Down