Skip to content

Commit

Permalink
fix: some import source fixes (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspar030 authored Dec 10, 2024
2 parents 9b27440 + 8f03147 commit a3df060
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased] - ReleaseDate

### Fixed

- always update git-cache of a branch or tag `imports` source
- track `imports:` sources, trigger new download if source has changed

## [0.1.27] - 2024-12-06

### Fixed
Expand Down
23 changes: 16 additions & 7 deletions src/data/import/download.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fs::{remove_dir_all, File};
use std::fs::remove_dir_all;

use anyhow::{Context as _, Error};
use camino::{Utf8Path, Utf8PathBuf};
Expand Down Expand Up @@ -32,6 +32,7 @@ fn git_clone_commit(url: &str, target_path: &Utf8Path, commit: &str) -> Result<(

fn git_clone_branch(url: &str, target_path: &Utf8Path, branch: &str) -> Result<(), Error> {
git_cloner(url, target_path)
.update(true)
.extra_clone_args(Some(vec!["--branch".into(), branch.into()]))
.do_clone()
}
Expand All @@ -43,8 +44,15 @@ impl Import for Download {

let mut skip_download = false;
if tagfile.exists() {
// TODO: check if tagfile was created with same info
skip_download = true;
let tagfile_contents = std::fs::read_to_string(tagfile.as_path())?;
let tagfile_source =
bincode::deserialize_from::<_, Source>(tagfile_contents.as_bytes());

if let Ok(tagfile_source) = tagfile_source {
if tagfile_source == self.source {
skip_download = true;
}
}
}
if !skip_download {
if target_path.exists() {
Expand All @@ -60,7 +68,7 @@ impl Import for Download {
format!("cloning git url: \"{url}\" commit: \"{commit}\"")
})?;

File::create(tagfile)?;
self.create_tagfile(tagfile)?;
}
Source::Git(Git::Branch {
url,
Expand All @@ -76,7 +84,7 @@ impl Import for Download {
format!("cloning git url: \"{url}\" branch/tag: \"{branch_or_tag}\"")
})?;

File::create(tagfile)?;
self.create_tagfile(tagfile)?;
}
Source::Git(Git::Default { url }) => {
println!("IMPORT Git {url} -> {target_path}");
Expand All @@ -85,7 +93,7 @@ impl Import for Download {
.do_clone()
.with_context(|| format!("cloning git url: \"{url}\""))?;

File::create(tagfile)?;
self.create_tagfile(tagfile)?;
}
Source::Laze(name) => {
let mut at_least_one = false;
Expand All @@ -107,7 +115,8 @@ impl Import for Download {
.with_context(|| format!("creating {filename}"))?;
}
if at_least_one {
File::create(&tagfile).with_context(|| format!("creating {tagfile}"))?;
self.create_tagfile(&tagfile)
.with_context(|| format!("creating {tagfile}"))?;
} else {
return Err(anyhow!("could not import from laze defaults: {name}"));
}
Expand Down
9 changes: 8 additions & 1 deletion src/download.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module deals with "download:" directives
use std::borrow::Cow;
use std::{borrow::Cow, path::Path};

use anyhow::Result;
use im::HashMap;
Expand Down Expand Up @@ -72,6 +72,13 @@ impl Download {
}
}

pub fn create_tagfile<P: AsRef<Path>>(&self, path: P) -> Result<()> {
let path = path.as_ref();
let contents = bincode::serialize(&self.source)?;
std::fs::write(path, contents)?;
Ok(())
}

fn render(
&self,
module: &Module,
Expand Down

0 comments on commit a3df060

Please sign in to comment.