Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git init #620

Merged
merged 8 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 88 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,91 @@ network: "testnet"
bee:
bee-api-endpoint: http://localhost:1633 # bee running on mainnet
postage-batch-id: <BATCH>
```
```

### Integrating git with dfs

To integrate git with dfs, we need to set the `git` configuration in the config file. We only need to set the credential helper for local git repositories.

We create a file `.dfs-credentials` with the following content at any given location
```
#!/bin/bash
token_file="<ABSOLUTE PATH FOR STRING ACCESS TOKEN>" # this needs to be absolute path

username="<USERNAME>"
password="<PASSWORD>"

dfs="<FAIROS-DFS SERVER URL>" # http://localhost:9090 for local running fairOS-dfs server

# Function to get the access token using the username and password
get_access_token() {
local response=$(curl -s -X POST "$dfs/v2/user/login" -H "Content-Type: application/json" -d "{\"userName\": \"$username\", \"password\": \"$password\"}")
access_token=$(echo "$response" | jq -r '.accessToken')
# check if response has access token
if [ "$access_token" == "null" ]; then
exit 1
fi
echo "$access_token" > "$token_file"
echo "$access_token"
}

get_saved_access_token() {
if [[ -f "$token_file" ]]; then
local saved_token=$(sed -n '1p' "$token_file")
if [ "$saved_token" == "null" ] || [ "$saved_token" == "" ]; then
return 1
fi
local response=$(curl --write-out '%{http_code}' --silent --output /dev/null -s -X POST "$dfs/v1/user/stat" -H "Content-Type: application/json" -H "Authorisation: Bearer $saved_token" )
# check if response had success http code
if [[ response -eq 200 ]]; then
echo "$saved_token"
return 0
else
rm "$token_file"
return 1
fi
fi
return 1
}

access_token=$(get_saved_access_token)
if [[ $? -ne 0 ]]; then
access_token=$(get_access_token)
fi
echo "username=$username"
echo "password=$access_token"

exit 0
```

After creating this file, we need to set the `credential.helper` in the git configuration

```
git config --local credential.helper "<Absolute path to .dfs-credentials file>"
```

#### How to push to dfs

Currently, we only support pushing once, so its sort of archive. We can push the git repository to dfs using the following command

```
git init # initialize the git repository, run this inside the directory that you want to push to dfs
git add . # add all the files to the git repository
git commit -m "Initial commit" # commit the changes
git remote add origin <DFS SERVER>/v1/git/<USERNAME>/<PODNAME>.git # add the remote origin
git config --local credential.helper "<Absolute path to .dfs-credentials file>" # set the credential helper
git push -u origin master # push the changes to the remote origin
```

### How to clone from dfs

```
git config --local credential.helper "<Absolute path to .dfs-credentials file>"
git clone <DFS SERVER>/v1/git/<USERNAME>/<PODNAME>.git
```

NOTE: Before pushing into a pod, we have to first create a pod if it does not exist. A pod can not be used as two different repos.
Dfs stores the files of the repo as a git pack file. So, we cannot access each file of the repo individually. although we can access other files from dfs api as expected.



2 changes: 1 addition & 1 deletion cmd/dfs/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func startDevServer() {
dfsApi := dfs.NewMockDfsAPI(mockClient, users, logger)
handler = api.NewMockHandler(dfsApi, logger, []string{"http://localhost:3000"})
defer handler.Close()
httpPort = ":9090"
httpPort = ":9093"
pprofPort = ":9091"
srv := startHttpService(logger)
fmt.Printf("Server running at:http://127.0.0.1%s\n", httpPort)
Expand Down
7 changes: 7 additions & 0 deletions cmd/dfs/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,13 @@ func startHttpService(logger logging.Logger) *http.Server {
docRouter.HandleFunc("/entry/get", handler.DocEntryGetHandler).Methods("GET")
docRouter.HandleFunc("/entry/del", handler.DocEntryDelHandler).Methods("DELETE")

gitRouter := baseRouter.PathPrefix("/git/").Subrouter()
gitRouter.Use(handler.GitAuthMiddleware)
gitRouter.HandleFunc("/{user}/{repo}.git/HEAD", handler.GitInfoRef).Methods("GET")
gitRouter.HandleFunc("/{user}/{repo}.git/info/refs", handler.GitInfoRef).Methods("GET")
gitRouter.HandleFunc("/{user}/{repo}.git/git-upload-pack", handler.GitUploadPack).Methods("POST")
gitRouter.HandleFunc("/{user}/{repo}.git/git-receive-pack", handler.GitReceivePack).Methods("POST")

var origins []string
for _, c := range corsOrigins {
c = strings.TrimSpace(c)
Expand Down
31 changes: 17 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module github.com/fairdatasociety/fairOS-dfs
go 1.21

require (
github.com/btcsuite/btcd/btcec/v2 v2.3.2
github.com/btcsuite/btcd/btcec/v2 v2.3.3
github.com/c-bata/go-prompt v0.2.6
github.com/dustin/go-humanize v1.0.1
github.com/ethereum/go-ethereum v1.13.14
github.com/ethereum/go-ethereum v1.14.0
github.com/ethersphere/bee/v2 v2.0.0
github.com/ethersphere/bmt v0.1.4
github.com/fairdatasociety/fairOS-dfs-utils v0.0.0-20221230123929-aec4ed8b854d
Expand All @@ -25,15 +25,15 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
github.com/swaggo/http-swagger v1.3.4
github.com/swaggo/swag v1.16.3
github.com/tinygrasshopper/bettercsv v0.0.1
github.com/tyler-smith/go-bip39 v1.1.0
github.com/wealdtech/go-ens/v3 v3.6.0
go.uber.org/goleak v1.3.0
golang.org/x/crypto v0.19.0
golang.org/x/term v0.17.0
golang.org/x/crypto v0.22.0
golang.org/x/term v0.19.0
gopkg.in/yaml.v2 v2.4.0
resenje.org/jsonhttp v0.2.3
)
Expand All @@ -52,11 +52,11 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/ethereum/c-kzg-4844 v0.4.0 // indirect
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
github.com/ethersphere/bee v1.10.0 // indirect
github.com/ethersphere/go-price-oracle-abi v0.2.0 // indirect
github.com/ethersphere/go-storage-incentives-abi v0.6.2 // indirect
Expand All @@ -73,7 +73,8 @@ require (
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.11.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
Expand Down Expand Up @@ -110,6 +111,7 @@ require (
github.com/multiformats/go-multihash v0.2.3 // indirect
github.com/multiformats/go-multistream v0.4.1 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/onsi/gomega v1.27.10 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand All @@ -120,6 +122,7 @@ require (
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
Expand All @@ -143,13 +146,13 @@ require (
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.15.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
golang.org/x/tools v0.20.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.2.1 // indirect
Expand Down
Loading
Loading