【问题标题】:bash - Switch git protocol for multiple git repository via bashbash - 通过 bash 为多个 git 存储库切换 git 协议
【发布时间】:2016-06-29 20:26:58
【问题描述】:

在 linux 上,有一些 git repo,其.git/config 包含以下 3 行:

[遥远的“起源”] url = git@github-eo:eric/workspace.git # url = https://eric@github.com/eric/workspace.git

(提示:最后 2 行以制表符开头。)

问题是:

  • 我想通过 bash 将# 从第 3 行移到第 2 行,也许可以通过 sed,也可以将其移回。我的意思是触发 2 行,如何在 bash 中调用 sed?

@更新:

通过脚本参数选择协议:

实际上,我会为脚本提供一个参数,它应该是sshhttps,参数将决定取消注释哪一行,以及注释哪一行,如果两个协议之一的行不'不存在,然后忽略它。

顺便说一句,我只需要sed部分修改2行,我可以编写bash的其他部分,以节省您的时间。

我想这样做的原因:

我想这样做,因为有时ssh由于网络或github错误而无法工作,我不知道,那时我需要将所有git repo从ssh切换到https,但是当ssh运行良好时,我会使用它,因为我不需要每次都输入密码。

我也问过这个question为什么时不时ssh不工作,但没有解决,所以我只好写一个脚本来帮助用一个命令触发它。


总结:

感谢您的回答和cmets,我将解决方案总结如下。

解决方案:

  • 使用bashsed,查看Eric Wang 的答案()。我猜它适用于linux,mac可能需要对tab匹配进行一些修改,windows可能无法使用它。
  • 使用bash & awk,查看anubhava的回答。
  • 定义多个远程,1 代表 ssh,1 代表 https,然后通过选择远程选择协议,请参阅William Pursell 问题下的 cmets。我猜这适用于所有系统。
  • 使用[include]到另一个指定协议行的文件,并在需要时更改软链接,可以编写一个shell脚本来帮助切换软链接而不是更改文件内容,请参阅gniourf_gniourf提出的cmets问题。我猜这至少适用于类 unix 系统。

【问题讨论】:

  • 什么是输入文件只有一行,即\turl = git@github-eo:ericoudesu/linux_workspace.git?还应该评论吗?
  • @anubhava 我更新了问题,请看。
  • 也许,您只是想介绍另一个遥控器?我的意思是一个遥控器专用于只读https://,另一个-用于读/写ssh://
  • @user3159253 我更新了问题以解释为什么我需要脚本,请参考。
  • 您是否尝试过编写sed 命令来执行您想要的操作?

标签: git bash shell sed


【解决方案1】:

awk 在这里可能是更好的选择:

awk -v kw='(https|ssh):' 'p ~ /^\turl/ && $0 ~ kw && $0 ~ /^\t#[[:blank:]]*url/ {
   sub(/^\t/, "\t# ", p)
   sub(/^\t*#[[:blank:]]*/, "\t")
}
p!="" {
   print p
}
{
   p=$0
}
END {
   print p
}' file

[remote "origin"]
    # url = git@github-eo:eric/workspace.git
    url = https://eric@github.com/eric/workspace.git

【讨论】:

  • 我测试过了,好像没有注释的行tab后面有一个空格,你能帮忙去掉吗?
  • 非常感谢,我认为这是一个解决方案。但我刚刚弄清楚如何在 sed 中做到这一点,请参阅我的另一个答案。
【解决方案2】:

我刚刚通过sed 弄清楚了如何做到这一点,以下是脚本:


代码

git_protocol_switch.sh:

#!/bin/bash

# repo array,
repo_arr=(
    "/mnt/star/git_repository/workspace" 
    "/media/ERIC/node"
    "/mnt/star/workplace/eclipse_j2ee_workplace/eric"
)

protocol="null"

url_prefix_comment=""
url_prefix_uncomment=""

repo_count=0

# read protocol,
if [ $# -ge 1 ]; then
    protocol=$1
fi

# check protocol,
if [ $protocol == "ssh" ]; then
    url_prefix_comment="https"
    url_prefix_uncomment="git"
elif [ $protocol == "https" ]; then
    url_prefix_comment="git"
    url_prefix_uncomment="https"
else
    echo "invalid protocol: $protocol"
    exit 1;
fi


# switch protocol for repo in array,
for i in "${!repo_arr[@]}"; do 
    if [ -d "${repo_arr[$i]}" ]; then
        sed -i "s/^\turl = $url_prefix_comment/\t# url = $url_prefix_comment/g" "${repo_arr[$i]}/.git/config"
        sed -i "s/^\t# url = $url_prefix_uncomment/\turl = $url_prefix_uncomment/g" "${repo_arr[$i]}/.git/config"

        repo_count=$(expr $repo_count + 1)
    else
        echo "non exists repo: ${repo_arr[$i]}"
    fi
done

echo "Done, switch to protocol [$protocol], repo count: {$repo_count}."

exit 0

叫它

# switch to ssh
./git_protocol_switch.sh ssh


# switch to https
./git_protocol_switch.sh https

它的作用

这将有助于为 bash 数组中列出的所有 repo 切换协议。 因为,需要先手动为每个 repo 添加 2 行 2 协议。

干杯:}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    相关资源
    最近更新 更多