Extracting single file from remote hg repository

Sietse Brouwer sbbrouwer at gmail.com
Wed Aug 21 08:15:52 UTC 2019


(Apologies to Scott for getting this message twice, I forgot to copy the 
list the first time I sent this.)

Gregor wrote:
 > I need to extract a single file from a
 > remote repository in an automated process.
 > [...]
 > The solution has to work at least on Windows and Linux.

On 19/08/2019 20:33, Scott Palmer wrote:
 > I don’t know as I’ve never used it, but if you are using SSH,
 > can’t you just run "hg cat” via SSH?

I've just tried Scott's suggestion on a virtual machine, and it works. 
Use SSH to run a command on the remote host; in this case, the command 
to retrieve a file is `hg cat -R {path/to/repo} -r {revision} 
{filename}`. That writes the file to stdout on your local machine; to 
create a local copy of the file, redirect stdout to a file.

     ssh {user at host} \
         hg -R {path/to/repo} -r {revision} {filename} \
         > {local_filename}

This solution assumes an SSH client on both Linux and Windows, and the 
ability to redirect to stdout.


## Demonstration that it works:

```
~$ ssh vagrant at localhost hg -R /home/vagrant/testrepo cat README
zeven vlaamse reuzen

~$ ssh vagrant at localhost hg -R /home/vagrant/testrepo cat -r 1 README
zeven vlaamse reuzen

# In modern Mercurials, --cwd is a good alternative to -R:
# it can specify the repository *and* the directory to run in.
~$ ssh vagrant at localhost hg --cwd /home/vagrant/testrepo cat -r 0 README
aapje olifantje
```

## What doesn't work

I also tried passing a remote repo URL directly to the -R flag, but that 
does not work.

```
~$ hg -R ssh://vagrant@localhost//home/vagrant/testrepo cat README
abort: repository 'ssh://vagrant@localhost//home/vagrant/testrepo' is 
not local
```

Hope this helps,
Sietse



More information about the Mercurial mailing list