Skip to content

Commit 4d4d078

Browse files
committed
fix: support workspaces without git
1 parent c774d79 commit 4d4d078

4 files changed

Lines changed: 21 additions & 17 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ jobs:
5050
- name: Set up Steiger
5151
run: cargo install --path .
5252
- name: Run tests
53-
run: steiger --dir tests/${{ matrix.test }} build --profile ${{ matrix.profile }}
53+
run: steiger --dir tests/${{ matrix.test }} build

src/config.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use serde_yml::{Mapping, Value};
1010

1111
use crate::git;
1212

13-
const DEFAULT_TAG_FORMAT: &str = "${gitTag:$gitShortCommit}${gitDirty:}";
13+
const DEFAULT_TAG_FORMAT: &str = "${gitTag:${gitShortCommit:unknown}}${gitDirty:}";
1414

1515
#[derive(Debug, Deserialize, Clone)]
1616
#[serde(rename_all = "camelCase")]
@@ -128,11 +128,15 @@ fn template(vars: &HashMap<String, String>, config: Value) -> Result<Value, subs
128128
fn extract_git_vars(state: git::State) -> HashMap<String, String> {
129129
let mut vars = HashMap::new();
130130

131-
vars.insert("gitShortCommit".to_string(), state.commit[0..6].to_string());
132-
vars.insert("gitCommit".to_string(), state.commit);
131+
if let Some(commit) = state.commit {
132+
vars.insert("gitShortCommit".to_string(), commit[0..6].to_string());
133+
vars.insert("gitCommit".to_string(), commit);
134+
}
135+
133136
if let Some(tag) = state.tag {
134137
vars.insert("gitTag".to_string(), tag);
135138
}
139+
136140
if state.dirty {
137141
vars.insert("gitDirty".to_string(), "-dirty".to_string());
138142
}

src/git.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,16 @@ fn is_dirty(repo: &Repository) -> Result<bool, gix::status::is_dirty::Error> {
5050
pub struct State {
5151
pub dirty: bool,
5252
pub tag: Option<String>,
53-
pub commit: String,
53+
pub commit: Option<String>,
5454
}
5555

5656
pub async fn state() -> Result<State, GitError> {
57-
let repo = gix::open(".")?;
57+
let repo = match gix::open(".") {
58+
Ok(repo) => repo,
59+
Err(gix::open::Error::NotARepository { .. }) => return Ok(State::default()),
60+
Err(e) => return Err(GitError::Open(e)),
61+
};
62+
5863
let mut head = repo.head()?;
5964
let mut state = State {
6065
dirty: is_dirty(&repo)?,
@@ -68,7 +73,7 @@ pub async fn state() -> Result<State, GitError> {
6873
}
6974

7075
if let Ok(commit) = head.peel_to_commit_in_place() {
71-
state.commit = commit.id.to_hex().to_string();
76+
state.commit = Some(commit.id.to_hex().to_string());
7277
}
7378

7479
Ok(state)

src/main.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ enum AppError {
111111
#[error(transparent)]
112112
#[diagnostic(transparent)]
113113
Deploy(#[from] cmd::deploy::Error),
114-
#[error("failed to get current dir")]
115-
CurrentDir(std::io::Error),
116114
#[error("failed to set current dir")]
117115
SetCurrentDir(std::io::Error),
118116
#[error("failed to create temp file")]
@@ -128,15 +126,12 @@ impl From<cmd::build::Error> for AppError {
128126
}
129127

130128
async fn run(opts: Opts) -> Result<(), AppError> {
131-
let dir = opts
132-
.dir
133-
.map(Ok)
134-
.unwrap_or_else(env::current_dir)
135-
.map_err(AppError::CurrentDir)?;
136-
let config_path = opts.config.unwrap_or_else(|| dir.join("steiger.yml"));
137-
let detected_platform = detect_platform().await;
129+
if let Some(dir) = opts.dir {
130+
env::set_current_dir(&dir).map_err(AppError::SetCurrentDir)?;
131+
}
138132

139-
env::set_current_dir(&dir).map_err(AppError::SetCurrentDir)?;
133+
let config_path = opts.config.unwrap_or_else(|| "steiger.yml".into());
134+
let detected_platform = detect_platform().await;
140135

141136
match opts.cmd {
142137
Cmd::Build {

0 commit comments

Comments
 (0)