-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfast_commit.ps1
More file actions
102 lines (81 loc) · 2.87 KB
/
Copy pathfast_commit.ps1
File metadata and controls
102 lines (81 loc) · 2.87 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Function to initialize repository with master and develop branches
function Initialize-GitBranches {
$branches = git branch | ForEach-Object { $_.TrimStart('* ') } | Where-Object { $_ }
if ($branches.Count -eq 0) {
Write-Host "`nNo branches found. Initializing repository with master and develop branches..."
# Create and switch to master branch
git checkout -b master
# Create initial commit if needed
if (-not (git log -n 1 2>$null)) {
Write-Host "Creating initial commit..."
git commit --allow-empty -m "Initial commit"
}
# Create and push develop branch
git checkout -b develop
# Push both branches to remote
git push -u origin master
git push -u origin develop
Write-Host "Repository initialized with master and develop branches"
return $true
}
return $false
}
# Fetch latest changes from remote
Write-Host "Fetching latest changes from remote..."
git fetch
Write-Host "Fetch completed`n"
# Check and initialize branches if needed
$initialized = Initialize-GitBranches
if ($initialized) {
Write-Host "`nPlease run the script again to commit your changes."
exit
}
# Function to select branch
function Select-GitBranch {
Write-Host "Available branches (local and remote):"
$branches = git branch -a | ForEach-Object { $_.TrimStart('* ').TrimStart('remotes/origin/') } | Where-Object { $_ -and $_ -ne 'HEAD' } | Sort-Object -Unique
$branches | ForEach-Object { Write-Host " $_" }
do {
$selection = Read-Host "Enter branch name"
$branch = $branches | Where-Object { $_ -eq $selection }
if (-not $branch) {
Write-Host "Invalid branch name. Please try again."
}
} until ($branch)
Write-Host "Selected branch: $branch"
return $branch
}
# Get current branch
$current_branch = git branch --show-current
Write-Host "Current branch: $current_branch"
# Ask for commit message
do {
$commit_message = Read-Host "Enter commit message"
} until ($commit_message)
# Show status and ask for confirmation
git status
$confirm = Read-Host "`nCommit these changes? (y/n)"
if ($confirm -ne "y") {
Write-Host "Commit cancelled"
exit
}
# Select target branch
Write-Host "`nSelect target branch:"
$branch = Select-GitBranch
# Commit and push
git add .
git commit -m $commit_message
# If current branch is different from target, ask to switch
if ($branch -ne $current_branch) {
$switch_confirm = Read-Host "`nSwitch to $branch and merge changes? (y/n)"
if ($switch_confirm -eq "y") {
git checkout $branch
git merge $current_branch
}
}
# Ask to push
$push_confirm = Read-Host "`nPush changes to remote? (y/n)"
if ($push_confirm -eq "y") {
git push origin $branch
Write-Host "Changes pushed successfully"
}