Browse Source

Colorize and merge branch update lines into one.

tags/v0.2
Ben Kurtovic 10 years ago
parent
commit
9a1473ef8b
1 changed files with 19 additions and 15 deletions
  1. +19
    -15
      gitup/update.py

+ 19
- 15
gitup/update.py View File

@@ -14,9 +14,10 @@ from git.util import RemoteProgress
__all__ = ["update_bookmarks", "update_directories"] __all__ = ["update_bookmarks", "update_directories"]


BOLD = Style.BRIGHT BOLD = Style.BRIGHT
RED = Fore.RED + BOLD
GREEN = Fore.GREEN + BOLD
BLUE = Fore.BLUE + BOLD BLUE = Fore.BLUE + BOLD
GREEN = Fore.GREEN + BOLD
RED = Fore.RED + BOLD
YELLOW = Fore.YELLOW + BOLD
RESET = Style.RESET_ALL RESET = Style.RESET_ALL


INDENT1 = " " * 3 INDENT1 = " " * 3
@@ -103,57 +104,57 @@ def _is_up_to_date(repo, branch, upstream):


def _rebase(repo, name): def _rebase(repo, name):
"""Rebase the current HEAD of *repo* onto the branch *name*.""" """Rebase the current HEAD of *repo* onto the branch *name*."""
print(" rebasing...", end="")
print(GREEN + "rebasing...", end="")
try: try:
res = repo.git.rebase(name) res = repo.git.rebase(name)
except exc.GitCommandError as err: except exc.GitCommandError as err:
msg = err.stderr.replace("\n", " ").strip() msg = err.stderr.replace("\n", " ").strip()
if "unstaged changes" in msg: if "unstaged changes" in msg:
print(" error:", "unstaged changes.")
print(RED, "error:" + RESET, "unstaged changes", end=")")
elif "uncommitted changes" in msg: elif "uncommitted changes" in msg:
print(" error:", "uncommitted changes.")
print(RED, "error:" + RESET, "uncommitted changes", end=")")
else: else:
try: try:
repo.git.rebase("--abort") repo.git.rebase("--abort")
except exc.GitCommandError: except exc.GitCommandError:
pass pass
print(" error:", msg if msg else "rebase conflict")
print(RED, "error:" + RESET, msg if msg else "conflict", end=")")
else: else:
print(" done.")
print("\b" * 6 + " " * 6 + "\b" * 6 + GREEN + "ed", end=")")


def _merge(repo, name): def _merge(repo, name):
"""Merge the branch *name* into the current HEAD of *repo*.""" """Merge the branch *name* into the current HEAD of *repo*."""
print(" merging...", end="")
print(GREEN + "merging...", end="")
try: try:
repo.git.merge(name) repo.git.merge(name)
except exc.GitCommandError as err: except exc.GitCommandError as err:
msg = err.stderr.replace("\n", " ").strip() msg = err.stderr.replace("\n", " ").strip()
if "local changes" in msg and "would be overwritten" in msg: if "local changes" in msg and "would be overwritten" in msg:
print(" error:", "uncommitted changes.")
print(RED, "error:" + RESET, "uncommitted changes", end=")")
else: else:
try: try:
repo.git.merge("--abort") repo.git.merge("--abort")
except exc.GitCommandError: except exc.GitCommandError:
pass pass
print(" error:", msg if msg else "merge conflict")
print(RED, "error:" + RESET, msg if msg else "conflict", end=")")
else: else:
print(" done.")
print("\b" * 6 + " " * 6 + "\b" * 6 + GREEN + "ed", end=")")


def _update_branch(repo, branch, merge, rebase, stasher=None): def _update_branch(repo, branch, merge, rebase, stasher=None):
"""Update a single branch.""" """Update a single branch."""
print(INDENT2, "Updating", branch, end="...")
print(BOLD + branch.name, end=" (")
upstream = branch.tracking_branch() upstream = branch.tracking_branch()
if not upstream: if not upstream:
print(" skipped: no upstream is tracked.")
print(YELLOW + "skipped:" + RESET, "no upstream is tracked", end=")")
return return


try: try:
branch.commit, upstream.commit branch.commit, upstream.commit
except ValueError: except ValueError:
print(" skipped: branch contains no revisions.")
print(YELLOW + "skipped:" + RESET, "branch has no revisions", end=")")
return return
if _is_up_to_date(repo, branch, upstream): if _is_up_to_date(repo, branch, upstream):
print(" up to date.")
print(BLUE + "up to date", end=")")
return return


if stasher: if stasher:
@@ -167,16 +168,19 @@ def _update_branch(repo, branch, merge, rebase, stasher=None):


def _update_branches(repo, active, merge, rebase): def _update_branches(repo, active, merge, rebase):
"""Update a list of branches.""" """Update a list of branches."""
print(INDENT2, "Updating: ", end="")
_update_branch(repo, active, merge, rebase) _update_branch(repo, active, merge, rebase)
branches = set(repo.heads) - {active} branches = set(repo.heads) - {active}
if branches: if branches:
stasher = _Stasher(repo) stasher = _Stasher(repo)
try: try:
for branch in sorted(branches, key=lambda b: b.name): for branch in sorted(branches, key=lambda b: b.name):
print(", ", end="")
_update_branch(repo, branch, merge, rebase, stasher) _update_branch(repo, branch, merge, rebase, stasher)
finally: finally:
active.checkout() active.checkout()
stasher.restore() stasher.restore()
print(".")


def _update_repository(repo, current_only=False, rebase=False, merge=False): def _update_repository(repo, current_only=False, rebase=False, merge=False):
"""Update a single git repository by fetching remotes and rebasing/merging. """Update a single git repository by fetching remotes and rebasing/merging.


Loading…
Cancel
Save