Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Explore objects in a remote

Tags:

git

git-remote

I'm doing some advanced usage of Git, querying objects in order to make optimizations of test runs of my code, so bear with me if the following sounds very far from the usage that most people do with Git.

I want to do something like git cat-file -p [...], except on objects on a remote, without fetching them. i.e., I want to say, "On remote origin, show me commit A, specifically tell me what's the id of its tree; then show me the contents of the tree (list of blobs and subtrees.)" I don't need to fetch actual contents of files, just the information above.

I know I could just fetch the commit from the remote to my local repo and use git cat-file on it, but then it'll involve fetching all the parents of the commit from the remote, which can take a long time. I need this to be quick because it's done in a program that should run hundreds of times a day on many different repositories.

Is there a way to do the above without fetching the commit?

like image 796
Ram Rachum Avatar asked Feb 26 '17 18:02

Ram Rachum


People also ask

How do I see remote files in git?

You can list all references on remote with "git ls-remote <URL>". "git archive --remote=<URL> HEAD". "git clone --depth=1 <URL>". If server provides git web interface to repository, you can use it to browse.

How do I pull from a remote repository?

The content of the multiple remote repositories can be pulled to the local drive by using the command, `git pull origin` or `git pull upstream`.

How do I checkout a remote local branch?

In order to checkout a remote branch you have to first fetch the contents of the branch. In modern versions of Git, you can then checkout the remote branch like a local branch. Older versions of Git require the creation of a new branch based on the remote .

How to add a remote to a git repository?

The git remote add command allows you to add a remote to a Git repository. If you encounter a “fatal” error when running the command, you should choose a name for your new remote or rename or delete the existing remote with the name you want to use. Now you’re ready to start using the git remote add command line operation like an expert!

What does gitgit remote do?

git remote The "remote" command helps you to manage connections to remote repositories. It allows you to show which remotes are currently connected, but also to add new connections or remove existing ones.

How do I check if a remote exists in Git?

You can check which remotes currently exist with the git remote -v command: This error means that the remote name you want to use already exists. To solve this, either use a different remote name, or rename the original remote. Use the git remote rm command to remove a remote URL from your repository.

How do I see all URLs in a git repository?

You can also specify -v, which shows you the URLs that Git has stored for the shortname to be used when reading and writing to that remote: If you have more than one remote, the command lists them all. For example, a repository with multiple remotes for working with several collaborators might look something like this.


1 Answers

I assume that you already verified that git fetch --depth=... does not do what you want.

If you have ssh/scp access to remote, you can simply grab the indiviudal file for that commit (if the commit as the hash a1b2c3d4, it is stored inside the file objects/a1/b2c3d4). Place it in your local .git/objects under the same directory/name. Then use the local git cat-file as usual. From its output, you can parse the hash of the tree, and go on from there, fetching individual object files until done. git cat-file does not care about any missing bits, if you have a hash for which the file exists, it will happily output its content.

To avoid problems in your "real" local repository, you can do this in an empty repos, i.e. one you just created with git init tmprepos or whatever. It does not hurt that there is nothing else in there.

EDIT: git stores some objects inside pack files. See https://git-scm.com/book/en/v1/Git-Internals-Transfer-Protocols for instructions how to get at them.

like image 67
AnoE Avatar answered Oct 09 '22 02:10

AnoE