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
     
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
18
18
    

{{ repository_name }}

19
19
    
20
20
21
21
{% endblock header %}
22
22
23
23
{% block main %}
24
24
25
25
26
26
    
{% translate "Author" %}:
27
27
    
{{ commit.author }}
28
28
29
29
30
30
31
31
32
32
    

{{ commit.msg }}

33
33
    

{% translate "Commited by" %} {{ commit.author }}

34
34
35
35
36
36
{% for mod_file in commit.modified_files %}
37
37
    
38
38
        

{{ mod_file.filename }}

39
39
        
40
40
            {% for line in file_html_code. %}
41
41
            
42
42
                
43
43
                    
44
44
                    
{{ forloop.counter }}
45
45
                
{{ line|safe }}
46
46
            
47
47
            {% endfor %}
48
48
        
49
49
    
50
50
    
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
    

55
55
    Voor {{ mod_file.filename }}
56
56
    {{ mod_file.source_code_before }}
57
57
    Na: 
58
58
    {{ mod_file.source_code }}
59
59
    

60
60
61
61
    {% for row in rows %}
+
62
    {% for row in rows %}
62
63
    {% if row.type == "deletion" %}
63
64
    
64
65
    {% elif row.type == "addition" %}
+
66
            {{ row.old_num }}
+
67
        
+
68
        
+
69
    {% elif row.type == "addition" %}
65
70
    
66
71
    {% else %}
+
72
        
+
73
            {{ row.new_num }}
+
74
        
+
75
    {% else %}
67
76
    
68
77
    {% endif %}
+
78
            {{ row.old_num }}
+
79
        
+
80
            {{ row.new_num }}
+
81
        
+
82
    {% endif %}
69
83
        {% if row.type == "deletion" %}
70
-
        
{{ row.old_num }}
71
-
        
72
-
        {% elif row.type == "addition" %}
73
-
        
74
-
        
{{ row.new_num }}
75
-
        {% else %}
76
-
        
{{ row.old_num }}
77
-
        
{{ row.new_num }}
78
-
        {% endif %}
79
-
80
-
        }}">{{ row.old_num }}
81
-
        {% else %} {# Not a change #}
82
-
        
83
-
        
84
-
85
-
    
86
-
    
+
84
    
87
85
    {% endfor %}
88
86
    
89
-
90
-
91
-
    
+
87
    
92
88
{% endfor %}
93
89
94
90
{% endblock main %}