Skip to content

Commit

Permalink
subfeature support (#153)
Browse files Browse the repository at this point in the history
* Add new 'subfeature' command to create subfeatures
* Update 'feature' to properly handle a base branch
* Fix warning message about bad tracked branches
* Update help message to include arguments

Closes #151

Signed-off-by: Phil Dibowitz <phil@ipom.com>
  • Loading branch information
jaymzh authored Mar 21, 2024
1 parent 0ba1e66 commit cc4ff44
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
20 changes: 13 additions & 7 deletions bin/sj
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ COMMANDS:
Same as "amend" but without changing the message. Alias for
"git commit --amend --no-edit".
bclean
If safe, delete the current branch. Unlike "git branch -d",
bclean can handle squash-merged branches. Think of it as
a smarter "git branch -d".
bclean [<branch>]
If safe, delete the current branch (or the specified branch).
Unlike "git branch -d", bclean can handle squash-merged branches.
Think of it as a smarter "git branch -d".
bcleanall
Walk all branches, and try to delete them if it's safe. See
Expand All @@ -127,7 +127,7 @@ COMMANDS:
br
Verbose branch list. An alias for "git branch -v".
feature
feature, f <branch_name>
Create a "feature" branch. It's morally equivalent to
"git checkout -b" except it defaults to creating it based on
some form of 'master' instead of your current branch. In order
Expand Down Expand Up @@ -177,11 +177,17 @@ COMMANDS:
A smart wrapper to "git push" that runs whatever is defined in
"on_push" in .sugarjar.yml, and only pushes if they succeed.
subfeature, sf <feature>
An alias for 'sj feature <feature> <current_branch>'
unit
Run any unitests configured in .sugarjar.yaml.
up
Rebase the current branch on upstream/master or origin/master.
up [<branch>]
Rebase the current branch (or specified branch) intelligently.
In most causes this will check for a main (or master) branch on
upstream, then origin. If a branch explicitly tracks something
else, then that will be used, instead.
upall
Same as "up", but for all branches.
Expand Down
35 changes: 28 additions & 7 deletions lib/sugarjar/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,25 @@ def feature(name, base = nil)
name = fprefix(name)
die("#{name} already exists!") if all_local_branches.include?(name)
base ||= most_main
base_pieces = base.split('/')
git('fetch', base_pieces[0]) if base_pieces.length > 1
# If our base is a local branch, don't try to parse it for a remote name
unless all_local_branches.include?(base)
base_pieces = base.split('/')
git('fetch', base_pieces[0]) if base_pieces.length > 1
end
git('checkout', '-b', name, base)
SugarJar::Log.info(
"Created feature branch #{color(name, :green)} based on " +
color(base, :green),
)
end
alias f feature

def subfeature(name)
assert_in_repo
SugarJar::Log.debug("Subfature: #{name}")
feature(name, current_branch)
end
alias sf subfeature

def bclean(name = nil)
assert_in_repo
Expand Down Expand Up @@ -133,11 +144,14 @@ def smartlog

alias sl smartlog

def up
def up(branch = nil)
assert_in_repo
branch ||= current_branch
branch = fprefix(branch)
# get a copy of our current branch, if rebase fails, we won't
# be able to determine it without backing out
curr = current_branch
git('checkout', branch)
result = gitup
if result['so'].error?
backout = ''
Expand All @@ -156,6 +170,8 @@ def up
SugarJar::Log.info(
"#{color(current_branch, :green)} rebased on #{result['base']}",
)
# go back to where we were if we rebased a different branch
git('checkout', curr) if branch != curr
end
end

Expand Down Expand Up @@ -719,6 +735,10 @@ def all_local_branches
branches
end

def all_remotes
git('remote').stdout.lines.map(&:strip)
end

def safe_to_clean(branch)
# cherry -v will output 1 line per commit on the target branch
# prefixed by a - or + - anything with a - can be dropped, anything
Expand Down Expand Up @@ -795,13 +815,16 @@ def gitup
fetch_upstream
curr = current_branch
base = tracked_branch
# If this is a subfeature based on a local branch which has since
# been deleted, 'tracked branch' will automatically return <most_main>
# so we don't need any special handling for that
if !MAIN_BRANCHES.include?(curr) && base == "origin/#{curr}"
SugarJar::Log.warn(
"This branch is tracking origin/#{curr}, which is probably your " +
'downstream (where you push _to_) as opposed to your upstream ' +
'(where you pull _from_). This means that "sj up" is probably ' +
'rebasing on the wrong thing and doing nothing. You probably want ' +
'to do a "git branch -u upstream".',
"to do a 'git branch -u #{most_main}'.",
)
end
SugarJar::Log.debug('Rebasing')
Expand Down Expand Up @@ -844,9 +867,7 @@ def most_main
def upstream
return @remote if @remote

s = git('remote')

remotes = s.stdout.lines.map(&:strip)
remotes = all_remotes
SugarJar::Log.debug("remotes is #{remotes}")
if remotes.empty?
@remote = nil
Expand Down

0 comments on commit cc4ff44

Please sign in to comment.