Skip to content

Commit 9945f64

Browse files
committed
Add --location flag
1 parent 854fb94 commit 9945f64

File tree

3 files changed

+67
-13
lines changed

3 files changed

+67
-13
lines changed

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Just put `git-get` somewhere in your path :)
1111
## Usage
1212

1313
```
14-
git get [--print-path] <repository> [<args>]
14+
git get [--print-path] [--location <dir>] <repository> [<args>]
1515
```
1616

1717
### Arguments
@@ -22,6 +22,7 @@ git get [--print-path] <repository> [<args>]
2222
### Options
2323

2424
- `--print-path` - Print the full path where the repository would be cloned and exit without cloning
25+
- `--location <dir>` - Override the base directory for this clone only
2526
- `-h, --help` - Show help message and exit
2627

2728
## Examples
@@ -61,3 +62,27 @@ By default, git-get places all repositories under `~/code` but you can change th
6162
```shell
6263
git config --global get.location ~/projects
6364
```
65+
66+
### Additional Examples
67+
68+
```shell
69+
# See where a repository would be cloned without actually cloning
70+
git get --print-path https://github.com/stilvoid/git-get
71+
72+
# Override the base directory for one clone only
73+
git get --location ~/temp https://github.com/stilvoid/git-get
74+
# → ~/temp/github.com/stilvoid/git-get
75+
76+
# Preview where a repository would be cloned with a custom location
77+
git get --location ~/projects --print-path [email protected]:user/repo.git
78+
# → ~/projects/github.com/user/repo
79+
80+
# Show help and usage information
81+
git get --help
82+
83+
# Pass additional arguments to git clone (shallow clone)
84+
git get --depth 1 https://github.com/stilvoid/git-get
85+
86+
# Clone a specific branch
87+
git get --branch main https://github.com/stilvoid/git-get
88+
```

git-get

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
set -eu -o pipefail
44

55
function print_help {
6-
echo "Usage: git get [--print-path] <repository> [<args>]"
6+
echo "Usage: git get [--print-path] [--location <dir>] <repository> [<args>]"
77
echo
88
echo " git-get clones <repository> into a folder derived from the repository URL"
99
echo
@@ -16,32 +16,33 @@ function print_help {
1616
echo " --print-path will print out the full path that the repository would be cloned into"
1717
echo " and then exits immediately without cloning"
1818
echo
19+
echo " --location <dir> override the base directory for this clone only"
20+
echo
1921
echo " -h, --help show this help message and exit"
2022
echo
2123
echo " Any other arguments are passed to the \"git clone\" command"
2224
}
2325

24-
# Set base directory for git checkout
25-
base_dir="$(git config get.location || true)"
26-
27-
if [ -z "$base_dir" ]; then
28-
base_dir="$HOME/code"
29-
else
30-
# Replace a tilde with $HOME
31-
base_dir="${base_dir/#~/$HOME}"
32-
fi
33-
3426
# Parse CLI opts
3527
COMMAND="checkout"
3628
REPO=""
3729
EXTRA_ARGS=()
30+
OVERRIDE_LOCATION=""
3831

3932
while [[ $# -gt 0 ]]; do
4033
case $1 in
4134
--print-path)
4235
COMMAND="print"
4336
shift
4437
;;
38+
--location)
39+
if [ $# -lt 2 ] || [ -z "${2:-}" ]; then
40+
echo "Error: --location requires a directory path"
41+
exit 1
42+
fi
43+
OVERRIDE_LOCATION="$2"
44+
shift 2
45+
;;
4546
-h|--help)
4647
print_help
4748
exit
@@ -68,6 +69,22 @@ if [ -z "$REPO" ] ; then
6869
exit 1
6970
fi
7071

72+
# Set base directory for git checkout
73+
if [ -n "$OVERRIDE_LOCATION" ]; then
74+
# Use override location, expanding tilde if present
75+
base_dir="${OVERRIDE_LOCATION/#~/$HOME}"
76+
else
77+
# Use configured or default location
78+
base_dir="$(git config get.location || true)"
79+
80+
if [ -z "$base_dir" ]; then
81+
base_dir="$HOME/code"
82+
else
83+
# Replace a tilde with $HOME
84+
base_dir="${base_dir/#~/$HOME}"
85+
fi
86+
fi
87+
7188
# Strip scheme
7289
dir="${REPO#*://}"
7390

@@ -80,6 +97,9 @@ dir="${dir/://}"
8097
# Remove .git
8198
dir="${dir%.git}"
8299

100+
# Remove trailing slash from base_dir if present
101+
base_dir="${base_dir%/}"
102+
83103
dir="${base_dir}/${dir}"
84104

85105
if [ "$COMMAND" == "print" ]; then

git-get.1

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
git-get \- clone a git repository into a folder structure derived from the repository URL
44
.SH SYNOPSIS
55
.B git-get
6-
[\fB\-h\fR | \fB\-\-help\fR | \fB\-\-print\-path\fR]
6+
[\fB\-h\fR | \fB\-\-help\fR | \fB\-\-print\-path\fR | \fB\-\-location\fR \fIdir\fR]
77
.IR repository
88
[\fIargs\fR...]
99
.SH DESCRIPTION
@@ -18,6 +18,9 @@ and the suffix \fI.git\fR if it exists.
1818
.B \-\-print\-path
1919
Print the full path where the repository would be cloned and exit without cloning.
2020
.TP
21+
.B \-\-location \fIdir\fR
22+
Override the base directory for this clone only.
23+
.TP
2124
.B \-h, \-\-help
2225
Show help message and exit.
2326
.PP
@@ -53,3 +56,9 @@ Preview where a repository would be cloned:
5356
.RS
5457
$ git get --print-path https://github.com/stilvoid/git-get.git
5558
.RE
59+
.PP
60+
Override the base directory for one clone:
61+
.PP
62+
.RS
63+
$ git get --location ~/temp [email protected]:stilvoid/git-get.git
64+
.RE

0 commit comments

Comments
 (0)