@@ -1262,29 +1262,30 @@ int git_diff__from_iterators(
1262
1262
return error ;
1263
1263
}
1264
1264
1265
- #define DIFF_FROM_ITERATORS (MAKE_FIRST , FLAGS_FIRST , MAKE_SECOND , FLAGS_SECOND ) do { \
1266
- git_iterator *a = NULL, *b = NULL; \
1267
- char *pfx = (opts && !(opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) ? \
1268
- git_pathspec_prefix(&opts->pathspec) : NULL; \
1269
- git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT, \
1270
- b_opts = GIT_ITERATOR_OPTIONS_INIT; \
1271
- a_opts.flags = FLAGS_FIRST; \
1272
- a_opts.start = pfx; \
1273
- a_opts.end = pfx; \
1274
- b_opts.flags = FLAGS_SECOND; \
1275
- b_opts.start = pfx; \
1276
- b_opts.end = pfx; \
1277
- GIT_ERROR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); \
1278
- if (opts && (opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) { \
1279
- a_opts.pathlist.strings = opts->pathspec.strings; \
1280
- a_opts.pathlist.count = opts->pathspec.count; \
1281
- b_opts.pathlist.strings = opts->pathspec.strings; \
1282
- b_opts.pathlist.count = opts->pathspec.count; \
1283
- } \
1284
- if (!error && !(error = MAKE_FIRST) && !(error = MAKE_SECOND)) \
1285
- error = git_diff__from_iterators(&diff, repo, a, b, opts); \
1286
- git__free(pfx); git_iterator_free(a); git_iterator_free(b); \
1287
- } while (0)
1265
+ static int diff_prepare_iterator_opts (char * * prefix , git_iterator_options * a , int aflags ,
1266
+ git_iterator_options * b , int bflags ,
1267
+ const git_diff_options * opts )
1268
+ {
1269
+ GIT_ERROR_CHECK_VERSION (opts , GIT_DIFF_OPTIONS_VERSION , "git_diff_options" );
1270
+
1271
+ * prefix = NULL ;
1272
+
1273
+ if (opts && (opts -> flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH )) {
1274
+ a -> pathlist .strings = opts -> pathspec .strings ;
1275
+ a -> pathlist .count = opts -> pathspec .count ;
1276
+ b -> pathlist .strings = opts -> pathspec .strings ;
1277
+ b -> pathlist .count = opts -> pathspec .count ;
1278
+ } else if (opts ) {
1279
+ * prefix = git_pathspec_prefix (& opts -> pathspec );
1280
+ }
1281
+
1282
+ a -> flags = aflags ;
1283
+ b -> flags = bflags ;
1284
+ a -> start = b -> start = * prefix ;
1285
+ a -> end = b -> end = * prefix ;
1286
+
1287
+ return 0 ;
1288
+ }
1288
1289
1289
1290
int git_diff_tree_to_tree (
1290
1291
git_diff * * out ,
@@ -1293,8 +1294,12 @@ int git_diff_tree_to_tree(
1293
1294
git_tree * new_tree ,
1294
1295
const git_diff_options * opts )
1295
1296
{
1296
- git_diff * diff = NULL ;
1297
1297
git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE ;
1298
+ git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT ,
1299
+ b_opts = GIT_ITERATOR_OPTIONS_INIT ;
1300
+ git_iterator * a = NULL , * b = NULL ;
1301
+ git_diff * diff = NULL ;
1302
+ char * prefix = NULL ;
1298
1303
int error = 0 ;
1299
1304
1300
1305
assert (out && repo );
@@ -1308,13 +1313,19 @@ int git_diff_tree_to_tree(
1308
1313
if (opts && (opts -> flags & GIT_DIFF_IGNORE_CASE ) != 0 )
1309
1314
iflag = GIT_ITERATOR_IGNORE_CASE ;
1310
1315
1311
- DIFF_FROM_ITERATORS (
1312
- git_iterator_for_tree (& a , old_tree , & a_opts ), iflag ,
1313
- git_iterator_for_tree (& b , new_tree , & b_opts ), iflag
1314
- );
1316
+ if ((error = diff_prepare_iterator_opts (& prefix , & a_opts , iflag , & b_opts , iflag , opts )) < 0 ||
1317
+ (error = git_iterator_for_tree (& a , old_tree , & a_opts )) < 0 ||
1318
+ (error = git_iterator_for_tree (& b , new_tree , & b_opts )) < 0 ||
1319
+ (error = git_diff__from_iterators (& diff , repo , a , b , opts )) < 0 )
1320
+ goto out ;
1315
1321
1316
- if (!error )
1317
- * out = diff ;
1322
+ * out = diff ;
1323
+ diff = NULL ;
1324
+ out :
1325
+ git_iterator_free (a );
1326
+ git_iterator_free (b );
1327
+ git_diff_free (diff );
1328
+ git__free (prefix );
1318
1329
1319
1330
return error ;
1320
1331
}
@@ -1337,9 +1348,13 @@ int git_diff_tree_to_index(
1337
1348
git_index * index ,
1338
1349
const git_diff_options * opts )
1339
1350
{
1340
- git_diff * diff = NULL ;
1341
1351
git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE |
1342
1352
GIT_ITERATOR_INCLUDE_CONFLICTS ;
1353
+ git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT ,
1354
+ b_opts = GIT_ITERATOR_OPTIONS_INIT ;
1355
+ git_iterator * a = NULL , * b = NULL ;
1356
+ git_diff * diff = NULL ;
1357
+ char * prefix = NULL ;
1343
1358
bool index_ignore_case = false;
1344
1359
int error = 0 ;
1345
1360
@@ -1352,17 +1367,23 @@ int git_diff_tree_to_index(
1352
1367
1353
1368
index_ignore_case = index -> ignore_case ;
1354
1369
1355
- DIFF_FROM_ITERATORS (
1356
- git_iterator_for_tree (& a , old_tree , & a_opts ), iflag ,
1357
- git_iterator_for_index (& b , repo , index , & b_opts ), iflag
1358
- );
1370
+ if ((error = diff_prepare_iterator_opts (& prefix , & a_opts , iflag , & b_opts , iflag , opts )) < 0 ||
1371
+ (error = git_iterator_for_tree (& a , old_tree , & a_opts )) < 0 ||
1372
+ (error = git_iterator_for_index (& b , repo , index , & b_opts )) < 0 ||
1373
+ (error = git_diff__from_iterators (& diff , repo , a , b , opts ) < 0 ))
1374
+ goto out ;
1359
1375
1360
1376
/* if index is in case-insensitive order, re-sort deltas to match */
1361
- if (! error && index_ignore_case )
1377
+ if (index_ignore_case )
1362
1378
git_diff__set_ignore_case (diff , true);
1363
1379
1364
- if (!error )
1365
- * out = diff ;
1380
+ * out = diff ;
1381
+ diff = NULL ;
1382
+ out :
1383
+ git_iterator_free (a );
1384
+ git_iterator_free (b );
1385
+ git_diff_free (diff );
1386
+ git__free (prefix );
1366
1387
1367
1388
return error ;
1368
1389
}
@@ -1373,7 +1394,11 @@ int git_diff_index_to_workdir(
1373
1394
git_index * index ,
1374
1395
const git_diff_options * opts )
1375
1396
{
1397
+ git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT ,
1398
+ b_opts = GIT_ITERATOR_OPTIONS_INIT ;
1399
+ git_iterator * a = NULL , * b = NULL ;
1376
1400
git_diff * diff = NULL ;
1401
+ char * prefix = NULL ;
1377
1402
int error = 0 ;
1378
1403
1379
1404
assert (out && repo );
@@ -1383,20 +1408,24 @@ int git_diff_index_to_workdir(
1383
1408
if (!index && (error = diff_load_index (& index , repo )) < 0 )
1384
1409
return error ;
1385
1410
1386
- DIFF_FROM_ITERATORS (
1387
- git_iterator_for_index (& a , repo , index , & a_opts ),
1388
- GIT_ITERATOR_INCLUDE_CONFLICTS ,
1389
-
1390
- git_iterator_for_workdir (& b , repo , index , NULL , & b_opts ),
1391
- GIT_ITERATOR_DONT_AUTOEXPAND
1392
- );
1393
-
1394
- if (!error && (diff -> opts .flags & GIT_DIFF_UPDATE_INDEX ) != 0 &&
1395
- ((git_diff_generated * )diff )-> index_updated )
1396
- error = git_index_write (index );
1397
-
1398
- if (!error )
1399
- * out = diff ;
1411
+ if ((error = diff_prepare_iterator_opts (& prefix , & a_opts , GIT_ITERATOR_INCLUDE_CONFLICTS ,
1412
+ & b_opts , GIT_ITERATOR_DONT_AUTOEXPAND , opts )) < 0 ||
1413
+ (error = git_iterator_for_index (& a , repo , index , & a_opts )) < 0 ||
1414
+ (error = git_iterator_for_workdir (& b , repo , index , NULL , & b_opts )) < 0 ||
1415
+ (error = git_diff__from_iterators (& diff , repo , a , b , opts )) < 0 )
1416
+ goto out ;
1417
+
1418
+ if ((diff -> opts .flags & GIT_DIFF_UPDATE_INDEX ) && ((git_diff_generated * )diff )-> index_updated )
1419
+ if ((error = git_index_write (index )) < 0 )
1420
+ goto out ;
1421
+
1422
+ * out = diff ;
1423
+ diff = NULL ;
1424
+ out :
1425
+ git_iterator_free (a );
1426
+ git_iterator_free (b );
1427
+ git_diff_free (diff );
1428
+ git__free (prefix );
1400
1429
1401
1430
return error ;
1402
1431
}
@@ -1407,24 +1436,33 @@ int git_diff_tree_to_workdir(
1407
1436
git_tree * old_tree ,
1408
1437
const git_diff_options * opts )
1409
1438
{
1439
+ git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT ,
1440
+ b_opts = GIT_ITERATOR_OPTIONS_INIT ;
1441
+ git_iterator * a = NULL , * b = NULL ;
1410
1442
git_diff * diff = NULL ;
1443
+ char * prefix = NULL ;
1411
1444
git_index * index ;
1412
- int error = 0 ;
1445
+ int error ;
1413
1446
1414
1447
assert (out && repo );
1415
1448
1416
1449
* out = NULL ;
1417
1450
1418
- if ((error = git_repository_index__weakptr (& index , repo )))
1419
- return error ;
1420
-
1421
- DIFF_FROM_ITERATORS (
1422
- git_iterator_for_tree (& a , old_tree , & a_opts ), 0 ,
1423
- git_iterator_for_workdir (& b , repo , index , old_tree , & b_opts ), GIT_ITERATOR_DONT_AUTOEXPAND
1424
- );
1425
-
1426
- if (!error )
1427
- * out = diff ;
1451
+ if ((error = diff_prepare_iterator_opts (& prefix , & a_opts , 0 ,
1452
+ & b_opts , GIT_ITERATOR_DONT_AUTOEXPAND , opts ) < 0 ) ||
1453
+ (error = git_repository_index__weakptr (& index , repo )) < 0 ||
1454
+ (error = git_iterator_for_tree (& a , old_tree , & a_opts )) < 0 ||
1455
+ (error = git_iterator_for_workdir (& b , repo , index , old_tree , & b_opts )) < 0 ||
1456
+ (error = git_diff__from_iterators (& diff , repo , a , b , opts )) < 0 )
1457
+ goto out ;
1458
+
1459
+ * out = diff ;
1460
+ diff = NULL ;
1461
+ out :
1462
+ git_iterator_free (a );
1463
+ git_iterator_free (b );
1464
+ git_diff_free (diff );
1465
+ git__free (prefix );
1428
1466
1429
1467
return error ;
1430
1468
}
@@ -1468,24 +1506,35 @@ int git_diff_index_to_index(
1468
1506
git_index * new_index ,
1469
1507
const git_diff_options * opts )
1470
1508
{
1471
- git_diff * diff ;
1472
- int error = 0 ;
1509
+ git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT ,
1510
+ b_opts = GIT_ITERATOR_OPTIONS_INIT ;
1511
+ git_iterator * a = NULL , * b = NULL ;
1512
+ git_diff * diff = NULL ;
1513
+ char * prefix = NULL ;
1514
+ int error ;
1473
1515
1474
1516
assert (out && old_index && new_index );
1475
1517
1476
1518
* out = NULL ;
1477
1519
1478
- DIFF_FROM_ITERATORS (
1479
- git_iterator_for_index (& a , repo , old_index , & a_opts ), GIT_ITERATOR_DONT_IGNORE_CASE ,
1480
- git_iterator_for_index (& b , repo , new_index , & b_opts ), GIT_ITERATOR_DONT_IGNORE_CASE
1481
- );
1520
+ if ((error = diff_prepare_iterator_opts (& prefix , & a_opts , GIT_ITERATOR_DONT_IGNORE_CASE ,
1521
+ & b_opts , GIT_ITERATOR_DONT_IGNORE_CASE , opts ) < 0 ) ||
1522
+ (error = git_iterator_for_index (& a , repo , old_index , & a_opts )) < 0 ||
1523
+ (error = git_iterator_for_index (& b , repo , new_index , & b_opts )) < 0 ||
1524
+ (error = git_diff__from_iterators (& diff , repo , a , b , opts )) < 0 )
1525
+ goto out ;
1482
1526
1483
1527
/* if index is in case-insensitive order, re-sort deltas to match */
1484
- if (! error && ( old_index -> ignore_case || new_index -> ignore_case ) )
1528
+ if (old_index -> ignore_case || new_index -> ignore_case )
1485
1529
git_diff__set_ignore_case (diff , true);
1486
1530
1487
- if (!error )
1488
- * out = diff ;
1531
+ * out = diff ;
1532
+ diff = NULL ;
1533
+ out :
1534
+ git_iterator_free (a );
1535
+ git_iterator_free (b );
1536
+ git_diff_free (diff );
1537
+ git__free (prefix );
1489
1538
1490
1539
return error ;
1491
1540
}
0 commit comments