Class: Cp8Cli::Branch

Inherits:
Object
  • Object
show all
Defined in:
lib/cp8_cli/branch.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Branch

Returns a new instance of Branch.



15
16
17
# File 'lib/cp8_cli/branch.rb', line 15

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/cp8_cli/branch.rb', line 13

def name
  @name
end

Class Method Details

.currentObject



19
20
21
# File 'lib/cp8_cli/branch.rb', line 19

def self.current
  new Command.read("git rev-parse --abbrev-ref HEAD")
end

.from_story(story) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/cp8_cli/branch.rb', line 27

def self.from_story(story)
  new BranchName.new(
    user: CurrentUser.new,
    target: current,
    title: story.title,
    short_link: story.short_link
  ).to_s
end

.suggestionObject



23
24
25
# File 'lib/cp8_cli/branch.rb', line 23

def self.suggestion
  new("suggestion-#{SecureRandom.hex(8)}")
end

Instance Method Details

#checkoutObject



40
41
42
# File 'lib/cp8_cli/branch.rb', line 40

def checkout
  Command.run "git checkout #{name} >/dev/null 2>&1 || git checkout -b #{name}"
end

#open_ciObject



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

def open_ci
  Ci.new(branch_name: name, repo: Repo.current).open
end

#open_story_in_browserObject



52
53
54
55
56
57
58
# File 'lib/cp8_cli/branch.rb', line 52

def open_story_in_browser
  if story
    Command.open_url story.url
  else
    Command.error "Not currently on story branch"
  end
end

#pushObject



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

def push
  Command.run "git push origin #{name} -u"
end

#resetObject



64
65
66
67
68
69
70
# File 'lib/cp8_cli/branch.rb', line 64

def reset
  if dirty?
    Command.error "Dirty working directory, not resetting."
  else
    Command.run("git reset --hard origin/#{name}")
  end
end

#storyObject



36
37
38
# File 'lib/cp8_cli/branch.rb', line 36

def story
  @_story ||= StoryQuery.new(short_link).find if short_link
end

#targetObject



60
61
62
# File 'lib/cp8_cli/branch.rb', line 60

def target
  name_parts[2] || "master"
end

#to_sObject



72
73
74
# File 'lib/cp8_cli/branch.rb', line 72

def to_s
  name
end