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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/* Prism Versioning
* Copyright 2014 Jeffrey Armstrong <jeff@rainbow-100.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "prism.h"
#include "db.h"
#include "rfile.h"
static void banner()
{
printf("Prism Versioning\nRelease %s\nBy Jeff Armstrong <jeff@rainbow-100.com>\n",VERSION);
}
static void usage(const char *name)
{
banner();
printf("\nUsage: %s <command> ...\n\n", name);
printf("Commands:\n\n");
printf("\tabout\tprints a banner and exits\n");
printf("\tadd\tadds the specified file to the commit queue\n");
printf("\tcommit\tcommits currently queued files\n");
printf("\thash\tdisplays a file's hash\n");
printf("\tinfo\tshow information about repository\n");
printf("\tinit\tinitializes this directory for versioning\n");
printf("\trevert\treverts the specified file to the last revision\n");
printf("\n");
}
static void init_task()
{
printf("Initializing Prism...\n");
switch(initdb()) {
case PRET_EXISTS:
printf(" ERR: already initialized\n");
break;
case PRET_PERMISSION:
printf(" ERR: permissions error\n");
break;
case PRET_ERROR:
printf(" ERR: general error\n");
break;
default:
printf(" OK: initialized\n");
}
}
static void add_task(const char *filename)
{
char new_hash[33];
int res;
struct db_file oldfile;
if(filename == NULL) {
printf("Nothing to do...\n");
return;
}
printf("Adding '%s' to the queue...\n", filename);
res = get_db_fileinfo(filename, &oldfile);
if(res == PRET_NOTTRACKED) {
add_file(filename);
printf(" new file is queued\n");
} else if(res == PRET_OK) {
res = get_file_hash(filename, new_hash);
if(strcmp(oldfile.hash, new_hash) == 0) {
printf(" ERR: %s hasn't changed", filename);
} else {
add_file(filename);
printf(" modified file is queued\n");
}
} else
printf(" ERR: problem retrieving previous hash\n");
}
static void commit_task(const char *message)
{
int fcount;
int rev;
printf("Committing queue...\n");
fcount = disp_queue();
if(fcount <= 0) {
printf(" ERR: no files queued\n");
return;
}
rev = increment_revision();
if(commit_queue(rev) == PRET_OK)
printf(" commit completed\n");
else
printf(" ERR: commit failed\n");
}
static void info_task()
{
int res;
printf("Retrieving Prism information...\n");
res = get_revision();
if(res < 0) {
printf(" ERR: no revision could be retrieved\n");
return;
} else
printf(" current revision: %d\n", res);
res = get_file_count();
if(res < 0) {
printf(" ERR: file count error\n");
return;
} else
printf(" tracking %d files\n", res);
res = disp_queue();
if(res == PRET_READERROR)
printf(" no files in queue\n");
}
static void hash_task(const char *filename)
{
int res;
char hash[33];
memset(hash, 0, 33);
printf("Computing hash for %s...\n", filename);
res = get_file_hash(filename, hash);
if(res == PRET_OK)
printf(" hash: %s\n", hash);
else
printf(" ERR: hash failed\n");
}
static void revert_task(const char *filename)
{
int res;
printf("Reverting changes to %s...\n", filename);
res = revert(filename, -1);
switch(res) {
case PRET_OK:
printf(" changes reverted\n");
break;
case PRET_GZFAILED:
printf(" ERR: decompression failure\n");
break;
case PRET_NOREVISION:
printf(" ERR: no applicable revision found\n");
break;
default:
printf(" ERR: revert failed\n");
}
}
static void diff_task(const char *filename)
{
int res;
printf("Comparing current %s to latest repository revision...\n", filename);
res = diff_db_file(filename, -1);
switch(res) {
case PRET_OK:
printf(" diff complete\n");
break;
case PRET_GZFAILED:
printf(" ERR: decompression failure\n");
break;
case PRET_NOREVISION:
printf(" ERR: no applicable revision found\n");
break;
default:
printf(" ERR: diff failed\n");
}
}
int main(int argc, char *argv[])
{
if(argc == 1) {
usage(argv[0]);
return 0;
}
if(strcmp(argv[1], "about") == 0)
banner();
else if(strcmp(argv[1], "add") == 0 && argc == 3)
add_task(argv[2]);
else if(strcmp(argv[1], "commit") == 0 && argc == 3)
commit_task(argv[2]);
else if(strcmp(argv[1], "commit") == 0)
commit_task(NULL);
else if(strcmp(argv[1], "init") == 0)
init_task();
else if(strcmp(argv[1], "info") == 0)
info_task();
else if(strcmp(argv[1], "hash") == 0 && argc == 3)
hash_task(argv[2]);
else if(strcmp(argv[1], "revert") == 0 && argc == 3)
revert_task(argv[2]);
else if(strcmp(argv[1], "diff") == 0 && argc == 3)
diff_task(argv[2]);
else {
usage(argv[0]);
printf("Error processing command...\n\n");
}
printf("\n");
return 0;
}
|