本文介绍了一个更好的方式做混帐克隆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是懒惰的程序员的要求。我想创建一个shell脚本自动执行以下过程:

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>

所以这里的想法是立即复制一个URL,然后 CD 克隆文件夹。这里的技巧是从URL模式确定克隆文件夹

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.

现在,我们可以假设URL结构是这种模式 ... /&LT;克隆文件夹方式&gt;的.git 即网址

For now we can assume that url structure is in this pattern .../<cloned-folder>.git i.e. the url.

我想知道如果我们实际上可以使用 AWK 或者一些工具,如这一点。我坚持的部分是找到一个合适的正则表达式,我猜。

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.

使用CASE :这里使用情况是,如果你克隆一个网址,要尽快处于repofolder。如果你想运行像 git的日志或<$ C任何混帐命令是pre-要求$ C>伴侣。这是我们做的99%的时间。

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.

先谢谢了。

推荐答案

bash函数来做到这一点(在zsh的作品也可以):

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

AWK 命令打印在最后 / (例如:从 HTTP部分: //example.com/myrepo.git myrepo.git )。在 SED 命令删除尾随的.git

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

用法:

$ 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

这篇关于一个更好的方式做混帐克隆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 13:08