我如何在远程 git 存储库中找到重新调整基数的提交

问题描述 投票:0回答:1

现在,我正在尝试使用 Python 在远程 git 存储库中查找重新调整的提交。 我使用Github api找到它。 存储库很大,有很多贡献者和分支。 在某些分支中,找到新的提交不是问题。 但我无法识别哪些提交是在该分支新提交的,哪些提交是从其他分支重新建立的。

如果有人知道请帮助我

python git branch commit
1个回答
0
投票
def compare_states(current_state, previous_state):
new_branches = []
updated_branches = []
deleted_branches = []
rebased_commits = []

# Set of branch keys (repo_owner, repo_name, branch_name) for the current state
current_branch_keys = {(b['repo_owner'], b['repo_name'], b['branch_name']) for b in current_state}

for current_branch in current_state:
    repo_full_name = f"{current_branch['repo_owner']}/{current_branch['repo_name']}"
    repo = g.get_repo(repo_full_name)
    
    # Find the corresponding branch in the previous state
    previous_branch = next((b for b in previous_state 
                            if b["repo_owner"] == current_branch["repo_owner"] 
                            and b["repo_name"] == current_branch["repo_name"] 
                            and b["branch_name"] == current_branch["branch_name"]), None)
    
    # Case 1: New Branch
    if previous_branch is None:
        # Check against the default branch to detect new branches
        parent_branch = find_parent_branch(repo, current_branch)  # Implement this to find the parent
        comparison = repo.compare(parent_branch, current_branch["branch_name"])
        
        # If commits exist in the comparison, it's a new branch
        if comparison.commits:
            new_branches.append({
                "repo_owner": current_branch["repo_owner"],
                "repo_name": current_branch["repo_name"],
                "branch_name": current_branch["branch_name"],
                "commit_hash": current_branch["commit_hash"],
                "commits": convert_commits(comparison.commits)
            })
    
    # Case 2: Updated or Rebasing Branch
    else:
        # Compare the current and previous commit hashes to detect changes
        if current_branch["commit_hash"] != previous_branch["commit_hash"]:
            comparison = repo.compare(previous_branch["commit_hash"], current_branch["commit_hash"])
            updated_commits = convert_commits(comparison.commits)
            # Case 2a: Rebasing branch
            for commit in updated_commits:
                if is_rebased(commit["sha"], repo_full_name):
                    rebased_commits.append(commit)
            # Case 2b: Regular update (but not rebased)
            else:
                updated_branches.append({
                    "repo_owner": current_branch["repo_owner"],
                    "repo_name": current_branch["repo_name"],
                    "branch_name": current_branch["branch_name"],
                    "current_commit_hash": current_branch["commit_hash"],
                    "previous_commit_hash": previous_branch["commit_hash"],
                    "commits": updated_commits
                })

# Case 3: Deleted Branches
for previous_branch in previous_state:
    # If a branch in previous_state is not in current_state, it's deleted
    if (previous_branch['repo_owner'], previous_branch['repo_name'], previous_branch['branch_name']) not in current_branch_keys:
        deleted_branches.append(previous_branch)

# Return the results as 4 separate lists: new, updated, deleted, and rebased branches
return new_branches, updated_branches, deleted_branches, rebased_commits

通过检查基本提交 SHA 来确定分支是否已重新建立基础。

def is_rebased(commit_sha, repo_full_name): pass # 在这里实现逻辑

© www.soinside.com 2019 - 2024. All rights reserved.