def start(self, name, starting_point=None): """Start a new step (branch).""" # make sure this is not a known checkpoint if name in self.points(): raise TutException("Duplicate checkpoint.") # make sure the repo is clean if self._repo_dirty(): raise TutException("Dirty tree.") # create the new branch git.branch(name) if starting_point is None: args = ('-b', name) else: args = ('-b', name, starting_point, '--track') # add the branch to config config = self._config() points = config['points'] if self.current(): points.insert(points.index(self.current()) + 1, name) else: points.append(name) self._update_config( config, log='Adding new point %s' % name, ) # checkout the new branch git.checkout(name)
def cleanup_merge(): git.checkout("master") # if something failed, the branch might still exist if self.gd.branch_exists("to_merge_1"): git.branch("-D", "to_merge_1") git.branch("-D", "base_branch")
def reply_comment_email(from_email, subject, message): try: m = re.search('\[(\d+)\-(\w+)\]', subject) branch_name = m.group(1) article = m.group(2) message = decode_best_effort(message) # safe logic: no answer or unknown answer is a go for publishing if message[:2].upper() == 'NO': logger.info('discard comment: %s' % branch_name) email_body = get_template('drop_comment').render(original=message) mail(pecosys.get_config('post', 'from_email'), pecosys.get_config('post', 'to_email'), 'Re: ' + subject, email_body) else: if pecosys.get_config("git", "disabled"): logger.debug("GIT usage disabled (debug mode)") else: git.merge(branch_name) if pecosys.get_config("git", "remote"): git.push() logger.info('commit comment: %s' % branch_name) # execute post-commit command asynchronously postcommand = pecosys.get_config('post', 'postcommand') if postcommand: executor.execute(postcommand) # send approval confirmation email to admin email_body = get_template('approve_comment').render(original=message) mail(pecosys.get_config('post', 'from_email'), pecosys.get_config('post', 'to_email'), 'Re: ' + subject, email_body) # notify reader once comment is published reader_email, article_url = get_email_metadata(message) if reader_email: notify_reader(reader_email, article_url) # notify subscribers every time a new comment is published notify_subscribers(article) if pecosys.get_config("git", "disabled"): logger.debug("GIT usage disabled (debug mode)") else: git.branch("-D", branch_name) except: logger.exception("new email failure")
def setUp(self): super(TestBranch, self).setUp() # Build up an interesting mock repo. utils_lib.write_file(TRACKED_FP, contents=TRACKED_FP_CONTENTS_1) git.add(TRACKED_FP) git.commit(TRACKED_FP, m='1') utils_lib.write_file(TRACKED_FP, contents=TRACKED_FP_CONTENTS_2) git.commit(TRACKED_FP, m='2') utils_lib.write_file(UNTRACKED_FP, contents=UNTRACKED_FP_CONTENTS) utils_lib.write_file('.gitignore', contents='{0}'.format(IGNORED_FP)) utils_lib.write_file(IGNORED_FP) git.branch(BRANCH) self.curr_b = self.repo.current_branch
def newest_study_id(self): "Return the numeric part of the newest study_id" os.chdir(self.repo) git.checkout("master") dirs = [] # first we look for studies already in our master branch for f in os.listdir("study/"): if os.path.isdir("study/%s" % f): # ignore alphabetic prefix, o = created by opentree API if f[0].isalpha(): dirs.append(int(f[1:])) else: dirs.append(int(f)) # next we must look at local branch names for new studies # without --no-color we get terminal color codes in the branch output branches = git.branch("--no-color") branches = [ b.strip() for b in branches ] for b in branches: mo = re.match(".+_o(\d+)",b) if mo: dirs.append(int(mo.group(1))) dirs.sort() return dirs[-1]
def newest_study_id(self): "Return the numeric part of the newest study_id" os.chdir(self.repo) git.checkout("master") dirs = [] # first we look for studies already in our master branch for f in os.listdir("study/"): if os.path.isdir("study/%s" % f): # ignore alphabetic prefix, o = created by opentree API if f[0].isalpha(): dirs.append(int(f[1:])) else: dirs.append(int(f)) # next we must look at local branch names for new studies # without --no-color we get terminal color codes in the branch output branches = git.branch("--no-color") branches = [b.strip() for b in branches] for b in branches: mo = re.match(".+_o(\d+)", b) if mo: dirs.append(int(mo.group(1))) dirs.sort() return dirs[-1]
def check_branch_exists(branch): with Spinner(f"Checking for {branch} branch"): all_branches = [ l.strip() for l in git.branch(all=True, _tty_out=False).strip().split("\n") ] for b in all_branches: if b.endswith(branch): return True return False
def delete(version_number): heading1("Deleting version " + version_number + "\n") cd(base_dir) heading2("Removing branch from git.") print(git.checkout("master")) print(git.branch("-D", version_number)) print(git.remote("prune", "origin")) heading2("Deleting files.") print(rm("-rf", base_dir + "/fedora-kickstarts"))
def list_branches(package, ignore): for name in get_immediate_subdirectories("."): if name in packages: cd(name) branches = sed(git.branch(), r="s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g") branches = [str(branch.strip()) for branch in branches] active = [branch for branch in branches if branch[0] == '*'][0] active = active.strip("* ") if not active in ignore: print "{2}: {0} {1}".format(active, branches, name) cd("..")
def test_branch_switch_performance(self): MAX_TOLERANCE = 100 gl.commit('f1', m='commit') t = time.time() gl.branch(c='develop') gl.switch('develop') gl_t = time.time() - t # go back to previous state gl.switch('master') # do the same for git t = time.time() git.branch('gitdev') git.stash.save('--all') git.checkout('gitdev') git_t = time.time() - t self.assertTrue(gl_t < git_t * MAX_TOLERANCE, msg='gl_t {0}, git_t {1}'.format(gl_t, git_t))
def test_branch_switch_performance(self): MAX_TOLERANCE = 100 gl.commit(o='f1', m='commit') t = time.time() gl.branch(c='develop') gl.switch('develop') gl_t = time.time() - t # go back to previous state gl.switch('master') # do the same for git t = time.time() git.branch('gitdev') git.stash.save('--all') git.checkout('gitdev') git_t = time.time() - t self.assertTrue( gl_t < git_t*MAX_TOLERANCE, msg='gl_t {0}, git_t {1}'.format(gl_t, git_t))
def init(self): """Create a new repository with an initial commit.""" cwd = os.getcwd() # initialize the empty repository git.init(self.path) os.chdir(self.path) git.commit( m='Initializing empty Tut project.', allow_empty=True, ) # create the empty configuration file git.branch('tut') self._update_config( DEFAULT_CONFIG, log='Initializing Tut configuration.', ) git.checkout('master') os.chdir(cwd)
def auto_update() -> bool: """Updates the application from a github repo.""" add_remote = git.bake('remote', 'add', '-f', '--tags', '-m', 'master', '-t', 'master', 'origin', 'https://github.com/threadreaper/autodomme.git') if '.git' not in os.listdir(os.getcwd()): git('init') add_remote() git.branch('--set-upstream-to', 'origin/master') elif git('remote').exit_code != 0: add_remote() git.branch('--set-upstream-to', 'origin/master') files = [ line for line in git('--no-pager', 'diff', '--name-only', _iter=True) ] if files: filenames = [filter_filename(file) for file in files[:-1]] update_popup(filenames) return True else: return False
def merge(self, branch, base_branch="master"): """ Merge the the given WIP branch to master (or base_branch, if specified) If the merge fails, the merge will be aborted and then a MergeException will be thrown. The message of the MergeException will be the "git status" output, so details about merge conflicts can be determined. """ os.chdir(self.repo) current_branch = self.current_branch() if current_branch != base_branch: git.checkout(base_branch) # Always create a merge commit, even if we could fast forward, so we know # when merges occured try: merge_output = git.merge("--no-ff", branch) except sh.ErrorReturnCode: # attempt to reset things so other operations can # continue output = git.status() git.merge("--abort") # re-raise the exception so other code can decide # what to do with it raise MergeException(output) # the merge succeeded, so remove the local WIP branch git.branch("-d", branch) new_sha = git("rev-parse","HEAD") return new_sha.strip()
def merge(self, branch, base_branch="master"): """ Merge the the given WIP branch to master (or base_branch, if specified) If the merge fails, the merge will be aborted and then a MergeException will be thrown. The message of the MergeException will be the "git status" output, so details about merge conflicts can be determined. """ os.chdir(self.repo) current_branch = self.current_branch() if current_branch != base_branch: git.checkout(base_branch) # Always create a merge commit, even if we could fast forward, so we know # when merges occured try: merge_output = git.merge("--no-ff", branch) except sh.ErrorReturnCode: # attempt to reset things so other operations can # continue output = git.status() git.merge("--abort") # re-raise the exception so other code can decide # what to do with it raise MergeException(output) # the merge succeeded, so remove the local WIP branch git.branch("-d", branch) new_sha = git("rev-parse", "HEAD") return new_sha.strip()
def new_comment(author, email, site, url, article, message, subscribe): try: logger.info("new comment received: author %s url %s subscribe %s" % (author, url, subscribe)) if site and site[:4] != "http": site = "http://" + site # Create comment now = datetime.now() comment_list = ( 'author: %s' % author, 'email: %s' % email, 'site: %s' % site, 'date: %s' % now.strftime("%Y-%m-%d %H:%M:%S"), 'url: %s' % url, 'article: %s' % article, '', '%s' % message, '' ) comment = '\n'.join(comment_list) logger.debug(comment) # Git branch_name = now.strftime("%Y%m%d%H%M%S%f") if pecosys.get_config("git", "disabled"): logger.debug("GIT usage disabled (debug mode)") else: git.checkout('master') if pecosys.get_config("git", "remote"): git.pull() try: git.checkout(branch_name) except: git.branch(branch_name) git.checkout(branch_name) comment_filename = 'comment-%s.md' % now.strftime("%Y%m%d-%H%M%S") comment_pathname = '%s/%s/%s' % (pecosys.get_config('git', 'comment_path'), now.year, comment_filename) with open(comment_pathname, 'wb') as f: f.write(comment.encode('UTF-8')) git.add(comment_pathname) git.commit('-m', 'new comment') git.checkout('master') # Render email body template email_body = get_template('new_comment').render(url=url, comment=comment) # Send email mail(pecosys.get_config('post', 'from_email'), pecosys.get_config('post', 'to_email'), '[' + branch_name + '-' + article + ']', email_body) # Reader subscribes to further comments if subscribe and email: subscribe_reader(email, article, url) logger.debug("new comment processed ") except: logger.exception("new_comment failure")
def cleanup_newest(): git.checkout("master") git.branch("-D","leto_study_o9999")
def tearDownClass(self): git.branch("-D",self.testing_branch_name)
def cleanup_write(): git.checkout("master") git.branch("-D","johndoe_study_9998") git.branch("-D","johndoe_study_9999")
def cleanup_remove(): git.checkout("master") git.branch("-D","johndoe_study_777")
def cleanup_write(): git.checkout("master") git.branch("-D", "johndoe_study_9998") git.branch("-D", "johndoe_study_9999")
def current_branch(): branch = git.branch(show_current=True).strip() print(branch) return branch
def add_to_blacklist(**kwargs): if 'windows' in str(platform.platform()).lower(): log('warning', "Git support not available in Windows.") return (False, "Git support not available in Windows.") blacklist = kwargs.get("blacklist", "") item_to_blacklist = kwargs.get("item_to_blacklist", "") username = kwargs.get("username", "") chat_profile_link = kwargs.get("chat_profile_link", "http://chat.stackexchange.com/users") code_permissions = kwargs.get("code_permissions", False) # Make sure git credentials are set up if git.config("--global", "--get", "user.name", _ok_code=[0, 1]) == "": return (False, "Tell someone to run `git config --global user.name \"SmokeDetector\"`") if git.config("--global", "--get", "user.email", _ok_code=[0, 1]) == "": return (False, "Tell someone to run `git config --global user.email \"[email protected]\"`") if blacklist == "": # If we broke the code, and this isn't assigned, error out before doing anything, but do # so gracefully with a nice error message. return (False, "Programming Error - Critical information missing for GitManager: blacklist") if item_to_blacklist == "": # If we broke the code, and this isn't assigned, error out before doing anything, but do # so gracefully with a nice error message. return (False, "Programming Error - Critical information missing for GitManager: item_to_blacklist") item_to_blacklist = item_to_blacklist.replace("\s", " ") if blacklist == "website": blacklist_file_name = "blacklisted_websites.txt" ms_search_option = "&body_is_regex=1&body=" elif blacklist == "keyword": blacklist_file_name = "bad_keywords.txt" ms_search_option = "&body_is_regex=1&body=" elif blacklist == "username": blacklist_file_name = "blacklisted_usernames.txt" ms_search_option = "&username_is_regex=1&username="******"Invalid blacklist type specified, something has broken badly!") git.checkout("master") try: git.pull() except: pass # Check that we're up-to-date with origin (GitHub) git.remote.update() if git("rev-parse", "refs/remotes/origin/master").strip() != git("rev-parse", "master").strip(): return (False, "HEAD isn't at tip of origin's master branch") # Check that blacklisted_websites.txt isn't modified locally. That could get ugly fast if blacklist_file_name in git.status(): # Also ugly return (False, "{0} is modified locally. This is probably bad.".format(blacklist_file_name)) # Add item to file with open(blacklist_file_name, "a+") as blacklist_file: last_character = blacklist_file.read()[-1:] if last_character != "\n": blacklist_file.write("\n") blacklist_file.write(item_to_blacklist + "\n") # Checkout a new branch (for PRs for non-code-privileged people) branch = "auto-blacklist-{0}".format(str(time.time())) git.checkout("-b", branch) # Clear HEAD just in case git.reset("HEAD") git.add(blacklist_file_name) git.commit("--author='SmokeDetector <*****@*****.**>'", "-m", u"Auto blacklist of {0} by {1} --autopull".format(item_to_blacklist, username)) if code_permissions: git.checkout("master") git.merge(branch) git.push("origin", "master") git.branch('-D', branch) # Delete the branch in the local git tree since we're done with it. else: git.push("origin", branch) git.checkout("master") if GlobalVars.github_username is None or GlobalVars.github_password is None: return (False, "Tell someone to set a GH password") payload = {"title": u"{0}: Blacklist {1}".format(username, item_to_blacklist), "body": u"[{0}]({1}) requests the blacklist of the {2} {3}. See the Metasmoke search [here]" "(https://metasmoke.erwaysoftware.com/search?utf8=%E2%9C%93{4}{5})\n" u"<!-- METASMOKE-BLACKLIST-{6} {3} -->".format(username, chat_profile_link, blacklist, item_to_blacklist, ms_search_option, item_to_blacklist.replace(" ", "+"), blacklist.upper()), "head": branch, "base": "master"} response = requests.post("https://api.github.com/repos/Charcoal-SE/SmokeDetector/pulls", auth=HTTPBasicAuth(GlobalVars.github_username, GlobalVars.github_password), data=json.dumps(payload)) log('debug', response.json()) try: git.checkout("deploy") # Return to deploy, pending the accept of the PR in Master. git.branch('-D', branch) # Delete the branch in the local git tree since we're done with it. return (True, "You don't have code privileges, but I've [created a pull request for you]({0}).".format( response.json()["html_url"])) except KeyError: git.checkout("deploy") # Return to deploy # Delete the branch in the local git tree, we'll create it again if the # command is run again. This way, we keep things a little more clean in # the local git tree git.branch('-D', branch) # Error capture/checking for any "invalid" GH reply without an 'html_url' item, # which will throw a KeyError. if "bad credentials" in str(response.json()['message']).lower(): # Capture the case when GH credentials are bad or invalid return (False, "Something is wrong with the GH credentials, tell someone to check them.") else: # Capture any other invalid response cases. return (False, "A bad or invalid reply was received from GH, the message was: %s" % response.json()['message']) git.checkout("deploy") # Return to deploy to await CI. return (True, "Blacklisted {0}".format(item_to_blacklist))
def list(): heading1("Current versions") print (git.branch("--list")) print(path.dirname(path.realpath(__file__)))
def gen_branchs(file_path): '''return generator with branchs''' branches = git.branch('--all', '--color=never', _cwd=dir_path(file_path)) return branches
def tearDownClass(self): git.branch("-D", self.testing_branch_name)
def cleanup_remove(): git.checkout("master") git.branch("-D", "johndoe_study_777")
def push(remote_name, remote_branch, to_be_pushed_branch, commit_hash_list): git.status() current_branch = get_stdout(git('symbolic-ref', '--short', '-q', 'HEAD'))[0:-1] mark_message = '***mark***' # 校验是否tag已存在 tag_name = f"merging({current_branch}-->{remote_name}/{remote_branch})" code, output = subprocess.getstatusoutput(f"git tag | grep '^{tag_name}$'") if code == 0: print(f"错误,tag:{tag_name} 已存在") exit(-1) # 校验远程分支是否存在 code, output = subprocess.getstatusoutput('git fetch') if code != 0: print(output) print("fetch 失败") exit(-1) code, output = subprocess.getstatusoutput( f"git branch -r | cat | grep '^ *{remote_name}/{remote_branch}'") if code != 0: print("错误,远程分支%s不存在" % remote_branch) exit(-1) # 如果工作区不干净,mark提交 res = git.status() is_clean = re.match(".*nothing to commit, working tree clean.*", get_stdout(res), re.S) if not is_clean: print('工作区不干净,mark提交...') git.add('--all') git.commit('-a', '-m', mark_message) # 检出到远程临时分支 tmp_branch = f'tmp/local_{remote_name}_{remote_branch}' print(f'检出到远程临时分支"{tmp_branch}"...') git.checkout('-b', tmp_branch, f'{remote_name}/{remote_branch}') # cherry-pick try: print('开始cherry-pick...') res = git('cherry-pick', commit_hash_list) except sh.ErrorReturnCode_1: print(get_stderr(res)) print("请解决冲突,然后选择后续操作...") while True: operation = input('请输入处理方式') if operation == 0: print('execute: git cherry-pick --continue...') code, output = subprocess.getstatusoutput( 'git cherry-pick --continue') print(output) if code != 0: continue else: print('解决冲突成功') break elif operation == 1: print('退出,停在当前位置') git('check-pick', '--abort') exit(-1) elif operation == 2: print('撤销,回到起点...') git('check-pick', '--abort') # 切换会原来的分支,并去掉mark提交 git.switch(current_branch) git.branch('-D', tmp_branch) de_mark_commit(current_branch, mark_message) exit(-1) else: print('输入不合法,请重新输入') except ErrorReturnCode: print(get_stderr(res)) exit(-1) print('推送中...') git.push('-f', remote_name, f'HEAD:{to_be_pushed_branch}') # 切换会原来的分支,并去掉mark提交 print('切换会原来的分支,并去掉mark提交') git.switch(current_branch) git.branch('-D', tmp_branch) de_mark_commit(current_branch, mark_message) print(f'添加tag"{tag_name}"...') git.tag('-a', tag_name, '-m', "nothing") print('完成!')
def clean(): if path.exists('dist'): rmtree('dist') vers = input('Did you change the version? (y/n) ') if vers != 'y' and vers != 'n': print('Expected "y" or "n", instead found {}. Exiting.'.format(vers)) exit(1) if vers == 'n': exit(0) status = git.status() branch = git.branch() if 'master' not in branch: print('Must be on branch "master". Exiting.') exit(1) if 'Changes not staged for commit' in status or 'Untracked files' in status or 'Changes to be committed' in status: # get user input for if they want to commit all current changes # and continue with the deploy script # exit if no commit = input( 'Changes were detected, would you like to commit all current changes? (y/n) ' ) if commit != 'y' and commit != 'n': print('Expected "y" or "n", instead found {}. Exiting.'.format(commit)) exit(1)
# All operation succeed for the current compiled version. branch.writeCachedHash() return True if "branch" in sys.argv: #client = IrcClient() cd(blenderSourceDir) git.remote("prune", "origin") git.pull("--rebase") git.remote("prune", "origin") if "--branch" in sys.argv: branchNames = [sys.argv[sys.argv.index("--branch") + 1]] else: branchNames = str(git.branch("--all", "--color=never")).split(" ") branchNames = [name[15:-1] for name in branchNames if name.startswith("remotes/origin") and name != "remotes/origin/HEAD"] with open(branchMaskFile, "a+") as mask: mask.seek(0) maskBranch = [line[:-1] for line in mask.readlines()] for branchName in branchNames: if not os.path.exists(prefixSourceDir + "blender_" + branchName) and not branchName in maskBranch: while True: answer = input("Do you want mask branch " + branchName + "? (y/n): ") if answer == "y": mask.write(branchName + "\n") break; elif answer == "n": break;
def main(): searchDir(sys.argv[1]) print(git.branch("-v"))
def cleanup_newest(): git.checkout("master") git.branch("-D", "leto_study_o9999")