开发者

How to determine if a remote git repo is "safe" to pull from

开发者 https://www.devze.com 2023-02-28 06:33 出处:网络
Is it possible to determine that I can safely pull from a new remote repository, i.e. the two repositories share a common ancestry? I\'m writing a program that automates cloning and updating a large n

Is it possible to determine that I can safely pull from a new remote repository, i.e. the two repositories share a common ancestry? I'm writing a program that automates cloning and updating a large number of git, hg, etc. repositories, and I would like to seamlessly handle the ca开发者_如何学编程se where the remote repository moves to a new URL. It seems that git will allow me to pull from any remote, potentially creating massive conflicts. Svn has the repository uuid to determine if you can switch to a new url. I realize that git is distributed and meant to be easily forkable, so a uuid doesn't really make sense, but is there any comparable feature/function in git?


You can add and fetch the remote branch to your repository without merging it in to your history. You can then test to see if there's any common ancestor using git merge-base

git remote add test-remote foo@bar:/path/to/repo.git
git remote update
git merge-base test-remote/master master || echo "No common ancestors!"


I realize that git is distributed and meant to be easily forkable, so a uuid doesn't really make sense, but is there any comparable feature/function in git?

On the contrary, part of what makes of git's so useful as a distributed version control system is that every commit has an ID that uniquely defines both that commit and its complete history. (These are the hashes that you see associated with commits in git, properly known as "object names".) It doesn't matter which repository a commit has been fetched from or pushed to, it still represents exactly the same state of the tree and exactly the same history, including authorship, etc..

It's not quite clear to me from your question whether what you want to know is (a) if there's any common ancestry at all or (b) whether the history of branch A entirely contains the history of branch B (i.e. so that merging A into B would be a "fast-forward").

If you're after (a) then Brian Campbell's answer is what you want, i.e. essentially testing whether git merge-base test-remote/master master returns successfully.

On the other hand, if you're after (b) then you should test whether the output of git merge-base test-remote/master master is the same as git rev-parse --verify master.


A "pull" in git is just a "fetch" followed by a "merge" into the current branch. When you do a git "fetch" on a remote that has no shared ancestry, you get this warning:

warning: no common commits

You should be able to do a fetch from the URL, then detect this message from the output of that operation.

0

精彩评论

暂无评论...
验证码 换一张
取 消