Skip to content

Commit afb5229

Browse files
committed
src/scripts/config-diff.py: simplify sparse_branch_checkout_* functions and add files names to POSIX diff
Refactored `sparse_branch_checkout_skip_clone` and `sparse_branch_checkout_remote_repo_skip_clone` to accept and use branch/tag names directly instead of constructing `ref_sha` strings throughout the code. Also include filenames from where the configuration values are coming from in the POSIX diff. This helps identify the config options faster in case of descrepancies. Signed-off-by: Naveen Naidu <[email protected]>
1 parent fc3dd98 commit afb5229

File tree

1 file changed

+24
-35
lines changed

1 file changed

+24
-35
lines changed

src/script/config-diff/config_diff.py

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -60,43 +60,45 @@ def git_show_yaml_files(hexsha: str, repo: Repo):
6060
return config_options
6161

6262

63-
def sparse_branch_checkout_remote_repo_skip_clone(remote_repo, ref_sha) -> Repo:
63+
def sparse_branch_checkout_remote_repo_skip_clone(
64+
remote_repo, remote_branch_name, local_branch_name
65+
) -> Repo:
6466
repo = Repo(".", search_parent_directories=True)
6567
git_cmd = repo.git
6668

6769
local_branches = [
6870
branch.strip().lstrip("*").strip() for branch in git_cmd.branch("--list", "-r").splitlines()
6971
]
7072

71-
branch_name = ref_sha.split(":")[1]
72-
branch_present = any(branch_name in branch for branch in local_branches)
73+
branch_present = any(local_branch_name in branch for branch in local_branches)
7374
if not branch_present:
75+
ref_sha = remote_branch_name + ":" + local_branch_name
7476
git_cmd.remote("add", REMOTE_REPO_GIT_REMOTE_NAME, remote_repo)
7577
git_cmd.fetch(
7678
REMOTE_REPO_GIT_REMOTE_NAME,
7779
ref_sha,
7880
"--depth=1",
7981
)
8082

81-
if not folder_exists_in_branch(branch_name, git_cmd, CEPH_CONFIG_OPTIONS_FOLDER_PATH):
83+
if not folder_exists_in_branch(local_branch_name, git_cmd, CEPH_CONFIG_OPTIONS_FOLDER_PATH):
8284
git_cmd.sparse_checkout("add", CEPH_CONFIG_OPTIONS_FOLDER_PATH)
8385
git_cmd.checkout()
8486

8587
return repo
8688

8789

88-
def sparse_branch_checkout_skip_clone(ref_sha) -> Repo:
90+
def sparse_branch_checkout_skip_clone(branch_name) -> Repo:
8991
repo = Repo(".", search_parent_directories=True)
9092
git_cmd = repo.git
9193

9294
local_branches = [
9395
branch.strip().lstrip("*").strip() for branch in git_cmd.branch("--list").splitlines()
9496
]
9597

96-
branch_name = ref_sha.split(":")[0]
9798
branch_present = any(branch_name in branch for branch in local_branches)
9899

99100
if not branch_present:
101+
ref_sha = branch_name + ":" + branch_name
100102
git_cmd.fetch(
101103
"origin",
102104
ref_sha,
@@ -185,21 +187,21 @@ def print_diff_posix_format(diff_result: dict):
185187
# Handle added configurations
186188
for daemon, added_configs in diff_result.get("added", {}).items():
187189
for config in added_configs:
188-
print(f"+ added: {config}")
190+
print(f"+ added: {config} ({daemon})")
189191

190192
# Handle deleted configurations
191193
for daemon, deleted_configs in diff_result.get("deleted", {}).items():
192194
for config in deleted_configs:
193-
print(f"- removed: {config}")
195+
print(f"- removed: {config} ({daemon})")
194196

195197
# Handle modified configurations
196198
for daemon, modified_configs in diff_result.get("modified", {}).items():
197199
for config, changes in modified_configs.items():
198200
for key, change in changes.items():
199201
before = change.get("before", "")
200202
after = change.get("after", "")
201-
print(f"! changed: {config}: old: {before}")
202-
print(f"! changed: {config}: new: {after}")
203+
print(f"! changed: {config}: old: {before} ({daemon})")
204+
print(f"! changed: {config}: new: {after} ({daemon})")
203205

204206

205207
def get_daemons_config_names(daemons, daemon_configs):
@@ -377,10 +379,8 @@ def diff_branch(
377379
final_result = {}
378380

379381
if skip_clone:
380-
ref_sha = ref_branch + ":" + ref_branch
381-
cmp_sha = cmp_branch + ":" + cmp_branch
382-
ref_git_repo = sparse_branch_checkout_skip_clone(ref_sha)
383-
cmp_git_repo = sparse_branch_checkout_skip_clone(cmp_sha)
382+
ref_git_repo = sparse_branch_checkout_skip_clone(ref_branch)
383+
cmp_git_repo = sparse_branch_checkout_skip_clone(cmp_branch)
384384
ref_config_dict = git_show_yaml_files(ref_branch, ref_git_repo)
385385
config_dict = git_show_yaml_files(cmp_branch, cmp_git_repo)
386386
final_result = diff_config(ref_config_dict, config_dict)
@@ -405,12 +405,6 @@ def diff_branch(
405405
print()
406406

407407

408-
"""
409-
ref_sha = "'refs/tags/" + ref_tag + ":" + "refs/tags/" + ref_tag + "'"
410-
cmp_sha = "'refs/tags/" + cmp_tag + ":" + "refs/tags/" + cmp_tag + "'"
411-
"""
412-
413-
414408
def diff_tags(ref_repo: str, ref_tag: str, cmp_tag: str, skip_clone: bool, format_type: str):
415409
"""
416410
Perform a diff between two tags in the same repository.
@@ -425,15 +419,10 @@ def diff_tags(ref_repo: str, ref_tag: str, cmp_tag: str, skip_clone: bool, forma
425419
final_result = {}
426420

427421
if skip_clone:
428-
ref_sha_local_repo_name = "refs/tags/" + ref_tag + "'"
429-
cmp_sha_local_repo_name = "refs/tags/" + cmp_tag + "'"
430-
ref_sha = "'refs/tags/" + ref_tag + ":" + ref_sha_local_repo_name
431-
cmp_sha = "'refs/tags/" + cmp_tag + ":" + cmp_sha_local_repo_name
432-
433-
ref_git_repo = sparse_branch_checkout_skip_clone(ref_sha)
434-
cmp_git_repo = sparse_branch_checkout_skip_clone(cmp_sha)
435-
ref_config_dict = git_show_yaml_files(ref_sha_local_repo_name, ref_git_repo)
436-
config_dict = git_show_yaml_files(cmp_sha_local_repo_name, cmp_git_repo)
422+
ref_git_repo = sparse_branch_checkout_skip_clone(ref_tag)
423+
cmp_git_repo = sparse_branch_checkout_skip_clone(cmp_tag)
424+
ref_config_dict = git_show_yaml_files(ref_tag, ref_git_repo)
425+
config_dict = git_show_yaml_files(cmp_tag, cmp_git_repo)
437426
final_result = diff_config(ref_config_dict, config_dict)
438427

439428
ref_git_repo.close()
@@ -477,16 +466,16 @@ def diff_branch_remote_repo(
477466
"""
478467
final_result = {}
479468
if skip_clone:
480-
ref_sha = ref_branch + ":" + ref_branch
481-
cmp_sha_local_branch_name = REMOTE_REPO_GIT_REMOTE_NAME + "/" + cmp_branch
482-
cmp_sha = cmp_branch + ":" + cmp_sha_local_branch_name
483-
ref_git_repo = sparse_branch_checkout_skip_clone(ref_sha)
484-
remote_git_repo = sparse_branch_checkout_remote_repo_skip_clone(remote_repo, cmp_sha)
469+
cmp_branch_local_branch_name = REMOTE_REPO_GIT_REMOTE_NAME + "/" + cmp_branch
470+
ref_git_repo = sparse_branch_checkout_skip_clone(ref_branch)
471+
remote_git_repo = sparse_branch_checkout_remote_repo_skip_clone(
472+
remote_repo, cmp_branch, cmp_branch_local_branch_name
473+
)
485474
ref_config_dict = git_show_yaml_files(ref_branch, ref_git_repo)
486475

487476
# To show the files from remote repo, you need to append the remote name
488477
# before the branch
489-
config_dict = git_show_yaml_files(cmp_sha_local_branch_name, remote_git_repo)
478+
config_dict = git_show_yaml_files(cmp_branch_local_branch_name, remote_git_repo)
490479

491480
final_result = diff_config(ref_config_dict, config_dict)
492481

0 commit comments

Comments
 (0)