7
7
#include "git-compat-util.h"
8
8
#include "abspath.h"
9
9
#include "gettext.h"
10
+ #include "hex.h"
10
11
#include "parse-options.h"
11
12
#include "config.h"
12
13
#include "run-command.h"
15
16
#include "fsmonitor-settings.h"
16
17
#include "refs.h"
17
18
#include "dir.h"
19
+ #include "object-file.h"
18
20
#include "packfile.h"
19
21
#include "help.h"
20
22
#include "setup.h"
23
+ #include "wrapper.h"
21
24
#include "trace2.h"
22
25
#include "json-parser.h"
26
+ #include "path.h"
27
+
28
+ static int is_unattended (void ) {
29
+ return git_env_bool ("Scalar_UNATTENDED" , 0 );
30
+ }
23
31
24
32
static void setup_enlistment_directory (int argc , const char * * argv ,
25
33
const char * const * usagestr ,
@@ -106,6 +114,19 @@ static int run_git(const char *arg, ...)
106
114
return res ;
107
115
}
108
116
117
+ static const char * ensure_absolute_path (const char * path , char * * absolute )
118
+ {
119
+ struct strbuf buf = STRBUF_INIT ;
120
+
121
+ if (is_absolute_path (path ))
122
+ return path ;
123
+
124
+ strbuf_realpath_forgiving (& buf , path , 1 );
125
+ free (* absolute );
126
+ * absolute = strbuf_detach (& buf , NULL );
127
+ return * absolute ;
128
+ }
129
+
109
130
struct scalar_config {
110
131
const char * key ;
111
132
const char * value ;
@@ -418,6 +439,87 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
418
439
return 0 ; /* error out quietly */
419
440
}
420
441
442
+ static char * default_cache_root (const char * root )
443
+ {
444
+ const char * env ;
445
+
446
+ if (is_unattended ())
447
+ return xstrfmt ("%s/.scalarCache" , root );
448
+
449
+ #ifdef WIN32
450
+ (void )env ;
451
+ return xstrfmt ("%.*s.scalarCache" , offset_1st_component (root ), root );
452
+ #elif defined(__APPLE__ )
453
+ if ((env = getenv ("HOME" )) && * env )
454
+ return xstrfmt ("%s/.scalarCache" , env );
455
+ return NULL ;
456
+ #else
457
+ if ((env = getenv ("XDG_CACHE_HOME" )) && * env )
458
+ return xstrfmt ("%s/scalar" , env );
459
+ if ((env = getenv ("HOME" )) && * env )
460
+ return xstrfmt ("%s/.cache/scalar" , env );
461
+ return NULL ;
462
+ #endif
463
+ }
464
+
465
+ static int get_repository_id (struct json_iterator * it )
466
+ {
467
+ if (it -> type == JSON_STRING &&
468
+ !strcasecmp (".repository.id" , it -> key .buf )) {
469
+ * (char * * )it -> fn_data = strbuf_detach (& it -> string_value , NULL );
470
+ return 1 ;
471
+ }
472
+
473
+ return 0 ;
474
+ }
475
+
476
+ /* Needs to run this in a worktree; gvfs-helper requires a Git repository */
477
+ static char * get_cache_key (const char * url )
478
+ {
479
+ struct child_process cp = CHILD_PROCESS_INIT ;
480
+ struct strbuf out = STRBUF_INIT ;
481
+ char * cache_key = NULL ;
482
+
483
+ cp .git_cmd = 1 ;
484
+ strvec_pushl (& cp .args , "gvfs-helper" , "--remote" , url ,
485
+ "endpoint" , "vsts/info" , NULL );
486
+ if (!pipe_command (& cp , NULL , 0 , & out , 512 , NULL , 0 )) {
487
+ char * id = NULL ;
488
+ struct json_iterator it =
489
+ JSON_ITERATOR_INIT (out .buf , get_repository_id , & id );
490
+
491
+ if (iterate_json (& it ) < 0 )
492
+ warning ("JSON parse error (%s)" , out .buf );
493
+ else if (id )
494
+ cache_key = xstrfmt ("id_%s" , id );
495
+ free (id );
496
+ }
497
+
498
+ if (!cache_key ) {
499
+ struct strbuf downcased = STRBUF_INIT ;
500
+ int hash_algo_index = hash_algo_by_name ("sha1" );
501
+ const struct git_hash_algo * hash_algo = hash_algo_index < 0 ?
502
+ the_hash_algo : & hash_algos [hash_algo_index ];
503
+ struct git_hash_ctx ctx ;
504
+ unsigned char hash [GIT_MAX_RAWSZ ];
505
+
506
+ strbuf_addstr (& downcased , url );
507
+ strbuf_tolower (& downcased );
508
+
509
+ hash_algo -> init_fn (& ctx );
510
+ hash_algo -> update_fn (& ctx , downcased .buf , downcased .len );
511
+ hash_algo -> final_fn (hash , & ctx );
512
+
513
+ strbuf_release (& downcased );
514
+
515
+ cache_key = xstrfmt ("url_%s" ,
516
+ hash_to_hex_algop (hash , hash_algo ));
517
+ }
518
+
519
+ strbuf_release (& out );
520
+ return cache_key ;
521
+ }
522
+
421
523
static char * remote_default_branch (const char * url )
422
524
{
423
525
struct child_process cp = CHILD_PROCESS_INIT ;
@@ -512,14 +614,52 @@ void load_builtin_commands(const char *prefix UNUSED,
512
614
die ("not implemented" );
513
615
}
514
616
617
+ static int init_shared_object_cache (const char * url ,
618
+ const char * local_cache_root )
619
+ {
620
+ struct strbuf buf = STRBUF_INIT ;
621
+ int res = 0 ;
622
+ char * cache_key = NULL , * shared_cache_path = NULL , * alternates = NULL ;
623
+
624
+ if (!(cache_key = get_cache_key (url ))) {
625
+ res = error (_ ("could not determine cache key for '%s'" ), url );
626
+ goto cleanup ;
627
+ }
628
+
629
+ shared_cache_path = xstrfmt ("%s/%s" , local_cache_root , cache_key );
630
+ if (set_config ("gvfs.sharedCache=%s" , shared_cache_path )) {
631
+ res = error (_ ("could not configure shared cache" ));
632
+ goto cleanup ;
633
+ }
634
+
635
+ strbuf_addf (& buf , "%s/pack" , shared_cache_path );
636
+ switch (safe_create_leading_directories (buf .buf )) {
637
+ case SCLD_OK : case SCLD_EXISTS :
638
+ break ; /* okay */
639
+ default :
640
+ res = error_errno (_ ("could not initialize '%s'" ), buf .buf );
641
+ goto cleanup ;
642
+ }
643
+
644
+ alternates = repo_git_path (the_repository , "objects/info/alternates" );
645
+ write_file (alternates , "%s\n" , shared_cache_path );
646
+
647
+ cleanup :
648
+ strbuf_release (& buf );
649
+ free (shared_cache_path );
650
+ free (cache_key );
651
+ free (alternates );
652
+ return res ;
653
+ }
654
+
515
655
static int cmd_clone (int argc , const char * * argv )
516
656
{
517
657
const char * branch = NULL ;
518
658
char * branch_to_free = NULL ;
519
659
int full_clone = 0 , single_branch = 0 , show_progress = isatty (2 );
520
660
int src = 1 , tags = 1 ;
521
- const char * cache_server_url = NULL ;
522
- char * default_cache_server_url = NULL ;
661
+ const char * cache_server_url = NULL , * local_cache_root = NULL ;
662
+ char * default_cache_server_url = NULL , * local_cache_root_abs = NULL ;
523
663
struct option clone_options [] = {
524
664
OPT_STRING ('b' , "branch" , & branch , N_ ("<branch>" ),
525
665
N_ ("branch to checkout after clone" )),
@@ -535,6 +675,9 @@ static int cmd_clone(int argc, const char **argv)
535
675
OPT_STRING (0 , "cache-server-url" , & cache_server_url ,
536
676
N_ ("<url>" ),
537
677
N_ ("the url or friendly name of the cache server" )),
678
+ OPT_STRING (0 , "local-cache-path" , & local_cache_root ,
679
+ N_ ("<path>" ),
680
+ N_ ("override the path for the local Scalar cache" )),
538
681
OPT_END (),
539
682
};
540
683
const char * const clone_usage [] = {
@@ -576,11 +719,23 @@ static int cmd_clone(int argc, const char **argv)
576
719
if (is_directory (enlistment ))
577
720
die (_ ("directory '%s' exists already" ), enlistment );
578
721
722
+ ensure_absolute_path (enlistment , & enlistment );
723
+
579
724
if (src )
580
725
dir = xstrfmt ("%s/src" , enlistment );
581
726
else
582
727
dir = xstrdup (enlistment );
583
728
729
+ if (!local_cache_root )
730
+ local_cache_root = local_cache_root_abs =
731
+ default_cache_root (enlistment );
732
+ else
733
+ local_cache_root = ensure_absolute_path (local_cache_root ,
734
+ & local_cache_root_abs );
735
+
736
+ if (!local_cache_root )
737
+ die (_ ("could not determine local cache root" ));
738
+
584
739
strbuf_reset (& buf );
585
740
if (branch )
586
741
strbuf_addf (& buf , "init.defaultBranch=%s" , branch );
@@ -600,8 +755,28 @@ static int cmd_clone(int argc, const char **argv)
600
755
601
756
setup_git_directory ();
602
757
758
+ git_config (git_default_config , NULL );
759
+
760
+ /*
761
+ * This `dir_inside_of()` call relies on git_config() having parsed the
762
+ * newly-initialized repository config's `core.ignoreCase` value.
763
+ */
764
+ if (dir_inside_of (local_cache_root , dir ) >= 0 ) {
765
+ struct strbuf path = STRBUF_INIT ;
766
+
767
+ strbuf_addstr (& path , enlistment );
768
+ if (chdir ("../.." ) < 0 ||
769
+ remove_dir_recursively (& path , 0 ) < 0 )
770
+ die (_ ("'--local-cache-path' cannot be inside the src "
771
+ "folder;\nCould not remove '%s'" ), enlistment );
772
+
773
+ die (_ ("'--local-cache-path' cannot be inside the src folder" ));
774
+ }
775
+
603
776
/* common-main already logs `argv` */
604
777
trace2_def_repo (the_repository );
778
+ trace2_data_intmax ("scalar" , the_repository , "unattended" ,
779
+ is_unattended ());
605
780
606
781
if (!branch && !(branch = branch_to_free = remote_default_branch (url ))) {
607
782
res = error (_ ("failed to get default branch for '%s'" ), url );
@@ -631,6 +806,8 @@ static int cmd_clone(int argc, const char **argv)
631
806
supports_gvfs_protocol (url , & default_cache_server_url );
632
807
633
808
if (gvfs_protocol ) {
809
+ if ((res = init_shared_object_cache (url , local_cache_root )))
810
+ goto cleanup ;
634
811
if (!cache_server_url )
635
812
cache_server_url = default_cache_server_url ;
636
813
if (set_config ("core.useGVFSHelper=true" ) ||
@@ -708,6 +885,7 @@ static int cmd_clone(int argc, const char **argv)
708
885
free (dir );
709
886
strbuf_release (& buf );
710
887
free (default_cache_server_url );
888
+ free (local_cache_root_abs );
711
889
return res ;
712
890
}
713
891
@@ -1119,6 +1297,12 @@ int cmd_main(int argc, const char **argv)
1119
1297
struct strbuf scalar_usage = STRBUF_INIT ;
1120
1298
int i ;
1121
1299
1300
+ if (is_unattended ()) {
1301
+ setenv ("GIT_ASKPASS" , "" , 0 );
1302
+ setenv ("GIT_TERMINAL_PROMPT" , "false" , 0 );
1303
+ git_config_push_parameter ("credential.interactive=false" );
1304
+ }
1305
+
1122
1306
while (argc > 1 && * argv [1 ] == '-' ) {
1123
1307
if (!strcmp (argv [1 ], "-C" )) {
1124
1308
if (argc < 3 )
0 commit comments