如何检查远程文件是否存在?

发布于 2024-10-01 08:24:36 字数 127 浏览 4 评论 0原文

有没有办法检查指定相对路径下的文件是否存在于远程中?如果这是唯一的选择,我可以先获取信息。换句话说,我正在寻找带有指定远程和分支选项的 git-ls-files 。我只对文件是否存在感兴趣(分支上的文件列表也可以),我不关心哈希值、差异等。

Is there a way to check if a file under specified, relative path exist in a remote? I'm fine with fetching the info first if it's the only option. In other words I'm looking for git-ls-files with an option to specify remote and branch. I'm only interested if the file exists (a list of files on the branch will do as well), I don't care about hashes, diffs etc.

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

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

发布评论

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

评论(2

欲拥i 2024-10-08 08:24:36

您可以使用

git cat-file -e <remote>:<filename>

当文件存在时, 它将以零退出。您可以使用远程分支名称,而不是上面的 (但它实际上可以是任何树形对象引用)。要使用这样的远程分支,您需要配置并获取远程存储库(即通过使用git remote add + git fetch)。

一个具体的例子:

$ git cat-file -e origin/master:README && echo README exists
README exists

$ git cat-file -e origin/master:FAILME
fatal: Not a valid object name origin/master:FAILME

需要注意的两件事:

  • 使用/作为文件名中的路径分隔符,即使在Windows等系统上也是如此。
  • 是相对于存储库根目录的完整路径(例如 foo/bar/README)。

You can use

git cat-file -e <remote>:<filename>

which will exit with zero when the file exists. Instead of <remote> above you'd use a remote branch name (but it could in fact be any tree-ish object reference). To use such a remote branch, you'll need to have the remote repository configured and fetched (i.e. by using git remote add + git fetch).

A concrete example:

$ git cat-file -e origin/master:README && echo README exists
README exists

$ git cat-file -e origin/master:FAILME
fatal: Not a valid object name origin/master:FAILME

Two things to note:

  • Use / as path delimiter in filenames, even on e.g. Windows.
  • <filename> is a full path (such as foo/bar/README), relative to the root of the repository.
在巴黎塔顶看东京樱花 2024-10-08 08:24:36

您可以使用 git archive 访问单个文件,而无需下载存储库的任何其他部分:

if git archive --format=tar \
               --remote=<remote_name-or-URL> master README >/dev/null; then
  echo 'master has README'
else
  echo 'master does not have README (or other error)'
fi

存档服务 (upload-archive) 可能不会在所有服务器或存储库上启用,但您必须针对您要使用的服务器和存储库对其进行测试。需要访问。

如果存档服务不可用,您将必须通过正常方式获取对象。

如果您还没有对相关存储库进行远程设置,您可以对 FETCH_HEAD 进行“浅层”提取(这需要在 Git 存储库中完成,但它可以完全不相关,甚至是空的):

git fetch --depth=1 remote_name-or-URL master
if git rev-parse --verify --quiet FETCH_HEAD:README >/dev/null; then
  echo "repository's master has README"
else
  echo "repository's master does not have README"
fi

如果您有为存储库定义的远程,那么您可能只想更新它并通过正常的远程跟踪分支访问该文件:

git fetch remote_name
if git rev-parse --verify --quiet remote_name/master:README >/dev/null; then
  echo "remote's master has README"
else
  echo "remote's master does not have README"
fi

You can use git archive to access individual files without downloading any other part of a repository:

if git archive --format=tar \
               --remote=<remote_name-or-URL> master README >/dev/null; then
  echo 'master has README'
else
  echo 'master does not have README (or other error)'
fi

The archive service (upload-archive) may not be enabled on all servers or repositories though, you will have to test it for the servers and repositories that you need to access.

If the archive service is not available, you will have to fetch objects through normal means.

If you do not already have a remote setup for the repository in question you can do a “shallow” fetch into FETCH_HEAD (this needs to be done in a Git repository, but it can be completely unrelated or even empty):

git fetch --depth=1 remote_name-or-URL master
if git rev-parse --verify --quiet FETCH_HEAD:README >/dev/null; then
  echo "repository's master has README"
else
  echo "repository's master does not have README"
fi

If you have a remote defined for the repository, then you probably just want to update it and access the file through the normal remote-tracking branches:

git fetch remote_name
if git rev-parse --verify --quiet remote_name/master:README >/dev/null; then
  echo "remote's master has README"
else
  echo "remote's master does not have README"
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文