# Buggy code
def get_commands_from_history(image_layer):
    """Given the image layer object and the shell, get the list of command
    objects that created the layer"""
    # set up notice origin for the layer
    origin_layer = "Layer: " + image_layer.fs_hash[:10]
    if image_layer.created_by:
        instruction = created_to_instruction(image_layer.created_by)
        image_layer.origins.add_notice_to_origins(
            origin_layer,
            Notice(
                formats.dockerfile_line.format(dockerfile_instruction=instruction),
                "info",
            ),
        )
    else:
        image_layer.origins.add_notice_to_origins(
            origin_layer, Notice(formats.no_created_by, "warning")
        )
    command_line = instruction.split(" ", 1)[1]
    # Image layers are created with the directives RUN, ADD and COPY
    # For ADD and COPY instructions, there is no information about the
    # packages added
    if "ADD" in instruction or "COPY" in instruction:
        image_layer.origins.add_notice_to_origins(
            origin_layer,
            Notice(errors.unknown_content.format(files=command_line), "warning"),
        )
        # return an empty list as we cannot find any commands
        return []
    # for RUN instructions we can return a list of commands
    command_list, msg = common.filter_install_commands(command_line)
    if msg:
        image_layer.origins.add_notice_to_origins(origin_layer, Notice(msg, "warning"))
    return command_list


# Traceback (UnboundLocalError)
/* snip */
2020-04-14 20:17:02,098 - DEBUG - common - Reading files in filesystem...
Traceback (most recent call last):
File "/usr/local/bin/tern", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.8/dist-packages/tern/__main__.py", line 201, in main
do_main(args)
File "/usr/local/lib/python3.8/dist-packages/tern/__main__.py", line 105, in do_main
run.execute_docker_image(args)
File "/usr/local/lib/python3.8/dist-packages/tern/analyze/docker/run.py", line 90, in execute_docker_image
analyze(full_image, args)
File "/usr/local/lib/python3.8/dist-packages/tern/analyze/docker/run.py", line 69, in analyze
analyze_docker_image(image_obj, args.redo, dfile_lock, dfobj)
File "/usr/local/lib/python3.8/dist-packages/tern/analyze/docker/analyze.py", line 42, in analyze_docker_image
analyze_subsequent_layers(image_obj, shell, master_list, redo, dfobj,
File "/usr/local/lib/python3.8/dist-packages/tern/analyze/docker/analyze.py", line 129, in analyze_subsequent_layers
command_list = dhelper.get_commands_from_history(
File "/usr/local/lib/python3.8/dist-packages/tern/analyze/docker/helpers.py", line 158, in get_commands_from_history
command_line = instruction.split(' ', 1)[1]
UnboundLocalError: local variable 'instruction' referenced before assignment

# Fixed code
def get_commands_from_history(image_layer):
    """Given the image layer object and the shell, get the list of command
    objects that created the layer"""
    # set up notice origin for the layer
    origin_layer = "Layer: " + image_layer.fs_hash[:10]
    if image_layer.created_by:
        instruction = created_to_instruction(image_layer.created_by)
        image_layer.origins.add_notice_to_origins(
            origin_layer,
            Notice(
                formats.dockerfile_line.format(dockerfile_instruction=instruction),
                "info",
            ),
        )
        command_line = instruction.split(" ", 1)[1]
    else:
        instruction = ""
        image_layer.origins.add_notice_to_origins(
            origin_layer, Notice(formats.no_created_by, "warning")
        )
        command_line = instruction
    # Image layers are created with the directives RUN, ADD and COPY
    # For ADD and COPY instructions, there is no information about the
    # packages added
    if "ADD" in instruction or "COPY" in instruction:
        image_layer.origins.add_notice_to_origins(
            origin_layer,
            Notice(errors.unknown_content.format(files=command_line), "warning"),
        )
        # return an empty list as we cannot find any commands
        return []
    # for RUN instructions we can return a list of commands
    command_list, msg = common.filter_install_commands(command_line)
    if msg:
        image_layer.origins.add_notice_to_origins(origin_layer, Notice(msg, "warning"))
    return command_list