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
|
/* 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");
}
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");
}
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
printf("Error processing command...\n");
return 0;
}
|