def merge_message_path(): """Return the path to .git/MERGE_MSG or .git/SQUASH_MSG.""" for basename in ('MERGE_MSG', 'SQUASH_MSG'): path = git.git_path(basename) if os.path.exists(path): return path return None
def current_branch(git=git): """Return the current branch""" head = git.git_path('HEAD') try: key = os.stat(head).st_mtime if _current_branch.key == key: return _current_branch.value except OSError, e: pass
def abort_merge(): """Abort a merge by reading the tree at HEAD.""" # Reset the worktree git.read_tree('HEAD', reset=True, u=True, v=True) # remove MERGE_HEAD merge_head = git.git_path('MERGE_HEAD') if os.path.exists(merge_head): os.unlink(merge_head) # remove MERGE_MESSAGE, etc. merge_msg_path = merge_message_path() while merge_msg_path: os.unlink(merge_msg_path) merge_msg_path = merge_message_path()
def _read_git_head(head, default='master', git=git): """Pure-python .git/HEAD reader""" # Legacy .git/HEAD symlinks if os.path.islink(head): refs_heads = os.path.realpath(git.git_path('refs', 'heads')) path = os.path.abspath(head).replace('\\', '/') if path.startswith(refs_heads + '/'): return path[len(refs_heads)+1:] # Common .git/HEAD "ref: refs/heads/master" file elif os.path.isfile(head): data = utils.slurp(core.decode(head)).rstrip() ref_prefix = 'ref: ' if data.startswith(ref_prefix): return data[len(ref_prefix):] # Detached head return data return default
def merge_message(revision): """Return a merge message for FETCH_HEAD.""" fetch_head = git.git_path('FETCH_HEAD') if os.path.exists(fetch_head): return git.fmt_merge_msg('--file', fetch_head) return "Merge branch '%s'" % revision