1

我经常需要使用命令行替换我的默认 git osxkeychain 凭据。我手动操作的方式如下:

git credential-osxkeychain erase <Enter>
host=github.com <Enter>
protocol=https <Enter>

password=foo <Enter>
username=bar <Enter>
<Enter><Enter>

然后再次执行该过程,但使用git credential-osxkeychain store新凭据除外。我想自动化它,比如

alias git_newcred='git credential-osxkeychain erase; host=github.com; protocol=https;git credential-osxkeychain erase; host=github.com; protocol=https;'

换行符;代替 enter 键的位置。然而,就这样默默地失败了。

read_credential源代码中方法的开头如下所示

while (fgets(buf, sizeof(buf), stdin)) {
    char *v;

    if (!strcmp(buf, "\n"))
        break;
    buf[strlen(buf)-1] = '\0';

如何给作为单个命令一部分的多行条目起别名?还是有更好的方法来做到这一点?

(使用 zsh,但我不认为有任何特定于 zsh 的事情发生。)

4

1 回答 1

3

您应该使用函数,而不是别名。运行后git,其余数据将输入到该命令,而不是由 shell 运行的其他命令。您可以使用此处的文档来执行此操作

git_newcred () {
    git credential-osxkeychain erase <<EOF
host=github.com
protocol=https
password=foo
username=bar
EOF
}

或用管道。

git_newcred () {
    # POSIX-compatible version
    # printf '%s\n' ... | git credential-osxkeychain
    print -l "host=github.com" "protocol=https" "password=foo" "username=bar" | git credential-osxkeychain
}
于 2020-04-06T12:05:11.153 回答