~kornellapacz/gmnigit

38fe8bd9b2148c5f3b03ccf5de2a51a405199431 — Korneliusz Łapacz 5 months ago 3d32dc6
use command line flags
4 files changed, 37 insertions(+), 19 deletions(-)

M README.md
M commits.go
M files.go
M main.go
M README.md => README.md +11 -6
@@ 8,7 8,7 @@ This repository is available at [sourcehut](https://git.sr.ht/~kornellapacz/gmni

Unfortunately because of `replace` directive in `go.mod` you cannot simply get binary by running `go get` but you can install it by running

```sh
```
git clone https://git.sr.ht/~kornellapacz/gmnigit
cd gmnigit
go build -o gmnigit


@@ 18,14 18,19 @@ place `gmnigit` binary somewhere in your `$PATH`.

## Usage

(optional) in root of bare git repository put file called `url` with url for cloning, for example
```
echo "https://git.sr.ht/~kornellapacz/gmnigit" > url
Usage of gmnigit:
  -dist string
        destination path (if exists will be overwritten)
  -repo string
        path to repository
```

if the destination folder exists it will be deleted!
### Specifying url for cloning (optional)

In root of bare git repository put file called `url` with url for cloning, for example
```
gmnigit ./path/to/repository/dir ./path/to/dest/dir
echo "https://git.sr.ht/~kornellapacz/gmnigit" > url
```

## Tips


@@ 37,6 42,6 @@ You can automate building pages on push with git `post-receive` hook
while read -r _ _ ref; do
	[ "$ref" != "refs/heads/master" ] && continue

	gmnigit . /dest/dir
	gmnigit --repo . --dist /dest/dir
done
```

M commits.go => commits.go +1 -1
@@ 55,7 55,7 @@ func iterateOverCommits(r *git.Repository, callback func(*object.Commit, *object
	check(err)
}

func createCommitsFiles(distPath string, repository *git.Repository) {
func createCommitsFiles(repository *git.Repository) {
	commitsPath := filepath.Join(distPath, commitsSubPath)
	check(os.Mkdir(commitsPath, os.ModePerm))


M files.go => files.go +2 -2
@@ 71,7 71,7 @@ func arrangeDirectory(root, path string) {
	check(index.Close())
}

func createBrowsableTree(distPath, repositoryPath string) {
func createBrowsableTree() {
	treePath := filepath.Join(distPath, treeSubPath)

	_, err := git.PlainClone(treePath, false, &git.CloneOptions{


@@ 84,7 84,7 @@ func createBrowsableTree(distPath, repositoryPath string) {
	arrangeDirectory(treePath, "")
}

func createIndexFile(distPath, repositoryPath string) {
func createIndexFile() {
	// check if in bare repository is `url` file with git url for cloning
	url, err := os.ReadFile(filepath.Join(repositoryPath, "url"))


M main.go => main.go +23 -10
@@ 1,6 1,7 @@
package main

import (
	"flag"
	"log"
	"os"
	"sync"


@@ 15,15 16,21 @@ const (
	commitsSubPath = "commits"
)

func check(err error) {
	if err != nil {
		log.Fatal(err)
	}
}
var (
	repositoryPath string
	distPath       string
)

func main() {
	repositoryPath := os.Args[1]
	distPath := os.Args[2]
	flag.StringVar(&repositoryPath, "repo", "", "path to repository")
	flag.StringVar(&distPath, "dist", "", "destination path (if exists will be overwritten)")

	flag.Parse()

	if repositoryPath == "" || distPath == "" {
		flag.Usage()
		return
	}

	if err := os.RemoveAll(distPath); err != nil {
		log.Fatalf("cannot remove dist directory: %s", err)


@@ 41,9 48,15 @@ func main() {

	wg.Add(1)

	go createCommitsFiles(distPath, repository)
	createBrowsableTree(distPath, repositoryPath)
	createIndexFile(distPath, repositoryPath)
	go createCommitsFiles(repository)
	createBrowsableTree()
	createIndexFile()

	wg.Wait()
}

func check(err error) {
	if err != nil {
		log.Fatal(err)
	}
}