Skip to content

Commit 4194ff9

Browse files
committed
fix(cli): preserve uploaded dir name for git-filtered paths
1 parent c49a3bf commit 4194ff9

5 files changed

Lines changed: 67 additions & 4 deletions

File tree

‎architecture/sandbox-connect.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ openshell sandbox download <name> <sandbox-path> [<local-path>]
306306
```
307307

308308
- **Upload**: `sandbox_upload()` streams a tar archive of the local path to `ssh ... tar xf - -C <dest>` on the sandbox side. Default destination: `/sandbox`.
309+
- Directory uploads keep the historical flat behavior by default: uploading `./src` to `/sandbox/src` extracts the contents into `/sandbox/src`. Passing `--preserve-dir` wraps directory entries under the source basename, so uploading `./src` to `/sandbox` lands at `/sandbox/src`. This applies to both regular uploads and git-filtered uploads.
309310
- **Download**: `sandbox_download()` runs `ssh ... tar cf - -C <dir> <path>` on the sandbox side and extracts the output locally via `tar::Archive`. Default destination: `.` (current directory).
310311
- No compression for v1 — the SSH tunnel is local-network; compression adds CPU cost with marginal bandwidth savings.
311312

‎crates/openshell-cli/src/main.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,12 +2348,23 @@ async fn main() -> Result<()> {
23482348
let dest_display = sandbox_dest.unwrap_or("~");
23492349
eprintln!("Uploading {} -> sandbox:{}", local.display(), dest_display);
23502350
if !no_git_ignore && let Ok((base_dir, files)) = run::git_sync_files(local) {
2351+
let tar_prefix = if preserve_dir && local.is_dir() {
2352+
Some(
2353+
local
2354+
.file_name()
2355+
.map(|n| n.to_os_string())
2356+
.unwrap_or_else(|| ".".into()),
2357+
)
2358+
} else {
2359+
None
2360+
};
23512361
run::sandbox_sync_up_files(
23522362
&ctx.endpoint,
23532363
&name,
23542364
&base_dir,
23552365
&files,
23562366
sandbox_dest,
2367+
tar_prefix,
23572368
&tls,
23582369
)
23592370
.await?;

‎crates/openshell-cli/src/run.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,6 +2404,7 @@ pub async fn sandbox_create(
24042404
&base_dir,
24052405
&files,
24062406
dest,
2407+
None,
24072408
&effective_tls,
24082409
)
24092410
.await?;

‎crates/openshell-cli/src/ssh.rs‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ enum UploadSource {
459459
FileList {
460460
base_dir: PathBuf,
461461
files: Vec<String>,
462+
tar_prefix: Option<std::ffi::OsString>,
462463
},
463464
}
464465

@@ -526,17 +527,22 @@ async fn ssh_tar_upload(
526527
));
527528
}
528529
}
529-
UploadSource::FileList { base_dir, files } => {
530+
UploadSource::FileList {
531+
base_dir,
532+
files,
533+
tar_prefix,
534+
} => {
530535
for file in &files {
531536
let full_path = base_dir.join(file);
537+
let tar_name = file_list_tar_name(file, tar_prefix.as_ref());
532538
if full_path.is_file() {
533539
archive
534-
.append_path_with_name(&full_path, file)
540+
.append_path_with_name(&full_path, &tar_name)
535541
.into_diagnostic()
536542
.wrap_err_with(|| format!("failed to add {file} to tar archive"))?;
537543
} else if full_path.is_dir() {
538544
archive
539-
.append_dir_all(file, &full_path)
545+
.append_dir_all(&tar_name, &full_path)
540546
.into_diagnostic()
541547
.wrap_err_with(|| {
542548
format!("failed to add directory {file} to tar archive")
@@ -590,6 +596,7 @@ pub async fn sandbox_sync_up_files(
590596
base_dir: &Path,
591597
files: &[String],
592598
dest: Option<&str>,
599+
tar_prefix: Option<std::ffi::OsString>,
593600
tls: &TlsOptions,
594601
) -> Result<()> {
595602
if files.is_empty() {
@@ -602,6 +609,7 @@ pub async fn sandbox_sync_up_files(
602609
UploadSource::FileList {
603610
base_dir: base_dir.to_path_buf(),
604611
files: files.to_vec(),
612+
tar_prefix,
605613
},
606614
tls,
607615
)
@@ -699,6 +707,13 @@ fn directory_upload_prefix(local_path: &Path) -> std::ffi::OsString {
699707
.unwrap_or_else(|| ".".into())
700708
}
701709

710+
fn file_list_tar_name(file: &str, tar_prefix: Option<&std::ffi::OsString>) -> PathBuf {
711+
match tar_prefix {
712+
Some(prefix) if prefix != "." => Path::new(prefix).join(file),
713+
_ => PathBuf::from(file),
714+
}
715+
}
716+
702717
/// Pull a path from a sandbox to a local destination using tar-over-SSH.
703718
pub async fn sandbox_sync_down(
704719
server: &str,
@@ -1321,6 +1336,30 @@ mod tests {
13211336
);
13221337
}
13231338

1339+
#[test]
1340+
fn file_list_tar_name_wraps_entries_under_prefix() {
1341+
let prefix = std::ffi::OsString::from("src");
1342+
1343+
assert_eq!(
1344+
file_list_tar_name("lib/main.rs", Some(&prefix)),
1345+
PathBuf::from("src/lib/main.rs")
1346+
);
1347+
}
1348+
1349+
#[test]
1350+
fn file_list_tar_name_keeps_entries_flat_for_dot_or_missing_prefix() {
1351+
let dot = std::ffi::OsString::from(".");
1352+
1353+
assert_eq!(
1354+
file_list_tar_name("lib/main.rs", Some(&dot)),
1355+
PathBuf::from("lib/main.rs")
1356+
);
1357+
assert_eq!(
1358+
file_list_tar_name("lib/main.rs", None),
1359+
PathBuf::from("lib/main.rs")
1360+
);
1361+
}
1362+
13241363
#[test]
13251364
fn split_sandbox_path_handles_root_and_bare_names() {
13261365
// File directly under root

‎examples/sync-files.md‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ Push a specific directory to a custom destination:
1717
openshell sandbox upload my-sandbox ./src /sandbox/src
1818
```
1919

20+
By default, directory uploads extract the directory's contents into the
21+
destination. To keep the source directory name, use `--preserve-dir`:
22+
23+
```bash
24+
openshell sandbox upload my-sandbox ./src /sandbox --preserve-dir
25+
# files land under /sandbox/src/
26+
```
27+
2028
Push a single file:
2129

2230
```bash
@@ -46,7 +54,10 @@ openshell sandbox create --sync -- python main.py
4654
```
4755

4856
This collects tracked and untracked (non-ignored) files via
49-
`git ls-files` and streams them into `/sandbox` before the command runs.
57+
`git ls-files` and streams them into `/sandbox` before the command runs. The
58+
same git-aware filtering is used by `sandbox upload` unless you pass
59+
`--no-git-ignore`; `--preserve-dir` still wraps filtered directory uploads
60+
under the source directory name.
5061

5162
## Workflow: iterate on code in a sandbox
5263

0 commit comments

Comments
 (0)