Skip to content

Commit 5427b3d

Browse files
committed
Rewrite the README around an example
The README opened on the drivers, then installing gitlib, before any code. It now starts with badges, a line on what gitlib is, and an example reading a log, a diff and a blame, which runs as it is. Then what gitlib does, with links to the documentation, its site, the two drivers, how to install it, and the license.
1 parent ff02401 commit 5427b3d

1 file changed

Lines changed: 70 additions & 57 deletions

File tree

‎README.md‎

Lines changed: 70 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,58 @@
1-
Gitlib for Gitonomy
2-
===================
1+
gitlib
2+
======
33

4-
This library provides methods to access Git repository from PHP 8.4+.
4+
[![Tests](https://github.com/gitonomy/gitlib/actions/workflows/tests.yml/badge.svg)](https://github.com/gitonomy/gitlib/actions/workflows/tests.yml)
5+
[![Latest Stable Version](https://img.shields.io/packagist/v/gitonomy/gitlib)](https://packagist.org/packages/gitonomy/gitlib)
6+
[![Total Downloads](https://img.shields.io/packagist/dt/gitonomy/gitlib)](https://packagist.org/packages/gitonomy/gitlib)
57

6-
By default, it runs the `git` binary (1.8.4 or later) for every operation: convenient, and it
7-
doesn't need anything built to use. It can instead run [libgit2](https://libgit2.org/) in-process
8-
via PHP's `FFI` extension, an experimental driver which is faster for small reads but slower on
9-
long histories: see
10-
["Choosing a driver"](doc/repository.md#choosing-a-driver) and
11-
["Performance"](doc/repository.md#performance).
8+
Access git repositories from PHP 8.4+, through the `git` binary or libgit2.
129

13-
Quick Start
14-
-----------
10+
```php
11+
use Gitonomy\Git\Repository;
1512

16-
You can install gitlib using [Composer](https://getcomposer.org/). Simply require the version you need:
13+
$repository = new Repository('/path/to/repository');
1714

18-
```bash
19-
$ composer require gitonomy/gitlib
20-
```
15+
// The last 5 commits of main that changed src/
16+
foreach ($repository->getLog('main', 'src', limit: 5) as $commit) {
17+
echo $commit->getShortHash().' '.$commit->getSubjectMessage().PHP_EOL;
18+
}
2119

22-
or edit your `composer.json` file by hand:
20+
// The files the last commit changed
21+
foreach ($repository->getHeadCommit()->getDiff()->getFiles() as $file) {
22+
echo $file->getName().': +'.$file->getAdditions().' -'.$file->getDeletions().PHP_EOL;
23+
}
2324

24-
```json
25-
{
26-
"require": {
27-
"gitonomy/gitlib": "^2.0"
28-
}
25+
// Who wrote the first lines of README.md
26+
foreach ($repository->getBlame('main', 'README.md', 1, 3)->getLines() as $line) {
27+
echo $line->getCommit()->getAuthorName().': '.$line->getContent().PHP_EOL;
2928
}
3029
```
3130

32-
Upgrading from 1.x? Read [UPGRADE-2.0.md](UPGRADE-2.0.md).
33-
34-
Example Usage
35-
-------------
31+
The documentation is at **[gitonomy.github.io/gitlib](https://gitonomy.github.io/gitlib/)**.
3632

37-
```php
38-
<?php
33+
What it does
34+
------------
3935

40-
use Gitonomy\Git\Repository;
41-
42-
$repository = new Repository('/path/to/repository');
36+
- **Open** a bare repository, a working tree, a linked worktree or a submodule, or
37+
**create** one: init, clone, mirror. See [Opening a repository](doc/repository.md) and
38+
[Creating repositories](doc/admin.md).
39+
- **Read** commits, logs, trees, blobs, diffs, blames, branches and tags, as PHP objects.
40+
- **Change** the working copy: checkout, new branches, merges, and the diffs of what is
41+
pending or staged. See [Working copy](doc/workingcopy.md).
42+
- **Manage** the hooks of a repository: see [Hooks](doc/hooks.md).
43+
- **Run** any other git command: see
44+
[Running git commands](doc/repository.md#running-git-commands).
4345

44-
foreach ($repository->getReferences()->getBranches() as $branch) {
45-
echo '- '.$branch->getName().PHP_EOL;
46-
}
47-
48-
echo $repository->getHeadCommit()->getSubjectMessage().PHP_EOL;
46+
Two drivers
47+
-----------
4948

50-
// Any git command, with the default driver only
51-
$repository->run('fetch', ['--all']);
52-
```
49+
`Repository` runs git through a driver, its last argument:
5350

54-
The last argument of `Repository` is the driver running git. It is `CliDriver`, which runs the
55-
`git` binary, by default. To run libgit2 instead, pass a `LibGit2Driver`: everything above works
56-
the same, except `run()`, which needs the `git` binary.
51+
- `CliDriver`, the default, runs the `git` binary, 1.8.4 or later, and needs nothing else;
52+
- `LibGit2Driver`, experimental, runs [libgit2](https://libgit2.org/) 1.9 in-process, through
53+
PHP's `FFI` extension. It is faster for many small reads in a long-running process, and
54+
slower on long histories, like blames: see
55+
["Choosing a driver"](doc/repository.md#choosing-a-driver).
5756

5857
```php
5958
use Gitonomy\Git\Driver\Ffi\LibGit2Driver;
@@ -62,26 +61,40 @@ use Gitonomy\Git\Repository;
6261
$repository = new Repository('/path/to/repository', driver: new LibGit2Driver());
6362
```
6463

65-
API Documentation
66-
-----------------
67-
68-
+ [Blame](doc/blame.md)
69-
+ [Blob](doc/blob.md)
70-
+ [Branch](doc/branch.md)
71-
+ [Commit](doc/commit.md)
72-
+ [Creating repositories](doc/admin.md)
73-
+ [Diff](doc/diff.md)
74-
+ [Hooks](doc/hooks.md)
75-
+ [Log](doc/log.md)
76-
+ [References](doc/references.md)
77-
+ [Repository](doc/repository.md): opening a repository, drivers, errors
78-
+ [Revision](doc/revision.md)
79-
+ [Tree](doc/tree.md)
80-
+ [Working Copy](doc/workingcopy.md)
64+
Installation
65+
------------
66+
67+
```bash
68+
composer require gitonomy/gitlib
69+
```
70+
71+
Upgrading from 1.x? Read [UPGRADE-2.0.md](UPGRADE-2.0.md) and the [changelog](CHANGELOG.md).
72+
73+
Documentation
74+
-------------
75+
76+
- [Opening a repository](doc/repository.md): paths, drivers, errors, git configuration
77+
- [Creating repositories](doc/admin.md)
78+
- [Revisions](doc/revision.md)
79+
- [Commits](doc/commit.md)
80+
- [Log](doc/log.md)
81+
- [Trees](doc/tree.md)
82+
- [Blobs](doc/blob.md)
83+
- [Diffs](doc/diff.md)
84+
- [Blame](doc/blame.md)
85+
- [Tags and branches](doc/references.md)
86+
- [Branches](doc/branch.md)
87+
- [Working copy](doc/workingcopy.md)
88+
- [Hooks](doc/hooks.md)
8189

8290
For Enterprise
8391
--------------
8492

8593
Available as part of the Tidelift Subscription
8694

8795
The maintainers of `gitonomy/gitlib` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/packagist-gitonomy-gitlib?utm_source=packagist-gitonomy-gitlib&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
96+
97+
License
98+
-------
99+
100+
gitlib is released under the [MIT license](https://github.com/gitonomy/gitlib/blob/main/LICENSE).

0 commit comments

Comments
 (0)