|
1 | 1 | package internal |
2 | 2 |
|
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | +) |
| 7 | + |
3 | 8 | // DiffStatus describes how a task changed between two checkpoints |
4 | 9 | type DiffStatus string |
5 | 10 |
|
@@ -42,3 +47,88 @@ type DiffChange struct { |
42 | 47 | // Value in checkpoint B |
43 | 48 | After any `json:"after,omitempty"` |
44 | 49 | } |
| 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