From 2fc6d1f6264154e41ae234e15ae76ccaf70dfbf4 Mon Sep 17 00:00:00 2001 From: Sasha Levin Date: Mon, 9 Jun 2025 21:43:26 -0400 Subject: [PATCH 1/2] fix(results): Display correct git URL when using --giturl parameter The results summary command was not displaying the provided git URL when using the --giturl parameter. Instead, it would show the default torvalds/linux.git URL. This fix ensures the correct URL is displayed along with branch and commit information when explicitly provided. Signed-off-by: Sasha Levin --- kcidev/libs/git_repo.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/kcidev/libs/git_repo.py b/kcidev/libs/git_repo.py index 53fa300..e31fdbc 100644 --- a/kcidev/libs/git_repo.py +++ b/kcidev/libs/git_repo.py @@ -101,6 +101,14 @@ def get_latest_commit(origin, giturl, branch): def set_giturl_branch_commit(origin, giturl, branch, commit, latest, git_folder): if not giturl or not branch or not ((commit != None) ^ latest): giturl, branch, commit = get_folder_repository(git_folder, branch) + else: + # Print git folder and tree info when giturl is provided + kci_msg("git folder: " + str(git_folder)) + kci_msg("tree: " + giturl) + if branch: + kci_msg("branch: " + branch) + if commit: + kci_msg("commit: " + commit) if latest: commit = get_latest_commit(origin, giturl, branch) return giturl, branch, commit From d66e7daf6d303aa1af2fc52c0b531ca5a2a2575c Mon Sep 17 00:00:00 2001 From: Sasha Levin Date: Mon, 9 Jun 2025 21:49:25 -0400 Subject: [PATCH 2/2] refactor(results): Auto-fill missing git parameters from local repository Refactored set_giturl_branch_commit to automatically fill in any missing parameters (giturl, branch, or commit) from the local git repository. This provides a more flexible and user-friendly interface where users can omit parameters and have them intelligently defaulted from their local git context. Signed-off-by: Sasha Levin --- kcidev/libs/git_repo.py | 129 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 119 insertions(+), 10 deletions(-) diff --git a/kcidev/libs/git_repo.py b/kcidev/libs/git_repo.py index e31fdbc..f5a97c9 100644 --- a/kcidev/libs/git_repo.py +++ b/kcidev/libs/git_repo.py @@ -88,6 +88,97 @@ def get_folder_repository(git_folder, branch): raise click.Abort() +def get_current_branch_name(git_folder): + """Get the current branch name from a git repository.""" + if git_folder: + current_folder = git_folder + else: + current_folder = os.getcwd() + + previous_folder = os.getcwd() + if os.path.isdir(current_folder): + os.chdir(current_folder) + else: + os.chdir(previous_folder) + return None + + if is_inside_work_tree(): + process = subprocess.Popen( + ["git", "branch", "--show-current"], stdout=subprocess.PIPE, text=True + ) + branch_name, _ = process.communicate() + branch_name = branch_name.strip() + os.chdir(previous_folder) + return branch_name + else: + os.chdir(previous_folder) + return None + + +def get_current_commit_hash(git_folder): + """Get the current commit hash from a git repository.""" + if git_folder: + current_folder = git_folder + else: + current_folder = os.getcwd() + + previous_folder = os.getcwd() + if os.path.isdir(current_folder): + os.chdir(current_folder) + else: + os.chdir(previous_folder) + return None + + if is_inside_work_tree(): + process = subprocess.Popen( + ["git", "rev-parse", "HEAD"], stdout=subprocess.PIPE, text=True + ) + commit_hash, _ = process.communicate() + commit_hash = commit_hash.strip() + os.chdir(previous_folder) + return commit_hash + else: + os.chdir(previous_folder) + return None + + +def get_repository_url(git_folder): + """Get the repository URL from a git repository.""" + if git_folder: + current_folder = git_folder + else: + current_folder = os.getcwd() + + previous_folder = os.getcwd() + if os.path.isdir(current_folder): + os.chdir(current_folder) + else: + os.chdir(previous_folder) + return None + + dot_git_folder = os.path.join(current_folder, ".git") + if is_inside_work_tree(): + while not os.path.exists(dot_git_folder): + current_folder = os.path.join(current_folder, "..") + dot_git_folder = os.path.join(current_folder, ".git") + + if os.path.exists(dot_git_folder): + git_config_path = os.path.join(dot_git_folder, "config") + git_config = configparser.ConfigParser(strict=False) + git_config.read(git_config_path) + try: + git_url = git_config.get('remote "origin"', "url") + git_url = repository_url_cleaner(git_url) + os.chdir(previous_folder) + return git_url + except: + os.chdir(previous_folder) + return None + else: + os.chdir(previous_folder) + return None + + def get_latest_commit(origin, giturl, branch): trees = dashboard_fetch_tree_list(origin, False) for t in trees: @@ -99,16 +190,34 @@ def get_latest_commit(origin, giturl, branch): def set_giturl_branch_commit(origin, giturl, branch, commit, latest, git_folder): - if not giturl or not branch or not ((commit != None) ^ latest): - giturl, branch, commit = get_folder_repository(git_folder, branch) - else: - # Print git folder and tree info when giturl is provided - kci_msg("git folder: " + str(git_folder)) - kci_msg("tree: " + giturl) - if branch: - kci_msg("branch: " + branch) - if commit: - kci_msg("commit: " + commit) + # Fill in any missing parameters from local git repository + if not giturl: + giturl = get_repository_url(git_folder) + if not giturl: + kci_err("No git URL provided and could not determine from local repository") + raise click.Abort() + + if not branch: + branch = get_current_branch_name(git_folder) + if not branch: + kci_err("No branch provided and could not determine from local repository") + raise click.Abort() + + if not commit and not latest: + commit = get_current_commit_hash(git_folder) + if not commit: + kci_err("No commit provided and could not determine from local repository") + raise click.Abort() + + # Print the final values + kci_msg("git folder: " + str(git_folder)) + kci_msg("tree: " + giturl) + kci_msg("branch: " + branch) + if commit: + kci_msg("commit: " + commit) + if latest: commit = get_latest_commit(origin, giturl, branch) + kci_msg("commit: " + commit) + return giturl, branch, commit