Skip to content

Commit

Permalink
config: expand tilde in ssh key filepaths
Browse files Browse the repository at this point in the history
Add home directory expansion for SSH key filepaths. This allows the
`signing.key` configuration value to work more universally across both
Linux and macOS without requiring an absolute path.

This moved and renamed the previous `expand_git_path` function to a more
generic location, and the prior use was updated accordingly.
  • Loading branch information
elasticdog committed Aug 13, 2024
1 parent a609580 commit e803bed
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
* The following diff formats now include information about copies and moves:
`--color-words`, `--summary`

* A tilde (`~`) at the start of the path will now be expanded to the user's home
directory when configuring a `signing.key` for SSH commit signing.

### Fixed bugs

## [0.20.0] - 2024-08-07
Expand Down
4 changes: 2 additions & 2 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ use jj_lib::workspace::{
default_working_copy_factories, LockedWorkspace, WorkingCopyFactories, Workspace,
WorkspaceLoadError, WorkspaceLoader,
};
use jj_lib::{dag_walk, fileset, git, op_heads_store, op_walk, revset};
use jj_lib::{dag_walk, file_util, fileset, git, op_heads_store, op_walk, revset};
use once_cell::unsync::OnceCell;
use tracing::instrument;
use tracing_chrome::ChromeLayerBuilder;
Expand Down Expand Up @@ -806,7 +806,7 @@ impl WorkspaceCommandHelper {
if let Some(value) = config.string("core.excludesFile") {
let path = str::from_utf8(&value)
.ok()
.map(crate::git_util::expand_git_path)?;
.map(file_util::expand_home_path)?;
// The configured path is usually absolute, but if it's relative,
// the "git" command would read the file at the work-tree directory.
Some(self.workspace_root().join(path))
Expand Down
10 changes: 0 additions & 10 deletions cli/src/git_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,3 @@ export or their "parent" branches."#,
}
Ok(())
}

/// Expands "~/" to "$HOME/" as Git seems to do for e.g. core.excludesFile.
pub fn expand_git_path(path_str: &str) -> PathBuf {
if let Some(remainder) = path_str.strip_prefix("~/") {
if let Ok(home_dir_str) = std::env::var("HOME") {
return PathBuf::from(home_dir_str).join(remainder);
}
}
PathBuf::from(path_str)
}
2 changes: 1 addition & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ sign-all = true
backend = "ssh"
key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGj+J6N6SO+4P8dOZqfR1oiay2yxhhHnagH52avUqw5h"
## You can also use a path instead of embedding the key
# key = "/home/me/.ssh/id_for_signing.pub"
# key = "~/.ssh/id_for_signing.pub"
```

By default the ssh backend will look for a `ssh-keygen` binary on your path. If you want
Expand Down
10 changes: 10 additions & 0 deletions lib/src/file_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ pub fn remove_dir_contents(dirname: &Path) -> Result<(), PathError> {
Ok(())
}

/// Expands "~/" to "$HOME/".
pub fn expand_home_path(path_str: &str) -> PathBuf {
if let Some(remainder) = path_str.strip_prefix("~/") {
if let Ok(home_dir_str) = std::env::var("HOME") {
return PathBuf::from(home_dir_str).join(remainder);
}
}
PathBuf::from(path_str)
}

/// Turns the given `to` path into relative path starting from the `from` path.
///
/// Both `from` and `to` paths are supposed to be absolute and normalized in the
Expand Down
4 changes: 2 additions & 2 deletions lib/src/ssh_signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ fn run_command(command: &mut Command, stdin: &[u8]) -> SshResult<Vec<u8>> {
fn ensure_key_as_file(key: &str) -> SshResult<Either<PathBuf, tempfile::TempPath>> {
let is_inlined_ssh_key = key.starts_with("ssh-");
if !is_inlined_ssh_key {
let key_path = Path::new(key);
return Ok(either::Left(key_path.to_path_buf()));
let key_path = crate::file_util::expand_home_path(key);
return Ok(either::Left(key_path));
}

let mut pub_key_file = tempfile::Builder::new()
Expand Down

0 comments on commit e803bed

Please sign in to comment.