diff --git a/git-mem b/git-mem index 4133ec1..9915736 100755 --- a/git-mem +++ b/git-mem @@ -913,21 +913,23 @@ cmd_source() { mkdir -p "$GIT_MEMORY_SOURCES_DIR" local dest="$GIT_MEMORY_SOURCES_DIR/$name" + local real_path + real_path=$(realpath "$location" 2>/dev/null || echo "$location") + local primary_real + primary_real=$(realpath "$GIT_MEMORY_DIR" 2>/dev/null || echo "$GIT_MEMORY_DIR") - if [[ "$location" =~ ^(https?|git|ssh):// ]]; then + [[ "$real_path" == "$primary_real" ]] && die "Cannot add primary memory store as a source" + + if [[ -d "$real_path/.git" ]]; then + # Local path — symlink into sources dir + ln -s "$real_path" "$dest" || die "Symlink failed" + elif [[ "$location" =~ ^(https?|git|ssh):// || "$location" =~ ^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+:.+ ]]; then # URL — clone into sources dir [[ -e "$dest" ]] && die "Directory already exists: $dest" echo "Cloning $location..." git clone "$location" "$dest" 2>&1 || die "Clone failed" else - # Local path — symlink into sources dir - local real_path - real_path=$(realpath "$location" 2>/dev/null || echo "$location") - local primary_real - primary_real=$(realpath "$GIT_MEMORY_DIR" 2>/dev/null || echo "$GIT_MEMORY_DIR") - [[ "$real_path" == "$primary_real" ]] && die "Cannot add primary memory store as a source" - [[ ! -d "$real_path/.git" ]] && die "Not a git repository: $real_path" - ln -s "$real_path" "$dest" || die "Symlink failed" + die "Not a git repository: $real_path" fi local count diff --git a/tests/test-source.sh b/tests/test-source.sh index 5422e89..836afa9 100644 --- a/tests/test-source.sh +++ b/tests/test-source.sh @@ -41,6 +41,10 @@ assert_exit_0 "add local source (hermes)" bash "$GIT_MEM" source add her assert_exit_nonzero "add duplicate name fails" bash "$GIT_MEM" source add team /tmp/git-mem-src-team-$$ assert_exit_nonzero "add primary as source fails" bash "$GIT_MEM" source add mine "$GIT_MEMORY_DIR" assert_exit_nonzero "reserved name 'mine' rejected" bash "$GIT_MEM" source add mine /tmp/git-mem-src-team-$$ +assert_output_contains "scp-style SSH URL treated as remote clone" "Clone failed" \ + bash "$GIT_MEM" source add sshremote1 git@127.0.0.1:/tmp/git-mem-nonexistent-a-$$ +assert_output_not_contains "scp-style SSH URL not treated as local path" "Not a git repository" \ + bash "$GIT_MEM" source add sshremote2 git@127.0.0.1:/tmp/git-mem-nonexistent-b-$$ # Verify symlinks created [[ -L "$GIT_MEMORY_SOURCES_DIR/team" ]] && pass "team is a symlink" || fail "team symlink missing"