Skip to content

Commit 00de6e9

Browse files
committed
refactoring and branches api
1 parent d57a883 commit 00de6e9

File tree

13 files changed

+121
-83
lines changed

13 files changed

+121
-83
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ build:
44

55
run:
66
go mod tidy
7-
go run main.go serve
7+
go install github.com/cosmtrek/air@latest
8+
air serve
89

910
icon:
1011
go install github.com/akavel/rsrc@latest

app/git/rembran.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,5 @@ func RemoteBranches(path string) []*plumbing.Reference {
1818
InsecureSkipTLS: true,
1919
})
2020

21-
// Print the remote branches
22-
// for _, ref := range refs {
23-
// if ref.Name().IsRemote() {
24-
// fmt.Println(ref.Name().Short())
25-
// }
26-
// }
27-
2821
return refs
2922
}

app/hdlr/branches.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package hdlr
2+
3+
import (
4+
"encoding/json"
5+
"github.com/gorilla/mux"
6+
"github.com/typomedia/gitti/app/git"
7+
"github.com/typomedia/gitti/app/helper"
8+
"github.com/typomedia/gitti/app/msg"
9+
"net/http"
10+
)
11+
12+
func Branches(w http.ResponseWriter, r *http.Request) {
13+
vars := mux.Vars(r)
14+
project := vars["project"]
15+
16+
// Get the repo from config
17+
repo := helper.GetRepo(project)
18+
19+
// Fetch the latest changes from remote
20+
git.Fetch(repo.Path)
21+
22+
// Get a list of all the remote branches
23+
refs := git.RemoteBranches(repo.Path)
24+
25+
branches := Refs{}
26+
for _, ref := range refs {
27+
if ref.Name().IsBranch() {
28+
branches.Branches = append(branches.Branches, Branch{
29+
Name: ref.Name().Short(),
30+
Revision: ref.Hash().String(),
31+
})
32+
}
33+
}
34+
35+
//json.NewEncoder(w).Encode(branches)
36+
jsonData, err := json.Marshal(branches)
37+
msg.Check(err)
38+
39+
// set application/json header
40+
w.Header().Set("Content-Type", "application/json")
41+
w.Write(jsonData)
42+
}

app/hdlr/checkout.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,21 @@ import (
44
"fmt"
55
"github.com/go-git/go-git/v5/plumbing"
66
"github.com/gorilla/mux"
7-
"github.com/spf13/viper"
87
"github.com/typomedia/gitti/app"
98
"github.com/typomedia/gitti/app/git"
109
"github.com/typomedia/gitti/app/git/ext"
10+
"github.com/typomedia/gitti/app/helper"
1111
"github.com/typomedia/gitti/app/msg"
1212
"github.com/typomedia/gitti/app/str"
1313
"net/http"
1414
)
1515

1616
func Checkout(w http.ResponseWriter, r *http.Request) {
17-
repo := Repo{}
1817
vars := mux.Vars(r)
1918
project := vars["project"]
20-
config := viper.AllSettings()
21-
repos := config["repos"].(map[string]interface{})
22-
23-
for name, path := range repos {
24-
if name == project {
25-
repo = Repo{
26-
Name: name,
27-
Path: path.(string),
28-
}
29-
}
30-
}
19+
20+
// Get the repo from config
21+
repo := helper.GetRepo(project)
3122

3223
branch := "master"
3324
if r.URL.Query().Get("branch") != "" {

app/hdlr/index.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,22 @@ package hdlr
22

33
import (
44
"fmt"
5-
"github.com/spf13/viper"
65
"github.com/typomedia/gitti/app"
76
"github.com/typomedia/gitti/app/git"
7+
"github.com/typomedia/gitti/app/helper"
88
"net/http"
99
)
1010

1111
func Index(w http.ResponseWriter, r *http.Request) {
12-
repo := Repo{}
13-
config := viper.AllSettings()
14-
repos := config["repos"].(map[string]interface{})
15-
1612
fmt.Fprintln(w, app.App.Banner)
1713
fmt.Fprintln(w, app.App.Name, app.App.Version)
1814
fmt.Fprintln(w, app.App.Author)
1915
fmt.Fprintf(w, "%v \n\n", app.App.Description)
2016

21-
for name, path := range repos {
22-
repo = Repo{
23-
Name: name,
24-
Path: path.(string),
25-
}
17+
// Get the repo from config
18+
repos := helper.GetRepos()
19+
20+
for _, repo := range repos.Repos {
2621

2722
fmt.Fprintf(w, "%v: %v \n", repo.Name, repo.Path)
2823

app/hdlr/log.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,17 @@ package hdlr
33
import (
44
"fmt"
55
"github.com/gorilla/mux"
6-
"github.com/spf13/viper"
76
"github.com/typomedia/gitti/app/git"
7+
"github.com/typomedia/gitti/app/helper"
88
"net/http"
99
)
1010

1111
func Log(w http.ResponseWriter, r *http.Request) {
12-
repo := Repo{}
1312
vars := mux.Vars(r)
1413
project := vars["project"]
15-
config := viper.AllSettings()
16-
repos := config["repos"].(map[string]interface{})
1714

18-
for name, path := range repos {
19-
if name == project {
20-
repo = Repo{
21-
Name: name,
22-
Path: path.(string),
23-
}
24-
}
25-
}
15+
// Get the repo from config
16+
repo := helper.GetRepo(project)
2617

2718
_, commit := git.Log(repo.Path)
2819

app/hdlr/prune.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,18 @@ package hdlr
33
import (
44
"fmt"
55
"github.com/gorilla/mux"
6-
"github.com/spf13/viper"
76
"github.com/typomedia/gitti/app/git"
7+
"github.com/typomedia/gitti/app/helper"
88
"github.com/typomedia/gitti/app/msg"
99
"net/http"
1010
)
1111

1212
func Prune(w http.ResponseWriter, r *http.Request) {
13-
repo := Repo{}
1413
vars := mux.Vars(r)
1514
project := vars["project"]
16-
config := viper.AllSettings()
17-
repos := config["repos"].(map[string]interface{})
1815

19-
for name, path := range repos {
20-
if name == project {
21-
repo = Repo{
22-
Name: name,
23-
Path: path.(string),
24-
}
25-
}
26-
}
16+
// Get the repo from config
17+
repo := helper.GetRepo(project)
2718

2819
res := git.Prune(repo.Path)
2920

app/hdlr/pull.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,17 @@ package hdlr
33
import (
44
"fmt"
55
"github.com/gorilla/mux"
6-
"github.com/spf13/viper"
76
"github.com/typomedia/gitti/app/git/ext"
7+
"github.com/typomedia/gitti/app/helper"
88
"net/http"
99
)
1010

1111
func Pull(w http.ResponseWriter, r *http.Request) {
12-
repo := Repo{}
1312
vars := mux.Vars(r)
1413
project := vars["project"]
15-
config := viper.AllSettings()
16-
repos := config["repos"].(map[string]interface{})
1714

18-
for name, path := range repos {
19-
if name == project {
20-
repo = Repo{
21-
Name: name,
22-
Path: path.(string),
23-
}
24-
}
25-
}
15+
// Get the repo from config
16+
repo := helper.GetRepo(project)
2617

2718
pull := ext.Pull(repo.Path)
2819

app/hdlr/status.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,17 @@ package hdlr
33
import (
44
"fmt"
55
"github.com/gorilla/mux"
6-
"github.com/spf13/viper"
76
"github.com/typomedia/gitti/app/git/ext"
7+
"github.com/typomedia/gitti/app/helper"
88
"net/http"
99
)
1010

1111
func Status(w http.ResponseWriter, r *http.Request) {
12-
repo := Repo{}
1312
vars := mux.Vars(r)
1413
project := vars["project"]
15-
config := viper.AllSettings()
16-
repos := config["repos"].(map[string]interface{})
1714

18-
for name, path := range repos {
19-
if name == project {
20-
repo = Repo{
21-
Name: name,
22-
Path: path.(string),
23-
}
24-
}
25-
}
15+
// Get the repo from config
16+
repo := helper.GetRepo(project)
2617

2718
res := ext.Status(repo.Path)
2819

app/hdlr/structs.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package hdlr
22

3-
type Repo struct {
4-
Name string
5-
Path string
3+
type Refs struct {
4+
Branches []Branch
5+
}
6+
7+
type Branch struct {
8+
Name string
9+
Revision string
610
}

0 commit comments

Comments
 (0)