Class: GitDj

Inherits:
Object
  • Object
show all
Defined in:
lib/git_dj.rb,
lib/git_dj/version.rb

Defined Under Namespace

Classes: CommandFailedError

Constant Summary collapse

INTEGRATION_BRANCH =
'staging'
RELEASE_BRANCH =
'master'
LOG_FILE_NAME =
'/tmp/gdj_activity'
VERSION =
"0.0.9"

Instance Method Summary collapse

Constructor Details

#initializeGitDj

Returns a new instance of GitDj.



9
10
# File 'lib/git_dj.rb', line 9

def initialize
end

Instance Method Details

#continue_prev_commandsObject



98
99
100
101
# File 'lib/git_dj.rb', line 98

def continue_prev_commands
  cmds = File.read(LOG_FILE_NAME).chomp.strip.split("\n")
  run_cmds(cmds)
end

#create_pull_requestObject



37
38
39
40
41
42
# File 'lib/git_dj.rb', line 37

def create_pull_request
  run_cmds [
    "git push origin #{current_branch_name}",
    "open #{current_repo_url}/compare/#{current_branch_name}?expand=1"
]
end

#current_branch_nameObject



118
119
120
121
122
123
124
125
126
127
# File 'lib/git_dj.rb', line 118

def current_branch_name
  out = %x[git branch]
  branch_string = out.split("\n").detect do |str|
    str.index('*') == 0
  end


  branch_string.gsub!(/^\*/, '')
  branch_string.chomp.strip
end

#current_repo_urlObject



129
130
131
132
# File 'lib/git_dj.rb', line 129

def current_repo_url
  url = %x(git ls-remote --get-url origin)
  url[0..-6]
end

#get_updates_from_originObject



103
104
105
106
107
# File 'lib/git_dj.rb', line 103

def get_updates_from_origin
  drop_commands_cache
  cur_branch = current_branch_name
  run_cmds [ "git pull origin #{cur_branch}" ]
end

#integrate_current_branchObject



44
45
46
# File 'lib/git_dj.rb', line 44

def integrate_current_branch
  send_branch_to_integration('staging')
end

#integrate_current_branch2Object



48
49
50
# File 'lib/git_dj.rb', line 48

def integrate_current_branch2
  send_branch_to_integration('staging2')
end

#integrate_current_branch_to_anotherObject



52
53
54
# File 'lib/git_dj.rb', line 52

def integrate_current_branch_to_another
  send_branch_to_integration(ARGV[1])
end

#performObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/git_dj.rb', line 12

def perform
  case ARGV[0]
  when 'integrate'
    integrate_current_branch
  when 'integrate2'
    integrate_current_branch2
  when 'release'
    release_current_branch
  when 'get'
    get_updates_from_origin
  when 'put'
    push_updates_to_origin
  when 'continue'
    continue_prev_commands
  when "i"
    integrate_current_branch_to_another
  when 'pr'
    create_pull_request
  when 'help'
    print_help
  else
    print_help
  end
end


134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/git_dj.rb', line 134

def print_help
  puts %Q{Git DJ #{VERSION}

Usage:
#{green_color('gdj integrate')} - merge current branch in staging branch, and switch back
#{green_color('gdj integrate2')} - merge current branch in staging2 branch, and switch back
#{green_color('gdj release')} - merge current branch into master, and switch back
#{green_color('gdj get')} - pull changes from origin to local
#{green_color('gdj put')} - pull, then push changes from origin to local
#{green_color('gdj pr')} - push current branch to origin, then open pull request in browser
#{green_color('gdj continue')} - continue previous failed command (after merge, etc)

}
end

#push_updates_to_originObject



109
110
111
112
113
114
115
116
# File 'lib/git_dj.rb', line 109

def push_updates_to_origin
  drop_commands_cache
  cur_branch = current_branch_name
  run_cmds [
    "git pull origin #{cur_branch}",
    "git push origin #{cur_branch}"
  ]
end

#release_current_branchObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/git_dj.rb', line 78

def release_current_branch
  drop_commands_cache
  cur_branch = current_branch_name
  if has_uncommited_changes
    puts red_color("Failed to release #{cur_branch}: you have uncommited changes")
  elsif cur_branch == RELEASE_BRANCH || cur_branch == INTEGRATION_BRANCH
    puts red_color("Can not integrate #{cur_branch} into #{RELEASE_BRANCH}")
  else
    run_cmds [
      "git checkout #{RELEASE_BRANCH}",
      "git merge #{cur_branch}",
      "git pull origin #{RELEASE_BRANCH}",
      "git push origin #{RELEASE_BRANCH}",
      "git checkout #{cur_branch}"
    ]

    puts green_color("Successfully released #{cur_branch}")
  end
end

#send_branch_to_integration(integration_branch_name) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/git_dj.rb', line 56

def send_branch_to_integration(integration_branch_name)
  drop_commands_cache
  cur_branch = current_branch_name
  integration_branch = integration_branch_name

  if has_uncommited_changes
    puts red_color("Failed to integrate #{cur_branch}: you have uncommited changes")
  elsif cur_branch == integration_branch
    puts red_color("Can not integrate #{integration_branch} into #{integration_branch}")
  else
    run_cmds [
      "git checkout #{integration_branch}",
      "git pull origin #{integration_branch}",
      "git merge --no-edit #{cur_branch}",
      "git push origin #{integration_branch}",
      "git checkout #{cur_branch}"
    ]

    puts green_color("Successfully integrated #{cur_branch}")
  end
end