Skip to content

Commit c552b89

Browse files
committed
refactoring: gathering some identical code in a single function
1 parent 1bc4040 commit c552b89

File tree

5 files changed

+36
-30
lines changed

5 files changed

+36
-30
lines changed

constants.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* snapshot files' extensions */
2+
#define FULL_SNAPSHOT_EXT "snapshot"
3+
#define DIFF_SNAPSHOT_EXT "diff"

layout/versions.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ int versions_init(char *repo){
2929
}
3030

3131
char **revs = NULL;
32-
char *extension = NULL;
3332
int i = 0;
3433

3534
// printf("[Function: init_versions] Received repo path %s;\n", path);
@@ -39,12 +38,7 @@ int versions_init(char *repo){
3938
versions_init_finish(-1);
4039
read_layout_versions(revs[rev_count[0] - 1], NULL);
4140
for (i = rev_count[0] - 1; i >= 0; i--){
42-
extension = gpthext(revs[i]);
43-
if (strcmp(extension, "snapshot") == 0)
44-
snapshot_copy(revs[i], CURRENT_SNAPSHOT, data_dir);
45-
else // strcmp(extension, "diff") == 0)
46-
snapshot_append(revs[i], CURRENT_SNAPSHOT, data_dir);
47-
gstrdel(extension);
41+
add_snapshot(revs[i], CURRENT_SNAPSHOT, data_dir);
4842
read_revision_versions(revs[i], rev_count[0] - i - 1, NULL, -1);
4943
};
5044
versions_init_finish(0);
@@ -71,7 +65,6 @@ int versions_init_multi(int count, char **repos){
7165

7266
int i = 0, j = 0, k = 0;
7367
char **revs = NULL;
74-
char *extension = NULL;
7568
char *snapshot = NULL;
7669

7770
gtreenew(&version_tree);
@@ -88,12 +81,7 @@ int versions_init_multi(int count, char **repos){
8881
continue;
8982
};
9083
for (j = rev_count[i] - 1; j >= 0; j--){
91-
extension = gpthext(revs[j]);
92-
if (strcmp(extension, "snapshot") == 0)
93-
snapshot_copy(revs[j], CURRENT_SNAPSHOT, data_dir);
94-
else // strcmp(extension, "diff") == 0)
95-
snapshot_append(revs[j], CURRENT_SNAPSHOT, data_dir);
96-
gstrdel(extension);
84+
add_snapshot(revs[j], CURRENT_SNAPSHOT, data_dir);
9785
read_revision_versions(revs[j], rev_count[i] - j - 1, repo_names[i], i);
9886
};
9987
versions_init_multi_free_revs;

structure/full.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ int full_build(char *repo){
3131
}
3232

3333
char **revs = NULL;
34-
char *extension = NULL;
3534
int i = 0;
3635

3736
gtreenew(&structure_tree);
@@ -40,12 +39,7 @@ int full_build(char *repo){
4039
if ((rev_count[0] = gather_revisions(repo, data_dir, &revs)) == -1)
4140
full_build_finish(-1);
4241
for (i = rev_count[0] - 1; i >= 0; i--){
43-
extension = gpthext(revs[i]);
44-
if (strcmp(extension, "snapshot") == 0)
45-
snapshot_copy(revs[i], CURRENT_SNAPSHOT, data_dir);
46-
else // strcmp(extension, "diff") == 0)
47-
snapshot_append(revs[i], CURRENT_SNAPSHOT, data_dir);
48-
gstrdel(extension);
42+
add_snapshot(revs[i], CURRENT_SNAPSHOT, data_dir);
4943
read_revision_all(NULL, revs[i], -1, rev_count[0] - i - 1);
5044
};
5145
full_build_finish(0);
@@ -76,7 +70,6 @@ int full_build_multi(int count, char **repo){
7670
int k = 0;
7771
char **revs = NULL;
7872
char *repo_dir = NULL;
79-
char *extension = NULL;
8073
char *snapshot = NULL;
8174

8275
//printf("[Function: init_multi] Received %d repos;\n", count);
@@ -91,12 +84,7 @@ int full_build_multi(int count, char **repo){
9184
continue;
9285
};
9386
for (j = rev_count[i] - 1; j >= 0; j--){
94-
extension = gpthext(revs[j]);
95-
if (strcmp(extension, "snapshot") == 0)
96-
snapshot_copy(revs[j], CURRENT_SNAPSHOT, data_dir);
97-
else // strcmp(extension, "diff") == 0)
98-
snapshot_append(revs[j], CURRENT_SNAPSHOT, data_dir);
99-
gstrdel(extension);
87+
add_snapshot(revs[j], CURRENT_SNAPSHOT, data_dir);
10088
read_revision_all(repo_names[i], revs[j], i, rev_count[i] - j - 1);
10189
};
10290
full_build_multi_free_revs;

support/grdiff.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "grdiff.h"
2+
#include "../constants.h"
23

34
// prototypes:
45

@@ -256,6 +257,23 @@ int get_revisions(char *where, int count, char **revs){
256257

257258
}
258259

260+
int add_snapshot(char *revision, char *target, char *directory){
261+
262+
char *extension = NULL;
263+
int result = 0;
264+
265+
extension = gpthext(revision);
266+
if (strcmp(extension, FULL_SNAPSHOT_EXT) == 0)
267+
result = snapshot_copy(revision, target, directory);
268+
else if (strcmp(extension, DIFF_SNAPSHOT_EXT) == 0)
269+
result = snapshot_append(revision, target, directory);
270+
else
271+
result = -1;
272+
gstrdel(extension);
273+
return result;
274+
275+
};
276+
259277
int snapshot_copy(char *revision, char *target, char *directory){
260278

261279
char *path = NULL;

support/grdiff.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,16 @@ int read_stats(struct stats *stats, FILE *file);
5959
int get_revisions(char *, int, char **);
6060

6161
/*
62-
* copy given revision to a certain target in a given directory
62+
* adds given revision to target file in a given directory
63+
*
64+
* @1: revision file name to be added
65+
* @2: target file name
66+
* @3: directory where both files are stored
67+
*/
68+
int add_snapshot(char *, char *, char *);
69+
70+
/*
71+
* copy given revision to target file in a given directory
6372
*
6473
* @1: revision file name to be copied
6574
* @2: target file name
@@ -70,7 +79,7 @@ int get_revisions(char *, int, char **);
7079
int snapshot_copy(char *, char *, char *);
7180

7281
/*
73-
* append given revision to a certain target in a given directory
82+
* append given revision to target file in a given directory
7483
*
7584
* @1: revision file name to be appended
7685
* @2: target file name

0 commit comments

Comments
 (0)