Use Python to get a list of branches in Git

Source: Internet
Author: User

Through this collection of GIT Project branch information examples, to illustrate the Python and terminal information interaction, and the simple processing of strings.
The code is as follows:

?
1234567891011121314151617 import osimport subprocessdef get_branches(project_dir):    try:        os.chdir(project_dir)        #转到工程路径下    except Exception,error:        print error    branches_str = subprocess.check_output(["git", "branch"])    #终端运行“git branch”命令,并且将终端的输出str转存到branches_str里     branches = branches_str.split(‘\n‘)    #使用str的split方法将其按照‘\n‘分割    branch_list = []    for branch in branches[0:-1]:        branch_list.append(branch.lstrip(‘* ‘))        #使用str的lstrip方法将字符串的前的空格和当前branch前的“*”标记去除    return branch_list

Use Python to get a list of branches in Git

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.