summaryrefslogtreecommitdiffstats
path: root/src/subgit-add
diff options
context:
space:
mode:
Diffstat (limited to 'src/subgit-add')
-rwxr-xr-xsrc/subgit-add50
1 files changed, 43 insertions, 7 deletions
diff --git a/src/subgit-add b/src/subgit-add
index 1411dc8..e1e0d19 100755
--- a/src/subgit-add
+++ b/src/subgit-add
@@ -2,11 +2,14 @@
usage() {
die $(cat <<-EOF
- Usage: subgit add [-c COMMIT] [-b BRANCH] [ARG..] REMOTE DEST
+ Usage: subgit add [-c COMMIT] [-b BRANCH] REMOTE DEST
+ subgit add [-c COMMIT] [-b BRANCH] PATH
- Begin tracking a remote repository, optionally at a specific branch
- or commit, as a subgit repo in the current path. Can also be used
- to add an existing repos.
+ Begin tracking a remote repository, optionally at a specific branch
+ or commit, as a subgit repo in the current path.
+
+ REMOTE DEST: Clone from remote URL to destination path
+ PATH: Add existing local repo, extracting remote/commit from it
EOF
)
}
@@ -24,9 +27,42 @@ while [ "$1" ]; do
*) break;;
esac
done
-[ $# -ne 2 ] && usage
-remote=$1
-path=$(realpath -m $2)
+
+if [ $# -eq 1 ]; then
+ # Single argument - existing local repo
+ if [ ! -d "$1" ]; then
+ die "Directory does not exist: $1"
+ fi
+
+ path=$(realpath "$1")
+
+ if [ ! -d "$path/.git" ]; then
+ die "Not a git repository: $path"
+ fi
+
+ # Extract remote URL from existing repo
+ remote=$(git -C "$path" remote get-url origin 2>/dev/null || echo "")
+ if [ -z "$remote" ]; then
+ die "No remote 'origin' found in $path"
+ fi
+
+ # Extract current branch if not specified with -b
+ if [ -z "$branch" ]; then
+ branch=$(git -C "$path" branch --show-current)
+ fi
+
+ # Extract current commit if not specified with -c
+ if [ -z "$commit" ]; then
+ commit=$(git -C "$path" rev-parse HEAD)
+ fi
+
+elif [ $# -eq 2 ]; then
+ # Two arguments - remote and destination (original behavior)
+ remote=$1
+ path=$(realpath -m "$2")
+else
+ usage
+fi
root=$(realpath .)
[ "${path#$root/}" = "$path" ] && die "Not a subdirectory"