-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean.c
90 lines (68 loc) · 2.47 KB
/
clean.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "helper.h"
#include "svc.h"
/** This file contains all the helper functions used in
* the cleanup function. They all return a dynamically allocated array
* with all files, branches, commits etc. Then they are freed in cleanup.
**/
// Get all of the tracked files and return an array.
struct file** all_files(void* helper, int* n_files) {
struct head* h = (struct head*) helper;
struct file* t_files = h->tracked_files;
struct file** all_files = malloc(sizeof(struct file*));
while (t_files != NULL) {
all_files[*n_files - 1] = t_files;
*n_files += 1;
all_files = realloc(all_files, *n_files * sizeof(char*));
t_files = t_files->prev_file;
}
return all_files;
}
// Get all of the branches and return an array.
struct branch** all_branches(void* helper, int* n_branches) {
struct head* h = (struct head*) helper;
struct branch* cur_branch = h->cur_branch;
struct branch** all_branches = malloc(sizeof(struct branch*));
do {
all_branches[*n_branches - 1] = cur_branch;
*n_branches += 1;
all_branches = realloc(all_branches, *n_branches * sizeof(struct branch*));
cur_branch = cur_branch->next_branch;
} while (cur_branch != h->cur_branch);
return all_branches;
}
// Get all of the commits from all branches and return an array.
struct commit** all_commits(void* helper, int* n_commits) {
struct head* h = (struct head*) helper;
struct commit* cur_commit = h->cur_branch->active_commit;
// We get all the branches first.
int n_branches;
// List branches noout does not output branch names for cleanup.
char** b_list = list_branches_noout(helper, &n_branches);
// Array to store all branches' commits in.
struct commit** all_commits = malloc(sizeof(struct commit*));
int commit_exists = 0;
// Checkout all the branches and store its commits into the array all_commits.
for (int i = 0; i < n_branches; i++) {
svc_checkout(helper, b_list[i]);
while (cur_commit != NULL) {
commit_exists = 0;
// Check if commit exists.
for (int i = 0; i < (*n_commits - 1); i++) {
if (all_commits[i] == cur_commit) {
commit_exists = 1;
}
}
if (!commit_exists) {
all_commits[*n_commits - 1] = cur_commit;
*n_commits += 1;
all_commits = realloc(all_commits, *n_commits * sizeof(struct commit*));
}
cur_commit = cur_commit->prev_commit;
}
}
free(b_list);
return all_commits;
}