Skip to content

Commit 126698e

Browse files
author
root
committed
fixing error releated to first commit
1 parent 5d74f1f commit 126698e

16 files changed

Lines changed: 645 additions & 551 deletions

File tree

Roadmap.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
1. Select directory
2+
2. EnsureGitRepo
3+
3. EnsureSafeDirectory
4+
4. EnsureBranchSync
5+
5. EnsureGitIdentity
6+
6. Ask Owner
7+
7. Ask Repo Name (default = folder name, but editable)
8+
8. Ask Token (only store, don’t validate)
9+
9. Try repo create (if fails → warn, continue)
10+
10. Configure remote (without token in URL)
11+
11. First push

build.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go fmt ./...
2+
go build -o git-genius ./cmd/genius

cmd/genius/main.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ func main() {
3232
}
3333
}
3434

35-
// --- Network check (best-effort only) ---
36-
system.CheckInternet()
37-
3835
// --- Start UI ---
3936
menu.Start()
4037
}

git-genius

-1.01 MB
Binary file not shown.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module git-genius
22

3-
go 1.21
3+
go 1.21

internal/doctor/doctor.go

Lines changed: 36 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,40 @@ package doctor
33
import (
44
"os"
55
"path/filepath"
6-
"strings"
76

87
"git-genius/internal/config"
98
"git-genius/internal/github"
109
"git-genius/internal/system"
1110
"git-genius/internal/ui"
1211
)
1312

14-
// Run performs full system + git health check (ANDROID SAFE)
13+
/*
14+
Doctor:
15+
Full system + git + github health check
16+
No offline mode
17+
Compatible with new system layer
18+
Android safe
19+
*/
1520
func Run() {
16-
ui.Header("Git Genius Doctor 🩺")
1721

18-
system.CheckInternet()
22+
ui.Header("Git Genius Doctor 🩺")
1923

2024
checkGitInstalled()
2125
checkWorkDir()
2226
checkGitRepo()
2327
checkGitBranch()
2428
checkGitIdentity()
2529
checkRemote()
26-
checkInternet()
2730
checkGitHubToken()
2831
checkGitHubRepo()
2932
checkErrorLog()
3033

3134
ui.Success("Doctor check completed")
3235
}
3336

34-
/* ============================================================
35-
CHECKS
36-
============================================================ */
37+
///////////////////////////////////////////////////////////////
38+
// CHECKS
39+
///////////////////////////////////////////////////////////////
3740

3841
func checkGitInstalled() {
3942
if system.CommandExists("git") {
@@ -42,7 +45,6 @@ func checkGitInstalled() {
4245
}
4346

4447
ui.Error("Git not found in PATH")
45-
ui.Info("Please install git manually for your environment")
4648
}
4749

4850
func checkWorkDir() {
@@ -69,47 +71,34 @@ func checkGitRepo() {
6971
}
7072

7173
ui.Warn("No git repository found")
72-
73-
if ui.Confirm("Initialize git repository here?") {
74-
if err := system.RunGitAt(dir, "init"); err != nil {
75-
ui.Error("Failed to initialize git repository")
76-
return
77-
}
78-
system.EnsureSafeDirectory(dir)
79-
ui.Success("Git repository initialized")
80-
}
74+
ui.Info("Run Setup to initialize repository")
8175
}
8276

8377
func checkGitBranch() {
8478
cfg := config.Load()
8579
dir := cfg.GetWorkDir()
8680

87-
current := system.CurrentGitBranchAt(dir)
88-
89-
if current == "" {
81+
branch, err := system.GitOutputAt(dir, "branch", "--show-current")
82+
if err != nil || branch == "" {
9083
ui.Warn("No commits yet (branch not created)")
9184
return
9285
}
9386

94-
ui.Success("Current git branch: " + current)
87+
ui.Success("Current git branch: " + branch)
9588

96-
if current != cfg.Branch {
89+
if branch != cfg.Branch {
9790
ui.Warn("Branch mismatch detected")
9891
ui.Info("Config branch : " + cfg.Branch)
99-
ui.Info("Git branch : " + current)
100-
ui.Info("Run Setup to safely sync branch")
92+
ui.Info("Git branch : " + branch)
10193
}
10294
}
10395

104-
/*
105-
Git identity check (ANDROID SAFE, LOCAL REPO ONLY)
106-
*/
10796
func checkGitIdentity() {
10897
cfg := config.Load()
10998
dir := cfg.GetWorkDir()
11099

111-
name := gitConfig(dir, "user.name")
112-
email := gitConfig(dir, "user.email")
100+
name, _ := system.GitOutputAt(dir, "config", "--get", "user.name")
101+
email, _ := system.GitOutputAt(dir, "config", "--get", "user.email")
113102

114103
if name != "" && email != "" {
115104
ui.Success("Git identity configured")
@@ -119,27 +108,7 @@ func checkGitIdentity() {
119108
}
120109

121110
ui.Warn("Git identity not configured")
122-
ui.Info("Commits may appear as root@localhost")
123-
124-
if !ui.Confirm("Configure git identity for THIS repo now?") {
125-
return
126-
}
127-
128-
if name == "" {
129-
val := ui.Input("Enter your name")
130-
if val != "" {
131-
_ = system.RunGitAt(dir, "config", "user.name", val)
132-
}
133-
}
134-
135-
if email == "" {
136-
val := ui.Input("Enter your email")
137-
if val != "" {
138-
_ = system.RunGitAt(dir, "config", "user.email", val)
139-
}
140-
}
141-
142-
ui.Success("Git identity configured (local repository)")
111+
ui.Info("Run Setup to configure git identity")
143112
}
144113

145114
func checkRemote() {
@@ -151,41 +120,33 @@ func checkRemote() {
151120
return
152121
}
153122

154-
if err := system.RunGitAt(dir, "remote", "get-url", cfg.Remote); err != nil {
123+
_, err := system.GitOutputAt(dir, "remote", "get-url", cfg.Remote)
124+
if err != nil {
155125
ui.Warn("Remote not found: " + cfg.Remote)
156-
ui.Info("Run Tools → Create / Link GitHub Repository")
126+
ui.Info("Run Create / Link GitHub Repository")
157127
return
158128
}
159129

160130
ui.Success("Git remote configured: " + cfg.Remote)
161131
}
162132

163-
func checkInternet() {
164-
if system.Online {
165-
ui.Success("Internet connection available")
166-
} else {
167-
ui.Warn("Offline mode detected")
168-
ui.Info("GitHub validation & push may fail")
169-
}
170-
}
171-
172133
func checkGitHubToken() {
134+
173135
token := github.GetToken()
174136
if token == "" {
175137
ui.Warn("GitHub token not configured")
176-
ui.Info("Run Setup to configure token")
177138
return
178139
}
179140

180-
user, err := github.Validate()
141+
client, err := github.NewClient()
181142
if err != nil {
182-
ui.Error("GitHub token invalid or expired")
183-
ui.Info("Run Setup to reconfigure token")
143+
ui.Error("Invalid GitHub token")
184144
return
185145
}
186146

187-
if user == "offline-mode" {
188-
ui.Warn("GitHub token validation skipped (offline)")
147+
user, err := client.GetAuthenticatedUser()
148+
if err != nil {
149+
ui.Warn("GitHub token invalid or expired")
189150
return
190151
}
191152

@@ -209,13 +170,18 @@ func checkGitHubRepo() {
209170
ui.Success("GitHub repository exists")
210171
} else {
211172
ui.Warn("GitHub repository does not exist")
212-
ui.Info("Run Tools → Create / Link GitHub Repository")
213173
}
214174
}
215175

216176
func checkErrorLog() {
217177
cfg := config.Load()
218-
logPath := filepath.Join(cfg.GetWorkDir(), ".git", ".genius", "error.log")
178+
179+
logPath := filepath.Join(
180+
cfg.GetWorkDir(),
181+
".git",
182+
".genius",
183+
"error.log",
184+
)
219185

220186
if _, err := os.Stat(logPath); err == nil {
221187
ui.Warn("Error log exists")
@@ -224,18 +190,3 @@ func checkErrorLog() {
224190
ui.Success("No error log found")
225191
}
226192
}
227-
228-
/* ============================================================
229-
HELPERS
230-
============================================================ */
231-
232-
func gitConfig(dir, key string) string {
233-
var out strings.Builder
234-
cmd := system.GitCmdAt(dir, "config", "--get", key)
235-
cmd.Stdout = &out
236-
237-
if err := cmd.Run(); err != nil {
238-
return ""
239-
}
240-
return strings.TrimSpace(out.String())
241-
}

internal/github/client.go

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

33
import (
4+
"encoding/json"
45
"errors"
6+
"fmt"
7+
"io"
58
"net/http"
69
"time"
710
)
@@ -11,23 +14,86 @@ const apiBase = "https://api.github.com"
1114
type Client struct {
1215
http *http.Client
1316
token string
14-
user string
17+
user string // lazily resolved
1518
}
1619

20+
/*
21+
NewClient:
22+
- Lightweight
23+
- No validation here
24+
- Safe for repeated calls
25+
*/
1726
func NewClient() (*Client, error) {
27+
1828
token := GetToken()
1929
if token == "" {
2030
return nil, errors.New("github token not configured")
2131
}
2232

23-
user, err := Validate()
33+
return &Client{
34+
http: &http.Client{
35+
Timeout: 12 * time.Second,
36+
},
37+
token: token,
38+
}, nil
39+
}
40+
41+
/*
42+
newRequest:
43+
- Centralized request builder
44+
- Ensures headers are always correct
45+
*/
46+
func (c *Client) newRequest(method, url string, body io.Reader) (*http.Request, error) {
47+
48+
req, err := http.NewRequest(method, url, body)
2449
if err != nil {
2550
return nil, err
2651
}
2752

28-
return &Client{
29-
http: &http.Client{Timeout: 10 * time.Second},
30-
token: token,
31-
user: user,
32-
}, nil
53+
req.Header.Set("Authorization", "Bearer "+c.token)
54+
req.Header.Set("User-Agent", "git-genius")
55+
req.Header.Set("Accept", "application/vnd.github+json")
56+
57+
return req, nil
58+
}
59+
60+
/*
61+
getAuthenticatedUser:
62+
- Lazy resolution
63+
- Cached after first call
64+
*/
65+
func (c *Client) GetAuthenticatedUser() (string, error) {
66+
67+
if c.user != "" {
68+
return c.user, nil
69+
}
70+
71+
req, err := c.newRequest("GET", apiBase+"/user", nil)
72+
if err != nil {
73+
return "", err
74+
}
75+
76+
resp, err := c.http.Do(req)
77+
if err != nil {
78+
return "", err
79+
}
80+
defer resp.Body.Close()
81+
82+
if resp.StatusCode != 200 {
83+
return "", fmt.Errorf(
84+
"invalid or expired GitHub token (status %d)",
85+
resp.StatusCode,
86+
)
87+
}
88+
89+
var data struct {
90+
Login string `json:"login"`
91+
}
92+
93+
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
94+
return "", err
95+
}
96+
97+
c.user = data.Login
98+
return c.user, nil
3399
}

0 commit comments

Comments
 (0)