11"""
2- server.py — Lightweight HTTP server and REST API for git-diff.
3- No external dependencies — pure Python stdlib only.
2+ server.py -- Lightweight HTTP server and REST API for git-diff.
3+ No external dependencies -- pure Python stdlib only.
44"""
55import json
66import os
@@ -90,18 +90,18 @@ def pi(key, default=0):
9090 def _route (self , path , p , pi ):
9191 root = self .repo_root
9292
93- # ── Serve the SPA ──────────────────────────────────────────────
93+ # -- Serve the SPA ----------------------------------------------
9494 if path in ("/" , "/index.html" ):
9595 html = TEMPLATE_PATH .read_text (encoding = "utf-8" )
9696 self .send_html (html )
9797 return
9898
99- # ── Initial bundle ─────────────────────────────────────────────
99+ # -- Initial bundle ---------------------------------------------
100100 if path == "/api/data" :
101101 self .send_json (self .initial_data )
102102 return
103103
104- # ── Commit detail + diff ───────────────────────────────────────
104+ # -- Commit detail + diff ---------------------------------------
105105 if path == "/api/commit" :
106106 h = p ("hash" )
107107 if not h :
@@ -113,7 +113,7 @@ def _route(self, path, p, pi):
113113 self .send_json ({"diff" : diff , "detail" : detail })
114114 return
115115
116- # ── Paginated commit history ───────────────────────────────────
116+ # -- Paginated commit history -----------------------------------
117117 if path == "/api/commits" :
118118 branch = p ("branch" , "HEAD" )
119119 limit = pi ("limit" , 100 )
@@ -125,7 +125,7 @@ def _route(self, path, p, pi):
125125 self .send_json ({"commits" : commits [offset :offset + limit ], "total" : len (commits )})
126126 return
127127
128- # ── Range diff: branch-to-branch or SHA-to-SHA ────────────────
128+ # -- Range diff: branch-to-branch or SHA-to-SHA ----------------
129129 if path == "/api/range-diff" :
130130 base = p ("base" )
131131 compare = p ("compare" )
@@ -150,26 +150,26 @@ def _route(self, path, p, pi):
150150 self .send_json ({"diff" : diff , "commits" : range_commits , "base" : base , "compare" : compare })
151151 return
152152
153- # ── Staged diff ───────────────────────────────────────────────
153+ # -- Staged diff -----------------------------------------------
154154 if path == "/api/staged" :
155155 ctx = pi ("context" , 3 )
156156 self .send_json (get_staged_diff (root , context = ctx ))
157157 return
158158
159- # ── Unstaged diff ─────────────────────────────────────────────
159+ # -- Unstaged diff ---------------------------------------------
160160 if path == "/api/unstaged" :
161161 ctx = pi ("context" , 3 )
162162 self .send_json (get_unstaged_diff (root , context = ctx ))
163163 return
164164
165- # ── Stash diff ────────────────────────────────────────────────
165+ # -- Stash diff ------------------------------------------------
166166 if path == "/api/stash" :
167167 ref = p ("ref" , "stash@{0}" )
168168 ctx = pi ("context" , 3 )
169169 self .send_json ({"diff" : get_stash_diff (root , ref , context = ctx )})
170170 return
171171
172- # ── File content at ref ───────────────────────────────────────
172+ # -- File content at ref ---------------------------------------
173173 if path == "/api/file" :
174174 fp = p ("path" )
175175 ref = p ("ref" , "HEAD" )
@@ -179,7 +179,7 @@ def _route(self, path, p, pi):
179179 self .send_json (get_file_content (root , fp , ref ))
180180 return
181181
182- # ── File commit history ───────────────────────────────────────
182+ # -- File commit history ---------------------------------------
183183 if path == "/api/file-log" :
184184 fp = p ("path" )
185185 limit = pi ("limit" , 50 )
@@ -189,7 +189,7 @@ def _route(self, path, p, pi):
189189 self .send_json ({"commits" : get_file_log (root , fp , limit = limit )})
190190 return
191191
192- # ── File blame ────────────────────────────────────────────────
192+ # -- File blame ------------------------------------------------
193193 if path == "/api/blame" :
194194 fp = p ("path" )
195195 ref = p ("ref" , "HEAD" )
@@ -199,25 +199,25 @@ def _route(self, path, p, pi):
199199 self .send_json ({"blame" : get_file_blame (root , fp , ref )})
200200 return
201201
202- # ── Commit activity chart ─────────────────────────────────────
202+ # -- Commit activity chart -------------------------------------
203203 if path == "/api/activity" :
204204 days = pi ("days" , 90 )
205205 self .send_json ({"data" : get_commit_stats_by_day (root , days = days )})
206206 return
207207
208- # ── Language stats ────────────────────────────────────────────
208+ # -- Language stats --------------------------------------------
209209 if path == "/api/langs" :
210210 self .send_json ({"data" : get_language_stats (root )})
211211 return
212212
213- # ── Full refresh ──────────────────────────────────────────────
213+ # -- Full refresh ----------------------------------------------
214214 if path == "/api/refresh" :
215215 data = collect_all_data (root )
216216 GitDiffHandler .initial_data = data
217217 self .send_json ({"status" : "ok" , "timestamp" : int (time .time ())})
218218 return
219219
220- # ── Raw git command (safe read-only subset) ───────────────────
220+ # -- Raw git command (safe read-only subset) -------------------
221221 if path == "/api/git" :
222222 cmd = p ("cmd" )
223223 SAFE = {"log" , "show" , "diff" , "blame" , "ls-tree" , "ls-files" ,
@@ -258,8 +258,8 @@ def start_server(repo_root: str, data: dict, port: int = None, no_browser: bool
258258 server = HTTPServer ((host , port ), GitDiffHandler )
259259 url = f"http://{ host } :{ port } "
260260
261- print (f"\n 🌐 Server → { url } " )
262- print (f" 📁 Repo → { repo_root } " )
261+ print (f"\n Server -> { url } " )
262+ print (f" Repo -> { repo_root } " )
263263 print (f"\n Press Ctrl+C to stop\n " )
264264
265265 if not no_browser :
@@ -271,6 +271,6 @@ def _open():
271271 try :
272272 server .serve_forever ()
273273 except KeyboardInterrupt :
274- print ("\n 👋 git-diff stopped. Have a good one!\n " )
274+ print ("\n git-diff stopped. Have a good one!\n " )
275275 finally :
276276 server .server_close ()
0 commit comments