Merge branch 'feature/bare-repositories'

Closes #3
This commit is contained in:
Valentin Brandl 2019-04-19 12:19:11 +02:00
commit 48ce41e5fd
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D

View File

@ -12,8 +12,9 @@ use actix_web::{
use badge::{Badge, BadgeOptions}; use badge::{Badge, BadgeOptions};
use bytes::Bytes; use bytes::Bytes;
use futures::{unsync::mpsc, Stream}; use futures::{unsync::mpsc, Stream};
use git2::{Repository, ResetType}; use git2::Repository;
use std::{ use std::{
fs::create_dir_all,
path::{Path, PathBuf}, path::{Path, PathBuf},
process::Command, process::Command,
sync::Arc, sync::Arc,
@ -49,7 +50,6 @@ enum Error {
Git(git2::Error), Git(git2::Error),
Io(std::io::Error), Io(std::io::Error),
Badge(String), Badge(String),
Internal,
} }
impl std::fmt::Display for Error { impl std::fmt::Display for Error {
@ -58,7 +58,6 @@ impl std::fmt::Display for Error {
Error::Git(e) => write!(fmt, "Git({})", e), Error::Git(e) => write!(fmt, "Git({})", e),
Error::Io(e) => write!(fmt, "Io({})", e), Error::Io(e) => write!(fmt, "Io({})", e),
Error::Badge(s) => write!(fmt, "Badge({})", s), Error::Badge(s) => write!(fmt, "Badge({})", s),
Error::Internal => write!(fmt, "Internal"),
} }
} }
} }
@ -90,12 +89,10 @@ impl From<std::io::Error> for Error {
} }
fn pull(path: impl AsRef<Path>) -> Result<(), Error> { fn pull(path: impl AsRef<Path>) -> Result<(), Error> {
let repo = Repository::open(path)?; let repo = Repository::open_bare(path)?;
let mut origin = repo.find_remote("origin")?; let mut origin = repo.find_remote("origin")?;
origin.fetch(&["refs/heads/*:refs/heads/*"], None, None)?; origin.fetch(&["refs/heads/*:refs/heads/*"], None, None)?;
let head = repo.head()?.target().ok_or(Error::Internal)?; Ok(())
let obj = repo.find_object(head, None)?;
Ok(repo.reset(&obj, ResetType::Hard, None)?)
} }
fn hoc(repo: &str) -> Result<u64, Error> { fn hoc(repo: &str) -> Result<u64, Error> {
@ -139,10 +136,12 @@ fn calculate_hoc(
let path = format!("{}/{}", *state, service_path); let path = format!("{}/{}", *state, service_path);
let file = Path::new(&path); let file = Path::new(&path);
if !file.exists() { if !file.exists() {
Repository::clone(&format!("https://{}", service_path), file)?; create_dir_all(file)?;
} else { let repo = Repository::init_bare(file)?;
pull(&path)?; repo.remote_add_fetch("origin", "refs/heads/*:refs/heads/*")?;
repo.remote_set_url("origin", &format!("https://{}", service_path))?;
} }
pull(&path)?;
let hoc = hoc(&path)?; let hoc = hoc(&path)?;
let badge_opt = BadgeOptions { let badge_opt = BadgeOptions {
subject: "Hits-of-Code".to_string(), subject: "Hits-of-Code".to_string(),