Skip to content

Commit

Permalink
Add utility methods to Repository.
Browse files Browse the repository at this point in the history
  • Loading branch information
elibon99 committed Nov 10, 2023
1 parent c749658 commit 8953d87
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 38 deletions.
12 changes: 7 additions & 5 deletions gitbark/cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
add_branches_interactive,
add_commit_rules_interactive,
_confirm_commit,
checkout_or_orphan,
)

from gitbark.core import BARK_RULES_REF
Expand Down Expand Up @@ -72,6 +71,7 @@ def add_rules(ctx):
project = ctx.obj["project"]
add_commit_rules_interactive(project)
_confirm_commit(
project=project,
commit_message="Modify commit rules (made by bark).",
manual_action=(
"The 'commit_rules.yaml' file has been staged. "
Expand All @@ -86,15 +86,16 @@ def add_rules(ctx):
def add_modules(ctx):
"""Add bark modules."""
project = ctx.obj["project"]
curr_ref = cmd("git", "symbolic-ref", "HEAD")[0]
curr_ref = project.repo.current_ref
add_modules_interactive(project)
_confirm_commit(
project=project,
commit_message="Modify bark modules (made by bark).",
manual_action=(
"The 'bark_rules.yaml' file has been staged. Please commit the changes!"
),
)
checkout_or_orphan(project, curr_ref)
project.repo.checkout(branch_name(curr_ref), True)
click.echo("Bark modules configuration was committed successfully!")


Expand All @@ -108,15 +109,16 @@ def protect(ctx):
automatically.
"""
project = ctx.obj["project"]
curr_ref = cmd("git", "symbolic-ref", "HEAD")[0]
curr_ref = project.repo.current_ref
add_branches_interactive(project, curr_ref)
_confirm_commit(
project=project,
commit_message="Modify bark modules (made by bark).",
manual_action=(
"The 'bark_rules.yaml' file has been staged. " "Please commit the changes!"
),
)
checkout_or_orphan(project, curr_ref)
project.repo.checkout(branch_name(curr_ref))
click.echo("Bark modules configuration was committed successfully!")


Expand Down
52 changes: 19 additions & 33 deletions gitbark/commands/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ def newline() -> None:


def _confirm_commit(
project: Project,
commit_message: str,
prompt: str = "Do you want 'bark' to commit the changes?",
manual_action: str = "Please commit the changes and run 'bark setup' to continue!",
):
if click.confirm(prompt):
cmd("git", "commit", "-m", commit_message)
project.repo.commit(message=commit_message)
newline()
else:
click.echo(manual_action)
Expand Down Expand Up @@ -81,23 +82,6 @@ def dump_and_stage(project: Project, file: str, content: str) -> None:
cmd("git", "add", file)


def checkout_or_orphan(project: Project, ref: str) -> None:
curr_ref = cmd("git", "symbolic-ref", "HEAD")[0]
if curr_ref == ref:
return

try:
target = branch_name(ref)
except ValueError:
target = ref

if ref not in project.repo.references:
cmd("git", "checkout", "--orphan", target)
cmd("git", "reset", "--hard")
else:
cmd("git", "checkout", target)


def get_commit_rules(project: Project) -> dict:
cr_file = os.path.join(project.path, COMMIT_RULES)
if not os.path.exists(cr_file):
Expand Down Expand Up @@ -224,9 +208,7 @@ def add_branch_rules_interactive(ref: str) -> list:


def add_branches_interactive(project: Project, ref: str) -> None:
curr_ref = cmd("git", "symbolic-ref", "HEAD")[0]
if curr_ref != BARK_RULES_REF:
checkout_or_orphan(project, BARK_RULES_REF)
project.repo.checkout(branch_name(BARK_RULES_REF), True)

try:
bark_rules = get_bark_rules(project)
Expand Down Expand Up @@ -259,9 +241,7 @@ def add_branches_interactive(project: Project, ref: str) -> None:


def add_modules_interactive(project: Project) -> None:
curr_ref = cmd("git", "symbolic-ref", "--short", "HEAD")[0]
if curr_ref != BARK_RULES_REF:
checkout_or_orphan(project, BARK_RULES_REF)
project.repo.checkout(branch_name(BARK_RULES_REF), True)

click.echo("Define what Bark Modules to add!\n")
requirements = []
Expand All @@ -287,36 +267,42 @@ def add_modules_interactive(project: Project) -> None:


def setup(project: Project) -> None:
curr_ref = cmd("git", "symbolic-ref", "HEAD")[0]
curr_ref = project.repo.current_ref

if not has_valid_bark_rules(project):
if curr_ref != BARK_RULES_REF:
save_active_branch(project, curr_ref)
checkout_or_orphan(project, BARK_RULES_REF)
project.repo.checkout(branch_name(BARK_RULES_REF), True)
curr_ref = BARK_RULES_REF

add_modules_interactive(project)
newline()
add_commit_rules_interactive(project)

_confirm_commit(commit_message="Add initial modules and rules (made by bark).")
_confirm_commit(
project=project,
commit_message="Add initial modules and rules (made by bark).",
)

active_ref = get_active_branch(project)
if active_ref: # checkout if we have an active branch
checkout_or_orphan(project, active_ref)
project.repo.checkout(branch_name(active_ref), True)
curr_ref = active_ref
remove_active_branch(project)

if not get_commit_rules(project):
add_commit_rules_interactive(project)
_confirm_commit(commit_message="Initial rules (made by bark).")
_confirm_commit(project=project, commit_message="Initial rules (made by bark).")

if curr_ref != BARK_RULES_REF and not branch_in_bark_rules_yaml(project, curr_ref):
cmd("git", "checkout", branch_name(BARK_RULES_REF))
project.repo.checkout(branch_name(BARK_RULES_REF))
add_branches_interactive(project, curr_ref)
_confirm_commit(f"Add {curr_ref} to bark_rules (made by bark).")
cmd(
"git", "checkout", branch_name(curr_ref)
_confirm_commit(
project=project,
commit_message=f"Add {curr_ref} to bark_rules (made by bark).",
)
project.repo.checkout(
branch_name(curr_ref)
) # run this if the commit was made in interactive mode

click.echo("Bark is initialized!")
24 changes: 24 additions & 0 deletions gitbark/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

from .objects import RuleData
from .util import cmd, branch_name

from dataclasses import dataclass
from pygit2 import Commit as _Commit, Tree, Repository as _Repository
Expand Down Expand Up @@ -156,23 +157,46 @@ class Repository:
"""Git repo wrapper class"""

def __init__(self, path: str) -> None:
self._path = path
self._object = _Repository(path)

@property
def head(self) -> Commit:
return Commit(self._object.head.target.raw, self)

@property
def current_ref(self) -> str:
return self._object.references["HEAD"].target

@property
def references(self) -> dict[str, Commit]:
return {
ref.name: Commit(ref.target.raw, self)
for ref in self._object.references.iterator()
}

def branch_exists(self, branch: str) -> bool:
return branch in self._object.branches

def resolve(self, name: str) -> Tuple[Commit, Optional[str]]:
commit, ref = self._object.resolve_refish(name)
return Commit(commit.id.raw, self), (ref.name if ref else None)

def checkout(self, branch: str, orhpan: bool = False) -> None:
if branch_name(self.current_ref) == branch:
return
if not self.branch_exists(branch):
if orhpan:
cmd("git", "checkout", "--orphan", branch, cwd=self._path)
cmd("git", "reset", "--hard", cwd=self._path)
else:
cmd("git", "checkout", "-b", branch, cwd=self._path)
else:
cmd("git", "checkout", branch, cwd=self._path)

def commit(self, message: str = "Default msg", *options: str) -> None:
cmd("git", "commit", "-m", message, *options, cwd=self._path)


@dataclass
class ReferenceUpdate:
Expand Down

0 comments on commit 8953d87

Please sign in to comment.