LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   URL as a Command (https://www.linuxquestions.org/questions/linux-newbie-8/url-as-a-command-4175679392/)

blueray 07-26-2020 02:40 AM

URL as a Command
 
When I enter a git repo url in terminal it says:
Code:

% https://github.com/chmln/sd.git       
zsh: no such file or directory: https://github.com/chmln/sd.git

I want https://github.com/chmln/sd.git to act as a command which will

Code:

git clone https://github.com/chmln/sd.git
cd sd

How can I do that?

blueray 07-26-2020 04:31 AM

I tried preexec hook.

Code:

preexec () {
    print ">>>preexec start<<<"
    # print -l ${(qqq)@}

    if  [[ ${(qqq)@} =~ ^https.* ]]
    then
    echo URL
    fi
    print ">>>preexec end<<<"

}

The output is

Code:

% https://github.com/chmln/sd.git
>>>preexec start<<<
>>>preexec end<<<
zsh: no such file or directory: https://github.com/chmln/sd.git


blueray 07-26-2020 04:44 AM

The code that is working is

Code:

preexec () {
    print ">>>preexec start<<<"

    if  [[ $@[1] =~ ^https.*git ]]
    then
        git clone $@[1] && cd "$(basename $@[1] .git)"
    else
    echo did not match
    fi
    print ">>>preexec end<<<"

}

However, there is a little annoyance.

Code:

% https://github.com/chmln/sd.git
>>>preexec start<<<
Cloning into 'sd'...
remote: Enumerating objects: 78, done.
remote: Counting objects: 100% (78/78), done.
remote: Compressing objects: 100% (57/57), done.
remote: Total 792 (delta 34), reused 48 (delta 15), pack-reused 714
Receiving objects: 100% (792/792), 239.98 KiB | 318.00 KiB/s, done.
Resolving deltas: 100% (456/456), done.
>>>preexec end<<<
zsh: no such file or directory: https://github.com/chmln/sd.git

You can see that the message zsh: no such file or directory: https://github.com/chmln/sd.git is still there.

blueray 07-26-2020 05:53 AM

I got a solution where it uses

Code:

function command_not_found_handle {
        if [[ "$1" =~ "^(https|http|git|(git\\+)?ssh)://.*\$" ]]; then
                echo "Do something: $1"
                return 1
        else
                return 0
        fi
}

But in my zsh the error message (no such file or directory...) does not seem to be handled by this handler.

I am guessing this because, I used

Code:

if [[ -x /usr/lib/command-not-found ]] ; then
        if (( ! ${+functions[command_not_found_handler]} )) ; then
                function command_not_found_handler {
                        echo "The following command is not valid: \""$1\"""
                        [[ -x /usr/lib/command-not-found ]] || return 1
                        /usr/lib/command-not-found -- ${1+"$1"} && :
                }
        fi
fi

But The following command is not valid: is not shown before the line zsh: no such file or directory:

blueray 07-26-2020 08:45 AM

Code:

function _accept-line-with-url {

    if  [[ $BUFFER =~ ^https.*git ]]
    then
        # zle -U "git clone $BUFFER && cd \"$(basename $BUFFER .git)\""

        printf "\n"
        git clone $BUFFER && cd "$(basename $BUFFER .git)"

        zle kill-whole-line
        zle .accept-line
    else
        zle .accept-line
    fi
   
}

zle -N accept-line _accept-line-with-url


scasey 07-26-2020 12:17 PM

Quote:

Originally Posted by blueray (Post 6149379)
When I enter a git repo url in terminal it says:
Code:

% https://github.com/chmln/sd.git       
zsh: no such file or directory: https://github.com/chmln/sd.git

I want https://github.com/chmln/sd.git to act as a command which will

Code:

git clone https://github.com/chmln/sd.git
cd sd

How can I do that?

If you want to execute git with a URL as the argument just do that.
The shell is always going to report "no such file" for a URL unless you have an actual file named sd.gin in a directory named chmln in a directory named github.com in a directory named https:

Something like this in a file name rungit:
Code:

#!/bin/zsh
git $1
cd sd

then
Code:

rungit https://github.com/chmln/sd.git

blueray 07-26-2020 12:45 PM

Use the code in my last post and put it in your .zshrc

Alhamdulillah, it is actually working now.


All times are GMT -5. The time now is 07:09 AM.