0

There is a local folder /var/www/test/

There is also a Git repository that has a build/static folder. I need to get files and folders from this folder in /var/www/test/

But as a result, I got that in /var/www/test/, build was created with a static folder inside. And I need static to be at the root of /var/www/test/ (i.e. /var/www/test/static)

How to do it right? Thanks in advance.

Tried like this:

cd /var/www/test/
git init
git remote add -f origin <repo_address>
git config core.sparseCheckout true
echo 'build/static' >> .git/info/sparse-checkout
git pull origin master
Bastien
  • 694
  • 5
  • 19
Andrew
  • 1
  • 1
    Does this answer your question? [Git sparse checkout without leading directories](https://stackoverflow.com/questions/35097530/git-sparse-checkout-without-leading-directories) – Joachim Sauer May 17 '23 at 11:29
  • 2
    Independent of your actual question I'd avoid having the `.git` path in anything that's meant to be served to the public. You can do that by having the checkout separately from the .git directory and/or by using git worktree. – Joachim Sauer May 17 '23 at 11:30

2 Answers2

1

You could create a git bare repo on your remote host outside of /var/www. Add a pre-receive hook to it that deploys the contents of a sub folder of your repo into /var/www/test/static. This example will expect /var/www/test/static to exist and be empty. Also build is assumed to be at the top level of your repository. You could experiment with other directory structures, but this works for me.

When you push to the remote bare repo, the pre-receive hook should deploy the files.

Notes:

  • If files are manually updated in /var/www/test/static, git will refuse to overwrite them.
  • A third party git hosting service is not required.
#!/bin/sh
# pre-receive script for custom read/write-tree deploy

while read old new ref; do [[ $ref = refs/heads/master ]] && {
    export GIT_INDEX_FILE="$GIT_DIR/deploy_index"
    export GIT_WORK_TREE="/var/www/test/static"
    git read-tree -um $(git write-tree) $new:build/static  || exit 1
}; done
Cole Tierney
  • 9,571
  • 1
  • 27
  • 35
  • The `$(git write-tree)` looks interesting, . . . ah. I get it.That's clever. Automating dealing with interference looks doable, commit afterwards, then if the read-tree fails,add, commit then merge, so manual updates are carried forward if possible. – jthill May 20 '23 at 02:20
0

I propose an approach that will avoid having .git directory (i.e. Git local repository metadata) in a folder that is meant to be served, because you probably don’t want to risk making your project history public.

It also will be easier to work with in the future.

  1. Make sure you don’t have a static folder in /var/www/test/ (remove it if existing)
  2. Clone your repository (sparsely or not, does not impact the outcome) somewhere else (not in /var/www/test/)
  3. Link your checked out static folder into /var/www/test/:
    ln -s /path-to-your-cloned/build/static /var/www/test/static
    
    (note: the -s option is for creating a symbolic link, you can instead make a hardlink if you need it for some reason by removing this option)

Done! Any time you pull, the new content will automatically be made available in /var/www/test/.

Bastien
  • 694
  • 5
  • 19