{% extends "repo_master.html" %}

{% block title %}Commit - {{
    repo.namespace + '/' if repo.namespace }}{{ repo.name }} - {{ commitid }}{% endblock %}
{% set tag = "commit"%}

{% block repo %}

{% set splitted_message = commit.message.split('\n') %}
<div class="header">
  <h4>
    <span class="label label-default" title="{{ commitid }}">Commit {{ commitid|short }}</span>
    <span>{{ splitted_message[0] }}</span>
    <div class="pull-xs-right btn-group">
      <a class="btn btn-secondary btn-sm" href="{{ url_for(
        'view_raw_file',
        repo=repo.name,
        username=username,
        namespace=repo.namespace,
        identifier=commitid) }}" title="View as raw">raw</a>
      <a class="btn btn-secondary btn-sm" href="{{ url_for(
        'view_commit_patch',
        repo=repo.name,
        username=username,
        namespace=repo.namespace,
        commitid=commitid) }}">patch</a>
      <a class="btn btn-secondary btn-sm" href="{{ url_for(
        'view_tree', username=username, namespace=repo.namespace,
        repo=repo.name, identifier=commitid) }}">tree</a>
      {% if commit.parents|length == 1 %}
      <a class="btn btn-secondary btn-sm" title={{commit.parents[0].oid.hex}} href="{{ url_for(
        'view_commit',
        repo=repo.name,
        username=username,
        namespace=repo.namespace,
        commitid=commit.parents[0].oid.hex) }}">parent</a>
      {% elif commit.parents|length > 1 %}
      <div class="btn-group" role="group">
        <button id="parentsDrop" type="button" class="btn btn-secondary btn-sm dropdown-toggle"
            data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          parents
        </button>
        <div class="dropdown-menu dropdown-menu-right" aria-labelledby="parentsDrop">
          {% for parent in commit.parents %}
            <a class="dropdown-item" href="{{ url_for(
                'view_commit',
                repo=repo.name,
                username=username,
                namespace=repo.namespace,
                commitid=parent.oid.hex)
            }}" class="commitid">{{parent.oid.hex}}</a>
          {% endfor %}
        </div>
      </div>
      {% endif%}
    </div>
  </h4>
  <h5 class="text-muted">
    {% if commit.author| author2user == commit.committer| author2user %}
      <a href="#" id="diff_list_link">{{ diff|count}} file{{'s' if diff|count > 1 }}</a>
      Authored and Committed by {{ commit.author | author2user |safe }}
      <span data-toggle="tooltip" title="{{ commit.commit_time | format_ts }}">
        {{ commit.commit_time | humanize }}
      </span>
    {% else %}
      <a href="#" id="diff_list_link">{{ diff|count}} file{{'s' if diff|count > 1 }}</a>
      Authored by {{ commit.author | author2user |safe }}
      <span data-toggle="tooltip" title="{{ commit.commit_time | format_ts }}">
        {{ commit.commit_time | humanize }}
      </span>,
      Committed by {{ commit.committer | author2user |safe }}
      <span data-toggle="tooltip" title="{{ commit.commit_time | format_ts }}">
        {{ commit.commit_time | humanize }}
      </span>,
    {% endif%}
  </h5>
</div>

<div class="list-group" id="diff_list" style="display:none;">
  {% set filecount = 0 %}
  {% for patch in diff %}
      {% set filecount = filecount + 1 %}
      {% if patch | hasattr('new_file_path') %}
        <a class="list-group-item" href="#diff-file-{{filecount}}">
          {{  patch.new_file_path | unicode}}
      {% elif patch | hasattr('delta') %}
        <a class="list-group-item" href="#diff-file-{{filecount}}">
          {{  patch.delta.new_file.path | unicode }}
      {% endif %}
      <div class="pull-xs-right">
        {% if not patch.is_binary  %}
        {% if patch | hasattr('additions') %}
        {% if (patch.additions + patch.deletions) %}
          <span style="width: {{ (100.0 * patch.additions / (patch.additions + patch.deletions))|round|int }}%">
            {% if patch.additions > 0 %}<span class="label label-success">+{{ patch.additions }}</span>{% endif %}
            {% if patch.deletions > 0 %}<span class="label label-danger">-{{ patch.deletions }}</span>{% endif %}
          </span>
        {% endif %}
        {% else %}
        {% if (patch.line_stats[1] + patch.line_stats[2]) %}
          <span style="width: {{ (100.0 * patch.line_stats[1] / (patch.line_stats[1] + patch.line_stats[2]))|round|int }}%">
            {% if patch.line_stats[1] > 0 %}<span class="label label-success">+{{ patch.line_stats[1] }}</span>{% endif %}
            {% if patch.line_stats[2] > 0 %}<span class="label label-danger">-{{ patch.line_stats[2] }}</span>{% endif %}
          </span>
        {% endif %}
        {% endif %}
        {% endif %}
      </div>
      {{ patch.new_file_path | unicode }}
    </a>
  {% endfor %}
</div>

<div class="m-y-1">
    {% if splitted_message|length > 1 %}
    <pre class="commit_message_body">
        {% for message in splitted_message %}
{{ message }}
        {% endfor %}
    </pre>
    {% endif %}
</div>

{% set filecount = 0 %}
{% for patch in diff %}
  {% set filecount = filecount + 1 %}
<div class="card" id="diff-file-{{filecount}}">
  <div class="card-header">
    {% if patch | hasattr('new_file_path') %}
      <a href="{{ url_for(
          'view_file', username=username, namespace=repo.namespace,
          repo=repo.name, identifier=commitid,
          filename=patch.new_file_path) }}" title="View file as of {{ commitid|short }}">
        {{  patch.new_file_path | unicode }}
      </a>
    {% elif patch | hasattr('delta') %}
      <a href="{{ url_for(
          'view_file', username=username, namespace=repo.namespace,
          repo=repo.name, identifier=commitid,
          filename=patch.delta.new_file.path) }}" title="View file as of {{ commitid|short }}">
        {{  patch.delta.new_file.path | unicode }}
      </a>
    {% endif %}
        {% if not patch.is_binary  %}
          {% if patch | hasattr('additions') %}
          {% if (patch.additions + patch.deletions) %}
            <span style="width: {{ (100.0 * patch.additions / (patch.additions + patch.deletions))|round|int }}%">
              {% if patch.additions > 0 %}<span class="label label-success">+{{ patch.additions }}</span> {% endif %}
              {% if patch.deletions > 0 %}<span class="label label-danger">-{{ patch.deletions }}</span>{% endif %}
            </span>
          {% endif %}
          {% else %}
          {% if (patch.line_stats[1] + patch.line_stats[2]) %}
            <span style="width: {{ (100.0 * patch.line_stats[1] / (patch.line_stats[1] + patch.line_stats[2]))|round|int }}%">
              {% if patch.line_stats[1] > 0 %}<span class="label label-success">+{{ patch.line_stats[1] }}</span> {% endif %}
              {% if patch.line_stats[2] > 0 %}<span class="label label-danger">-{{ patch.line_stats[2] }}</span>{% endif %}
            </span>
          {% endif %}
          {% endif %}
          {% endif %}
  </div>

    {% if patch.is_binary %}
        <p class="noresult">Binary diffs cannot be rendered.</p>
    {% else %}
        {% autoescape false %}
        {{ patch|patch_to_diff|html_diff}}
        {% endautoescape %}
    {% endif %}
</div>

{% endfor %}

{% endblock %}



{% block jscripts %}
    {{ super() }}
    <script type="text/javascript">
      $(function(){
        $('#diff_list_link').click(function(){
          $('#diff_list').toggle();
        });
      });
      $.ajax({
        url: '{{ url_for("internal_ns.get_branches_of_commit") }}' ,
        type: 'POST',
        data: {
          repo: "{{ repo.name }}",
          repouser: "{{ repo.user.user if repo.is_fork else '' }}",
          namespace: "{{ repo.namespace }}",
          commit_id: "{{ commitid }}",
          csrf_token: "{{ form.csrf_token.current_token }}",
        },
        dataType: 'json',
        success: function(res) {
            if (res.branches.length == 0){
                return;
            }
            var _br = '';
            for (var i = 0; i < res.branches.length; ++i) {
                if (_br.length > 0){
                    _br += ', ';
                }
                _br += res.branches[i]
            }
            var el = $('#diff-file-1');
            if (!el){
                return;
            }
            el.before(
                '<div class=""><span class="oi" data-glyph="fork"></span> '
                + _br + '</div>');
        }
      });
    </script>
{% endblock %}
