@ -42,15 +42,12 @@ def parse_footers(message):
return footer_map
return footer_map
def matches_footer_key ( line , key ) :
def normalize_key ( line ) :
""" Returns whether line is a valid footer whose key matches a given one.
""" Returns the key of the footer line in normalized form. """
Keys are compared in normalized form .
"""
r = parse_footer ( line )
r = parse_footer ( line )
if r is None :
if r is None :
return False
return ' '
return normalize_name ( r [ 0 ] ) == normalize_name ( key )
return normalize_name ( r [ 0 ] )
def split_footers ( message ) :
def split_footers ( message ) :
@ -140,16 +137,27 @@ def add_footer(message, key, value, after_keys=None):
top_lines . append ( ' ' )
top_lines . append ( ' ' )
footer_lines = [ new_footer ]
footer_lines = [ new_footer ]
else :
else :
# find the index of the last footer with any of the after_keys.
after_keys = set ( map ( normalize_name , after_keys or [ ] ) )
after_keys = set ( map ( normalize_name , after_keys or [ ] ) )
after_indices = [
last_akey_idx = None
footer_lines . index ( x ) for x in footer_lines for k in after_keys
for idx in range ( len ( footer_lines ) ) :
if matches_footer_key ( x , k )
line = footer_lines [ idx ]
]
if after_indices :
# if the current footer is indented, it may be a continuation of
# after_keys takes precedence, even if there's a conflict.
# the previous line.
insert_idx = max ( after_indices ) + 1
if line and line [ 0 ] == ' ' :
else :
if last_akey_idx is None :
continue
if idx - last_akey_idx > 1 :
continue
elif normalize_key ( line ) not in after_keys :
continue
last_akey_idx = idx
if last_akey_idx is None :
insert_idx = len ( footer_lines )
insert_idx = len ( footer_lines )
else :
insert_idx = last_akey_idx + 1
footer_lines . insert ( insert_idx , new_footer )
footer_lines . insert ( insert_idx , new_footer )
return ' \n ' . join ( top_lines + footer_lines )
return ' \n ' . join ( top_lines + footer_lines )