# History

Byte can keep a Git history for each snippet: every save from the workspace
records a commit, and the workspace's History view shows what each commit
changed and restores an earlier version.

## Turning it on

History is off by default. Turn it on with a filter, in a must-use plugin or
your theme's `functions.php`:

```php
add_filter('sleekByte/snippets/git', '__return_true');
```

While it is off, the History view stays empty, saving records nothing, and
`Actions_gitRead` and `Actions_gitWrite` answer 409.

## How it records

Git runs in the browser, in IndexedDB. When a save touches a snippet, the
workspace commits that snippet's files and writes the snippet's `.git`
directory back into its folder on the server through `Actions_gitWrite`. The
first commit of a snippet's history is an "Initial snapshot" of the snippet
as it was saved before; every save after it is one commit, whose message is
`Save at` and the time in ISO form, the same in every language.

The repository on the server is an ordinary one, so standard Git tools read
it:

```text
cd wp-content/uploads/sleek-byte/my-snippet
git log --oneline
```

Another browser, or the same one after its data was cleared, loads the
server's `.git` directory through `Actions_gitRead` the first time it uses a
snippet's history after the page loads. Only saves from the workspace commit:
files written through WP-CLI or by other means appear in the next commit the
workspace records. `.git` folders never appear in the file tree, and Byte does
not write them as snippet files.

## Viewing and restoring

**History**, beside **Snippets** at the top of the file tree, switches the
tree to the history of the snippet the open file belongs to. It lists the
commits, newest first, with their time; a save reads "Saved changes" there,
in your language. It follows the open file to another
snippet; a save adds its commit at once. **Snippets** switches back to the
files.

Choosing a commit opens it as a read-only tab beside the open files, named
after the commit. The tab shows the files the commit changed, each with its
added and removed line counts and its diff against the commit before it, not
against today's files. Other tabs bring the files back; the commit's tab stays
open until you close it.

**Restore this version**, in the commit tab's toolbar, puts that commit's
files into the editor as unsaved changes and closes the tab: files that
differ become changed, files the commit did not have become deletions. Save
to keep them, which records a new commit, or discard.

`Actions_gitCheck` reports whether history is on and which snippets hold a
`.git` directory; it looks only at the top-level folders of each source, so a
snippet in a nested folder is not listed there, although its history works.
