Best way to make a git clone

This is a lazy programmer request. I would like to create a shell script to automate the following process:

git clone <remote-repo-url>
cd <cloned-folder>
open <cloned-folder> 

So, the idea here is to clone the URL, and then immediately cdinto cloned-folder. The trick here is to identify cloned-folderby url pattern.

Now we can assume that the url structure is in this template .../<cloned-folder>.giti.e. url

I am wondering if we can really use awkor some similar tools. I believe that the part I am stuck finds the right one regex.

USE CASE . This uses the case where you are cloning a URL, you want to be in repofolder as soon as possible. This is a prerequisite if you want to run any command git, for example, git logor mate .that we execute in 99% of cases.

Thanks in advance.

+5
source share
5 answers

The bash function for this (also works in zsh):

function lazyclone {
    url=$1;
    reponame=$(echo $url | awk -F/ '{print $NF}' | sed -e 's/.git$//');
    git clone $url $reponame;
    cd $reponame;
}

The command awkprints the part after the last /(for example, from http://example.com/myrepo.gitto myrepo.git). The command seddeletes the final.git

Using:

$ 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
+11
source

With git clone, you can specify the folder to clone instead of resolving its name automatically.

dir=myclone
git clone git://somerepo "$dir"
cd "$dir"
open "$dir"
+2
source

@dbr:

function lazyclone {
    reponame=${1##*/}
    reponame=${reponame%.git}
    git clone "$1" "$reponame";
    cd "$reponame";
}

Bash awk sed.

+1

bash , basename,

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";
}

, , .bashrc /, . gcmmt git commit -m. lazy , , , ...

0

, url, cd . script:

gclcd() {
    git clone --recursive $* && cd "$(ls -t | head -1)"
}
0

All Articles