Add Dockerfile

This commit is contained in:
Fabien Potencier 2024-03-02 09:40:47 +01:00
parent 4aa9676740
commit 777bdc3250
2 changed files with 90 additions and 28 deletions

38
Dockerfile Normal file
View file

@ -0,0 +1,38 @@
FROM golang:1.22-bookworm AS builder
WORKDIR /build
RUN apt update && apt install -y pkg-config cmake
# Cache modules and git2go build
COPY go.mod go.sum ./
RUN go mod download
# Build git2go
RUN git clone https://github.com/libgit2/git2go vendor/github.com/libgit2/git2go/v34
RUN cd vendor/github.com/libgit2/git2go/v34 && git checkout v34.0.0 && git submodule update --init && make install-static
RUN mv vendor/github.com/libgit2/git2go/v34 git2go
# Copy the code
COPY .git main.go ./
COPY splitter splitter/
RUN go mod vendor
RUN rm -rf vendor/github.com/libgit2/git2go/v34
RUN mv git2go vendor/github.com/libgit2/git2go/v34
# Build
RUN go build -tags static -ldflags="-s -w -X 'main.version=$(git describe --tags)'" -o splitsh-lite ./main.go
# Prepare files for the final image
WORKDIR /dist
RUN cp /build/splitsh-lite ./splitsh-lite
# Add dependent libraries
RUN ldd splitsh-lite | tr -s '[:blank:]' '\n' | grep '^/' | xargs -I % sh -c 'mkdir -p $(dirname ./%); cp % ./%;'
# Create the runtime image
FROM scratch
COPY --from=builder /dist /
WORKDIR /data
ENTRYPOINT ["/splitsh-lite"]

View file

@ -34,35 +34,44 @@ a monorepo, use the [tomono](https://github.com/unravelin/tomono) tool.
Installation
------------
Manual Installation
-------------------
Docker (recommended)
--------------------
First, you need to install `libgit2`, preferably using your package manager of
choice.
The recommended way to use the splitter is via the official Docker image:
If you get `libgit2` version `1.5`, you're all set and jump to the compilation
step below. If not, you first need to change the `git2go` version used in the
code. Using the table on the
[libgit2](https://github.com/libgit2/git2go#which-go-version-to-use) repository,
figure out which version of the `git2go` you need based on the `liggit2` library
you installed. Let's say you need version `v31`:
Manual Installation (not recommended)
-------------------------------------
To build the binary , you first need to install `libgit2`, preferably using
your package manager of choice:
* Via brew:
```bash
brew install libgit2@1.5
```
* Via apt:
```bash
apt install libgit2-dev
```
Note that the last version of `libgit2` supported (by git2go) is 1.5.
If you get `libgit2` version `1.5`, you're all set and jump to the build step
below. If not, you first need to change the `git2go` version used in the code.
Using the table on the
[libgit2](https://github.com/libgit2/git2go#which-go-version-to-use)
repository, figure out which version of the `git2go` you need based on the
`liggit2` library you installed. Let's say you need version `v31`:
```bash
sed -i -e 's/v34/v31/g' go.mod splitter/*.go
go mod tidy
```
Then, compile `splitsh-lite`:
```bash
go build -o splitsh-lite github.com/splitsh/lite
```
If everything goes fine, a `splitsh-lite` binary should be available in the
current directory.
If you get errors about an incompatible `libgit2` library, try exporting the
needed flags, e.g.
On MacOS, export the following flags:
```bash
export LDFLAGS="-L/opt/homebrew/opt/libgit2@1.5/lib"
@ -70,7 +79,14 @@ export CPPFLAGS="-I/opt/homebrew/opt/libgit2@1.5/include"
export PKG_CONFIG_PATH="/opt/homebrew/opt/libgit2@1.5/lib/pkgconfig"
```
before running `go build`.
Then, build the `splitsh-lite` binary:
```bash
go build -o splitsh-lite github.com/splitsh/lite
```
If everything goes fine, a `splitsh-lite` binary should be available in the
current directory.
If you want to integrate splitsh with Git, install it like this (and use it via
`git splitsh`):
@ -82,14 +98,18 @@ cp splitsh-lite "$(git --exec-path)"/git-splitsh
Usage
-----
Let's say you want to split the `lib/` directory of a repository to its own
branch; from the "master" Git repository (bare or clone), run:
Let's say you want to split the `lib/` directory of a repository stored in the
current directory from the current branch (bare or clone), run:
```bash
# Docker
docker run --rm -v $PWD:/data splitsh-lite --prefix=lib/
# Binary
splitsh-lite --prefix=lib/
```
The *sha1* of the split is displayed at the end of the execution:
The command outputs the *sha1* of the split:
```bash
SHA1=`splitsh-lite --prefix=lib/`
@ -119,10 +139,13 @@ split a different branch by passing it explicitly via the `--origin` flag
splitsh-lite --prefix=lib/ --origin=origin/master
```
You don't even need to run the command from the Git repository directory if you
pass the `--path` option:
You don't even need to run the command from the Git repository directory:
```bash
# Docker
docker run --rm -v /path/to/repo:/data splitsh-lite --prefix=lib/ --origin=origin/1.0
# Binary
splitsh-lite --prefix=lib/ --origin=origin/1.0 --path=/path/to/repo
```
@ -133,7 +156,8 @@ Available options:
`--prefix=from:to` syntax; split several directories by passing multiple
`--prefix` flags;
* `--path` is the path of the repository to split (current directory by default);
* `--path` is the path of the repository to split (current directory by
default, or use the `-v` option of Docker when using the Docker image);
* `--origin` is the Git reference for the origin (can be any Git reference
like `HEAD`, `heads/xxx`, `tags/xxx`, `origin/xxx`, or any `refs/xxx`);