This commit is contained in:
2025-02-19 05:53:29 +03:00
parent 0880823576
commit 49af31776e
4 changed files with 84 additions and 51 deletions

View File

@@ -171,36 +171,36 @@ pub fn cmd_git(mut cmdroot Command) {
description: 'Filter is part of path of repo e.g. threefoldtech/info_' description: 'Filter is part of path of repo e.g. threefoldtech/info_'
}) })
c.add_flag(Flag{ // c.add_flag(Flag{
flag: .string // flag: .string
required: false // required: false
name: 'repo' // name: 'repo'
abbrev: 'r' // abbrev: 'r'
description: 'name of repo' // description: 'name of repo'
}) // })
c.add_flag(Flag{ // c.add_flag(Flag{
flag: .string // flag: .string
required: false // required: false
name: 'branch' // name: 'branch'
abbrev: 'b' // abbrev: 'b'
description: 'branch of repo (optional)' // description: 'branch of repo (optional)'
}) // })
c.add_flag(Flag{ // c.add_flag(Flag{
flag: .string // flag: .string
required: false // required: false
name: 'account' // name: 'account'
abbrev: 'a' // abbrev: 'a'
description: 'name of account e.g. threefoldtech' // description: 'name of account e.g. threefoldtech'
}) // })
c.add_flag(Flag{ // c.add_flag(Flag{
flag: .string // flag: .string
required: false // required: false
name: 'provider' // name: 'provider'
abbrev: 'p' // abbrev: 'p'
description: 'name of provider e.g. github' // description: 'name of provider e.g. github'
}) // })
} }
for mut c_ in allcmdsref { for mut c_ in allcmdsref {
mut c := *c_ mut c := *c_
@@ -245,21 +245,21 @@ fn cmd_git_execute(cmd Command) ! {
// create the filter for doing group actions, or action on 1 repo // create the filter for doing group actions, or action on 1 repo
mut filter := cmd.flags.get_string('filter') or { '' } mut filter := cmd.flags.get_string('filter') or { '' }
mut branch := cmd.flags.get_string('branch') or { '' } // mut branch := cmd.flags.get_string('branch') or { '' }
mut repo := cmd.flags.get_string('repo') or { '' } // mut repo := cmd.flags.get_string('repo') or { '' }
mut account := cmd.flags.get_string('account') or { '' } // mut account := cmd.flags.get_string('account') or { '' }
mut provider := cmd.flags.get_string('provider') or { '' } // mut provider := cmd.flags.get_string('provider') or { '' }
if cmd.name != 'cd' { // if cmd.name != 'cd' {
// check if we are in a git repo // // check if we are in a git repo
if repo == '' && account == '' && provider == '' && filter == '' { // if repo == '' && account == '' && provider == '' && filter == '' {
if r0 := gs.get_working_repo() { // if r0 := gs.get_working_repo() {
repo = r0.name // repo = r0.name
account = r0.account // account = r0.account
provider = r0.provider // provider = r0.provider
} // }
} // }
} // }
if cmd.name in gittools.gitcmds.split(',') { if cmd.name in gittools.gitcmds.split(',') {
mut pull := cmd.flags.get_bool('pull') or { false } mut pull := cmd.flags.get_bool('pull') or { false }
@@ -271,11 +271,7 @@ fn cmd_git_execute(cmd Command) ! {
} }
mypath := gs.do( mypath := gs.do(
filter: filter filter: filter
repo: repo
reload: reload reload: reload
account: account
provider: provider
branch: branch
recursive: recursive recursive: recursive
cmd: cmd.name cmd: cmd.name
script: cmd.flags.get_bool('script') or { false } script: cmd.flags.get_bool('script') or { false }

View File

@@ -11,7 +11,11 @@ fn get_repo_status(gr GitRepo) !string {
statuses << 'COMMIT' statuses << 'COMMIT'
} }
if repo.need_push_or_pull()! { if repo.need_push()! {
statuses << 'PUSH'
}
if repo.need_pull()! {
statuses << 'PULL' statuses << 'PULL'
} }

View File

@@ -48,8 +48,8 @@ pub fn (mut repo GitRepo) need_commit() !bool {
return repo.has_changes return repo.has_changes
} }
// Check if the repository has changes that need to be pushed (is against the cached info). // Check if the repository has local changes that need to be pushed to remote
pub fn (mut repo GitRepo) need_push_or_pull() !bool { pub fn (mut repo GitRepo) need_push() !bool {
repo.status_update()! repo.status_update()!
last_remote_commit := repo.get_last_remote_commit() or { last_remote_commit := repo.get_last_remote_commit() or {
return error('Failed to get last remote commit: ${err}') return error('Failed to get last remote commit: ${err}')
@@ -57,10 +57,44 @@ pub fn (mut repo GitRepo) need_push_or_pull() !bool {
last_local_commit := repo.get_last_local_commit() or { last_local_commit := repo.get_last_local_commit() or {
return error('Failed to get last local commit: ${err}') return error('Failed to get last local commit: ${err}')
} }
// println('commit status: ${repo.name} ${last_local_commit} ${last_remote_commit}') // If remote commit is empty, it means the branch doesn't exist remotely yet
if last_remote_commit.len == 0 {
return true
}
// If local commit is different from remote and exists, we need to push
return last_local_commit != last_remote_commit return last_local_commit != last_remote_commit
} }
// Check if the repository needs to pull changes from remote
pub fn (mut repo GitRepo) need_pull() !bool {
repo.status_update()!
last_remote_commit := repo.get_last_remote_commit() or {
return error('Failed to get last remote commit: ${err}')
}
// If remote doesn't exist, no need to pull
if last_remote_commit.len == 0 {
return false
}
// Check if the remote commit exists in our local history
// If it doesn't exist, we need to pull
result := repo.exec('git merge-base --is-ancestor ${last_remote_commit} HEAD') or {
if err.msg().contains('exit code: 1') {
// Exit code 1 means the remote commit is not in our history
// Therefore we need to pull
return true
}
return error('Failed to check merge-base: ${err}')
}
// If we get here, the remote commit is in our history
// Therefore we don't need to pull
return false
}
// Legacy function for backward compatibility
pub fn (mut repo GitRepo) need_push_or_pull() !bool {
return repo.need_push()! || repo.need_pull()!
}
// Determine if the repository needs to checkout to a different branch or tag // Determine if the repository needs to checkout to a different branch or tag
fn (mut repo GitRepo) need_checkout() bool { fn (mut repo GitRepo) need_checkout() bool {
if repo.status_wanted.branch.len > 0 { if repo.status_wanted.branch.len > 0 {

View File

@@ -46,7 +46,6 @@ fn (mut repo GitRepo) load() ! {
repo.has_changes = repo.detect_changes() or { repo.has_changes = repo.detect_changes() or {
return error('Failed to detect changes in repository ${repo.name}: ${err}') return error('Failed to detect changes in repository ${repo.name}: ${err}')
} }
repo.cache_set() or { repo.cache_set() or {
return error('Failed to update cache for repository ${repo.name}: ${err}') return error('Failed to update cache for repository ${repo.name}: ${err}')
} }