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

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

View File

@@ -48,8 +48,8 @@ pub fn (mut repo GitRepo) need_commit() !bool {
return repo.has_changes
}
// Check if the repository has changes that need to be pushed (is against the cached info).
pub fn (mut repo GitRepo) need_push_or_pull() !bool {
// Check if the repository has local changes that need to be pushed to remote
pub fn (mut repo GitRepo) need_push() !bool {
repo.status_update()!
last_remote_commit := repo.get_last_remote_commit() or {
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 {
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
}
// 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
fn (mut repo GitRepo) need_checkout() bool {
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 {
return error('Failed to detect changes in repository ${repo.name}: ${err}')
}
repo.cache_set() or {
return error('Failed to update cache for repository ${repo.name}: ${err}')
}