有没有办法以只读模式运行 git?

发布于 2024-10-07 19:10:43 字数 513 浏览 5 评论 0原文

我正在编写一个脚本来在当前目录的每个子目录中运行诸如 git status 之类的东西,这样我就可以快速获取有关多个 git 存储库的信息。但是,我想阻止任何修改存储库的命令。我不想一次提交十个回购协议。我不能只使用 git 子命令的白名单,因为我可能想运行 gitbranch -a 来查看每个存储库中的分支,而不是 gitbranch new-branch-name< /code> 在每个存储库中创建一个新分支。

那么有没有一种方法可以运行 git,使其中止而不是对存储库进行任何修改,但在运行只读命令时工作正常?

编辑:我想做的是创建一个 git-subdirs 命令,其工作原理如下:当我输入 git subdirs COMMAND ARGS 时,我想做一些类似于 for dir in */; 的操作。执行 cd $dir && git 命令参数 &&光盘 ..;完成(仅进行额外的错误检查)。除非 git COMMAND ARGS 会以任何方式修改存储库,我不想这样做,因为我不太可能想要对许多不同的存储库进行相同的更改。

I'm writing a script to run things like git status in each of the subdirectories of the current directory, so I can quickly get information about several git repos. However, I want to prevent any commands that modify the repos. I wouldn't want to commit ten repos at once. I can't just use a whitelist of git subcommands, because I might want to run git branch -a to see the branches in each repo, but not git branch new-branch-name to create a new branch in each repo.

So is there a way to run git so that it will abort instead of making anymodifications to the repo, but work fine when running read-only commands?

Edit: What I want to do is make a git-subdirs command that works like this: When I type git subdirs COMMAND ARGS, I want to do something like for dir in */; do cd $dir && git COMMAND ARGS && cd ..; done (only with additional error checking). Except that if git COMMAND ARGS would modify the repo in any way, I don't want to do it, because it's exceedingly unlikely that I would want to make the same change to many different repos.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温馨耳语 2024-10-14 19:10:43

这有点笨拙,但您可以简单地以没有存储库写入权限的用户身份运行脚本。为了避免输入密码,您可以使用 ssh 密钥对并以该用户身份 ssh 进入您自己的计算机。 (或者可以通过 sudoers 配置它?)

与白名单不同,这保证不会有任何意外漏洞或过度限制。它不需要实际修改存储库的权限,只需要修改对其进行操作的用户的权限。而且它不需要像 git-daemon 那样进行任何额外的设置。

(我想我应该注意到,没有内置的“只读”选项可以传递给 git。它只是尝试做一些事情,如果它有权限,就会做这些事情。)

It's a little kludgy, but you could simply run your script as a user who doesn't have write permissions on the repository. To keep from having to enter a password, you could use an ssh keypair and ssh into your own machine as that user. (or maybe configure it through sudoers?)

Unlike a whitelist, this is guaranteed not to have any accidental holes or overzealous restrictions. It doesn't require actually modifying the permissions of the repo, just the permissions of the user acting on it. And it doesn't take any extra setup like git-daemon would.

(I suppose I should note that there's no built-in "read-only" option to pass to git. It simply tries to do things, and if it has permission to, does them.)

一袭白衣梦中忆 2024-10-14 19:10:43

为什么不拒绝对存储库目录的写访问?

$ chmod -R -w repo.git

Why not deny write access to the repository directory?

$ chmod -R -w repo.git
流云如水 2024-10-14 19:10:43

也许您确实可以使用命令白名单。例如。您可以使用 git-show-ref 或 git-for-each-ref 代替 git-status 使用 git-diff-files 和 git-branch。确保检查低级(管道)命令。您可能会找到一个简单的、集中的低级命令来满足您的所有脚本需求。

Perhaps you can indeed use a whitelist of commands. Eg. instead of git-status use git-diff-files and git-branch you could use git-show-ref or git-for-each-ref. Make sure to check out the low-level (plumbing) commands. Likely you will find a simple focused low level command for all your scripting needs.

嘴硬脾气大 2024-10-14 19:10:43

我使用 unionfs-fuse 实现了这一点。这是完全不可移植的,但它在 Ubuntu 上对我有用。在包含所有 git 存储库的目录中使用 git subdirs status 进行尝试。

您可以从 Github 获取它: https://github.com/DarwinAwardWinner/git-custom-commands< /a>

I implemented this using unionfs-fuse. This is completely non-portable, but it works for me on Ubuntu. Try it out with git subdirs status in the directory that contains all your git repos.

You can get it from Github: https://github.com/DarwinAwardWinner/git-custom-commands

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文