Class: GitAutocomplete::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/git_autocomplete/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run!(cmd_str) ⇒ Object



3
4
5
# File 'lib/git_autocomplete/command.rb', line 3

def self.run!(cmd_str)
  self.new.run!(cmd_str)
end

Instance Method Details

#branch_completion?(cmd_str) ⇒ Boolean

Returns the after_match string, if the prefix of the cmd_str is a valid command that expects a branch name.

Returns:

  • (Boolean)


20
21
22
23
24
25
# File 'lib/git_autocomplete/command.rb', line 20

def branch_completion?(cmd_str)
  valid_cmds = ['git checkout', 'git-checkout', 'git co', 'gco', 'git merge', 'git-merge', 'git diff', 'git-diff']
  valid_cmds_str = valid_cmds.join('|')
  return nil unless /^(#{valid_cmds_str})\b/ =~ cmd_str
  $'.strip # after_match
end

#branch_matches(prefix) ⇒ Object

Returns a sublist of all branches for this repository that start with prefix That is, if “git branch -a” returns the following list of branches:

master
  • newstuff origin/master origin/newstuff

Then branch_matches(“orig”) returns [‘origin/master’, ‘origin/newstuff’]



35
36
37
# File 'lib/git_autocomplete/command.rb', line 35

def branch_matches(prefix)
  branches.select { |branch| /^#{Regexp.escape prefix}/ =~ branch }
end

#branchesObject



39
40
41
42
# File 'lib/git_autocomplete/command.rb', line 39

def branches
  branches = `git branch -a | sed -e "s/..//"`
  branches.split("\n").sort
end

#run!(cmd_str) ⇒ Object

Returns a list of autocompletion options based on the command-line string enter already; e.g. “git checkout mas” would return [‘master’]

(assuming repos all have master branch)


11
12
13
14
15
16
# File 'lib/git_autocomplete/command.rb', line 11

def run!(cmd_str)
  if branch_prefix = branch_completion?(cmd_str)
    return branch_matches(branch_prefix)
  end
  []
end