[DEV] add git command file to centralize the git command access

This commit is contained in:
Edouard DUPIN 2019-05-30 13:55:05 +02:00
parent e4ce48df7c
commit 27936e3ac1

188
island/commands.py Normal file
View File

@ -0,0 +1,188 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
##
## @author Edouard DUPIN
##
## @copyright 2012, Edouard DUPIN, all right reserved
##
## @license MPL v2.0 (see license file)
##
import os
import shutil
import errno
import fnmatch
import stat
# Local import
from realog import debug
from . import env
from . import multiprocess
from . import debug
"""
"""
def check_repository_is_modify(path_repository):
# check if the repository is modify
cmd = "git diff --quiet"
debug.verbose("execute : " + cmd)
ret_diff = multiprocess.run_command(cmd, cwd=path_repository)
if ret_diff[0] == 0:
return False
return True
def get_list_branch_meta(path_repository):
# get local branch
cmd = "git branch -a"
debug.verbose("execute : " + cmd)
ret_branch = multiprocess.run_command(cmd, cwd=path_repository)
list_branch = ret_branch[1].split('\n')
out = []
for elem_branch in list_branch:
is_remote = False
branch_name = ""
is_selected = False
if len(elem_branch.split(" -> ")) != 1:
continue
# separate the remote element
if elem_branch[2:10] == "remotes/":
elem_branch = elem_branch[:2] + elem_branch[10:]
is_remote = True
# separate select branch
if elem_branch[:2] == "* ":
is_selected = True
branch_name = elem_branch[2:]
else:
branch_name = elem_branch[2:]
out.append({
"remote": is_remote,
"name": branch_name,
"select": is_selected
})
debug.extreme_verbose("List all branch Meta: " + str(out))
return out
def get_list_branch_all(path_repository):
tmp = get_list_branch_meta(path_repository)
out = []
for elem in tmp:
out.append(elem["name"])
debug.verbose("List all branch: " + str(out))
return out
def get_list_branch_local(path_repository):
tmp = get_list_branch_meta(path_repository)
out = []
for elem in tmp:
if elem["remote"] == False:
out.append(elem["name"])
debug.verbose("List local branch: " + str(out))
return out
def get_list_branch_remote(path_repository):
tmp = get_list_branch_meta(path_repository)
out = []
for elem in tmp:
if elem["remote"] == True:
out.append(elem["name"])
debug.verbose("List remote branch: " + str(out))
return out
def get_current_branch(path_repository):
tmp = get_list_branch_meta(path_repository)
for elem in tmp:
if elem["select"] == True:
debug.verbose("List local branch: " + str(elem["name"]))
return elem["name"]
debug.verbose("List local branch: None" )
return None
def get_current_tracking_branch(path_repository):
# get tracking branch
cmd = "git rev-parse --abbrev-ref --symbolic-full-name @{u}"
debug.verbose("execute : " + cmd)
return multiprocess.run_command(cmd, cwd=path_repository)[1]
def get_revision_list_to_branch(path_repository, branch):
cmd = "git rev-list " + branch
debug.verbose("execute : " + cmd)
return multiprocess.run_command(cmd, cwd=path_repository)[1].split('\n')
def get_specific_commit_message(path_repository, sha_1):
if sha_1 == None or sha_1 == "":
return ""
cmd = "git log --format=%B -n 1 " + sha_1
debug.verbose("execute : " + cmd)
return multiprocess.run_command(cmd, cwd=path_repository)[1].split('\n')[0]
def get_sha1_for_branch(path_repository, branch_name):
if branch_name == None or branch_name == "":
return None
cmd = "git rev-parse" + sha_1
debug.verbose("execute : " + cmd)
return multiprocess.run_command(cmd, cwd=path_repository)[1].split('\n')[0]
def get_tracking_branch(path_repository, remote_name, select_branch):
# get tracking branch
if remote_name == "" or remote_name == None:
return get_current_tracking_branch(path_repository)
list_branch_remote = commands.get_list_branch_remote(path_repository)
debug.extreme_verbose("check if exist " + remote_name + "/" + select_branch + " in " + str(list_branch_remote))
if remote_name + "/" + select_branch not in list_branch_remote:
debug.debug(" ==> can not get remote branch")
return None
return remote_name + "/" + select_branch
def merge_branch_on_master(path_repository, branch_name):
if branch_name == None or branch_name == "":
raise "Missing branch name"
cmd = "git merge --no-ff " + branch_name + " --message \"Merge branch '" + branch_name + "' into 'master'\""
debug.verbose("execute : " + cmd)
# TODO: check if the command work correctly
return multiprocess.run_command(cmd, cwd=path_repository)
def add_file(path_repository, file_path):
if file_path == None or file_path == "":
raise "Missing file_path name"
cmd = "git add " + file_path
debug.verbose("execute : " + cmd)
# TODO: check if the command work correctly
return multiprocess.run_command(cmd, cwd=path_repository)
def commit_all(path_repository, comment):
if comment == None or comment == "":
raise "Missing comment description"
cmd = 'git commit -a --message "' + comment +'"'
debug.verbose("execute : " + cmd)
# TODO: check if the command work correctly
return multiprocess.run_command(cmd, cwd=path_repository)
def tag(path_repository, tag_name):
if tag_name == None or tag_name == "":
raise "Missing tag name"
tag_name = tag_name.replace(" ", "_")
cmd = 'git tag ' + tag_name + ' --message "[TAG] create tag ' + tag_name +'"'
debug.verbose("execute : " + cmd)
# TODO: check if the command work correctly
return multiprocess.run_command(cmd, cwd=path_repository)
def checkout(path_repository, branch_name):
if branch_name == None or branch_name == "":
raise "Missing branch name"
cmd = 'git checkout ' + branch_name
debug.verbose("execute : " + cmd)
# TODO: check if the command work correctly
return multiprocess.run_command(cmd, cwd=path_repository)
def reset_hard(path_repository, destination):
if destination == None or destination == "":
raise "Missing destination 'sha1' or 'branch name'"
cmd = 'git reset --hard ' + destination
debug.verbose("execute : " + cmd)
# TODO: check if the command work correctly
return multiprocess.run_command(cmd, cwd=path_repository)