git herocmd improvements

This commit is contained in:
Timur Gordon
2025-08-29 10:17:34 +02:00
parent 566d871399
commit 3d86ec7cf5
5 changed files with 104 additions and 24 deletions

View File

@@ -5,7 +5,7 @@ import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.ui.console
import os
pub const gitcmds = 'clone,commit,pull,push,delete,reload,list,edit,sourcetree,cd'
pub const gitcmds = 'clone,commit,pull,push,delete,reload,list,edit,sourcetree,path,exists'
@[params]
pub struct ReposActionsArgs {
@@ -117,6 +117,20 @@ pub fn (mut gs GitStructure) do(args_ ReposActionsArgs) !string {
return ''
}
if args.cmd == 'exists' {
return gs.check_repos_exist(args)
}
if args.cmd == 'path' {
if repos.len == 0 {
return error('No repository found for path command')
}
if repos.len > 1 {
return error('Multiple repositories found for path command, please be more specific')
}
return repos[0].path()
}
// means we are on 1 repo
if args.cmd in 'sourcetree,edit'.split(',') {
if repos.len == 0 {

View File

@@ -39,9 +39,18 @@ pub fn (mut gitstructure GitStructure) clone(args GitCloneArgs) !&GitRepo {
key_ := repo.cache_key()
gitstructure.repos[key_] = &repo
mut repopath := repo.patho()!
if repopath.exists() {
return error("can't clone on existing path, came from url, path found is ${repopath.path}.\n")
if repo.exists() {
console.print_green("Repository already exists at ${repo.path()}")
// Load the existing repository status
repo.load_internal() or {
console.print_debug('Could not load existing repository status: ${err}')
}
return &repo
}
// Check if path exists but is not a git repository
if os.exists(repo.path()) {
return error("Path exists but is not a git repository: ${repo.path()}")
}
if args.sshkey.len > 0 {

View File

@@ -163,3 +163,37 @@ pub fn (mut repo GitRepo) open_vscode() ! {
mut vs_code := vscode.new(path)
vs_code.open()!
}
// Check if repository exists at its expected path
pub fn (repo GitRepo) exists() bool {
repo_path := repo.path()
if !os.exists(repo_path) {
return false
}
git_dir := os.join_path(repo_path, '.git')
return os.exists(git_dir)
}
// Check if any repositories exist based on filter criteria and return result for exists command
pub fn (mut gs GitStructure) check_repos_exist(args ReposActionsArgs) !string {
repos := gs.get_repos(
filter: args.filter
name: args.repo
account: args.account
provider: args.provider
)!
if repos.len > 0 {
// Repository exists - print path and return success
if !args.script {
console.print_green('Repository exists: ${repos[0].path()}')
}
return repos[0].path()
} else {
// Repository doesn't exist - return error for exit code 1
if !args.script {
console.print_stderr('Repository not found')
}
return error('Repository not found')
}
}