@@ -226,8 +226,43 @@ allowing for faster startup. Validation happens lazily on first use."
226226 (cl-loop for v being the hash-values of (codespaces--all-codespaces)
227227 collect (list nil (codespaces-space-name v))))
228228
229+ (defun codespaces--get-default-branch (repo )
230+ " Return the default branch for REPO or signal an error if not found."
231+ (let* ((null-device (if (eq system-type 'windows-nt ) " NUL" " /dev/null" ))
232+ (command (format " gh repo view %s --json defaultBranchRef -q .defaultBranchRef.name 2>%s "
233+ repo null-device))
234+ (branch (shell-command-to-string command)))
235+ (if (string-empty-p branch)
236+ (user-error " Error getting default branch. Likely, no repository exists or it's inaccessible" )
237+ (string-trim branch))))
238+
239+ (defun codespaces--create-from-args (repo branch machine )
240+ " Create a codespace for args REPO BRANCH and MACHINE."
241+ (message " Creating codespace... " )
242+ (let ((status
243+ (codespaces--locally
244+ (let ((inhibit-read-only t ))
245+ (shell-command
246+ (format " gh codespace create --machine %s --repo %s --branch %s "
247+ machine repo branch)
248+ nil " *codespaces-output*" )))))
249+ (if (zerop status)
250+ (message " Codespace created successfully! " )
251+ (user-error " Command `gh codespace create` failed... [See *codespaces-output* buffer for details]" ))))
252+
229253; ;; Public interface
230254
255+ (defun codespaces-create ()
256+ " Prompt for repository, branch, and machine type to create a Codespace."
257+ (interactive )
258+ (let* ((repo (read-string " Repository (user/repo): " ))
259+ (branch (read-string " Branch: " (or (codespaces--get-default-branch repo) " main" )))
260+ (machine (read-string " Machine type: " " basicLinux32gb" )))
261+ (when (string-empty-p repo)
262+ (user-error " Repository must not be empty" ))
263+ (message " Creating codespace... " )
264+ (codespaces--create-from-args repo branch machine)))
265+
231266(defun codespaces-stop ()
232267 " Stop a codespace chosen by `completing-read' ."
233268 (interactive )
0 commit comments