gitar

Add function to process file diffs in commits

Author
Maarten Vangeneugden
Date
March 9, 2022, 3:10 p.m.
Hash
5c86ea235ea472b411e8e45835e088e8f3fe30f5
Parent
b652e5f165894a130dddc4a420b25ce231cd48c0
Modified files
GitActions/CommitInfo.py
templates/gitar/commit.djhtml

GitActions/CommitInfo.py

43 additions and 0 deletions.

View changes Hide changes
1
1
# formatted file in a commit. 
2
2
3
3
# The function returns a list 
4
4
5
5
# If one line is removed, and one line is added, and both have the same line
6
6
# number, then that should be regarded as a changed line, not a removed+added line.
7
7
# This holds for each non-broken list of removed+added lines.
8
8
+
9
def process_file(source_before, source_after, deletions, additions): 
+
10
    rows = []
+
11
    before_count = 0
+
12
    after_count = 0
+
13
    while True:
+
14
        row = dict()
+
15
        # We check the deletion lines first; if a line was "changed", the
+
16
        # 'deleted' line needs to be listed first, and after that, the 'added'
+
17
        # line
+
18
        if before_count in deletions:
+
19
            row["type"] = "deletion"
+
20
            row["old_num"] = before_count
+
21
            row["line"] = deletions[before_count]
+
22
            before_count += 1
+
23
        elif after_count in additions:
+
24
            row["type"] = "addition"
+
25
            row["new_num"] = after_count
+
26
            row["line"] = additions[after_count]
+
27
            after_count += 1
+
28
        else:  # No change to this particular line
+
29
            row["old_num"] = before_count
+
30
            row["new_num"] = after_count
+
31
            if len(source_before) < before_count:
+
32
                row["line"] = source_before[before_count] 
+
33
            elif len(source_after) < after_count:
+
34
                row["line"] = source_after[after_count] 
+
35
            else:  # No lines left
+
36
                break
+
37
            # If not end of both files, increment both counters anyway
+
38
            before_count += 1
+
39
            after_count += 1
+
40
        # Adding the new row to the list of rows
+
41
        rows.append(row)
+
42
    return rows
+
43
+
44
+
45
+
46
+
47
+
48
        
+
49
+
50
    
+
51

templates/gitar/commit.djhtml

16 additions and 20 deletions.

View changes Hide changes
1
1
{% load i18n %}
2
2
{% load humanize %}
3
3
{% load static %}
4
4
5
5
{% block title %}{{ repository_name }} | Gitar{% endblock title %}
6
6
7
7
{% block stylesheets %}
8
8
    {{ block.super }}
9
9
     <link href="{% static "website/gitar.css" %}" rel="stylesheet" media="screen, projection" />
10
10
{% endblock stylesheets %}
11
11
12
12
{% block description %}
13
13
{{ repository_name }} {{ commit.hash|truncatechars:10 }}: {{ commit.msg }}
14
14
{% endblock description %}
15
15
16
16
{% block header %}
17
17
<header>
18
18
    <h1>{{ repository_name }}</h1>  
19
19
    <label for="nav-drawer-toggle"></label>
20
20
</header>
21
21
{% endblock header %}
22
22
23
23
{% block main %}
24
24
<aside>
25
25
<dl>
26
26
    <dt>{% translate "Author" %}:</dt>
27
27
    <dd>{{ commit.author }}</dd>
28
28
</dl>
29
29
</aside>
30
30
31
31
<section class="emphasis">
32
32
    <h4>{{ commit.msg }}</h4>
33
33
    <p>{% translate "Commited by" %} {{ commit.author }} </p>
34
34
</section>
35
35
36
36
{% for mod_file in commit.modified_files %}
37
37
    <section style="font-family: 'Fira Code', monospace;">
38
38
        <h3>{{ mod_file.filename }}</h3>
39
39
        <table class="highlight">
40
40
            {% for line in file_html_code. %}
41
41
            <tr>
42
42
                <td id="{{ forloop.counter }}" class="line-number" style=":hover { background-color: #00E676;}">
43
43
                    <a style=":hover { background-color: #00e676;}" href="#{{ forloop.counter }}">
44
44
                    <pre>{{ forloop.counter }}</pre></a></td>
45
45
                <td><pre>{{ line|safe }}</pre></td>
46
46
            </tr>
47
47
            {% endfor %}
48
48
        </table>
49
49
    </section>
50
50
    <section>
51
51
    {# Determining whether we're dealing with an addition, modification or deletion #}
52
52
    {# {% if mod_file.change_type == "Added" %} #}
53
53
54
54
    <p>
55
55
    Voor {{ mod_file.filename }}
56
56
    {{ mod_file.source_code_before }}
57
57
    Na: 
58
58
    {{ mod_file.source_code }}
59
59
    </p>
60
60
61
61
    {% for row in rows %}
+
62
    {% for row in rows %}
62
63
    {% if row.type == "deletion" %}
63
64
    <tr class="deletion">
64
65
    {% elif row.type == "addition" %}
+
66
            <a href="A{{ row.old_num }}">{{ row.old_num }}
+
67
        </a></td>
+
68
        <td></td>
+
69
    {% elif row.type == "addition" %}
65
70
    <tr class="addition">
66
71
    {% else %}
+
72
        <td id="B{{ row.new_num }}">
+
73
            <a href="B{{ row.new_num }}">{{ row.new_num }}
+
74
        </a></td>
+
75
    {% else %}
67
76
    <tr class="nochange">
68
77
    {% endif %}
+
78
            <a href="A{{ row.old_num }}">{{ row.old_num }}
+
79
        <td id="B{{ row.new_num }}">
+
80
            <a href="B{{ row.new_num }}">{{ row.new_num }}
+
81
        </a></td>
+
82
    {% endif %}
69
83
        {% if row.type == "deletion" %}
70
-
        <td id="{{ row.old_num }}">{{ row.old_num }}</td>
71
-
        <td></td>
72
-
        {% elif row.type == "addition" %}
73
-
        <td></td>
74
-
        <td id="{{ row.new_num }}">{{ row.new_num }}</td>
75
-
        {% else %}
76
-
        <td id="{{ row.old_num }}">{{ row.old_num }}</td>
77
-
        <td id="{{ row.new_num }}">{{ row.new_num }}</td>
78
-
        {% endif %}
79
-
80
-
        }}">{{ row.old_num }}</td>
81
-
        {% else %} {# Not a change #}
82
-
        <td id="">
83
-
        </td>
84
-
85
-
    
86
-
    </tr>
+
84
    </tr>
87
85
    {% endfor %}
88
86
    
89
-
90
-
91
-
    </section>
+
87
    </section>
92
88
{% endfor %}
93
89
94
90
{% endblock main %}
95
91