Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/cmd/rebase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,16 +367,16 @@ fn interactive_pushback(
return Ok(());
}

let filename = ".stgit-rebase-interactive.txt";
let filename = stack.repo.git_data_file(".stgit-rebase-interactive.txt");
std::fs::write(
filename,
&filename,
make_instructions_template(&stack, previously_applied),
)?;

let buf = patchedit::call_editor(filename, config)?;
let buf = patchedit::call_editor(&filename, config)?;
let buf = buf
.to_str()
.map_err(|_| anyhow!("`{filename}` is not valid UTF-8"))?;
.map_err(|_| anyhow!("`{}` is not valid UTF-8", filename.display()))?;
let mut instructions = parse_instructions(buf)?;

validate_instructions(&stack, &instructions)?;
Expand Down
16 changes: 16 additions & 0 deletions src/ext/repository.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-only

use std::borrow::Cow;
use std::path::PathBuf;

use anyhow::{anyhow, Result};
use bstr::BStr;
Expand Down Expand Up @@ -78,6 +79,13 @@ pub(crate) trait RepositoryExtended {

/// [`gix::Repository::rev_parse_single()`] with StGit-specific error mapping.
fn rev_parse_single_ex(&self, spec: &str) -> Result<gix::Id<'_>>;

/// Get path to a file in the git directory.
///
/// By default, this returns a path within the `.git` directory to avoid cluttering
/// the working directory. If the `STG_EDIT_IN_CWD` environment variable is set,
/// returns the path relative to the current working directory instead.
fn git_data_file(&self, path: &str) -> PathBuf;
}

/// Options for creating a git commit object.
Expand Down Expand Up @@ -330,4 +338,12 @@ impl RepositoryExtended for gix::Repository {
}
})
}

fn git_data_file(&self, path: &str) -> PathBuf {
// If STG_EDIT_IN_CWD is set, return path as is.
match std::env::var("STG_EDIT_IN_CWD") {
Comment on lines +343 to +344
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there known cases where someone might need to fallback to the old behavior?

If not, I'd prefer we not have this environment variable at all; keep it simple.

Another issue with this env var is that we're only checking its existence, not its value. So if someone set STG_EDIT_IN_CWD=0, for example, it would fallback to the old behavior the same as if they'd set STG_EDIT_IN_CWD=1.

Ok(_) => PathBuf::from(path),
Err(_) => self.path().join(path),
}
}
}
10 changes: 6 additions & 4 deletions src/patch/edit/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,21 @@ pub(super) fn edit_interactive(
patch_desc: &EditablePatchDescription,
config: &gix::config::Snapshot,
) -> Result<EditedPatchDescription> {
let filename = if patch_desc.diff.is_some() {
use crate::ext::RepositoryExtended;

let filename = config.repo.git_data_file(if patch_desc.diff.is_some() {
EDIT_FILE_NAME_DIFF
} else {
EDIT_FILE_NAME
};
});

{
let file = File::create(filename)?;
let file = File::create(&filename)?;
let mut stream = BufWriter::new(file);
patch_desc.write(&mut stream)?;
}

let buf = call_editor(filename, config)?;
let buf = call_editor(&filename, config)?;
let edited_desc = EditedPatchDescription::try_from(buf.as_slice())?;
Ok(edited_desc)
}
Expand Down
Loading