Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements git flow init (on python-rewrite) #135

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 56 additions & 4 deletions gitflow/bin/__init__.py
Expand Up @@ -14,6 +14,8 @@

"""
import argparse
import readline # this will trigger readline functionality for raw_input

from gitflow.core import GitFlow
from gitflow.util import itersubclasses

Expand All @@ -38,14 +40,64 @@ def register_parser(self, parent):
help='Initialize a repository for gitflow.')
p.add_argument('-f', '--force', action='store_true',
help='force reinitialization of the gitflow preferences')
p.add_argument('-d', '--use-defaults', action='store_true',
help='force the use of the defaults gitflow preferences')
p.set_defaults(func=self.run)
return p

def run(self, args):
print('-------------------')
print('Init has been run!')
print(args)
print('-------------------')
gitflow = GitFlow()
if gitflow.is_initialized() and not args.force:
warn('Git repository is already initialized.')
warn('')
warn('You can initialize it again:')
warn('')
warn(' git flow init --force')
warn('')
raise SystemExit()

if args.use_defaults:
gitflow.init(force_defaults=args.use_defaults)
else:
# Prompt master
master_suggestion = 'master'
if len(gitflow.repo.branches) == 0:
print('No branches exist yet. Base branches must be created now.')
else:
print('Which branch should be used for bringing forth production releases?')
for b in gitflow.repo.branches:
if b.name in ('production', 'main', 'master'):
master_suggestion = b.name
break
master = raw_input('Branch name for production releases: [%s] ' % master_suggestion)
master = master.strip() # remove whitespaces
if not master:
master = master_suggestion
# Prompt develop
develop_suggestion = 'develop'
if len(gitflow.repo.branches) > 0:
print('Which branch should be used for integration of the "next release"?')
for b in gitflow.repo.branches:
if b.name in ('develop', 'int', 'integration', 'master', 'next'):
develop_suggestion = b.name
break
develop = raw_input('Branch name for "next release" development: [%s] ' % develop_suggestion)
develop = develop.strip() # remove whitespaces
if not develop:
develop = develop_suggestion
if master == develop:
warn('Production and integration branches should differ.')
raise SystemExit()
# Prompt branch values
prefix = {}
for identifier in gitflow.managers:
mgr = gitflow.managers[identifier]
data = (mgr.identifier, mgr.prefix)
response = raw_input("What's the prefix for the %s branch? [%s] " % data)
if not response.strip():
response = mgr.prefix
prefix[identifier] = response
gitflow.init(master=master, develop=develop, prefixes=prefix)


class FeatureCommand(GitFlowCommand):
Expand Down
16 changes: 10 additions & 6 deletions gitflow/core.py
Expand Up @@ -86,22 +86,26 @@ def _init_config(self, master=None, develop=None, prefixes={}, force_defaults=Fa

def _init_initial_commit(self):
master = self.master_name()
# Only if 'master' branch does not exist
try:
self.repo.branches[master]
except:
# Only if 'master' branch does not exist
except (GitCommandError, IndexError):
c = self.repo.index.commit('Initial commit', head=False)
self.repo.create_head(master, c)

def _init_develop_branch(self):
# NOTE: This function assumes master already exists
try:
self.repo.branches[self.master_name()]
except (GitCommandError, IndexError):
raise NotInitialized("Master branch doesn't exist.")
develop_name = self.develop_name()
# Create branch if it doesn't exist
try:
self.repo.branches[develop_name]
except (GitCommandError, IndexError):
self.repo.create_head(develop_name, self.master_name())
except GitCommandError:
# on error, the branch existed already
pass



def init(self, master=None, develop=None, prefixes={}, force_defaults=False):
if self.repo is None:
Expand Down