11

I use some of Xcode's source control features, specifically the blame feature in the main editor, but use git on the command line for all of my actual version control. This means that, fairly often, git commands on the command line fail with the message:

fatal: Unable to create '/path/to/repo/.git/index.lock': File exists.

Xcode is creating this file to lock the repo as it runs its own git commands. I've already turned off all the unnecessary source control options in Preferences (Refresh local status automatically, Refresh server status automatically, and Add and remove files automatically.)

My current strategy is just to retry the command until it works, which rarely takes more than one attempt.

Is there a way to make Xcode any less aggressive with how often it creates index.lock?

Or, is there a way I can make git automatically retry commands until they succeed if they fail in this way?

5 Answers 5

5

Here's my hacky patchy for this: a few lines of bash which waits till the lock file disappears and then runs your git commands. Race conditions are certainly possible, this isn't bullet proof but would work most of the time.

function retry {
    while [ 1 == 1 ]; do
        if test -f /path/to/repo/.git/index.lock; then
            echo -n ".";
            sleep 0.5;
        else
            "$@";
            return;
        fi  
    done
}

You could throw this into your ~/.bashrc for example then to use it:

retry git commit -m "ohhh yeaaaa"

Cheers!

3
+125

I can make git automatically retry commands until they succeed if they fail in this way?

Not with native git: you would need to script a git command wrapper which will loop and essentially wait for index.lock to disappear (which is similar to the wait strategy described in the answers of "Git - fatal: Unable to create '/path/my_project/.git/index.lock': File exists")

That wrapper could:

  • include a timeout in order to not wait indefinitely,
  • try and search for a git process when the timeout and try and kill it, in order to finally free that lock.

That is what, for instance, an IDE like SublimeText does

root = git_root(self.get_working_dir())
if wait_for_lock and root and os.path.exists(os.path.join(root, '.git', 'index.lock')):
    print("waiting for index.lock", command)
    do_when(lambda: not os.path.exists(os.path.join(root, '.git', 'index.lock')),
        self.run_command, command, callback=callback, show_status=show_status, filter_empty_args=filter_empty_args, no_save=no_save, wait_for_lock=wait_for_lock, **kwargs)
2

Like others said there is nothing native to git but the Chromium devs did a python script called git-retry that is called by a shell wrapper.

Instead of typing for example:

git commit you would type git-retry commit

You can get the tool from their tools repository by cloning it:

https://chromium.googlesource.com/chromium/tools/depot_tools.git

0

I used to use Xcode's git client, and it was consistently causing errors including this one, so I've stopped using Xcode to setup my workspaces or make commits. To enable this setup, I installed the git support from the Command Line Tools for Xcode X.x, and refer to an external git GUI for any commands that result in a change to my local or remote copy, including switching branches.

Since I've started using an external client, I've had no problems with an index.lock file. When performing rare operations, like resetting a remote branch, or renaming a branch, I use the terminal. There are a few high-quality git clients out there that are free, including SourceTree.

If you want to use an external git client with a new project in Xcode, simply do not opt-in to the git workspace option, if it is displayed. Instead, use your external git client to 'Add' an additional git workspace, and choose the root directory of the Xcode project.

As a side note, when I do this, I usually have a root directory for a lot of related miscellaneous files, and that is also the home of my Xcode project directory which is the root of my git workspace. This allows me to have notes, and other files that I don't want to be version-controlled.

Here's a typical setup:

    + Project Directory
    ---+ Xcode project directory (git root)
    ---- Other project files (not under source control)

Hope that helps!

-1

Since this file is created and removed by git itself.
If you wish to remove it when its created you will need to set some "watch" on the file and when you see it to remove it.

A simple solution is to install some "watch" plugin which will "capture" when the file is created.

There are plenty of libraries and tools for this task.


Here is a list of few:

inotify-tools

inotify-tools is a C library and a set of command-line programs for Linux providing a simple interface to inotify. These programs can be used to monitor and act upon filesystem events


fswatch

fswatch is a file change monitor that receives notifications when the contents of the specified files or directories are modified.

Usage example:

fswatch -o ~/path/to/watch | xargs -n1 ~/script/to/run/when/files/change.sh

watchdog

Python API and shell utilities to monitor file system events.

You can use the shell-command subcommand to execute shell commands in response to events:

watchmedo shell-command \
    --patterns="*.lock" \
    --recursive \
    --command='rm index.lock' \
    .


You are a mac user but windows user can install any of the following programs which will monitor when file is created

https://www.raymond.cc/blog/3-portable-tools-monitor-files-folders-changes/

1
  • This is unsafe, because the lock file exists for a reason. You could corrupt your git repository! Commented Sep 6, 2016 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.