Skip to content
Draft
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
20 changes: 11 additions & 9 deletions git-mem
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/test-source.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down