@@ -51,13 +51,54 @@ def get_current_version_as_dict(cls):
5151 version = cls .get_release_version_as_dict ()
5252 info = Info ()
5353 try :
54- tweak = int (
55- Shell .get_output (
56- f"git rev-list --count { version ['githash' ]} ..HEAD" , verbose = True
54+ # Check if the commit is directly on the first-parent chain
55+ is_on_first_parent = Shell .get_output (
56+ f"git rev-list --first-parent HEAD | grep -q { version ['githash' ]} && echo 'yes' || echo 'no'" ,
57+ verbose = True ,
58+ ).strip ()
59+
60+ if is_on_first_parent == "yes" :
61+ # Commit is directly on the first-parent chain (upstream scenario)
62+ # Use simple first-parent counting
63+ tweak = int (
64+ Shell .get_output (
65+ f"git rev-list --count --first-parent { version ['githash' ]} ..HEAD" ,
66+ verbose = True ,
67+ )
5768 )
58- )
59- except ValueError :
60- # Shallow checkout
69+ else :
70+ # Commit is not on first-parent chain (fork with sync scenario)
71+ # Find the merge commit where this commit entered the first-parent chain
72+ # Iterate backwards to find the last commit that has target as ancestor (the merge point)
73+ merge_commit = Shell .get_output (
74+ f"commits=$(git rev-list --first-parent HEAD); "
75+ f"prev=''; "
76+ f"for commit in $commits; do "
77+ f"if git merge-base --is-ancestor { version ['githash' ]} $commit 2>/dev/null; then "
78+ f"prev=$commit; "
79+ f"else "
80+ f"echo $prev; break; "
81+ f"fi; done" ,
82+ verbose = True ,
83+ ).strip ()
84+
85+ if merge_commit :
86+ tweak = int (
87+ Shell .get_output (
88+ f"git rev-list --count --first-parent { merge_commit } ..HEAD" ,
89+ verbose = True ,
90+ )
91+ )
92+ else :
93+ # Fallback if we can't find the merge point
94+ tweak = int (
95+ Shell .get_output (
96+ f"git rev-list --count --first-parent { version ['githash' ]} ..HEAD" ,
97+ verbose = True ,
98+ )
99+ )
100+ except (ValueError , Exception ):
101+ # Shallow checkout or other error
61102 tweak = 1
62103 version_type = "testing"
63104 if info .pr_number == 0 and bool (
@@ -95,3 +136,8 @@ def get_release_sha(cls):
95136 @classmethod
96137 def store_version_data_in_ci_pipeline (cls ):
97138 Info ().store_kv_data ("version" , cls .get_current_version_as_dict ())
139+
140+
141+ if __name__ == "__main__" :
142+ # test;
143+ print (CHVersion .get_current_version_as_dict ())
0 commit comments