예제 #1
0
class GitFile:

    def __init__(self, path):
        self.__path = path
        self.__new = True
        self.__git = Git(dirname(path))

    def read(self):
        if exists(self.__path):
            with open(self.__path, 'r') as input:
                for line in input:
                    line = line.strip()
                    if line and not line.startswith('#'):
                        epoch, sentence = line.split(' ', 1)
                        yield int(epoch), sentence
                    self.__new = False

    def __header(self):
        with open(self.__path, 'w') as output:
            output.write('''
# each non-comment line contains a unix epoch, followed by a sentence,
# encoded in ascii, and terminated by a newline character (unix style).

# the epoch is the time at which the sentence was created.  obviously there
# may be some delay before it is visible.  sentences 'end' when the next one
# is created.  the gap is typically of order %d seconds, but is not
# guaranteed (there is a random - stochastic - component as well as the
# possibility of service downtimes, etc).

# blank lines and those starting with a # are comments.

''' % PERIOD)

    def __push(self):
        try:
            self.__git.add(self.__path)
            self.__git.commit(self.__path, m='new sentence')
            self.__git.push()
        except GitException as e:
            eprint(e)

    def write(self, append, epoch, sentence):
        if self.__new:
            self.__header()
            self.__push()
            self.__new = False
        if append:
            with open(self.__path, 'a') as output:
                output.write("%d %s\n" % (epoch, sentence))
            self.__push()
예제 #2
0
class GitCommandList:
    _repo = None
    _local_git_path = None
    _repo_url = None

    # to initialize the repository variable
    def init_git(self, local_git_path, repo_url):
        try:
            self._local_git_path = local_git_path
            self._repo_url = repo_url
            self._repo = Git(self._local_git_path, self._repo_url)
            self._repo.pull()
            return True
        except:
            return False

    # to make a commit, without doing a push on the remote repository on git
    def commit(self, message='Auto-commit: ' + str(datetime.now())):
        try:
            for file in local_tree(self._local_git_path):
                self._repo.add(file)
            self._repo.commit(message)
            return True
        except:
            return False

    # To push che files in the local repository.
    # A commit is generated automatically
    def push(self):
        try:

            self.commit()
            self._repo.pretty_log()
            r = self._repo.push()
            return '[*] Pushing at: ' + str(datetime.now()) + '\n' + r
        except:
            return False

    # to pull in the local from the remote repository
    def pull(self):
        try:
            self._repo.pretty_log()
            r = self._repo.pull()
            return '[*] Pulling at: ' + str(datetime.now()) + '\n' + r
        except Exception as e:
            print(e)
            return False
예제 #3
0
def test_basic():
    shutil.rmtree("/tmp/brigit_test", ignore_errors=True)
    git = Git("/tmp/brigit_test")
    with open("/tmp/brigit_test/file_1", "w") as f:
        f.write('1')
    git.add("/tmp/brigit_test/file_1")
    git.commit(message="Adding file_1")
    assert "Adding file_1" in git.log()
    assert len(list(git.pretty_log())) == 1
    with open("/tmp/brigit_test/file_2", "w") as f:
        f.write('2')
    git.add("/tmp/brigit_test/file_2")
    git.commit(message="Adding file_2")
    assert "Adding file_2" in git.log()
    assert len(list(git.pretty_log())) == 2
    git.reset("HEAD~1")
    assert len(list(git.pretty_log())) == 1
    assert "Untracked files:\n\tfile_2" in git.status()
    shutil.rmtree("/tmp/brigit_test")
예제 #4
0
def test_basic():
    shutil.rmtree("/tmp/brigit_test", ignore_errors=True)
    git = Git("/tmp/brigit_test")
    with open("/tmp/brigit_test/file_1", "w") as f:
        f.write('1')
    git.add("/tmp/brigit_test/file_1")
    git.commit(message="Adding file_1")
    assert "Adding file_1" in git.log()
    assert len(list(git.pretty_log())) == 1
    with open("/tmp/brigit_test/file_2", "w") as f:
        f.write('2')
    git.add("/tmp/brigit_test/file_2")
    git.commit(message="Adding file_2")
    assert "Adding file_2" in git.log()
    assert len(list(git.pretty_log())) == 2
    git.reset("HEAD~1")
    assert len(list(git.pretty_log())) == 1
    assert "Untracked files:\n\tfile_2" in git.status()
    git.clean("-fdx")
    git.branch("newbranch")
    with open("/tmp/brigit_test/file_3", "w") as f:
        f.write('3')
    git.add("/tmp/brigit_test/file_3")
    git.commit(message="Adding file_3")
    assert "Adding file_3" in git.log()
    git.checkout("newbranch")
    git.cherryPick("master")
    shutil.rmtree("/tmp/brigit_test")