Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
spderosso committed Oct 29, 2013
2 parents 0c1c9f4 + 7bd9a55 commit 6ec296f
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 33 deletions.
36 changes: 30 additions & 6 deletions gitpylib/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,45 @@
SUCCESS = 1
UNFETCHED_OBJECT = 2
INVALID_NAME = 3
NONEXISTENT_BRANCH = 4
BRANCH_ALREADY_EXISTS = 5
INVALID_SP = 6


def checkout(name):
"""Checkout branch.
Args:
name: the name of the branch to checkout.
Returns:
SUCCESS or NONEXISTENT_BRANCH
"""
common.safe_git_call('checkout %s' % name)
ok, _, _ = common.git_call('checkout %s' % name)
if not ok:
return NONEXISTENT_BRANCH
return SUCCESS


def create(name):
def create(name, sp='HEAD'):
"""Creates a new branch with the given name.
Args:
name: the name of the branch to create.
sp: starting point. The commit from where to 'branch out.' (Defaults to
HEAD.)
Returns:
SUCCESS, INVALID_NAME or BRANCH_ALREADY_EXISTS.
"""
ok, unused_out, err = common.git_call('branch %s' % name)
ok, _, err = common.git_call('branch {} {}'.format(name, sp))
if not ok:
# TODO(sperezde): check for other errors?
return INVALID_NAME
if 'is not a valid branch name' in err:
return INVALID_NAME
elif 'already exists' in err:
return BRANCH_ALREADY_EXISTS
elif 'Not a valid object name' in err:
return INVALID_SP
return SUCCESS


Expand All @@ -42,8 +60,14 @@ def force_delete(name):
Args:
name: the name of the branch to delete.
Returns:
SUCCESS or NONEXISTENT_BRANCH
"""
common.safe_git_call('branch -D %s' % name)
ok, _, _ = common.git_call('branch -D %s' % name)
if not ok:
return NONEXISTENT_BRANCH
return SUCCESS


def current():
Expand Down
2 changes: 1 addition & 1 deletion gitpylib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def remove_dups(list, key):
Given two elements e1, e2 from list, e1 is considered to be a duplicate of e2
if key(e1) == key(e2).
Args:
list: the list to read from.
key: a function that receives an element from list and returns its key.
Expand Down
28 changes: 28 additions & 0 deletions gitpylib/hook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# gitpylib - a Python library for Git.
# Copyright (c) 2013 Santiago Perez De Rosso.
# Licensed under GNU GPL, version 2.

"""Hook module for running Git hooks."""


import collections
import os
import subprocess

import common


def pre_commit():
"""Runs the pre-commit hook."""
return _hook_call('pre-commit')


def _hook_call(hook_name):
HookCall = collections.namedtuple('hook_call', ['ok', 'out', 'err'])
hook_path = '{}/hooks/{}'.format(common.git_dir(), hook_name)
if not os.path.exists(hook_path):
return HookCall(True, '', '')
p = subprocess.Popen(
hook_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = p.communicate()
return HookCall(p.returncode == 0, out, err)
2 changes: 0 additions & 2 deletions gitpylib/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import subprocess

import common


def log():
subprocess.call('git log', shell=True)
Expand Down
4 changes: 2 additions & 2 deletions gitpylib/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _status_from_output(s, fp):
if s == '?':
# We need to see if it is an ignored file.
out, unused_err = common.safe_git_call('status --porcelain "%s"' % fp)
if len(out) is 0:
if not len(out):
return IGNORED
return UNTRACKED
elif s == 'h':
Expand All @@ -93,7 +93,7 @@ def _status_from_output(s, fp):
# We need to use status --porcelain to figure out whether it's deleted,
# modified or not.
out, unused_err = common.safe_git_call('status --porcelain "%s"' % fp)
if len(out) is 0:
if not len(out):
return TRACKED_UNMODIFIED
# Output is in the form <status> <name>. We are only interested in the
# status part.
Expand Down
29 changes: 9 additions & 20 deletions gitpylib/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,24 @@
PUSH_FAIL = 7


def commit(files, msg):
def commit(files, msg, skip_checks=False, stage_files=False):
"""Record changes in the local repository.
Args:
files: the files to commit.
msg: the commit message.
skip_checks: if the pre-commit hook should be skipped or not (defaults to
False).
stage_files: whether to stage the given files before commiting or not.
Returns:
The output of the commit command.
the output of the commit command.
"""
out, unused_err = common.safe_git_call(
'commit -m"%s" "%s"' % (msg, '" "'.join(files)))
return out


def commit_include(files, msg):
"""Record changes in the local repository.
Before making a commit of changes staged so far, the files given are staged.
Args:
files: the files to stage before commiting.
msg: the commit message.
Returns:
The output of the commit command.
"""
out, unused_err = common.safe_git_call(
'commit -m\"%s\" -i %s' % (msg, ' '.join(files)))
'commit {}{}-m"{}" "{}"'.format(
'--no-verify ' if skip_checks else '',
'-i ' if stage_files else '',
msg, '" "'.join(files)))
return out


Expand Down
4 changes: 2 additions & 2 deletions setup.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env python

from distutils.core import setup
from setuptools import setup


setup(
name='gitpylib',
version='0.1',
version='0.2.1',
description='A Python library for Git',
long_description=open('README.md').read(),
author='Santiago Perez De Rosso',
Expand Down

0 comments on commit 6ec296f

Please sign in to comment.