Fossil SCM

Factor out the logic that tries to deduce an appropriate repository name from a remote URL and put it in the url_to_repo_basename() routine of url.c.

drh 2020-11-01 20:37 trunk
Commit f978fcdce15d4f82ef013646e86ab17f23cb9e8a4fbb476fdf0ac203c422d6fa
3 files changed +12 -15 +5 -3 +30
+12 -15
--- src/clone.c
+++ src/clone.c
@@ -171,33 +171,30 @@
171171
172172
if( g.argc < 3 ){
173173
usage("?OPTIONS? FILE-OR-URL ?NEW-REPOSITORY?");
174174
}
175175
db_open_config(0, 0);
176
- zRepo = g.argc==4 ? g.argv[3] : 0;
177
- if( zRepo!=0 && -1 != file_size(zRepo, ExtFILE) ){
178
- fossil_fatal("file already exists: %s", zRepo);
179
- }
180
- url_parse(g.argv[2], urlFlags);
181
- if( zRepo==0 ){
182
- int i;
183
- const char *z = file_tail(g.url.isFile ? g.url.name : g.url.path);
184
- if( z==0 || z[0]==0 ){
176
+ if( g.argc==4 ){
177
+ zRepo = g.argv[3];
178
+ }else{
179
+ char *zBase = url_to_repo_basename(g.argv[2]);
180
+ if( zBase==0 ){
185181
fossil_fatal(
186182
"unable to guess a repository name from the url \"%s\".\n"
187183
"give the repository filename as an additional argument.",
188184
g.argv[2]);
189185
}
190
- for(i=0; z[i] && z[i]!='.'; i++){}
186
+ zRepo = mprintf("./%s.fossil", zBase);
191187
if( zWorkDir==0 ){
192
- zWorkDir = mprintf("./%.*s", i, z);
188
+ zWorkDir = mprintf("./%s", zBase);
193189
}
194
- zRepo = mprintf("./%.*s.fossil", i, z);
195
- if( -1 != file_size(zRepo, ExtFILE) ){
196
- fossil_fatal("file already exists: %s", zRepo);
197
- }
190
+ fossil_free(zBase);
191
+ }
192
+ if( -1 != file_size(zRepo, ExtFILE) ){
193
+ fossil_fatal("file already exists: %s", zRepo);
198194
}
195
+ url_parse(g.argv[2], urlFlags);
199196
if( zDefaultUser==0 && g.url.user!=0 ) zDefaultUser = g.url.user;
200197
if( g.url.isFile ){
201198
file_copy(g.url.name, zRepo);
202199
db_close(1);
203200
db_open_repository(zRepo);
204201
--- src/clone.c
+++ src/clone.c
@@ -171,33 +171,30 @@
171
172 if( g.argc < 3 ){
173 usage("?OPTIONS? FILE-OR-URL ?NEW-REPOSITORY?");
174 }
175 db_open_config(0, 0);
176 zRepo = g.argc==4 ? g.argv[3] : 0;
177 if( zRepo!=0 && -1 != file_size(zRepo, ExtFILE) ){
178 fossil_fatal("file already exists: %s", zRepo);
179 }
180 url_parse(g.argv[2], urlFlags);
181 if( zRepo==0 ){
182 int i;
183 const char *z = file_tail(g.url.isFile ? g.url.name : g.url.path);
184 if( z==0 || z[0]==0 ){
185 fossil_fatal(
186 "unable to guess a repository name from the url \"%s\".\n"
187 "give the repository filename as an additional argument.",
188 g.argv[2]);
189 }
190 for(i=0; z[i] && z[i]!='.'; i++){}
191 if( zWorkDir==0 ){
192 zWorkDir = mprintf("./%.*s", i, z);
193 }
194 zRepo = mprintf("./%.*s.fossil", i, z);
195 if( -1 != file_size(zRepo, ExtFILE) ){
196 fossil_fatal("file already exists: %s", zRepo);
197 }
198 }
 
199 if( zDefaultUser==0 && g.url.user!=0 ) zDefaultUser = g.url.user;
200 if( g.url.isFile ){
201 file_copy(g.url.name, zRepo);
202 db_close(1);
203 db_open_repository(zRepo);
204
--- src/clone.c
+++ src/clone.c
@@ -171,33 +171,30 @@
171
172 if( g.argc < 3 ){
173 usage("?OPTIONS? FILE-OR-URL ?NEW-REPOSITORY?");
174 }
175 db_open_config(0, 0);
176 if( g.argc==4 ){
177 zRepo = g.argv[3];
178 }else{
179 char *zBase = url_to_repo_basename(g.argv[2]);
180 if( zBase==0 ){
 
 
 
 
181 fossil_fatal(
182 "unable to guess a repository name from the url \"%s\".\n"
183 "give the repository filename as an additional argument.",
184 g.argv[2]);
185 }
186 zRepo = mprintf("./%s.fossil", zBase);
187 if( zWorkDir==0 ){
188 zWorkDir = mprintf("./%s", zBase);
189 }
190 fossil_free(zBase);
191 }
192 if( -1 != file_size(zRepo, ExtFILE) ){
193 fossil_fatal("file already exists: %s", zRepo);
194 }
195 url_parse(g.argv[2], urlFlags);
196 if( zDefaultUser==0 && g.url.user!=0 ) zDefaultUser = g.url.user;
197 if( g.url.isFile ){
198 file_copy(g.url.name, zRepo);
199 db_close(1);
200 db_open_repository(zRepo);
201
+5 -3
--- src/db.c
+++ src/db.c
@@ -3505,13 +3505,15 @@
35053505
int rc; /* Result code from fossil_system() */
35063506
Blob cmd; /* Clone command to be run */
35073507
char *zCmd; /* String version of the clone command */
35083508
35093509
zUri = zRepo;
3510
- zNewBase = fossil_strdup(file_tail(zUri));
3511
- for(i=(int)strlen(zNewBase)-1; i>1 && zNewBase[i]!='.'; i--){}
3512
- if( zNewBase[i]=='.' ) zNewBase[i] = 0;
3510
+ zNewBase = url_to_repo_basename(zUri);
3511
+ if( zNewBase==0 ){
3512
+ fossil_fatal("unable to deduce a repository name from the url \"%s\"",
3513
+ zUri);
3514
+ }
35133515
if( zRepoDir==0 ) zRepoDir = zPwd;
35143516
zRepo = mprintf("%s/%s.fossil", zRepoDir, zNewBase);
35153517
fossil_free(zNewBase);
35163518
blob_init(&cmd, 0, 0);
35173519
blob_append_escaped_arg(&cmd, g.nameOfExe);
35183520
--- src/db.c
+++ src/db.c
@@ -3505,13 +3505,15 @@
3505 int rc; /* Result code from fossil_system() */
3506 Blob cmd; /* Clone command to be run */
3507 char *zCmd; /* String version of the clone command */
3508
3509 zUri = zRepo;
3510 zNewBase = fossil_strdup(file_tail(zUri));
3511 for(i=(int)strlen(zNewBase)-1; i>1 && zNewBase[i]!='.'; i--){}
3512 if( zNewBase[i]=='.' ) zNewBase[i] = 0;
 
 
3513 if( zRepoDir==0 ) zRepoDir = zPwd;
3514 zRepo = mprintf("%s/%s.fossil", zRepoDir, zNewBase);
3515 fossil_free(zNewBase);
3516 blob_init(&cmd, 0, 0);
3517 blob_append_escaped_arg(&cmd, g.nameOfExe);
3518
--- src/db.c
+++ src/db.c
@@ -3505,13 +3505,15 @@
3505 int rc; /* Result code from fossil_system() */
3506 Blob cmd; /* Clone command to be run */
3507 char *zCmd; /* String version of the clone command */
3508
3509 zUri = zRepo;
3510 zNewBase = url_to_repo_basename(zUri);
3511 if( zNewBase==0 ){
3512 fossil_fatal("unable to deduce a repository name from the url \"%s\"",
3513 zUri);
3514 }
3515 if( zRepoDir==0 ) zRepoDir = zPwd;
3516 zRepo = mprintf("%s/%s.fossil", zRepoDir, zNewBase);
3517 fossil_free(zNewBase);
3518 blob_init(&cmd, 0, 0);
3519 blob_append_escaped_arg(&cmd, g.nameOfExe);
3520
+30
--- src/url.c
+++ src/url.c
@@ -628,5 +628,35 @@
628628
&& isatty(fileno(stdin))
629629
){
630630
url_prompt_for_password();
631631
}
632632
}
633
+
634
+/*
635
+** Given a URL for a remote repository clone point, try to come up with a
636
+** reasonable basename of a local clone of that repository.
637
+**
638
+** * If the URL has a path, use the tail of the path, with any suffix
639
+** elided.
640
+**
641
+** * If the URL is just a domain name, without a path, then use the
642
+** first element of the domain name, except skip over "www." if
643
+** present.
644
+**
645
+** The string returned is obtained from fossil_malloc(). NULL might be
646
+** returned if there is an error.
647
+*/
648
+char *url_to_repo_basename(const char *zUrl){
649
+ char *zTail;
650
+ int i;
651
+ if( zUrl==0 ) return;
652
+ for(i=0; zUrl[i]; i++){
653
+ if( zUrl[i]=='?' ) break;
654
+ if( zUrl[i]=='/' && zUrl[i+1]!=0 ) zTail = &zUrl[i+1];
655
+ }
656
+ if( zTail==0 ) return 0;
657
+ if( sqlite3_strnicmp(zTail, "www.", 4)==0 ) zTail += 4;
658
+ if( zTail[i]==0 ) return 0;
659
+ for(i=0; zTail[i] && zTail[i]!='.' && zTail[i]!='?'; i++){}
660
+ if( i==0 ) return 0;
661
+ return mprintf("%.*s", i, zTail);
662
+}
633663
--- src/url.c
+++ src/url.c
@@ -628,5 +628,35 @@
628 && isatty(fileno(stdin))
629 ){
630 url_prompt_for_password();
631 }
632 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
633
--- src/url.c
+++ src/url.c
@@ -628,5 +628,35 @@
628 && isatty(fileno(stdin))
629 ){
630 url_prompt_for_password();
631 }
632 }
633
634 /*
635 ** Given a URL for a remote repository clone point, try to come up with a
636 ** reasonable basename of a local clone of that repository.
637 **
638 ** * If the URL has a path, use the tail of the path, with any suffix
639 ** elided.
640 **
641 ** * If the URL is just a domain name, without a path, then use the
642 ** first element of the domain name, except skip over "www." if
643 ** present.
644 **
645 ** The string returned is obtained from fossil_malloc(). NULL might be
646 ** returned if there is an error.
647 */
648 char *url_to_repo_basename(const char *zUrl){
649 char *zTail;
650 int i;
651 if( zUrl==0 ) return;
652 for(i=0; zUrl[i]; i++){
653 if( zUrl[i]=='?' ) break;
654 if( zUrl[i]=='/' && zUrl[i+1]!=0 ) zTail = &zUrl[i+1];
655 }
656 if( zTail==0 ) return 0;
657 if( sqlite3_strnicmp(zTail, "www.", 4)==0 ) zTail += 4;
658 if( zTail[i]==0 ) return 0;
659 for(i=0; zTail[i] && zTail[i]!='.' && zTail[i]!='?'; i++){}
660 if( i==0 ) return 0;
661 return mprintf("%.*s", i, zTail);
662 }
663

Keyboard Shortcuts

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