Skip to content

Commit 14d149e

Browse files
committed
Added the DiffTasks function definition in the diff_types file
Signed-off-by: Lorygold <lory.goldoni@gmail.com>
1 parent c6035dd commit 14d149e

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

internal/diff_types.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package internal
22

3+
import (
4+
"fmt"
5+
"reflect"
6+
)
7+
38
// DiffStatus describes how a task changed between two checkpoints
49
type DiffStatus string
510

@@ -42,3 +47,88 @@ type DiffChange struct {
4247
// Value in checkpoint B
4348
After any `json:"after,omitempty"`
4449
}
50+
51+
// DiffTasks compares two sets of tasks and returns their differences.
52+
func DiffTasks(
53+
tasksA []*Task,
54+
tasksB []*Task,
55+
psTreeCmd bool,
56+
psTreeEnv bool,
57+
files bool,
58+
sockets bool,
59+
) ([]DiffTask, error) {
60+
61+
if tasksA == nil || tasksB == nil {
62+
return nil, fmt.Errorf("nil task list provided")
63+
}
64+
65+
// Index tasks by CheckpointFilePath for matching
66+
indexA := make(map[string]*Task)
67+
indexB := make(map[string]*Task)
68+
69+
for _, t := range tasksA {
70+
indexA[t.CheckpointFilePath] = t
71+
}
72+
for _, t := range tasksB {
73+
indexB[t.CheckpointFilePath] = t
74+
}
75+
76+
var diffs []DiffTask
77+
78+
// Tasks present in A
79+
for id, taskA := range indexA {
80+
taskB, exists := indexB[id]
81+
82+
if !exists {
83+
// Removed task
84+
diffs = append(diffs, DiffTask{
85+
ID: id,
86+
Status: Removed,
87+
Before: taskA,
88+
})
89+
continue
90+
}
91+
92+
// Exists in both → compare
93+
if reflect.DeepEqual(taskA, taskB) {
94+
diffs = append(diffs, DiffTask{
95+
ID: id,
96+
Status: Unchanged,
97+
Before: taskA,
98+
After: taskB,
99+
})
100+
continue
101+
}
102+
103+
// Modified task
104+
diffs = append(diffs, DiffTask{
105+
ID: id,
106+
Status: Modified,
107+
Before: taskA,
108+
After: taskB,
109+
Changes: []DiffChange{
110+
{
111+
Category: "task",
112+
Field: "struct",
113+
Before: taskA,
114+
After: taskB,
115+
},
116+
},
117+
})
118+
}
119+
120+
// Tasks only in B → Added
121+
for id, taskB := range indexB {
122+
if _, exists := indexA[id]; exists {
123+
continue
124+
}
125+
126+
diffs = append(diffs, DiffTask{
127+
ID: id,
128+
Status: Added,
129+
After: taskB,
130+
})
131+
}
132+
133+
return diffs, nil
134+
}

0 commit comments

Comments
 (0)