schuess
schuess

Reputation: 1079

Temporarily lock a git repository

Do you know if there is a way to block committing to a local git repository? In my build environment, I'm running some git commands through it and it breaks if the user makes a commit during the build.

I would like to lock the repository while the build is in progress then unlock when the build is finished.

Thanks!

Upvotes: 12

Views: 11992

Answers (3)

Tzunghsing David Wong
Tzunghsing David Wong

Reputation: 1424

Look into .git/hooks/pre-commit.sample. By manipulating this hook, you should be able to disable commit operation on remote or local git repository.

Upvotes: 5

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84453

TL;DR

That is not a Git-compatible work-flow. If you want an SCM with locking, use something else with a centralized-repository model. Some SCMs that support optional locks include SVN, CVS, and RCS.

Don't Push to Non-Bare Repositories

Unless you are using a shared repository configured with core.sharedRepository enabled, you should be pushing to a bare repository and running any continuous integration or build systems off a non-bare clone. By definition, any shared repository can have the working directory modified by anyone with the necessary permissions, so you shouldn't be using a shared repository if you need to ensure a stable working directory during the lifetime of some process.

There are certainly some use cases for the shared repository model. However, yours is not one of them.

Abusing Shared Repositories with Semaphores

You could certainly design your own scripts to abuse the shared-repository model. For example, you might use semaphore files (such as those created by lockfile from the lockfile-progs package) or flock in a wrapper script that turns off write or execute permissions on your shared directory before kicking off some long-running build process. That's a lot of work for a very small pay-off, since you could get the same benefit with less work with a clone. However, it is technically feasible, so I mention it here for completeness.

Upvotes: 5

SLaks
SLaks

Reputation: 888293

You should clone the repository and do your work in the fork.
Forks on the local disk can use symlinks to save time and space.

Git does not have any notion of a lock.

Upvotes: 2

Related Questions