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 ;
@@ -434,6 +455,87 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
434
455
return 0 ; /* error out quietly */
435
456
}
436
457
458
+ static char * default_cache_root (const char * root )
459
+ {
460
+ const char * env ;
461
+
462
+ if (is_unattended ())
463
+ return xstrfmt ("%s/.scalarCache" , root );
464
+
465
+ #ifdef WIN32
466
+ (void )env ;
467
+ return xstrfmt ("%.*s.scalarCache" , offset_1st_component (root ), root );
468
+ #elif defined(__APPLE__ )
469
+ if ((env = getenv ("HOME" )) && * env )
470
+ return xstrfmt ("%s/.scalarCache" , env );
471
+ return NULL ;
472
+ #else
473
+ if ((env = getenv ("XDG_CACHE_HOME" )) && * env )
474
+ return xstrfmt ("%s/scalar" , env );
475
+ if ((env = getenv ("HOME" )) && * env )
476
+ return xstrfmt ("%s/.cache/scalar" , env );
477
+ return NULL ;
478
+ #endif
479
+ }
480
+
481
+ static int get_repository_id (struct json_iterator * it )
482
+ {
483
+ if (it -> type == JSON_STRING &&
484
+ !strcasecmp (".repository.id" , it -> key .buf )) {
485
+ * (char * * )it -> fn_data = strbuf_detach (& it -> string_value , NULL );
486
+ return 1 ;
487
+ }
488
+
489
+ return 0 ;
490
+ }
491
+
492
+ /* Needs to run this in a worktree; gvfs-helper requires a Git repository */
493
+ static char * get_cache_key (const char * url )
494
+ {
495
+ struct child_process cp = CHILD_PROCESS_INIT ;
496
+ struct strbuf out = STRBUF_INIT ;
497
+ char * cache_key = NULL ;
498
+
499
+ cp .git_cmd = 1 ;
500
+ strvec_pushl (& cp .args , "gvfs-helper" , "--remote" , url ,
501
+ "endpoint" , "vsts/info" , NULL );
502
+ if (!pipe_command (& cp , NULL , 0 , & out , 512 , NULL , 0 )) {
503
+ char * id = NULL ;
504
+ struct json_iterator it =
505
+ JSON_ITERATOR_INIT (out .buf , get_repository_id , & id );
506
+
507
+ if (iterate_json (& it ) < 0 )
508
+ warning ("JSON parse error (%s)" , out .buf );
509
+ else if (id )
510
+ cache_key = xstrfmt ("id_%s" , id );
511
+ free (id );
512
+ }
513
+
514
+ if (!cache_key ) {
515
+ struct strbuf downcased = STRBUF_INIT ;
516
+ int hash_algo_index = hash_algo_by_name ("sha1" );
517
+ const struct git_hash_algo * hash_algo = hash_algo_index < 0 ?
518
+ the_hash_algo : & hash_algos [hash_algo_index ];
519
+ struct git_hash_ctx ctx ;
520
+ unsigned char hash [GIT_MAX_RAWSZ ];
521
+
522
+ strbuf_addstr (& downcased , url );
523
+ strbuf_tolower (& downcased );
524
+
525
+ hash_algo -> init_fn (& ctx );
526
+ hash_algo -> update_fn (& ctx , downcased .buf , downcased .len );
527
+ hash_algo -> final_fn (hash , & ctx );
528
+
529
+ strbuf_release (& downcased );
530
+
531
+ cache_key = xstrfmt ("url_%s" ,
532
+ hash_to_hex_algop (hash , hash_algo ));
533
+ }
534
+
535
+ strbuf_release (& out );
536
+ return cache_key ;
537
+ }
538
+
437
539
static char * remote_default_branch (const char * url )
438
540
{
439
541
struct child_process cp = CHILD_PROCESS_INIT ;
@@ -528,14 +630,52 @@ void load_builtin_commands(const char *prefix UNUSED,
528
630
die ("not implemented" );
529
631
}
530
632
633
+ static int init_shared_object_cache (const char * url ,
634
+ const char * local_cache_root )
635
+ {
636
+ struct strbuf buf = STRBUF_INIT ;
637
+ int res = 0 ;
638
+ char * cache_key = NULL , * shared_cache_path = NULL , * alternates = NULL ;
639
+
640
+ if (!(cache_key = get_cache_key (url ))) {
641
+ res = error (_ ("could not determine cache key for '%s'" ), url );
642
+ goto cleanup ;
643
+ }
644
+
645
+ shared_cache_path = xstrfmt ("%s/%s" , local_cache_root , cache_key );
646
+ if (set_config ("gvfs.sharedCache=%s" , shared_cache_path )) {
647
+ res = error (_ ("could not configure shared cache" ));
648
+ goto cleanup ;
649
+ }
650
+
651
+ strbuf_addf (& buf , "%s/pack" , shared_cache_path );
652
+ switch (safe_create_leading_directories (the_repository , buf .buf )) {
653
+ case SCLD_OK : case SCLD_EXISTS :
654
+ break ; /* okay */
655
+ default :
656
+ res = error_errno (_ ("could not initialize '%s'" ), buf .buf );
657
+ goto cleanup ;
658
+ }
659
+
660
+ alternates = repo_git_path (the_repository , "objects/info/alternates" );
661
+ write_file (alternates , "%s\n" , shared_cache_path );
662
+
663
+ cleanup :
664
+ strbuf_release (& buf );
665
+ free (shared_cache_path );
666
+ free (cache_key );
667
+ free (alternates );
668
+ return res ;
669
+ }
670
+
531
671
static int cmd_clone (int argc , const char * * argv )
532
672
{
533
673
const char * branch = NULL ;
534
674
char * branch_to_free = NULL ;
535
675
int full_clone = 0 , single_branch = 0 , show_progress = isatty (2 );
536
676
int src = 1 , tags = 1 , maintenance = 1 ;
537
- const char * cache_server_url = NULL ;
538
- char * default_cache_server_url = NULL ;
677
+ const char * cache_server_url = NULL , * local_cache_root = NULL ;
678
+ char * default_cache_server_url = NULL , * local_cache_root_abs = NULL ;
539
679
struct option clone_options [] = {
540
680
OPT_STRING ('b' , "branch" , & branch , N_ ("<branch>" ),
541
681
N_ ("branch to checkout after clone" )),
@@ -553,6 +693,9 @@ static int cmd_clone(int argc, const char **argv)
553
693
OPT_STRING (0 , "cache-server-url" , & cache_server_url ,
554
694
N_ ("<url>" ),
555
695
N_ ("the url or friendly name of the cache server" )),
696
+ OPT_STRING (0 , "local-cache-path" , & local_cache_root ,
697
+ N_ ("<path>" ),
698
+ N_ ("override the path for the local Scalar cache" )),
556
699
OPT_END (),
557
700
};
558
701
const char * const clone_usage [] = {
@@ -594,11 +737,23 @@ static int cmd_clone(int argc, const char **argv)
594
737
if (is_directory (enlistment ))
595
738
die (_ ("directory '%s' exists already" ), enlistment );
596
739
740
+ ensure_absolute_path (enlistment , & enlistment );
741
+
597
742
if (src )
598
743
dir = xstrfmt ("%s/src" , enlistment );
599
744
else
600
745
dir = xstrdup (enlistment );
601
746
747
+ if (!local_cache_root )
748
+ local_cache_root = local_cache_root_abs =
749
+ default_cache_root (enlistment );
750
+ else
751
+ local_cache_root = ensure_absolute_path (local_cache_root ,
752
+ & local_cache_root_abs );
753
+
754
+ if (!local_cache_root )
755
+ die (_ ("could not determine local cache root" ));
756
+
602
757
strbuf_reset (& buf );
603
758
if (branch )
604
759
strbuf_addf (& buf , "init.defaultBranch=%s" , branch );
@@ -618,8 +773,28 @@ static int cmd_clone(int argc, const char **argv)
618
773
619
774
setup_git_directory ();
620
775
776
+ git_config (git_default_config , NULL );
777
+
778
+ /*
779
+ * This `dir_inside_of()` call relies on git_config() having parsed the
780
+ * newly-initialized repository config's `core.ignoreCase` value.
781
+ */
782
+ if (dir_inside_of (local_cache_root , dir ) >= 0 ) {
783
+ struct strbuf path = STRBUF_INIT ;
784
+
785
+ strbuf_addstr (& path , enlistment );
786
+ if (chdir ("../.." ) < 0 ||
787
+ remove_dir_recursively (& path , 0 ) < 0 )
788
+ die (_ ("'--local-cache-path' cannot be inside the src "
789
+ "folder;\nCould not remove '%s'" ), enlistment );
790
+
791
+ die (_ ("'--local-cache-path' cannot be inside the src folder" ));
792
+ }
793
+
621
794
/* common-main already logs `argv` */
622
795
trace2_def_repo (the_repository );
796
+ trace2_data_intmax ("scalar" , the_repository , "unattended" ,
797
+ is_unattended ());
623
798
624
799
if (!branch && !(branch = branch_to_free = remote_default_branch (url ))) {
625
800
res = error (_ ("failed to get default branch for '%s'" ), url );
@@ -649,6 +824,8 @@ static int cmd_clone(int argc, const char **argv)
649
824
supports_gvfs_protocol (url , & default_cache_server_url );
650
825
651
826
if (gvfs_protocol ) {
827
+ if ((res = init_shared_object_cache (url , local_cache_root )))
828
+ goto cleanup ;
652
829
if (!cache_server_url )
653
830
cache_server_url = default_cache_server_url ;
654
831
if (set_config ("core.useGVFSHelper=true" ) ||
@@ -727,6 +904,7 @@ static int cmd_clone(int argc, const char **argv)
727
904
free (dir );
728
905
strbuf_release (& buf );
729
906
free (default_cache_server_url );
907
+ free (local_cache_root_abs );
730
908
return res ;
731
909
}
732
910
@@ -1161,6 +1339,12 @@ int cmd_main(int argc, const char **argv)
1161
1339
struct strbuf scalar_usage = STRBUF_INIT ;
1162
1340
int i ;
1163
1341
1342
+ if (is_unattended ()) {
1343
+ setenv ("GIT_ASKPASS" , "" , 0 );
1344
+ setenv ("GIT_TERMINAL_PROMPT" , "false" , 0 );
1345
+ git_config_push_parameter ("credential.interactive=false" );
1346
+ }
1347
+
1164
1348
while (argc > 1 && * argv [1 ] == '-' ) {
1165
1349
if (!strcmp (argv [1 ], "-C" )) {
1166
1350
if (argc < 3 )
0 commit comments