|
| 1 | +module Code |
| 2 | + |
| 3 | + class Git |
| 4 | + |
| 5 | + COLORS = {black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, teal: 36} |
| 6 | + |
| 7 | + def start(feature) |
| 8 | + ensure_feature_missing! feature |
| 9 | + |
| 10 | + pull main_branch |
| 11 | + checkout main_branch unless on_main_branch? |
| 12 | + |
| 13 | + create_branch feature |
| 14 | + checkout feature |
| 15 | + end |
| 16 | + |
| 17 | + def cancel |
| 18 | + if on_main_branch? |
| 19 | + puts "Nothing to cancel (already on #{main_branch})" |
| 20 | + else |
| 21 | + branch = current_branch |
| 22 | + checkout main_branch |
| 23 | + delete_branch branch, force: true |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + def publish |
| 28 | + ensure_clean_slate! |
| 29 | + push current_branch |
| 30 | + compare_in_browser current_branch |
| 31 | + end |
| 32 | + |
| 33 | + def compare_in_browser(branch) |
| 34 | + exec "hub compare #{branch}" |
| 35 | + end |
| 36 | + |
| 37 | + def finish |
| 38 | + branch = current_branch |
| 39 | + |
| 40 | + checkout main_branch |
| 41 | + fetch |
| 42 | + pull main_branch |
| 43 | + |
| 44 | + delete_branch branch |
| 45 | + end |
| 46 | + |
| 47 | + def files_changed? |
| 48 | + diff = `git diff --name-status`.strip |
| 49 | + diff != '' |
| 50 | + end |
| 51 | + |
| 52 | + def push(branch) |
| 53 | + call "push origin #{branch}" |
| 54 | + end |
| 55 | + |
| 56 | + def checkout(branch) |
| 57 | + call "checkout #{branch}" |
| 58 | + end |
| 59 | + |
| 60 | + def fetch |
| 61 | + call 'fetch' |
| 62 | + end |
| 63 | + |
| 64 | + def create_branch(branch) |
| 65 | + call "branch #{branch}" |
| 66 | + end |
| 67 | + |
| 68 | + def delete_branch(branch, force: false) |
| 69 | + ensure_safe_branch! branch |
| 70 | + |
| 71 | + flag = force ? '-D' : '-d' |
| 72 | + call "branch #{flag} #{branch}" |
| 73 | + end |
| 74 | + |
| 75 | + def current_branch |
| 76 | + `git symbolic-ref HEAD --short`.strip |
| 77 | + end |
| 78 | + |
| 79 | + def main_branch |
| 80 | + 'development' |
| 81 | + end |
| 82 | + |
| 83 | + def on_main_branch? |
| 84 | + on_branch? main_branch |
| 85 | + end |
| 86 | + |
| 87 | + def on_branch? branch |
| 88 | + current_branch == branch |
| 89 | + end |
| 90 | + |
| 91 | + def branch_exists?(branch) |
| 92 | + ref = `git show-ref refs/heads/#{branch}`.strip |
| 93 | + ref != '' |
| 94 | + end |
| 95 | + alias_method :feature_exists?, :branch_exists? |
| 96 | + |
| 97 | + def pull(branch) |
| 98 | + call "pull origin #{branch}" |
| 99 | + end |
| 100 | + |
| 101 | + def call(params) |
| 102 | + exec "git #{params}" |
| 103 | + end |
| 104 | + |
| 105 | + def color(script, color) |
| 106 | + value = COLORS[color] |
| 107 | + "\033[0;#{value}m" + script + "\033[m" |
| 108 | + end |
| 109 | + |
| 110 | + def green(script) |
| 111 | + color script, :green |
| 112 | + end |
| 113 | + |
| 114 | + def red(script) |
| 115 | + color script, :magenta |
| 116 | + end |
| 117 | + |
| 118 | + def ensure_safe_branch!(branch) |
| 119 | + error "branch #{branch} is protected" if protected_branch? branch |
| 120 | + end |
| 121 | + |
| 122 | + def protected_branch? branch |
| 123 | + %w{master development}.include? branch |
| 124 | + end |
| 125 | + |
| 126 | + def ensure_feature_missing!(feature) |
| 127 | + error "The #{feature} feature already exists" if feature_exists?(feature) |
| 128 | + end |
| 129 | + |
| 130 | + def ensure_clean_slate! |
| 131 | + if files_changed? |
| 132 | + error "Please stash or commit your code" |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + def error(message) |
| 137 | + abort red(message) |
| 138 | + end |
| 139 | + |
| 140 | + def exec(script) |
| 141 | + puts green(script) |
| 142 | + system script |
| 143 | + end |
| 144 | + |
| 145 | + end |
| 146 | + |
| 147 | +end |
0 commit comments