Module jumpscale.clients.git.git
Expand source code
import git
from jumpscale.clients.base import Client
from jumpscale.core.base import fields
from jumpscale.loader import j
class GitClient(Client):
path = fields.String()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__repo = None
@property
def repo(self):
if not self.__repo:
self.__repo = git.Repo(self.path)
return self.__repo
def set_remote_url(self, url, remote_name="origin"):
remote = self.repo.remote(remote_name)
remote.set_url(url)
@property
def remote_url(self):
return self.repo.remote().url
@property
def branch_name(self):
return self.repo.active_branch.name
def get_modified_files(self):
"""returns local changes in the repo
Returns:
dict: dict containing different types of changes(check git status man)
"""
modified_files = self.repo.git.status(porcelain=True).splitlines()
result_format = {}
for mod_file in modified_files:
if "??" in mod_file:
continue
state, file_name = mod_file.split()
result_format.setdefault(state, [])
result_format[state].append(file_name)
untracked_files = self.repo.untracked_files
if untracked_files:
result_format["N"] = untracked_files
return result_format
def pull(self):
"""Pulls from origin
Raises:
j.exceptions.Input: if there is locaal changes
"""
if self.get_modified_files():
raise j.exceptions.Input(message="Cannot pull:{}, files waiting to commit".format(self.path))
self.repo.git.pull()
def commit(self, message, add_all=True):
"""adds a commit
Args:
message (str): commit message
add_all (bool, optional): will add all changes before commiting. Defaults to True.
Returns:
[type]: [description]
"""
if add_all and self.get_modified_files():
self.repo.git.add("-A")
if self.repo.index.diff("HEAD"):
return self.repo.index.commit(message)
Classes
class GitClient (*args, **kwargs)
-
A simple attribute-based namespace.
SimpleNamespace(**kwargs)
base class implementation for any class with fields which supports getting/setting raw data for any instance fields.
any instance can have an optional name and a parent.
class Person(Base): name = fields.String() age = fields.Float() p = Person(name="ahmed", age="19") print(p.name, p.age)
Args
parent_
:Base
, optional- parent instance. Defaults to None.
instance_name_
:str
, optional- instance name. Defaults to None.
**values
- any given field values to initiate the instance with
Expand source code
class GitClient(Client): path = fields.String() def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__repo = None @property def repo(self): if not self.__repo: self.__repo = git.Repo(self.path) return self.__repo def set_remote_url(self, url, remote_name="origin"): remote = self.repo.remote(remote_name) remote.set_url(url) @property def remote_url(self): return self.repo.remote().url @property def branch_name(self): return self.repo.active_branch.name def get_modified_files(self): """returns local changes in the repo Returns: dict: dict containing different types of changes(check git status man) """ modified_files = self.repo.git.status(porcelain=True).splitlines() result_format = {} for mod_file in modified_files: if "??" in mod_file: continue state, file_name = mod_file.split() result_format.setdefault(state, []) result_format[state].append(file_name) untracked_files = self.repo.untracked_files if untracked_files: result_format["N"] = untracked_files return result_format def pull(self): """Pulls from origin Raises: j.exceptions.Input: if there is locaal changes """ if self.get_modified_files(): raise j.exceptions.Input(message="Cannot pull:{}, files waiting to commit".format(self.path)) self.repo.git.pull() def commit(self, message, add_all=True): """adds a commit Args: message (str): commit message add_all (bool, optional): will add all changes before commiting. Defaults to True. Returns: [type]: [description] """ if add_all and self.get_modified_files(): self.repo.git.add("-A") if self.repo.index.diff("HEAD"): return self.repo.index.commit(message)
Ancestors
Instance variables
var branch_name
-
Expand source code
@property def branch_name(self): return self.repo.active_branch.name
var path
-
getter method this property
will call
_get_value
, which would if the value is already defined and will get the default value if notReturns
any
- the field value
Expand source code
def getter(self): """ getter method this property will call `_get_value`, which would if the value is already defined and will get the default value if not Returns: any: the field value """ return self._get_value(name, field)
var remote_url
-
Expand source code
@property def remote_url(self): return self.repo.remote().url
var repo
-
Expand source code
@property def repo(self): if not self.__repo: self.__repo = git.Repo(self.path) return self.__repo
Methods
def commit(self, message, add_all=True)
-
adds a commit
Args
message
:str
- commit message
add_all
:bool
, optional- will add all changes before commiting. Defaults to True.
Returns
[type]
- [description]
Expand source code
def commit(self, message, add_all=True): """adds a commit Args: message (str): commit message add_all (bool, optional): will add all changes before commiting. Defaults to True. Returns: [type]: [description] """ if add_all and self.get_modified_files(): self.repo.git.add("-A") if self.repo.index.diff("HEAD"): return self.repo.index.commit(message)
def get_modified_files(self)
-
returns local changes in the repo
Returns
dict
- dict containing different types of changes(check git status man)
Expand source code
def get_modified_files(self): """returns local changes in the repo Returns: dict: dict containing different types of changes(check git status man) """ modified_files = self.repo.git.status(porcelain=True).splitlines() result_format = {} for mod_file in modified_files: if "??" in mod_file: continue state, file_name = mod_file.split() result_format.setdefault(state, []) result_format[state].append(file_name) untracked_files = self.repo.untracked_files if untracked_files: result_format["N"] = untracked_files return result_format
def pull(self)
-
Pulls from origin
Raises
j.exceptions.Input
- if there is locaal changes
Expand source code
def pull(self): """Pulls from origin Raises: j.exceptions.Input: if there is locaal changes """ if self.get_modified_files(): raise j.exceptions.Input(message="Cannot pull:{}, files waiting to commit".format(self.path)) self.repo.git.pull()
def set_remote_url(self, url, remote_name='origin')
-
Expand source code
def set_remote_url(self, url, remote_name="origin"): remote = self.repo.remote(remote_name) remote.set_url(url)
Inherited members