-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-pullff-main.ps1
More file actions
70 lines (60 loc) · 2.52 KB
/
git-pullff-main.ps1
File metadata and controls
70 lines (60 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# * カレントブランチのままで (= ローカル main ブランチを checkout せずに)
# fast forward 可能な場合のみ ローカル main ブランチを origin/main ブランチに更新
# - `git fetch origin main:main` だと fast forward 不可能な場合も上書き更新されるため
# * カレントブランチがローカル main ブランチの場合は何もせず終了 (detached HEAD を避けるため)
$localMain = "main"
$remote = "origin"
$remoteMain = "main"
#-----------------------------------------------------------------------------------------
# カレントブランチが $localMain の場合は処理を中止 ($localMain が進むと detatched HEAD になるため)
$currentBranch = (git branch --show-current).Trim()
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to get current branch"
exit 1
}
if ($currentBranch -eq $localMain) {
Write-Host "Current branch is already '$localMain'. Skipping update to avoid detached HEAD state."
exit 0
}
# リモートから fetch
git fetch $remote
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to fetch from $remote"
exit 1
}
# $localMain が $remote/$remoteMain と等しければ処理を終了
$localMainCommit = git rev-parse "$localMain"
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to get commit hash of $localMain"
exit 1
}
$remoteMainCommit = git rev-parse "$remote/$remoteMain"
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to get commit hash of $remote/$remoteMain"
exit 1
}
if ($localMainCommit -eq $remoteMainCommit) {
Write-Host "$localMain is already up to date with $remote/$remoteMain"
exit 0
}
# fast forward 可能か確認
git merge-base --is-ancestor "$localMain" "$remote/$remoteMain"
$isMainConflict = $LASTEXITCODE # 0: fast forward 可能, 1: conflict, それ以外: エラー
if ($isMainConflict -ne 0 -and $isMainConflict -ne 1) {
Write-Error "Failed to check merge-base"
exit 1
}
if ($isMainConflict -eq 0) {
# fast forward 可能な場合進める
git update-ref "refs/heads/$localMain" "refs/remotes/$remote/$remoteMain"
if ($LASTEXITCODE -eq 0) {
Write-Host "$localMain branch updated to $remote/$remoteMain"
} else {
Write-Error "Failed to update $localMain branch"
exit 1
}
} else {
# fast forward 不可能な場合、警告を表示して終了
Write-Host "Warning: $localMain has unpushed commits. Check with 'git log $localMain..$remote/$remoteMain'"
exit 1
}