-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathgit.rb
78 lines (68 loc) · 1.95 KB
/
git.rb
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
begin
if `git status`.include? "fatal: Not a git repository"
`git init`
puts "Git init - done."
else
puts "Git init - checked."
end
if `git remote -v`.empty?
puts "Paste remote git URL : "
remote = gets.chomp
`git remote add origin #{remote}`
puts "Git remote - done."
else
puts "Git remote - checked."
end
if (`git status`.include? "Changes not staged for commit:") || (`git status`.include? "Untracked files:")
`git add --all`
puts "Git add - done."
else
puts "Git add - checked."
end
if `git status`.include? "Changes to be committed:"
puts "Enter commit message : "
message = gets.chomp
if message.empty?
message = `git status`.split('(use "git reset HEAD <file>..." to unstage)')[1].strip.gsub("\n",",")+"."
`git commit -m "Ruby-git commit - #{message}"`
else
`git commit -m "#{message}"`
end
puts "Git commit - done."
else
puts "Git commit - checked."
end
active_branch = ""
`git branch`.split("\n").each do |each_branch|
if each_branch.include? "* "
active_branch = each_branch.gsub("* ","")
end
end
branches = `git branch`.gsub("* ","").gsub(" ","").split("\n")
if branches.count == 1
branch = active_branch
else
i = 0
while i < branches.count do
puts "#{i+1} : #{branches[i]}"
i = i+1
end
puts "Choose a branch (#{active_branch} is default) : "
branch_number = gets.chomp.to_i
if branch_number > 0 && branch_number <= branches.count
branch = branches[branch_number-1]
puts "Succesfully chosen #{branch} branch."
else
puts "Invalid branch. Choosing active branch #{active_branch} to push to."
branch = active_branch
end
end
if `git status`.include? "nothing to commit"
`git push origin #{branch}`
puts "Git push - done."
else
puts "Git push - checked."
end
rescue Exception => e
puts "Error occured : #{e.to_s}"
end