Skip to content

Commit

Permalink
fix: add files to index and correct dirty logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Clemens committed Jun 27, 2018
1 parent 6bd1415 commit 2a98b20
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions webserver/src/database/models/pastes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use chrono::{NaiveDateTime, Utc};
use diesel;
use diesel::prelude::*;

use git2::{Signature, Repository, DiffOptions};
use git2::{Signature, Repository, DiffOptions, IndexAddOption, Status};

use rocket::http::Status as HttpStatus;

Expand Down Expand Up @@ -127,10 +127,11 @@ impl Paste {

pub fn repo_dirty(&self) -> Result<bool> {
let repo = Repository::open(self.files_directory())?;
let mut options = DiffOptions::new();
options.ignore_submodules(true);
let diff = repo.diff_index_to_workdir(None, Some(&mut options))?;
Ok(diff.stats()?.files_changed() != 0)
let dirty = repo
.statuses(None)?
.iter()
.any(|x| x.status() != Status::CURRENT || x.status() != Status::IGNORED);
Ok(dirty)
}

pub fn commit_if_dirty(&self, username: &str, email: &str, message: &str) -> Result<()> {
Expand All @@ -142,10 +143,14 @@ impl Paste {
}

pub fn commit(&self, username: &str, email: &str, message: &str) -> Result<()> {
let repo = Repository::open(self.files_directory())?;
let files_dir = self.files_directory();

let repo = Repository::open(&files_dir)?;
let mut index = repo.index()?;

index.add_all(vec!["."], IndexAddOption::DEFAULT, None)?;
index.write()?;

let tree_id = index.write_tree()?;
let tree = repo.find_tree(tree_id)?;

Expand Down

0 comments on commit 2a98b20

Please sign in to comment.