0

I have a script that bootstraps a development environment from a git repository. The script lives in the same repo I want to bootstrap from. I would like to set up an alias that just does the bootstrap without exposing the details fo git clone/checkout or the repo since it will be used by ops people to deploy (I don't want them to have to worry about these details).

How can I get just this script, without having to clone/checkout/etc? I.e. I don't want to require a working copy just to get this one file.

In my svn days 'svn cat' did what I'm asking about here.

When I have a working copy I know how to use 'git show' to do something like this, but I don't know how to give the repo URL, branch and optional tag to git show. I also have seen mention of git-cat-file, but same problem there. I don't care which command, these two are just the most promising ones I've found. I don't want to require gitweb (if that would even help here) would like to do this with just git proper.

Thanks!

  • 1
    possible duplicate of [How to retrieve a single file from specific revision in Git?](http://stackoverflow.com/questions/610208/how-to-retrieve-a-single-file-from-specific-revision-in-git) – Matt S Aug 19 '14 at 14:41
  • yes possble dupicate .. try this git show commit_sha_id:path/to/some/file.cs – Raja Simon Aug 19 '14 at 14:42
  • 1
    http://stackoverflow.com/a/2467629/1147772 – Drax Aug 19 '14 at 14:43
  • To share code between the two projects it's usually best to make a 3rd repo and include it as an external dependency of your project. – Matt S Aug 19 '14 at 14:44
  • Thanks, I saw that possible duplicate post, but AFAIK that method requires an initial git clone . The clone is what I want to hide/abstract away. – Derek Davies Aug 19 '14 at 14:46

1 Answers1

0

In order to extract the file from the repository, you must have access to the repository. It's really that simple: you must have (at least enough of) the entire ball of hair so that you can get all the parts that make up one file. (In particular, you need the object file, if there is one, or the pack-file that contains the object, likely delta-compressed against other parts of the pack-file.) You do not need a work tree, but you do need a clone (though possibly a "shallow" one, depending on whether and how much you want to reach back in history, and possibly remote).

You can hide this by putting the repo somewhere behind the scenes, or even on another machine and using some secret interface to quietly extract the file from that other machine and bring it over—that's what gitweb does, in fact: the clone is on the web server and the "secret" interface is plain old http—but you must have access to the repo. In the answer Drax linked in a comment, the "secret interface" is any useable git transport (git:, ssh:, etc)—i.e., the same thing you would use to make a clone of the repository. The git archive command gives you shell-script-able read-only access to the remote repository, although its output format is a tar or zip archive (so you must extract the file(s) you want).

The oddball part of your question is this bit:

How can I get just this script, without having to clone/checkout/etc? I.e. I don't want to require a working copy just to get this one file.

and that's due to the phrase "working copy". Git uses "work tree" as a technical term, describing its three-part setup: the repository (or clone) itself, the "index" or "staging area", and the "work tree". It's not clear whether you mean "work tree" when you say "working copy".

Again, you need (access to) the repository: a clone, or access to one. You do not need a work tree. To extract a file whose path is filepath from a repository in repopath, from the revision named by rev, use git --git-dir=repopath show rev:filepath to extract the file to standard output. Any revision specifier from gitrevisions is suitable, so you may use a branch or tag name, or HEAD, or a raw commit ID: git --git-dir=/some/path/to/repo.git show deploy:path/to/file.txt for instance. (The repopath is relative to the repository, and may refer to a bare clone.)

To use any git transport to access a remote repository and extract a single file, use the git archive command as shown in that other answer, e.g., instead of git show deploy:path/to/file.txt, you might use git archive --remote=url deploy:path/to file.txt | tar xf - (which will extract one file into ./file.txt), or git archive --remote=url deploy: path/to/file.txt | tar xf - (which will extract the same file but into ./path/to/file.txt).

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775
  • I guess. The thing that bothers me here is that I have full access to the remote repository. Are you saying I /must/ clone the repo to "cat" a single file? I guess I can do that and then remove the local repository but it just seems unbelievably silly to me. – Derek Davies Aug 21 '14 at 03:14
  • No, you just need *access to* a clone. If you can get to the remote (via `git archive`, or `ssh ...`, or whatever) you can extract the interesting file(s) and bring them over. The most convenient ways simply occur when the clone is present locally. – torek Aug 21 '14 at 03:25
  • torek -- that's what I'm hoping for -- can you give an example command? Thank you! – Derek Davies Aug 22 '14 at 04:26
  • There are several `git archive` examples in the last paragraph of the answer above. – torek Aug 22 '14 at 05:02
  • Ahh! I missed the gem -- exactly what I'm looking for. Thank you all! – Derek Davies Aug 22 '14 at 17:43