@@ -76,7 +76,7 @@ static const char **internal_prefix_pathspec(const char *prefix,
76
76
return result ;
77
77
}
78
78
79
- static const char * add_slash (const char * path )
79
+ static char * add_slash (const char * path )
80
80
{
81
81
size_t len = strlen (path );
82
82
if (len && path [len - 1 ] != '/' ) {
@@ -86,7 +86,7 @@ static const char *add_slash(const char *path)
86
86
with_slash [len ] = 0 ;
87
87
return with_slash ;
88
88
}
89
- return path ;
89
+ return xstrdup ( path ) ;
90
90
}
91
91
92
92
#define SUBMODULE_WITH_GITDIR ((const char *)1)
@@ -111,7 +111,7 @@ static void prepare_move_submodule(const char *src, int first,
111
111
static int index_range_of_same_dir (const char * src , int length ,
112
112
int * first_p , int * last_p )
113
113
{
114
- const char * src_w_slash = add_slash (src );
114
+ char * src_w_slash = add_slash (src );
115
115
int first , last , len_w_slash = length + 1 ;
116
116
117
117
first = index_name_pos (the_repository -> index , src_w_slash , len_w_slash );
@@ -124,8 +124,8 @@ static int index_range_of_same_dir(const char *src, int length,
124
124
if (strncmp (path , src_w_slash , len_w_slash ))
125
125
break ;
126
126
}
127
- if ( src_w_slash != src )
128
- free (( char * ) src_w_slash );
127
+
128
+ free (src_w_slash );
129
129
* first_p = first ;
130
130
* last_p = last ;
131
131
return last - first ;
@@ -141,7 +141,7 @@ static int index_range_of_same_dir(const char *src, int length,
141
141
static int empty_dir_has_sparse_contents (const char * name )
142
142
{
143
143
int ret = 0 ;
144
- const char * with_slash = add_slash (name );
144
+ char * with_slash = add_slash (name );
145
145
int length = strlen (with_slash );
146
146
147
147
int pos = index_name_pos (the_repository -> index , with_slash , length );
@@ -159,8 +159,7 @@ static int empty_dir_has_sparse_contents(const char *name)
159
159
}
160
160
161
161
free_return :
162
- if (with_slash != name )
163
- free ((char * )with_slash );
162
+ free (with_slash );
164
163
return ret ;
165
164
}
166
165
@@ -178,7 +177,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
178
177
OPT_END (),
179
178
};
180
179
const char * * source , * * destination , * * dest_path , * * submodule_gitfile ;
181
- const char * dst_w_slash ;
180
+ char * dst_w_slash = NULL ;
182
181
const char * * src_dir = NULL ;
183
182
int src_dir_nr = 0 , src_dir_alloc = 0 ;
184
183
struct strbuf a_src_dir = STRBUF_INIT ;
@@ -243,10 +242,6 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
243
242
dst_mode = SPARSE ;
244
243
}
245
244
}
246
- if (dst_w_slash != dest_path [0 ]) {
247
- free ((char * )dst_w_slash );
248
- dst_w_slash = NULL ;
249
- }
250
245
251
246
/* Checking */
252
247
for (i = 0 ; i < argc ; i ++ ) {
@@ -265,12 +260,14 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
265
260
266
261
pos = index_name_pos (the_repository -> index , src , length );
267
262
if (pos < 0 ) {
268
- const char * src_w_slash = add_slash (src );
263
+ char * src_w_slash = add_slash (src );
269
264
if (!path_in_sparse_checkout (src_w_slash , the_repository -> index ) &&
270
265
empty_dir_has_sparse_contents (src )) {
266
+ free (src_w_slash );
271
267
modes [i ] |= SKIP_WORKTREE_DIR ;
272
268
goto dir_check ;
273
269
}
270
+ free (src_w_slash );
274
271
/* only error if existence is expected. */
275
272
if (!(modes [i ] & SPARSE ))
276
273
bad = _ ("bad source" );
@@ -310,7 +307,9 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
310
307
311
308
dir_check :
312
309
if (S_ISDIR (st .st_mode )) {
313
- int j , dst_len , n ;
310
+ char * dst_with_slash ;
311
+ size_t dst_with_slash_len ;
312
+ int j , n ;
314
313
int first = index_name_pos (the_repository -> index , src , length ), last ;
315
314
316
315
if (first >= 0 ) {
@@ -335,19 +334,21 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
335
334
REALLOC_ARRAY (modes , n );
336
335
REALLOC_ARRAY (submodule_gitfile , n );
337
336
338
- dst = add_slash (dst );
339
- dst_len = strlen (dst );
337
+ dst_with_slash = add_slash (dst );
338
+ dst_with_slash_len = strlen (dst_with_slash );
340
339
341
340
for (j = 0 ; j < last - first ; j ++ ) {
342
341
const struct cache_entry * ce = the_repository -> index -> cache [first + j ];
343
342
const char * path = ce -> name ;
344
343
source [argc + j ] = path ;
345
344
destination [argc + j ] =
346
- prefix_path (dst , dst_len , path + length + 1 );
345
+ prefix_path (dst_with_slash , dst_with_slash_len , path + length + 1 );
347
346
memset (modes + argc + j , 0 , sizeof (enum update_mode ));
348
347
modes [argc + j ] |= ce_skip_worktree (ce ) ? SPARSE : INDEX ;
349
348
submodule_gitfile [argc + j ] = NULL ;
350
349
}
350
+
351
+ free (dst_with_slash );
351
352
argc += last - first ;
352
353
goto act_on_entry ;
353
354
}
@@ -565,6 +566,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
565
566
COMMIT_LOCK | SKIP_IF_UNCHANGED ))
566
567
die (_ ("Unable to write new index file" ));
567
568
569
+ free (dst_w_slash );
568
570
string_list_clear (& src_for_dst , 0 );
569
571
string_list_clear (& dirty_paths , 0 );
570
572
UNLEAK (source );
0 commit comments