Skip to content
Merged
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
7 changes: 7 additions & 0 deletions native/host/src/files/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ pub async fn step_capture(cwd: &str, thread: &str, reg: &str) -> String {
/// caller's timeout (history.steps, #1110) leaves no git running.
pub async fn refs(cwd: &str, thread: &str) -> String {
if !super::threadpath::absolute(cwd) { return String::new() } let r = sh_read(REFS, &[cwd, &b64url(thread)], "/", &[]).await; if r.code == 0 { r.out } else { String::new() } }
/// Just the git dir and top lines of `refs.sh` (`history.steps` needs only those, not branches/tags/log).
pub async fn repo_dirs(cwd: &str) -> String {
if !super::threadpath::absolute(cwd) { return String::new() } let r = sh_read(REFS, &[cwd, "", "--dirs"], "/", &[]).await; if r.code == 0 { r.out } else { String::new() }
}
/// Git.show: a file as a commit has it (`git --git-dir G show OBJ`), None when it is not there.
pub async fn show(gitdir: &str, obj: &str) -> Option<Vec<u8>> {
let o = crate::proc::tokio_cmd(crate::proc::Kind::Local, "sh").args(["-c", "exec 2>/dev/null; git --git-dir=\"$1\" show \"$2\"", "sh", gitdir, obj]).current_dir("/").stdin(std::process::Stdio::null()).envs(GIT_ENV.iter().copied()).kill_on_drop(true).output().await.ok()?; // a pure read: killed with its future
Expand Down Expand Up @@ -417,6 +421,9 @@ mod tests {
std::fs::write(format!("{d}/b.kicad_pcb"), "(kicad_pcb 2)").unwrap();
let s2 = step_capture(&d, "th", &reg).await; assert_eq!(s2.split('\n').nth(1).unwrap(), l[0]);
let r = refs(&d, "th").await; assert!(r.lines().nth(0).unwrap().ends_with(".git") && r.contains("\n") );
let dirs = repo_dirs(&d).await; let dl: Vec<&str> = dirs.lines().collect();
assert_eq!(dl.len(), 2, "{dirs:?}");
assert_eq!(dl[0], r.lines().nth(0).unwrap()); assert_eq!(dl[1], r.lines().nth(1).unwrap());
let gd = l[2]; assert_eq!(show(gd, &format!("{}:b.kicad_pcb", l[0])).await.unwrap(), b"(kicad_pcb)"); assert!(show(gd, "nope:x").await.is_none());
// a folder outside git keeps its steps in .backplane/history.git
let f = tmp("shadow"); std::fs::write(format!("{f}/s.kicad_sch"), "(kicad_sch)").unwrap();
Expand Down
6 changes: 4 additions & 2 deletions native/host/src/files/scripts/refs.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
exec 2>/dev/null
bp_pwd() { pwd -W || pwd; }
cd "$1" || exit 0
name=$2
if top=$(git rev-parse --show-toplevel 2>/dev/null) && [ -n "$top" ]; then
# git names the top by its real path (macOS: /private/var/... for /var/...); the History compares it with the path the person
# opened, so keep the folder's path as we were given it: the same folder, reached the way "$1" reached it (not on Windows' C:/...)
case $top in /*) lt=$(cd "./$(git rev-parse --show-cdup)" && pwd) && [ "$lt" != "$top" ] && [ "$(cd "$lt" && pwd -P)" = "$(cd "$top" && pwd -P)" ] && top=$lt ;; esac
gd=$(cd "$(git rev-parse --git-common-dir)" && pwd) || exit 0
gd=$(cd "$(git rev-parse --git-common-dir)" && bp_pwd) || exit 0
printf '%s\n%s\n' "$gd" "$top"
[ "${3:-}" = --dirs ] && exit 0
h=$(git rev-parse -q --verify HEAD) && printf 'head\tHEAD\t%s\n' "$h"
for b in origin/HEAD origin/main origin/master main master; do
m=$(git merge-base HEAD "$b" 2>/dev/null) || continue
Expand All @@ -19,6 +21,6 @@ if top=$(git rev-parse --show-toplevel 2>/dev/null) && [ -n "$top" ]; then
git for-each-ref --sort=-committerdate --format='turn%09%(refname)%09%(objectname)' "refs/backplane/checkpoints/$name/turn/"
git for-each-ref --sort=-committerdate --count=40 --format='snap%09%(refname)%09%(objectname)' "refs/backplane/snapshots/$name/" | awk -F'\t' '$2 ~ /\/[0-9]+$/'
else
top=$(pwd)
top=$(bp_pwd)
printf '%s\n%s\n' "$top/.backplane/history.git" "$top"
fi
9 changes: 5 additions & 4 deletions native/host/src/files/scripts/step.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
exec 2>/dev/null
bp_pwd() { pwd -W || pwd; }
bp_safe() { n=${GIT_CONFIG_COUNT:-0}; ks=$(git config --show-scope --name-only --get-regexp '^filter[.].*[.](clean|smudge|process)$' 2>/dev/null | awk -F ' ' '$1 == "local" || $1 == "worktree" { print $2 }'); o=$IFS; IFS='
'; for k in $ks; do export "GIT_CONFIG_KEY_$n=$k" "GIT_CONFIG_VALUE_$n="; n=$((n+1)); done; IFS=$o; export GIT_CONFIG_COUNT=$n; };
cd "$1" || exit 0
Expand All @@ -8,16 +9,16 @@ if top=$(git rev-parse --show-toplevel) && [ -n "$top" ]; then
# git names the top by its real path (macOS: /private/var/... for /var/...); the History compares it with the path the person
# opened, so keep the folder's path as we were given it: the same folder, reached the way "$1" reached it (not on Windows' C:/...)
case $top in /*) lt=$(cd "./$(git rev-parse --show-cdup)" && pwd) && [ "$lt" != "$top" ] && [ "$(cd "$lt" && pwd -P)" = "$(cd "$top" && pwd -P)" ] && top=$lt ;; esac
gd=$(cd "$(git rev-parse --git-common-dir)" && pwd) || exit 0
gd=$(cd "$(git rev-parse --git-common-dir)" && bp_pwd) || exit 0
pd=$(git rev-parse --absolute-git-dir) || exit 0
shadow=
else
top=$(pwd); gd=$top/.backplane/history.git; pd=$gd; shadow=1
[ "$top" = / ] || [ "$top" = "$HOME" ] && exit 0
top=$(bp_pwd); ltop=$(pwd); gd=$top/.backplane/history.git; lgd=$ltop/.backplane/history.git; pd=$gd; shadow=1
[ "$top" = / ] || [ "$ltop" = / ] || [ "$top" = "$HOME" ] || [ "$ltop" = "$HOME" ] && exit 0
[ -d "$gd" ] || [ -L "$gd" ] || [ -n "$(find . -maxdepth 4 \( -name '*.kicad_pcb' -o -name '*.kicad_sch' -o -name '*.kicad_pro' -o -iname '*.pcbdoc' -o -iname '*.schdoc' -o -iname '*.prjpcb' \) -print | head -n 1)" ] || exit 0
if [ -e "$gd" ] || [ -L "$gd" ]; then
# trusted: the registry names this folder with its inode (the device number before the colon is not compared: it changes when the disk is mounted again)
if ! { [ -n "$reg" ] && [ ! -L "$gd" ] && [ -d "$gd" ] && i=$(ino "$gd") && awk -v i="${i#*:}" -v g="$gd" '{ k = $1; sub(/^[^:]*:/, "", k) } k == i && substr($0, length($1) + 2) == g { f = 1 } END { exit !f }' "$reg"; }; then
if ! { [ -n "$reg" ] && [ ! -L "$gd" ] && [ -d "$gd" ] && i=$(ino "$gd") && awk -v i="${i#*:}" -v g="$gd" -v g2="$lgd" '{ k = $1; sub(/^[^:]*:/, "", k); p = substr($0, length($1) + 2) } k == i && (p == g || p == g2) { f = 1 } END { exit !f }' "$reg"; }; then
mv "$gd" "$gd.untrusted-$(date +%s)" || exit 0
fi
fi
Expand Down
Loading
Loading