Reputation: 5945
This is lazy programmer request. I would like to create a shell script automate the following process:
git clone <remote-repo-url>
cd <cloned-folder>
open <cloned-folder>
So the idea here is to clone a URL and then immediately cd
into the cloned-folder
. The trick here is to identify the cloned-folder
from the url pattern.
For now we can assume that url structure is in this pattern .../<cloned-folder>.git
i.e. the url.
I am wondering if we can actually do with using awk
or some tools like that. The part i am stuck is finding a appropriate regex
, I guess.
USE CASE: Here the use case is if you clone a url, you want to be in the repofolder as soon as possible. The is the pre-requirement if you want to run any git
command like git log
or mate .
which we do 99% of the time.
Thanks in advance.
Upvotes: 8
Views: 13756
Reputation: 3064
Instead of getting directory name from url, we could just cd
into the latest created directory. Here is the corresponding shell script:
gclcd() {
git clone --recursive $* && cd "$(ls -t | head -1)"
}
Upvotes: 1
Reputation: 5566
The bash parameter version is quite nice, I went with basename
cuz it seemed cleaner
function gclocd {
# git clone and enter the cloned file
if [[ -z "$2" ]]; then
reponame=$(basename "$1" ".git");
else
reponame=$2;
fi
git clone "$1" "$reponame";
cd "$reponame";
}
I use short memorable tab-completable function names for a bunch of things like this and put them in my .bashrc
as aliases/functions, e.g. gcmmt
for git commit -m
. lazy
may well already be a prefix for things in your shell, which means it's that little bit more to type each time...
Upvotes: 0
Reputation: 1673
An improvement from @dbr's answer:
function lazyclone {
reponame=${1##*/}
reponame=${reponame%.git}
git clone "$1" "$reponame";
cd "$reponame";
}
With the help of Bash's parameter expansion, we can get rid of awk
and sed
.
Upvotes: 1
Reputation: 169573
bash function to do this (works in zsh also):
function lazyclone {
url=$1;
reponame=$(echo $url | awk -F/ '{print $NF}' | sed -e 's/.git$//');
git clone $url $reponame;
cd $reponame;
}
The awk
command prints the part after the last /
(e.g from http://example.com/myrepo.git
to myrepo.git
). The sed
command removes the trailing .git
Usage:
$ pwd
~/
$ lazyclone https://github.com/dbr/tvdb_api.git
tvdb_api
Cloning into 'tvdb_api'...
remote: Counting objects: 1477, done.
remote: Compressing objects: 100% (534/534), done.
remote: Total 1477 (delta 952), reused 1462 (delta 940)
Receiving objects: 100% (1477/1477), 268.48 KiB | 202 KiB/s, done.
Resolving deltas: 100% (952/952), done.
$ pwd
~/tvdb_api
Upvotes: 15
Reputation: 34934
With git clone, you can specify the folder to clone to, instead of allowing it to be named automatically.
dir=myclone
git clone git://somerepo "$dir"
cd "$dir"
open "$dir"
Upvotes: 2