@@ -252,16 +252,6 @@ static void changed_files(struct hashmap *result, const char *index_path,
252
252
strbuf_release (& buf );
253
253
}
254
254
255
- static NORETURN void exit_cleanup (const char * tmpdir , int exit_code )
256
- {
257
- struct strbuf buf = STRBUF_INIT ;
258
- strbuf_addstr (& buf , tmpdir );
259
- remove_dir_recursively (& buf , 0 );
260
- if (exit_code )
261
- warning (_ ("failed: %d" ), exit_code );
262
- exit (exit_code );
263
- }
264
-
265
255
static int ensure_leading_directories (char * path )
266
256
{
267
257
switch (safe_create_leading_directories (path )) {
@@ -333,16 +323,16 @@ static int checkout_path(unsigned mode, struct object_id *oid,
333
323
static int run_dir_diff (const char * extcmd , int symlinks , const char * prefix ,
334
324
struct child_process * child )
335
325
{
336
- char tmpdir [PATH_MAX ];
337
326
struct strbuf info = STRBUF_INIT , lpath = STRBUF_INIT ;
338
327
struct strbuf rpath = STRBUF_INIT , buf = STRBUF_INIT ;
339
328
struct strbuf ldir = STRBUF_INIT , rdir = STRBUF_INIT ;
340
329
struct strbuf wtdir = STRBUF_INIT ;
341
- char * lbase_dir , * rbase_dir ;
330
+ struct strbuf tmpdir = STRBUF_INIT ;
331
+ char * lbase_dir = NULL , * rbase_dir = NULL ;
342
332
size_t ldir_len , rdir_len , wtdir_len ;
343
333
const char * workdir , * tmp ;
344
334
int ret = 0 , i ;
345
- FILE * fp ;
335
+ FILE * fp = NULL ;
346
336
struct hashmap working_tree_dups = HASHMAP_INIT (working_tree_entry_cmp ,
347
337
NULL );
348
338
struct hashmap submodules = HASHMAP_INIT (pair_cmp , NULL );
@@ -351,7 +341,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
351
341
struct pair_entry * entry ;
352
342
struct index_state wtindex ;
353
343
struct checkout lstate , rstate ;
354
- int rc , flags = RUN_GIT_CMD , err = 0 ;
344
+ int flags = RUN_GIT_CMD , err = 0 ;
355
345
const char * helper_argv [] = { "difftool--helper" , NULL , NULL , NULL };
356
346
struct hashmap wt_modified , tmp_modified ;
357
347
int indices_loaded = 0 ;
@@ -360,11 +350,15 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
360
350
361
351
/* Setup temp directories */
362
352
tmp = getenv ("TMPDIR" );
363
- xsnprintf (tmpdir , sizeof (tmpdir ), "%s/git-difftool.XXXXXX" , tmp ? tmp : "/tmp" );
364
- if (!mkdtemp (tmpdir ))
365
- return error ("could not create '%s'" , tmpdir );
366
- strbuf_addf (& ldir , "%s/left/" , tmpdir );
367
- strbuf_addf (& rdir , "%s/right/" , tmpdir );
353
+ strbuf_add_absolute_path (& tmpdir , tmp ? tmp : "/tmp" );
354
+ strbuf_trim_trailing_dir_sep (& tmpdir );
355
+ strbuf_addstr (& tmpdir , "/git-difftool.XXXXXX" );
356
+ if (!mkdtemp (tmpdir .buf )) {
357
+ ret = error ("could not create '%s'" , tmpdir .buf );
358
+ goto finish ;
359
+ }
360
+ strbuf_addf (& ldir , "%s/left/" , tmpdir .buf );
361
+ strbuf_addf (& rdir , "%s/right/" , tmpdir .buf );
368
362
strbuf_addstr (& wtdir , workdir );
369
363
if (!wtdir .len || !is_dir_sep (wtdir .buf [wtdir .len - 1 ]))
370
364
strbuf_addch (& wtdir , '/' );
@@ -580,7 +574,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
580
574
flags = 0 ;
581
575
} else
582
576
setenv ("GIT_DIFFTOOL_DIRDIFF" , "true" , 1 );
583
- rc = run_command_v_opt (helper_argv , flags );
577
+ ret = run_command_v_opt (helper_argv , flags );
584
578
585
579
/* TODO: audit for interaction with sparse-index. */
586
580
ensure_full_index (& wtindex );
@@ -614,7 +608,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
614
608
if (!indices_loaded ) {
615
609
struct lock_file lock = LOCK_INIT ;
616
610
strbuf_reset (& buf );
617
- strbuf_addf (& buf , "%s/wtindex" , tmpdir );
611
+ strbuf_addf (& buf , "%s/wtindex" , tmpdir . buf );
618
612
if (hold_lock_file_for_update (& lock , buf .buf , 0 ) < 0 ||
619
613
write_locked_index (& wtindex , & lock , COMMIT_LOCK )) {
620
614
ret = error ("could not write %s" , buf .buf );
@@ -644,11 +638,14 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
644
638
}
645
639
646
640
if (err ) {
647
- warning (_ ("temporary files exist in '%s'." ), tmpdir );
641
+ warning (_ ("temporary files exist in '%s'." ), tmpdir . buf );
648
642
warning (_ ("you may want to cleanup or recover these." ));
649
- exit (1 );
650
- } else
651
- exit_cleanup (tmpdir , rc );
643
+ ret = 1 ;
644
+ } else {
645
+ remove_dir_recursively (& tmpdir , 0 );
646
+ if (ret )
647
+ warning (_ ("failed: %d" ), ret );
648
+ }
652
649
653
650
finish :
654
651
if (fp )
@@ -660,8 +657,9 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
660
657
strbuf_release (& rdir );
661
658
strbuf_release (& wtdir );
662
659
strbuf_release (& buf );
660
+ strbuf_release (& tmpdir );
663
661
664
- return ret ;
662
+ return ( ret < 0 ) ? 1 : ret ;
665
663
}
666
664
667
665
static int run_file_diff (int prompt , const char * prefix ,
0 commit comments