Skip to content

Commit

Permalink
Add support for reading and writing using Git (#32)
Browse files Browse the repository at this point in the history
Fixes #34
Fixes #35
Fixes #36
  • Loading branch information
AustinWise authored Feb 14, 2022
1 parent cabe7e2 commit 9e2259f
Show file tree
Hide file tree
Showing 17 changed files with 482 additions and 114 deletions.
5 changes: 0 additions & 5 deletions .cargo/config
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,3 @@
#rustflags = ["-C", "link-arg=-fuse-ld=lld"]
#[target.aarch64-unknown-linux-gnu]
#rustflags = ["-C", "link-arg=-fuse-ld=lld"]

# Statically link the MSVC C Runtime on Windows, so the EXEs can run without
# installing the C Runtime DLL.
[target.'cfg(all(target_env = "msvc"))']
rustflags = ["-C", "target-feature=+crt-static"]
11 changes: 11 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ jobs:
use-cross: false

steps:

# Build all crates with a statically linked MSVCRT. Specifically crates
# that use `cc` in their build scripts (like libgit2-sys), will detect this
# and compile objects appropriatly. If there is a way to put this into a
# Cargo config file some where, let me know. .cargo/config did not work.
- name: Statically link MSVCRT
shell: bash
if: matrix.os == 'windows'
run: |
echo "RUSTFLAGS=-C target-feature=+crt-static" >> $GITHUB_ENV
- name: Checkout repository
uses: actions/checkout@v2

Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
WIP
===

* Support reading files from a Git repo [#34](https://github.com/AustinWise/smeagol/issues/34)
* Support writing files to a Git repo [#35](https://github.com/AustinWise/smeagol/issues/35)
* Rename the settings in `smeagol.toml` to use kebob-case, rather than
snake_case. Specifically `h1_title` was renamed to `h1-title` and `index_page`
was renamed to `index-page`. This matches `Cargo.toml`'s use of kebob-case.
Expand Down
132 changes: 132 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ ring = "0.16.20"

[dependencies]
askama = "0.11"
bitflags = "1.3.2"
clap = { version = "3.0.0", features = ["derive"] }
git2 = { version = "0.13", default-features = false }
log = "0.4"
once_cell = "1.9.0"
pretty_env_logger = "0.4"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ There are a few command line options:
access it.
* `--port` - takes an argument that specifies which port to listen on. `8000` by
default.
* `--fs` - instructs Smeagol to load and save using the file system. By default
Smeagol uses Git to load files committed to a Git repository and saves them by
committing them to the current branch.

Additionally, the following settings can be put in a `smeagol.toml` file in the
root directory of the wiki:
Expand All @@ -63,6 +66,7 @@ root directory of the wiki:

* `index-page` on Gollum defaults to `Home`. Smeagol defaults to `README` to be
compatible with online code hosting systems such as GitHub and Azure Devops.
* The default port is `8000` rather than `4567`.

## Why Rust, please tell me more about why you love Rust

Expand Down
6 changes: 4 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ impl EmbeddedFile {
}

fn rebuild_on_file_change(context: &mut ContextWriter, path: &Path) -> Result<(), io::Error> {
println!("cargo:rerun-if-changed={}", path.canonicalize()?.to_str().unwrap());
println!(
"cargo:rerun-if-changed={}",
path.canonicalize()?.to_str().unwrap()
);
let mut file = File::open(path)?;
io::copy(&mut file, context)?;
Ok(())
Expand Down Expand Up @@ -146,6 +149,5 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("cargo:rustc-link-arg-bins=/MANIFESTINPUT:windows_manifest.xml");
}


Ok(())
}
13 changes: 10 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ use thiserror::Error;

#[derive(Error, Debug)]
pub enum MyError {
#[error("Bare Git repos are not yet supported.")]
BareGitRepo,
#[error("This is not valid Wiki folder: {path}")]
GitRepoDoesNotExist {
path: std::path::PathBuf
GitRepoDoesNotExist { path: std::path::PathBuf },
#[error("Failed to open Git repo: {err}")]
GitRepoDoesFailedToOpen { err: git2::Error },
#[error("Error when performing git operation: {source}")]
GitError {
#[from]
source: git2::Error,
},
#[error("Path is not valid.")]
InvalidPath,
Expand All @@ -19,7 +26,7 @@ pub enum MyError {
source: askama::Error,
},
#[error("Failed to read config file.")]
ConfigReadError { source: std::io::Error },
ConfigReadError { source: Box<MyError> },
#[error("Failed to parse config file.")]
ConfigParseError {
#[from]
Expand Down
13 changes: 9 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ mod settings;
mod templates;
mod wiki;

use clap::StructOpt;
use error::MyError;
use repository::create_file_system_repository;
use repository::create_repository;
use settings::parse_settings_from_args;
use wiki::Wiki;

Expand All @@ -21,9 +22,13 @@ use rocket::request::{FromRequest, Outcome, Request};
static WIKI: OnceCell<Wiki> = OnceCell::new();

fn create_wiki() -> Result<Wiki, MyError> {
let settings = parse_settings_from_args()?;
let repo = create_file_system_repository(settings.git_repo().clone())?;
Wiki::new(settings, Box::new(repo))
let args = settings::Args::parse();
let git_repo = args
.git_repo()
.unwrap_or_else(|| std::env::current_dir().unwrap());
let repo = create_repository(args.use_fs(), git_repo)?;
let settings = parse_settings_from_args(args, &repo)?;
Wiki::new(settings, repo)
}

#[rocket::async_trait]
Expand Down
7 changes: 1 addition & 6 deletions src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,7 @@ impl MarkupLanguage {
}
}

fn raw<S: Into<String>>(
&self,
file_stem: &str,
file_contents: S,
settings: &Settings,
) -> Page {
fn raw<S: Into<String>>(&self, file_stem: &str, file_contents: S, settings: &Settings) -> Page {
let file_contents: String = file_contents.into();
match self {
MarkupLanguage::Markdown => {
Expand Down
Loading

0 comments on commit 9e2259f

Please sign in to comment.