Fossil SCM

Enhance the "fossil add" command so that when a directory is named, all contents of that directory are added recursively. Ticket [e02ffabcdaaaf606099ac09227833ba282fdaace]

drh 2008-12-07 18:11 trunk
Commit 8c4e72e22320d3de04ffcd9fe63b63b98d946db8
1 file changed +86 -28
+86 -28
--- src/add.c
+++ src/add.c
@@ -25,11 +25,89 @@
2525
** from the local repository.
2626
*/
2727
#include "config.h"
2828
#include "add.h"
2929
#include <assert.h>
30
+#include <dirent.h>
31
+
32
+
33
+/*
34
+** Add a single file
35
+*/
36
+static void add_one_file(const char *zName, int vid, Blob *pOmit){
37
+ Blob pathname;
38
+ const char *zPath;
39
+
40
+ file_tree_name(zName, &pathname, 1);
41
+ zPath = blob_str(&pathname);
42
+ if( strcmp(zPath, "manifest")==0
43
+ || strcmp(zPath, "_FOSSIL_")==0
44
+ || strcmp(zPath, "manifest.uuid")==0
45
+ || blob_compare(&pathname, pOmit)==0
46
+ ){
47
+ fossil_warning("cannot add %s", zPath);
48
+ }else{
49
+ if( !file_is_simple_pathname(zPath) ){
50
+ fossil_fatal("filename contains illegal characters: %s", zPath);
51
+ }
52
+ if( db_exists("SELECT 1 FROM vfile WHERE pathname=%Q", zPath) ){
53
+ db_multi_exec("UPDATE vfile SET deleted=0 WHERE pathname=%Q", zPath);
54
+ }else{
55
+ db_multi_exec(
56
+ "INSERT INTO vfile(vid,deleted,rid,mrid,pathname)"
57
+ "VALUES(%d,0,0,0,%Q)", vid, zPath);
58
+ }
59
+ printf("ADDED %s\n", zPath);
60
+ }
61
+ blob_reset(&pathname);
62
+}
63
+
64
+/*
65
+** All content of the zDir directory to the SFILE table.
66
+*/
67
+void add_directory_content(const char *zDir){
68
+ DIR *d;
69
+ int origSize;
70
+ struct dirent *pEntry;
71
+ Blob path;
72
+
73
+ blob_zero(&path);
74
+ blob_append(&path, zDir, -1);
75
+ origSize = blob_size(&path);
76
+ d = opendir(zDir);
77
+ if( d ){
78
+ while( (pEntry=readdir(d))!=0 ){
79
+ char *zPath;
80
+ if( pEntry->d_name[0]=='.' ) continue;
81
+ blob_appendf(&path, "/%s", pEntry->d_name);
82
+ zPath = blob_str(&path);
83
+ if( file_isdir(zPath)==1 ){
84
+ add_directory_content(zPath);
85
+ }else if( file_isfile(zPath) ){
86
+ db_multi_exec("INSERT INTO sfile VALUES(%Q)", zPath);
87
+ }
88
+ blob_resize(&path, origSize);
89
+ }
90
+ }
91
+ closedir(d);
92
+ blob_reset(&path);
93
+}
3094
95
+/*
96
+** Add all content of a directory.
97
+*/
98
+void add_directory(const char *zDir, int vid, Blob *pOmit){
99
+ Stmt q;
100
+ add_directory_content(zDir);
101
+ db_prepare(&q, "SELECT x FROM sfile ORDER BY x");
102
+ while( db_step(&q)==SQLITE_ROW ){
103
+ const char *zName = db_column_text(&q, 0);
104
+ add_one_file(zName, vid, pOmit);
105
+ }
106
+ db_finalize(&q);
107
+ db_multi_exec("DELETE FROM sfile");
108
+}
31109
32110
/*
33111
** COMMAND: add
34112
**
35113
** Usage: %fossil add FILE...
@@ -49,46 +127,26 @@
49127
}
50128
db_begin_transaction();
51129
if( !file_tree_name(g.zRepositoryName, &repo, 0) ){
52130
blob_zero(&repo);
53131
}
132
+ db_multi_exec("CREATE TEMP TABLE sfile(x TEXT PRIMARY KEY)");
54133
for(i=2; i<g.argc; i++){
55134
char *zName;
56
- char *zPath;
57
- Blob pathname;
58135
int isDir;
59136
60137
zName = mprintf("%/", g.argv[i]);
61138
isDir = file_isdir(zName);
62
- if( isDir==1 ) continue;
63
- if( isDir==0 ){
139
+ if( isDir==1 ){
140
+ add_directory(zName, vid, &repo);
141
+ }else if( isDir==0 ){
64142
fossil_fatal("not found: %s", zName);
65
- }
66
- if( isDir==2 && access(zName, R_OK) ){
143
+ }else if( access(zName, R_OK) ){
67144
fossil_fatal("cannot open %s", zName);
68
- }
69
- file_tree_name(zName, &pathname, 1);
70
- zPath = blob_str(&pathname);
71
- if( strcmp(zPath, "manifest")==0
72
- || strcmp(zPath, "_FOSSIL_")==0
73
- || strcmp(zPath, "manifest.uuid")==0
74
- || blob_compare(&pathname, &repo)==0
75
- ){
76
- fossil_warning("cannot add %s", zPath);
77
- }else{
78
- if( !file_is_simple_pathname(zPath) ){
79
- fossil_fatal("filename contains illegal characters: %s", zPath);
80
- }
81
- if( db_exists("SELECT 1 FROM vfile WHERE pathname=%Q", zPath) ){
82
- db_multi_exec("UPDATE vfile SET deleted=0 WHERE pathname=%Q", zPath);
83
- }else{
84
- db_multi_exec(
85
- "INSERT INTO vfile(vid,deleted,rid,mrid,pathname)"
86
- "VALUES(%d,0,0,0,%Q)", vid, zPath);
87
- }
88
- }
89
- blob_reset(&pathname);
145
+ }else{
146
+ add_one_file(zName, vid, &repo);
147
+ }
90148
free(zName);
91149
}
92150
db_end_transaction(0);
93151
}
94152
95153
--- src/add.c
+++ src/add.c
@@ -25,11 +25,89 @@
25 ** from the local repository.
26 */
27 #include "config.h"
28 #include "add.h"
29 #include <assert.h>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
32 /*
33 ** COMMAND: add
34 **
35 ** Usage: %fossil add FILE...
@@ -49,46 +127,26 @@
49 }
50 db_begin_transaction();
51 if( !file_tree_name(g.zRepositoryName, &repo, 0) ){
52 blob_zero(&repo);
53 }
 
54 for(i=2; i<g.argc; i++){
55 char *zName;
56 char *zPath;
57 Blob pathname;
58 int isDir;
59
60 zName = mprintf("%/", g.argv[i]);
61 isDir = file_isdir(zName);
62 if( isDir==1 ) continue;
63 if( isDir==0 ){
 
64 fossil_fatal("not found: %s", zName);
65 }
66 if( isDir==2 && access(zName, R_OK) ){
67 fossil_fatal("cannot open %s", zName);
68 }
69 file_tree_name(zName, &pathname, 1);
70 zPath = blob_str(&pathname);
71 if( strcmp(zPath, "manifest")==0
72 || strcmp(zPath, "_FOSSIL_")==0
73 || strcmp(zPath, "manifest.uuid")==0
74 || blob_compare(&pathname, &repo)==0
75 ){
76 fossil_warning("cannot add %s", zPath);
77 }else{
78 if( !file_is_simple_pathname(zPath) ){
79 fossil_fatal("filename contains illegal characters: %s", zPath);
80 }
81 if( db_exists("SELECT 1 FROM vfile WHERE pathname=%Q", zPath) ){
82 db_multi_exec("UPDATE vfile SET deleted=0 WHERE pathname=%Q", zPath);
83 }else{
84 db_multi_exec(
85 "INSERT INTO vfile(vid,deleted,rid,mrid,pathname)"
86 "VALUES(%d,0,0,0,%Q)", vid, zPath);
87 }
88 }
89 blob_reset(&pathname);
90 free(zName);
91 }
92 db_end_transaction(0);
93 }
94
95
--- src/add.c
+++ src/add.c
@@ -25,11 +25,89 @@
25 ** from the local repository.
26 */
27 #include "config.h"
28 #include "add.h"
29 #include <assert.h>
30 #include <dirent.h>
31
32
33 /*
34 ** Add a single file
35 */
36 static void add_one_file(const char *zName, int vid, Blob *pOmit){
37 Blob pathname;
38 const char *zPath;
39
40 file_tree_name(zName, &pathname, 1);
41 zPath = blob_str(&pathname);
42 if( strcmp(zPath, "manifest")==0
43 || strcmp(zPath, "_FOSSIL_")==0
44 || strcmp(zPath, "manifest.uuid")==0
45 || blob_compare(&pathname, pOmit)==0
46 ){
47 fossil_warning("cannot add %s", zPath);
48 }else{
49 if( !file_is_simple_pathname(zPath) ){
50 fossil_fatal("filename contains illegal characters: %s", zPath);
51 }
52 if( db_exists("SELECT 1 FROM vfile WHERE pathname=%Q", zPath) ){
53 db_multi_exec("UPDATE vfile SET deleted=0 WHERE pathname=%Q", zPath);
54 }else{
55 db_multi_exec(
56 "INSERT INTO vfile(vid,deleted,rid,mrid,pathname)"
57 "VALUES(%d,0,0,0,%Q)", vid, zPath);
58 }
59 printf("ADDED %s\n", zPath);
60 }
61 blob_reset(&pathname);
62 }
63
64 /*
65 ** All content of the zDir directory to the SFILE table.
66 */
67 void add_directory_content(const char *zDir){
68 DIR *d;
69 int origSize;
70 struct dirent *pEntry;
71 Blob path;
72
73 blob_zero(&path);
74 blob_append(&path, zDir, -1);
75 origSize = blob_size(&path);
76 d = opendir(zDir);
77 if( d ){
78 while( (pEntry=readdir(d))!=0 ){
79 char *zPath;
80 if( pEntry->d_name[0]=='.' ) continue;
81 blob_appendf(&path, "/%s", pEntry->d_name);
82 zPath = blob_str(&path);
83 if( file_isdir(zPath)==1 ){
84 add_directory_content(zPath);
85 }else if( file_isfile(zPath) ){
86 db_multi_exec("INSERT INTO sfile VALUES(%Q)", zPath);
87 }
88 blob_resize(&path, origSize);
89 }
90 }
91 closedir(d);
92 blob_reset(&path);
93 }
94
95 /*
96 ** Add all content of a directory.
97 */
98 void add_directory(const char *zDir, int vid, Blob *pOmit){
99 Stmt q;
100 add_directory_content(zDir);
101 db_prepare(&q, "SELECT x FROM sfile ORDER BY x");
102 while( db_step(&q)==SQLITE_ROW ){
103 const char *zName = db_column_text(&q, 0);
104 add_one_file(zName, vid, pOmit);
105 }
106 db_finalize(&q);
107 db_multi_exec("DELETE FROM sfile");
108 }
109
110 /*
111 ** COMMAND: add
112 **
113 ** Usage: %fossil add FILE...
@@ -49,46 +127,26 @@
127 }
128 db_begin_transaction();
129 if( !file_tree_name(g.zRepositoryName, &repo, 0) ){
130 blob_zero(&repo);
131 }
132 db_multi_exec("CREATE TEMP TABLE sfile(x TEXT PRIMARY KEY)");
133 for(i=2; i<g.argc; i++){
134 char *zName;
 
 
135 int isDir;
136
137 zName = mprintf("%/", g.argv[i]);
138 isDir = file_isdir(zName);
139 if( isDir==1 ){
140 add_directory(zName, vid, &repo);
141 }else if( isDir==0 ){
142 fossil_fatal("not found: %s", zName);
143 }else if( access(zName, R_OK) ){
 
144 fossil_fatal("cannot open %s", zName);
145 }else{
146 add_one_file(zName, vid, &repo);
147 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148 free(zName);
149 }
150 db_end_transaction(0);
151 }
152
153

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button