Commit f8a36e221091eb68b439ebe4eb07a5d03b335c28

Stefan Sperling 2021-08-26T12:30:42

add 'got send' command for sending changes to remote repositories Known to work against git-daemon and github Git server implementations. Tests by abieber, naddy, jrick, and myself. Man page additions reviewed by Lucas.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
diff --git a/got/Makefile b/got/Makefile
index 29c7f19..14f769b 100644
--- a/got/Makefile
+++ b/got/Makefile
@@ -12,7 +12,7 @@ SRCS=		got.c blame.c commit_graph.c delta.c diff.c \
 		gotconfig.c diff_main.c diff_atomize_text.c \
 		diff_myers.c diff_output.c diff_output_plain.c \
 		diff_output_unidiff.c diff_output_edscript.c \
-		diff_patience.c
+		diff_patience.c send.c deltify.c pack_create.c
 
 MAN =		${PROG}.1 got-worktree.5 git-repository.5 got.conf.5
 
diff --git a/got/got.1 b/got/got.1
index 2fc7881..6ccabbd 100644
--- a/got/got.1
+++ b/got/got.1
@@ -1372,6 +1372,185 @@ in the repository.
 .It Cm ci
 Short alias for
 .Cm commit .
+.It Cm send Oo Fl a Oc Oo Fl b Ar branch Oc Oo Fl d Ar branch Oc Oo Fl f Oc Oo Fl r Ar repository-path Oc Oo Fl t Ar tag Oc Oo Fl T Oc Oo Fl q Oc Oo Fl v Oc Op Ar remote-repository
+Send new changes to a remote repository.
+If no
+.Ar remote-repository
+is specified,
+.Dq origin
+will be used.
+The remote repository's URL is obtained from the corresponding entry in
+.Xr got.conf 5
+or Git's
+.Pa config
+file of the local repository, as created by
+.Cm got clone .
+.Pp
+All objects corresponding to new changes will be written to a temporary
+pack file which is then uploaded to the server.
+Upon success, references in the
+.Dq refs/remotes/
+reference namespace of the local repository will be updated to point at
+the commits which have been sent.
+.Pp
+By default, changes will only be sent if they are based on up-to-date
+copies of relevant branches in the remote repository.
+If any changes to be sent are based on out-of-date copies, new changes
+must be fetched from the server with
+.Cm got fetch
+and local branches must be rebased with
+.Cm got rebase
+before
+.Cm got send
+can succeed.
+.Pp
+The options for
+.Cm got send
+are as follows:
+.Bl -tag -width Ds
+.It Fl a
+Send all branches from the local repository's
+.Dq refs/heads/
+reference namespace.
+The
+.Fl a
+option is equivalent to listing all branches with multiple
+.Fl b
+options.
+Cannot be used together with the
+.Fl b
+option.
+.It Fl b Ar branch
+Send the specified
+.Ar branch
+from the local repository's
+.Dq refs/heads/
+reference namespace.
+This option may be specified multiple times to build a list of branches
+to send.
+If this option is not specified, default to the work tree's current branch
+if invoked in a work tree, or to the repository's HEAD reference.
+Cannot be used together with the
+.Fl a
+option.
+.It Fl d Ar branch
+Delete the specified
+.Ar branch
+from the remote repository's
+.Dq refs/heads/
+reference namespace.
+This option may be specified multiple times to build a list of branches
+to delete.
+.Pp
+Only references are deleted.
+Any commit, tree, tag, and blob objects belonging to deleted branches
+may become subject to deletion by Git's garbage collector running on
+the server.
+.Pp
+Requesting deletion of branches results in an error if the server
+does not support this feature.
+.It Fl f
+Attempt to force the server to accept uploaded branches or tags in
+spite of failing client-side sanity checks.
+The server may reject forced requests regardless, depending on its
+configuration.
+.Pp
+Any commit, tree, tag, and blob objects belonging to overwritten branches
+or tags may become subject to deletion by Git's garbage collector running
+on the server.
+.Pp
+The
+.Dq refs/tags
+reference namespace is globally shared between all repositories.
+Use of the
+.Fl f
+option to overwrite tags is discouraged because it can lead to
+inconsistencies between the tags present in different repositories.
+In general, creating a new tag with a different name is recommended
+instead of overwriting an existing tag.
+.Pp
+Use of the
+.Fl f
+option is particularly discouraged if changes being sent are based
+on an out-of-date copy of a branch in the remote repository.
+Instead of using the
+.Fl f
+option, new changes should
+be fetched with
+.Cm got fetch
+and local branches should be rebased with
+.Cm got rebase ,
+followed by another attempt to send the changes.
+.Pp
+The
+.Fl f
+option should only be needed in situations where the remote repository's
+copy of a branch or tag is known to be out-of-date and is considered
+disposable.
+The risks of creating inconsistencies between different repositories
+should also be taken into account.
+.It Fl r Ar repository-path
+Use the repository at the specified path.
+If not specified, assume the repository is located at or above the current
+working directory.
+If this directory is a
+.Nm
+work tree, use the repository path associated with this work tree.
+.It Fl t Ar tag
+Send the specified
+.Ar tag
+from the local repository's
+.Dq refs/tags/
+reference namespace, in addition to any branches that are being sent.
+The
+.Fl t
+option may be specified multiple times to build a list of tags to send.
+No tags will be sent if the
+.Fl t
+option is not used.
+.Pp
+Raise an error if the specified
+.Ar tag
+already exists in the remote repository, unless the
+.Fl f
+option is used to overwrite the sever's copy of the tag.
+In general, creating a new tag with a different name is recommended
+instead of overwriting an existing tag.
+.Pp
+Cannot be used together with the
+.Fl T
+option.
+.It Fl T
+Attempt to send all tags from the local repository's
+.Dq refs/tags/
+reference namespace.
+The
+.Fl T
+option is equivalent to listing all tags with multiple
+.Fl t
+options.
+Cannot be used together with the
+.Fl t
+option.
+.It Fl q
+Suppress progress reporting output.
+The same option will be passed to
+.Xr ssh 1
+if applicable.
+.It Fl v
+Verbose mode.
+Causes
+.Cm got send
+to print debugging messages to standard error output.
+The same option will be passed to
+.Xr ssh 1
+if applicable.
+Multiple -v options increase the verbosity.
+The maximum is 3.
+.El
+.It Cm se
+Short alias for
+.Cm send .
 .It Cm cherrypick Ar commit
 Merge changes from a single
 .Ar commit
diff --git a/got/got.c b/got/got.c
index 71cc16e..5f57676 100644
--- a/got/got.c
+++ b/got/got.c
@@ -51,6 +51,7 @@
 #include "got_diff.h"
 #include "got_commit_graph.h"
 #include "got_fetch.h"
+#include "got_send.h"
 #include "got_blame.h"
 #include "got_privsep.h"
 #include "got_opentemp.h"
@@ -102,6 +103,7 @@ __dead static void	usage_add(void);
 __dead static void	usage_remove(void);
 __dead static void	usage_revert(void);
 __dead static void	usage_commit(void);
+__dead static void	usage_send(void);
 __dead static void	usage_cherrypick(void);
 __dead static void	usage_backout(void);
 __dead static void	usage_rebase(void);
@@ -130,6 +132,7 @@ static const struct got_error*		cmd_add(int, char *[]);
 static const struct got_error*		cmd_remove(int, char *[]);
 static const struct got_error*		cmd_revert(int, char *[]);
 static const struct got_error*		cmd_commit(int, char *[]);
+static const struct got_error*		cmd_send(int, char *[]);
 static const struct got_error*		cmd_cherrypick(int, char *[]);
 static const struct got_error*		cmd_backout(int, char *[]);
 static const struct got_error*		cmd_rebase(int, char *[]);
@@ -159,6 +162,7 @@ static struct got_cmd got_commands[] = {
 	{ "remove",	cmd_remove,	usage_remove,	"rm" },
 	{ "revert",	cmd_revert,	usage_revert,	"rv" },
 	{ "commit",	cmd_commit,	usage_commit,	"ci" },
+	{ "send",	cmd_send,	usage_send,	"se" },
 	{ "cherrypick",	cmd_cherrypick,	usage_cherrypick, "cy" },
 	{ "backout",	cmd_backout,	usage_backout,	"bo" },
 	{ "rebase",	cmd_rebase,	usage_rebase,	"rb" },
@@ -7353,6 +7357,498 @@ done:
 }
 
 __dead static void
+usage_send(void)
+{
+	fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
+	    "[-r repository-path] [-t tag] [-T] [-q] [-v] "
+	    "[remote-repository]\n", getprogname());
+	exit(1);
+}
+
+struct got_send_progress_arg {
+	char last_scaled_packsize[FMT_SCALED_STRSIZE];
+	int verbosity;
+	int last_ncommits;
+	int last_nobj_total;
+	int last_p_deltify;
+	int last_p_written;
+	int last_p_sent;
+	int printed_something;
+	int sent_something;
+	struct got_pathlist_head *delete_branches;
+};
+
+static const struct got_error *
+send_progress(void *arg, off_t packfile_size, int ncommits, int nobj_total,
+    int nobj_deltify, int nobj_written, off_t bytes_sent, const char *refname,
+    int success)
+{
+	struct got_send_progress_arg *a = arg;
+	char scaled_packsize[FMT_SCALED_STRSIZE];
+	char scaled_sent[FMT_SCALED_STRSIZE];
+	int p_deltify = 0, p_written = 0, p_sent = 0;
+	int print_searching = 0, print_total = 0;
+	int print_deltify = 0, print_written = 0, print_sent = 0;
+
+	if (a->verbosity < 0)
+		return NULL;
+
+	if (refname) {
+		const char *status = success ? "accepted" : "rejected";
+
+		if (success) {
+			struct got_pathlist_entry *pe;
+			TAILQ_FOREACH(pe, a->delete_branches, entry) {
+				const char *branchname = pe->path;
+				if (got_path_cmp(branchname, refname,
+				    strlen(branchname), strlen(refname)) == 0) {
+					status = "deleted";
+					break;
+				}
+			}
+		}
+
+		printf("\nServer has %s %s", status, refname);
+		a->printed_something = 1;
+		return NULL;
+	}
+
+	if (fmt_scaled(packfile_size, scaled_packsize) == -1)
+		return got_error_from_errno("fmt_scaled");
+	if (fmt_scaled(bytes_sent, scaled_sent) == -1)
+		return got_error_from_errno("fmt_scaled");
+
+	if (a->last_ncommits != ncommits) {
+		print_searching = 1;
+		a->last_ncommits = ncommits;
+	}
+
+	if (a->last_nobj_total != nobj_total) {
+		print_searching = 1;
+		print_total = 1;
+		a->last_nobj_total = nobj_total;
+	}
+
+	if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
+	    strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
+		if (strlcpy(a->last_scaled_packsize, scaled_packsize,
+		    FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
+			return got_error(GOT_ERR_NO_SPACE);
+	}
+
+	if (nobj_deltify > 0 || nobj_written > 0) {
+		if (nobj_deltify > 0) {
+			p_deltify = (nobj_deltify * 100) / nobj_total;
+			if (p_deltify != a->last_p_deltify) {
+				a->last_p_deltify = p_deltify;
+				print_searching = 1;
+				print_total = 1;
+				print_deltify = 1;
+			}
+		}
+		if (nobj_written > 0) {
+			p_written = (nobj_written * 100) / nobj_total;
+			if (p_written != a->last_p_written) {
+				a->last_p_written = p_written;
+				print_searching = 1;
+				print_total = 1;
+				print_deltify = 1;
+				print_written = 1;
+			}
+		}
+	}
+
+	if (bytes_sent > 0) {
+		p_sent = (bytes_sent * 100) / packfile_size;
+		if (p_sent != a->last_p_sent) {
+			a->last_p_sent = p_sent;
+			print_searching = 1;
+			print_total = 1;
+			print_deltify = 1;
+			print_written = 1;
+			print_sent = 1;
+		}
+		a->sent_something = 1;
+	}
+
+	if (print_searching || print_total || print_deltify || print_written ||
+	    print_sent)
+		printf("\r");
+	if (print_searching)
+		printf("packing %d reference%s", ncommits,
+		    ncommits == 1 ? "" : "s");
+	if (print_total)
+		printf("; %d object%s", nobj_total,
+		    nobj_total == 1 ? "" : "s");
+	if (print_deltify)
+		printf("; deltify: %d%%", p_deltify);
+	if (print_sent)
+		printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE,
+		    scaled_packsize, p_sent);
+	else if (print_written)
+		printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
+		    scaled_packsize, p_written);
+	if (print_searching || print_total || print_deltify ||
+	    print_written || print_sent) {
+		a->printed_something = 1;
+		fflush(stdout);
+	}
+	return NULL;
+}
+
+static const struct got_error *
+cmd_send(int argc, char *argv[])
+{
+	const struct got_error *error = NULL;
+	char *cwd = NULL, *repo_path = NULL;
+	const char *remote_name;
+	char *proto = NULL, *host = NULL, *port = NULL;
+	char *repo_name = NULL, *server_path = NULL;
+	const struct got_remote_repo *remotes, *remote = NULL;
+	int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
+	struct got_repository *repo = NULL;
+	struct got_worktree *worktree = NULL;
+	const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
+	struct got_pathlist_head branches;
+	struct got_pathlist_head tags;
+	struct got_reflist_head all_branches;
+	struct got_reflist_head all_tags;
+	struct got_pathlist_head delete_args;
+	struct got_pathlist_head delete_branches;
+	struct got_reflist_entry *re;
+	struct got_pathlist_entry *pe;
+	int i, ch, sendfd = -1, sendstatus;
+	pid_t sendpid = -1;
+	struct got_send_progress_arg spa;
+	int verbosity = 0, overwrite_refs = 0;
+	int send_all_branches = 0, send_all_tags = 0;
+	struct got_reference *ref = NULL;
+
+	TAILQ_INIT(&branches);
+	TAILQ_INIT(&tags);
+	TAILQ_INIT(&all_branches);
+	TAILQ_INIT(&all_tags);
+	TAILQ_INIT(&delete_args);
+	TAILQ_INIT(&delete_branches);
+
+	while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
+		switch (ch) {
+		case 'a':
+			send_all_branches = 1;
+			break;
+		case 'b':
+			error = got_pathlist_append(&branches, optarg, NULL);
+			if (error)
+				return error;
+			nbranches++;
+			break;
+		case 'd':
+			error = got_pathlist_append(&delete_args, optarg, NULL);
+			if (error)
+				return error;
+			break;
+		case 'f':
+			overwrite_refs = 1;
+			break;
+		case 'r':
+			repo_path = realpath(optarg, NULL);
+			if (repo_path == NULL)
+				return got_error_from_errno2("realpath",
+				    optarg);
+			got_path_strip_trailing_slashes(repo_path);
+			break;
+		case 't':
+			error = got_pathlist_append(&tags, optarg, NULL);
+			if (error)
+				return error;
+			ntags++;
+			break;
+		case 'T':
+			send_all_tags = 1;
+			break;
+		case 'v':
+			if (verbosity < 0)
+				verbosity = 0;
+			else if (verbosity < 3)
+				verbosity++;
+			break;
+		case 'q':
+			verbosity = -1;
+			break;
+		default:
+			usage_send();
+			/* NOTREACHED */
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
+	if (send_all_branches && !TAILQ_EMPTY(&branches))
+		option_conflict('a', 'b');
+	if (send_all_tags && !TAILQ_EMPTY(&tags))
+		option_conflict('T', 't');
+
+
+	if (argc == 0)
+		remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
+	else if (argc == 1)
+		remote_name = argv[0];
+	else
+		usage_send();
+
+	cwd = getcwd(NULL, 0);
+	if (cwd == NULL) {
+		error = got_error_from_errno("getcwd");
+		goto done;
+	}
+
+	if (repo_path == NULL) {
+		error = got_worktree_open(&worktree, cwd);
+		if (error && error->code != GOT_ERR_NOT_WORKTREE)
+			goto done;
+		else
+			error = NULL;
+		if (worktree) {
+			repo_path =
+			    strdup(got_worktree_get_repo_path(worktree));
+			if (repo_path == NULL)
+				error = got_error_from_errno("strdup");
+			if (error)
+				goto done;
+		} else {
+			repo_path = strdup(cwd);
+			if (repo_path == NULL) {
+				error = got_error_from_errno("strdup");
+				goto done;
+			}
+		}
+	}
+
+	error = got_repo_open(&repo, repo_path, NULL);
+	if (error)
+		goto done;
+
+	if (worktree) {
+		worktree_conf = got_worktree_get_gotconfig(worktree);
+		if (worktree_conf) {
+			got_gotconfig_get_remotes(&nremotes, &remotes,
+			    worktree_conf);
+			for (i = 0; i < nremotes; i++) {
+				if (strcmp(remotes[i].name, remote_name) == 0) {
+					remote = &remotes[i];
+					break;
+				}
+			}
+		}
+	}
+	if (remote == NULL) {
+		repo_conf = got_repo_get_gotconfig(repo);
+		if (repo_conf) {
+			got_gotconfig_get_remotes(&nremotes, &remotes,
+			    repo_conf);
+			for (i = 0; i < nremotes; i++) {
+				if (strcmp(remotes[i].name, remote_name) == 0) {
+					remote = &remotes[i];
+					break;
+				}
+			}
+		}
+	}
+	if (remote == NULL) {
+		got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
+		for (i = 0; i < nremotes; i++) {
+			if (strcmp(remotes[i].name, remote_name) == 0) {
+				remote = &remotes[i];
+				break;
+			}
+		}
+	}
+	if (remote == NULL) {
+		error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
+		goto done;
+	}
+
+	error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
+	    &repo_name, remote->url);
+	if (error)
+		goto done;
+
+	if (strcmp(proto, "git") == 0) {
+#ifndef PROFILE
+		if (pledge("stdio rpath wpath cpath fattr flock proc exec "
+		    "sendfd dns inet unveil", NULL) == -1)
+			err(1, "pledge");
+#endif
+	} else if (strcmp(proto, "git+ssh") == 0 ||
+	    strcmp(proto, "ssh") == 0) {
+#ifndef PROFILE
+		if (pledge("stdio rpath wpath cpath fattr flock proc exec "
+		    "sendfd unveil", NULL) == -1)
+			err(1, "pledge");
+#endif
+	} else if (strcmp(proto, "http") == 0 ||
+	    strcmp(proto, "git+http") == 0) {
+		error = got_error_path(proto, GOT_ERR_NOT_IMPL);
+		goto done;
+	} else {
+		error = got_error_path(proto, GOT_ERR_BAD_PROTO);
+		goto done;
+	}
+
+	if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
+		if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
+			error = got_error_from_errno2("unveil",
+			    GOT_FETCH_PATH_SSH);
+			goto done;
+		}
+	}
+	error = apply_unveil(got_repo_get_path(repo), 0, NULL);
+	if (error)
+		goto done;
+
+	if (send_all_branches) {
+		error = got_ref_list(&all_branches, repo, "refs/heads",
+		    got_ref_cmp_by_name, NULL);
+		if (error)
+			goto done;
+		TAILQ_FOREACH(re, &all_branches, entry) {
+			const char *branchname = got_ref_get_name(re->ref);
+			error = got_pathlist_append(&branches,
+			    branchname, NULL);
+			if (error)
+				goto done;
+			nbranches++;
+		}
+	}
+
+	if (send_all_tags) {
+		error = got_ref_list(&all_tags, repo, "refs/tags",
+		    got_ref_cmp_by_name, NULL);
+		if (error)
+			goto done;
+		TAILQ_FOREACH(re, &all_tags, entry) {
+			const char *tagname = got_ref_get_name(re->ref);
+			error = got_pathlist_append(&tags,
+			    tagname, NULL);
+			if (error)
+				goto done;
+			ntags++;
+		}
+	}
+
+	/*
+	 * To prevent accidents only branches in refs/heads/ can be deleted
+	 * with 'got send -d'.
+	 * Deleting anything else requires local repository access or Git.
+	 */
+	TAILQ_FOREACH(pe, &delete_args, entry) {
+		const char *branchname = pe->path;
+		char *s;
+		struct got_pathlist_entry *new;
+		if (strncmp(branchname, "refs/heads/", 11) == 0) {
+			s = strdup(branchname);
+			if (s == NULL) {
+				error = got_error_from_errno("strdup");
+				goto done;
+			}
+		} else {
+			if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
+				error = got_error_from_errno("asprintf");
+				goto done;
+			}
+		}
+		error = got_pathlist_insert(&new, &delete_branches, s, NULL);
+		if (error || new == NULL /* duplicate */)
+			free(s);
+		if (error)
+			goto done;
+		ndelete_branches++;
+	}
+
+	if (nbranches == 0 && ndelete_branches == 0) {
+		struct got_reference *head_ref;
+		if (worktree)
+			error = got_ref_open(&head_ref, repo,
+			    got_worktree_get_head_ref_name(worktree), 0);
+		else
+			error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
+		if (error)
+			goto done;
+		if (got_ref_is_symbolic(head_ref)) {
+			error = got_ref_resolve_symbolic(&ref, repo, head_ref);
+			got_ref_close(head_ref);
+			if (error)
+				goto done;
+		} else
+			ref = head_ref;
+		error = got_pathlist_append(&branches, got_ref_get_name(ref),
+		   NULL);
+		if (error)
+			goto done;
+		nbranches++;
+	}
+
+	if (verbosity >= 0)
+		printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
+		    port ? ":" : "", port ? port : "");
+
+	error = got_send_connect(&sendpid, &sendfd, proto, host, port,
+	    server_path, verbosity);
+	if (error)
+		goto done;
+
+	memset(&spa, 0, sizeof(spa));
+	spa.last_scaled_packsize[0] = '\0';
+	spa.last_p_deltify = -1;
+	spa.last_p_written = -1;
+	spa.verbosity = verbosity;
+	spa.delete_branches = &delete_branches;
+	error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
+	    verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
+	    check_cancelled, NULL);
+	if (spa.printed_something)
+		putchar('\n');
+	if (error)
+		goto done;
+	if (!spa.sent_something && verbosity >= 0)
+		printf("Already up-to-date\n");
+done:
+	if (sendpid > 0) {
+		if (kill(sendpid, SIGTERM) == -1)
+			error = got_error_from_errno("kill");
+		if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
+			error = got_error_from_errno("waitpid");
+	}
+	if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
+		error = got_error_from_errno("close");
+	if (repo) {
+		const struct got_error *close_err = got_repo_close(repo);
+		if (error == NULL)
+			error = close_err;
+	}
+	if (worktree)
+		got_worktree_close(worktree);
+	if (ref)
+		got_ref_close(ref);
+	got_pathlist_free(&branches);
+	got_pathlist_free(&tags);
+	got_ref_list_free(&all_branches);
+	got_ref_list_free(&all_tags);
+	got_pathlist_free(&delete_args);
+	TAILQ_FOREACH(pe, &delete_branches, entry)
+		free((char *)pe->path);
+	got_pathlist_free(&delete_branches);
+	free(cwd);
+	free(repo_path);
+	free(proto);
+	free(host);
+	free(port);
+	free(server_path);
+	free(repo_name);
+	return error;
+}
+
+__dead static void
 usage_cherrypick(void)
 {
 	fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
diff --git a/include/got_error.h b/include/got_error.h
index eab64a8..5eb5cc5 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -148,6 +148,13 @@
 #define GOT_ERR_CANNOT_PACK	131
 #define GOT_ERR_LONELY_PACKIDX	132
 #define GOT_ERR_OBJ_CSUM	133
+#define GOT_ERR_SEND_BAD_REF	134
+#define GOT_ERR_SEND_FAILED	135
+#define GOT_ERR_SEND_EMPTY	136
+#define GOT_ERR_SEND_ANCESTRY	137
+#define GOT_ERR_CAPA_DELETE_REFS 138
+#define GOT_ERR_SEND_DELETE_REF	139
+#define GOT_ERR_SEND_TAG_EXISTS	140
 
 static const struct got_error {
 	int code;
@@ -304,6 +311,13 @@ static const struct got_error {
 	{ GOT_ERR_LONELY_PACKIDX, "pack index has no corresponding pack file; "
 	    "pack file must be restored or 'gotadmin cleanup -p' must be run" },
 	{ GOT_ERR_OBJ_CSUM, "bad object checksum" },
+	{ GOT_ERR_SEND_BAD_REF, "reference cannot be sent" },
+	{ GOT_ERR_SEND_FAILED, "could not send pack file" },
+	{ GOT_ERR_SEND_EMPTY, "no references to send" },
+	{ GOT_ERR_SEND_ANCESTRY, "fetch and rebase required" },
+	{ GOT_ERR_CAPA_DELETE_REFS, "server cannot delete references" },
+	{ GOT_ERR_SEND_DELETE_REF, "reference cannot be deleted" },
+	{ GOT_ERR_SEND_TAG_EXISTS, "tag already exists on server" },
 };
 
 /*
diff --git a/include/got_send.h b/include/got_send.h
new file mode 100644
index 0000000..2f0388e
--- /dev/null
+++ b/include/got_send.h
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
+ * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/* IANA assigned */
+#define GOT_DEFAULT_GIT_PORT		9418
+#define GOT_DEFAULT_GIT_PORT_STR	"9418"
+
+#ifndef GOT_SEND_PATH_SSH
+#define GOT_SEND_PATH_SSH	"/usr/bin/ssh"
+#endif
+
+#define GOT_SEND_DEFAULT_REMOTE_NAME	"origin"
+
+#define GOT_SEND_PKTMAX	65536
+
+/*
+ * Attempt to open a connection to a server using the provided protocol
+ * scheme, hostname port number (as a string) and server-side path.
+ * A verbosity level can be specified; it currently controls the amount
+ * of -v options passed to ssh(1). If the level is -1 ssh(1) will be run
+ * with the -q option.
+ *
+ * If successful return an open file descriptor for the connection which can
+ * be passed to other functions below, and must be disposed of with close(2).
+ *
+ * If an ssh(1) process was started return its PID as well, in which case
+ * the caller should eventually send SIGTERM to the procress and wait for
+ * the process to exit with waitpid(2). Otherwise, return PID -1.
+ */
+const struct got_error *got_send_connect(pid_t *, int *, const char *,
+    const char *, const char *, const char *, int);
+
+/* A callback function which gets invoked with progress information to print. */
+typedef const struct got_error *(*got_send_progress_cb)(void *,
+    off_t packfile_size, int ncommits, int nobj_total,
+    int nobj_deltify, int nobj_written, off_t bytes_sent,
+    const char *refname, int success);
+
+/*
+ * Attempt to generate a pack file and sent it to a server.
+ * This pack file will contain objects which are reachable in the local
+ * repository via the specified branches and tags. Any objects which are
+ * already present in the remote repository will be omitted from the
+ * pack file.
+ *
+ * If the server supports deletion of references, attempt to delete
+ * branches on the specified delete_branches list from the server.
+ * Such branches are not required to exist in the local repository.
+ * Requesting deletion of branches results in an error if the server
+ * does not support this feature.
+ */
+const struct got_error *got_send_pack(const char *remote_name,
+    struct got_pathlist_head *branch_names,
+    struct got_pathlist_head *tag_names,
+    struct got_pathlist_head *delete_branches, int verbosity,
+    int overwrite_refs, int sendfd, struct got_repository *repo,
+    got_send_progress_cb progress_cb, void *progress_arg,
+    got_cancel_cb cancel_cb, void *cancel_arg);
diff --git a/lib/got_lib_pack_create.h b/lib/got_lib_pack_create.h
index 180adae..8a84ff3 100644
--- a/lib/got_lib_pack_create.h
+++ b/lib/got_lib_pack_create.h
@@ -24,6 +24,6 @@
 const struct got_error *got_pack_create(uint8_t *pack_sha1, FILE *packfile,
     struct got_object_id **theirs, int ntheirs,
     struct got_object_id **ours, int nours,
-    struct got_repository *repo, int loose_obj_only,
+    struct got_repository *repo, int loose_obj_only, int allow_empty,
     got_pack_progress_cb progress_cb, void *progress_arg,
     got_cancel_cb cancel_cb, void *cancel_arg);
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index 6268cad..daaf7f3 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -126,6 +126,14 @@ enum got_imsg_type {
 	GOT_IMSG_IDXPACK_OUTFD,
 	GOT_IMSG_IDXPACK_PROGRESS,
 	GOT_IMSG_IDXPACK_DONE,
+	GOT_IMSG_SEND_REQUEST,
+	GOT_IMSG_SEND_REF,
+	GOT_IMSG_SEND_REMOTE_REF,
+	GOT_IMSG_SEND_REF_STATUS,
+	GOT_IMSG_SEND_PACK_REQUEST,
+	GOT_IMSG_SEND_PACKFD,
+	GOT_IMSG_SEND_UPLOAD_PROGRESS,
+	GOT_IMSG_SEND_DONE,
 
 	/* Messages related to pack files. */
 	GOT_IMSG_PACKIDX,
@@ -335,6 +343,41 @@ struct got_imsg_fetch_download_progress {
 	off_t packfile_bytes;
 };
 
+/* Structure for GOT_IMSG_SEND_REQUEST data. */
+struct got_imsg_send_request {
+	int verbosity;
+	size_t nrefs;
+	/* Followed by nrefs GOT_IMSG_SEND_REF messages. */
+} __attribute__((__packed__));
+
+/* Structure for GOT_IMSG_SEND_UPLOAD_PROGRESS data. */
+struct got_imsg_send_upload_progress {
+	/* Number of packfile data bytes uploaded so far. */
+	off_t packfile_bytes;
+};
+
+/* Structure for GOT_IMSG_SEND_REF data. */
+struct got_imsg_send_ref {
+	uint8_t id[SHA1_DIGEST_LENGTH];
+	int delete;
+	size_t name_len;
+	/* Followed by name_len data bytes. */
+} __attribute__((__packed__));
+
+/* Structure for GOT_IMSG_SEND_REMOTE_REF data. */
+struct got_imsg_send_remote_ref {
+	uint8_t id[SHA1_DIGEST_LENGTH];
+	size_t name_len;
+	/* Followed by name_len data bytes. */
+} __attribute__((__packed__));
+
+/* Structure for GOT_IMSG_SEND_REF_STATUS data. */
+struct got_imsg_send_ref_status {
+	int success;
+	size_t name_len;
+	/* Followed by name_len data bytes. */
+} __attribute__((__packed__));
+
 /* Structure for GOT_IMSG_IDXPACK_REQUEST data. */
 struct got_imsg_index_pack_request {
 	uint8_t pack_hash[SHA1_DIGEST_LENGTH];
@@ -466,6 +509,13 @@ const struct got_error *got_privsep_send_fetch_outfd(struct imsgbuf *, int);
 const struct got_error *got_privsep_recv_fetch_progress(int *,
     struct got_object_id **, char **, struct got_pathlist_head *, char **,
     off_t *, uint8_t *, struct imsgbuf *);
+const struct got_error *got_privsep_send_send_req(struct imsgbuf *, int,
+   struct got_pathlist_head *, struct got_pathlist_head *, int);
+const struct got_error *got_privsep_recv_send_remote_refs(
+    struct got_pathlist_head *, struct imsgbuf *);
+const struct got_error *got_privsep_send_packfd(struct imsgbuf *, int);
+const struct got_error *got_privsep_recv_send_progress(int *, off_t *,
+    int *, char **, struct imsgbuf *);
 const struct got_error *got_privsep_get_imsg_obj(struct got_object **,
     struct imsg *, struct imsgbuf *);
 const struct got_error *got_privsep_recv_obj(struct got_object **,
diff --git a/lib/pack_create.c b/lib/pack_create.c
index 73b754d..1455b6d 100644
--- a/lib/pack_create.c
+++ b/lib/pack_create.c
@@ -1271,7 +1271,7 @@ const struct got_error *
 got_pack_create(uint8_t *packsha1, FILE *packfile,
     struct got_object_id **theirs, int ntheirs,
     struct got_object_id **ours, int nours,
-    struct got_repository *repo, int loose_obj_only,
+    struct got_repository *repo, int loose_obj_only, int allow_empty,
     got_pack_progress_cb progress_cb, void *progress_arg,
     got_cancel_cb cancel_cb, void *cancel_arg)
 {
@@ -1284,15 +1284,16 @@ got_pack_create(uint8_t *packsha1, FILE *packfile,
 	if (err)
 		return err;
 
-	if (nmeta == 0) {
+	if (nmeta == 0 && !allow_empty) {
 		err = got_error(GOT_ERR_CANNOT_PACK);
 		goto done;
 	}
-
-	err = pick_deltas(meta, nmeta, nours, repo,
-	    progress_cb, progress_arg, cancel_cb, cancel_arg);
-	if (err)
-		goto done;
+	if (nmeta > 0) {
+		err = pick_deltas(meta, nmeta, nours, repo,
+		    progress_cb, progress_arg, cancel_cb, cancel_arg);
+		if (err)
+			goto done;
+	}
 
 	err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
 	    progress_cb, progress_arg, cancel_cb, cancel_arg);
diff --git a/lib/privsep.c b/lib/privsep.c
index 274f3c3..ecad96e 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -862,6 +862,249 @@ done:
 	return err;
 }
 
+static const struct got_error *
+send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
+    int delete, struct imsgbuf *ibuf)
+{
+	const struct got_error *err = NULL;
+	size_t len;
+	struct ibuf *wbuf;
+
+	len = sizeof(struct got_imsg_send_ref) + name_len;
+	wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
+	if (wbuf == NULL)
+		return got_error_from_errno("imsg_create SEND_REF");
+
+	/* Keep in sync with struct got_imsg_send_ref! */
+	if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
+		err = got_error_from_errno("imsg_add SEND_REF");
+		ibuf_free(wbuf);
+		return err;
+	}
+	if (imsg_add(wbuf, &delete, sizeof(delete)) == -1) {
+		err = got_error_from_errno("imsg_add SEND_REF");
+		ibuf_free(wbuf);
+		return err;
+	}
+	if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
+		err = got_error_from_errno("imsg_add SEND_REF");
+		ibuf_free(wbuf);
+		return err;
+	}
+	if (imsg_add(wbuf, name, name_len) == -1) {
+		err = got_error_from_errno("imsg_add SEND_REF");
+		ibuf_free(wbuf);
+		return err;
+	}
+
+	wbuf->fd = -1;
+	imsg_close(ibuf, wbuf);
+	return flush_imsg(ibuf);
+}
+
+const struct got_error *
+got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
+   struct got_pathlist_head *have_refs,
+   struct got_pathlist_head *delete_refs,
+   int verbosity)
+{
+	const struct got_error *err = NULL;
+	struct got_pathlist_entry *pe;
+	struct got_imsg_send_request sendreq;
+	struct got_object_id zero_id;
+
+	memset(&zero_id, 0, sizeof(zero_id));
+	memset(&sendreq, 0, sizeof(sendreq));
+	sendreq.verbosity = verbosity;
+	TAILQ_FOREACH(pe, have_refs, entry)
+		sendreq.nrefs++;
+	TAILQ_FOREACH(pe, delete_refs, entry)
+		sendreq.nrefs++;
+	if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
+	    &sendreq, sizeof(sendreq)) == -1) {
+		err = got_error_from_errno(
+		    "imsg_compose FETCH_SERVER_PROGRESS");
+		goto done;
+	}
+
+	err = flush_imsg(ibuf);
+	if (err)
+		goto done;
+	fd = -1;
+
+	TAILQ_FOREACH(pe, have_refs, entry) {
+		const char *name = pe->path;
+		size_t name_len = pe->path_len;
+		struct got_object_id *id = pe->data;
+		err = send_send_ref(name, name_len, id, 0, ibuf);
+		if (err)
+			goto done;
+	}
+
+	TAILQ_FOREACH(pe, delete_refs, entry) {
+		const char *name = pe->path;
+		size_t name_len = pe->path_len;
+		err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
+		if (err)
+			goto done;
+	}
+done:
+	if (fd != -1 && close(fd) == -1 && err == NULL)
+		err = got_error_from_errno("close");
+	return err;
+
+}
+
+const struct got_error *
+got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
+    struct imsgbuf *ibuf)
+{
+	const struct got_error *err = NULL;
+	struct imsg imsg;
+	size_t datalen;
+	int done = 0;
+	struct got_imsg_send_remote_ref iremote_ref;
+	struct got_object_id *id = NULL;
+	char *refname = NULL;
+	struct got_pathlist_entry *new;
+
+	while (!done) {
+		err = got_privsep_recv_imsg(&imsg, ibuf, 0);
+		if (err)
+			return err;
+		datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
+		switch (imsg.hdr.type) {
+		case GOT_IMSG_ERROR:
+			if (datalen < sizeof(struct got_imsg_error)) {
+				err = got_error(GOT_ERR_PRIVSEP_LEN);
+				goto done;
+			}
+			err = recv_imsg_error(&imsg, datalen);
+			goto done;
+		case GOT_IMSG_SEND_REMOTE_REF:
+			if (datalen < sizeof(iremote_ref)) {
+				err = got_error(GOT_ERR_PRIVSEP_MSG);
+				goto done;
+			}
+			memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
+			if (datalen != sizeof(iremote_ref) +
+			    iremote_ref.name_len) {
+				err = got_error(GOT_ERR_PRIVSEP_MSG);
+				goto done;
+			}
+			id = malloc(sizeof(*id));
+			if (id == NULL) {
+				err = got_error_from_errno("malloc");
+				goto done;
+			}
+			memcpy(id->sha1, iremote_ref.id, SHA1_DIGEST_LENGTH);
+			refname = strndup(imsg.data + sizeof(iremote_ref),
+			    datalen - sizeof(iremote_ref));
+			if (refname == NULL) {
+				err = got_error_from_errno("strndup");
+				goto done;
+			}
+			err = got_pathlist_insert(&new, remote_refs,
+			    refname, id);
+			if (err)
+				goto done;
+			if (new == NULL) { /* duplicate which wasn't inserted */
+				free(id);
+				free(refname);
+			}
+			id = NULL;
+			refname = NULL;
+			break;
+		case GOT_IMSG_SEND_PACK_REQUEST:
+			if (datalen != 0) {
+				err = got_error(GOT_ERR_PRIVSEP_MSG);
+				goto done;
+			}
+			/* got-send-pack is now waiting for a pack file. */
+			done = 1;
+			break;
+		default:
+			err = got_error(GOT_ERR_PRIVSEP_MSG);
+			break;
+		}
+	}
+done:
+	free(id);
+	free(refname);
+	imsg_free(&imsg);
+	return err;
+}
+
+const struct got_error *
+got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
+{
+	return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
+}
+
+const struct got_error *
+got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
+    int *success, char **refname, struct imsgbuf *ibuf)
+{
+	const struct got_error *err = NULL;
+	struct imsg imsg;
+	size_t datalen;
+	struct got_imsg_send_ref_status iref_status;
+
+	/* Do not reset the current value of 'bytes_sent', it accumulates. */
+	*done = 0;
+	*success = 0;
+	*refname = NULL;
+
+	err = got_privsep_recv_imsg(&imsg, ibuf, 0);
+	if (err)
+		return err;
+
+	datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
+	switch (imsg.hdr.type) {
+	case GOT_IMSG_ERROR:
+		if (datalen < sizeof(struct got_imsg_error)) {
+			err = got_error(GOT_ERR_PRIVSEP_LEN);
+			break;
+		}
+		err = recv_imsg_error(&imsg, datalen);
+		break;
+	case GOT_IMSG_SEND_UPLOAD_PROGRESS:
+		if (datalen < sizeof(*bytes_sent)) {
+			err = got_error(GOT_ERR_PRIVSEP_MSG);
+			break;
+		}
+		memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
+		break;
+	case GOT_IMSG_SEND_REF_STATUS:
+		if (datalen < sizeof(iref_status)) {
+			err = got_error(GOT_ERR_PRIVSEP_MSG);
+			break;
+		}
+		memcpy(&iref_status, imsg.data, sizeof(iref_status));
+		if (datalen != sizeof(iref_status) + iref_status.name_len) {
+			err = got_error(GOT_ERR_PRIVSEP_MSG);
+			break;
+		}
+		*success = iref_status.success;
+		*refname = strndup(imsg.data + sizeof(iref_status),
+		    iref_status.name_len);
+		break;
+	case GOT_IMSG_SEND_DONE:
+		if (datalen != 0) {
+			err = got_error(GOT_ERR_PRIVSEP_MSG);
+			break;
+		}
+		*done = 1;
+		break;
+	default:
+		err = got_error(GOT_ERR_PRIVSEP_MSG);
+		break;
+	}
+
+	imsg_free(&imsg);
+	return err;
+}
+
 const struct got_error *
 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
     int fd)
@@ -2451,6 +2694,7 @@ got_privsep_unveil_exec_helpers(void)
 	    GOT_PATH_PROG_READ_GOTCONFIG,
 	    GOT_PATH_PROG_FETCH_PACK,
 	    GOT_PATH_PROG_INDEX_PACK,
+	    GOT_PATH_PROG_SEND_PACK,
 	};
 	size_t i;
 
diff --git a/lib/repository_admin.c b/lib/repository_admin.c
index deb76e2..cf1fd8a 100644
--- a/lib/repository_admin.c
+++ b/lib/repository_admin.c
@@ -198,7 +198,7 @@ got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
 	}
 
 	err = got_pack_create((*pack_hash)->sha1, *packfile, theirs, ntheirs,
-	    ours, nours, repo, loose_obj_only, progress_cb, progress_arg,
+	    ours, nours, repo, loose_obj_only, 0, progress_cb, progress_arg,
 	    cancel_cb, cancel_arg);
 	if (err)
 		goto done;
diff --git a/lib/send.c b/lib/send.c
new file mode 100644
index 0000000..52f062e
--- /dev/null
+++ b/lib/send.c
@@ -0,0 +1,841 @@
+/*
+ * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
+ * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/queue.h>
+#include <sys/uio.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+#include <sys/resource.h>
+#include <sys/socket.h>
+
+#include <endian.h>
+#include <errno.h>
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <sha1.h>
+#include <unistd.h>
+#include <zlib.h>
+#include <ctype.h>
+#include <limits.h>
+#include <imsg.h>
+#include <time.h>
+#include <uuid.h>
+#include <netdb.h>
+#include <netinet/in.h>
+
+#include "got_error.h"
+#include "got_reference.h"
+#include "got_repository.h"
+#include "got_path.h"
+#include "got_cancel.h"
+#include "got_worktree.h"
+#include "got_object.h"
+#include "got_opentemp.h"
+#include "got_send.h"
+#include "got_repository_admin.h"
+#include "got_commit_graph.h"
+
+#include "got_lib_delta.h"
+#include "got_lib_inflate.h"
+#include "got_lib_object.h"
+#include "got_lib_object_parse.h"
+#include "got_lib_object_create.h"
+#include "got_lib_pack.h"
+#include "got_lib_sha1.h"
+#include "got_lib_privsep.h"
+#include "got_lib_object_cache.h"
+#include "got_lib_repository.h"
+#include "got_lib_pack_create.h"
+
+#ifndef nitems
+#define nitems(_a)	(sizeof((_a)) / sizeof((_a)[0]))
+#endif
+
+#ifndef ssizeof
+#define ssizeof(_x) ((ssize_t)(sizeof(_x)))
+#endif
+
+#ifndef MIN
+#define	MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
+#endif
+
+static const struct got_error *
+dial_ssh(pid_t *sendpid, int *sendfd, const char *host, const char *port,
+    const char *path, const char *direction, int verbosity)
+{
+	const struct got_error *error = NULL;
+	int pid, pfd[2];
+	char cmd[64];
+	char *argv[11];
+	int i = 0, j;
+
+	*sendpid = -1;
+	*sendfd = -1;
+
+	argv[i++] = GOT_SEND_PATH_SSH;
+	if (port != NULL) {
+		argv[i++] = "-p";
+		argv[i++] = (char *)port;
+	}
+	if (verbosity == -1) {
+		argv[i++] = "-q";
+	} else {
+		/* ssh(1) allows up to 3 "-v" options. */
+		for (j = 0; j < MIN(3, verbosity); j++)
+			argv[i++] = "-v";
+	}
+	argv[i++] = "--";
+	argv[i++] = (char *)host;
+	argv[i++] = (char *)cmd;
+	argv[i++] = (char *)path;
+	argv[i++] = NULL;
+
+	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
+		return got_error_from_errno("socketpair");
+
+	pid = fork();
+	if (pid == -1) {
+		error = got_error_from_errno("fork");
+		close(pfd[0]);
+		close(pfd[1]);
+		return error;
+	} else if (pid == 0) {
+		int n;
+		if (close(pfd[1]) == -1)
+			err(1, "close");
+		if (dup2(pfd[0], 0) == -1)
+			err(1, "dup2");
+		if (dup2(pfd[0], 1) == -1)
+			err(1, "dup2");
+		n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
+		if (n < 0 || n >= ssizeof(cmd))
+			err(1, "snprintf");
+		if (execv(GOT_SEND_PATH_SSH, argv) == -1)
+			err(1, "execv");
+		abort(); /* not reached */
+	} else {
+		if (close(pfd[0]) == -1)
+			return got_error_from_errno("close");
+		*sendpid = pid;
+		*sendfd = pfd[1];
+		return NULL;
+	}
+}
+
+static const struct got_error *
+dial_git(int *sendfd, const char *host, const char *port, const char *path,
+    const char *direction)
+{
+	const struct got_error *err = NULL;
+	struct addrinfo hints, *servinfo, *p;
+	char *cmd = NULL;
+	int fd = -1, len, r, eaicode;
+
+	*sendfd = -1;
+
+	if (port == NULL)
+		port = GOT_DEFAULT_GIT_PORT_STR;
+
+	memset(&hints, 0, sizeof hints);
+	hints.ai_family = AF_UNSPEC;
+	hints.ai_socktype = SOCK_STREAM;
+	eaicode = getaddrinfo(host, port, &hints, &servinfo);
+	if (eaicode) {
+		char msg[512];
+		snprintf(msg, sizeof(msg), "%s: %s", host,
+		    gai_strerror(eaicode));
+		return got_error_msg(GOT_ERR_ADDRINFO, msg);
+	}
+
+	for (p = servinfo; p != NULL; p = p->ai_next) {
+		if ((fd = socket(p->ai_family, p->ai_socktype,
+		    p->ai_protocol)) == -1)
+			continue;
+		if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
+			err = NULL;
+			break;
+		}
+		err = got_error_from_errno("connect");
+		close(fd);
+	}
+	if (p == NULL)
+		goto done;
+
+	if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
+		err = got_error_from_errno("asprintf");
+		goto done;
+	}
+	len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
+	r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
+	if (r < 0)
+		err = got_error_from_errno("dprintf");
+done:
+	free(cmd);
+	if (err) {
+		if (fd != -1)
+			close(fd);
+	} else
+		*sendfd = fd;
+	return err;
+}
+
+const struct got_error *
+got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
+    const char *host, const char *port, const char *server_path, int verbosity)
+{
+	const struct got_error *err = NULL;
+
+	*sendpid = -1;
+	*sendfd = -1;
+
+	if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
+		err = dial_ssh(sendpid, sendfd, host, port, server_path,
+		    "receive", verbosity);
+	else if (strcmp(proto, "git") == 0)
+		err = dial_git(sendfd, host, port, server_path, "receive");
+	else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
+		err = got_error_path(proto, GOT_ERR_NOT_IMPL);
+	else
+		err = got_error_path(proto, GOT_ERR_BAD_PROTO);
+	return err;
+}
+
+struct pack_progress_arg {
+    got_send_progress_cb progress_cb;
+    void *progress_arg;
+
+    off_t packfile_size;
+    int ncommits;
+    int nobj_total;
+    int nobj_deltify;
+    int nobj_written;
+};
+
+static const struct got_error *
+pack_progress(void *arg, off_t packfile_size, int ncommits,
+    int nobj_total, int nobj_deltify, int nobj_written)
+{
+	const struct got_error *err;
+	struct pack_progress_arg *a = arg;
+
+	err = a->progress_cb(a->progress_arg, packfile_size, ncommits,
+	    nobj_total, nobj_deltify, nobj_written, 0, NULL, 0);
+	if (err)
+		return err;
+
+	a->packfile_size = packfile_size;
+	a->ncommits = ncommits;
+	a->nobj_total = nobj_total;
+	a->nobj_deltify = nobj_deltify;
+	a->nobj_written = nobj_written;
+	return NULL;
+}
+
+static const struct got_error *
+insert_ref(struct got_reflist_head *refs, const char *refname,
+    struct got_repository *repo)
+{
+	const struct got_error *err;
+	struct got_reference *ref;
+	struct got_reflist_entry *new;
+
+	err = got_ref_open(&ref, repo, refname, 0);
+	if (err)
+		return err;
+
+	err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
+	if (err || new == NULL /* duplicate */)
+		got_ref_close(ref);
+
+	return err;
+}
+
+static const struct got_error *
+check_linear_ancestry(const char *refname, struct got_object_id *my_id,
+    struct got_object_id *their_id, struct got_repository *repo,
+    got_cancel_cb cancel_cb, void *cancel_arg)
+{
+	const struct got_error *err = NULL;
+	struct got_object_id *yca_id;
+	int obj_type;
+
+	err = got_object_get_type(&obj_type, repo, their_id);
+	if (err)
+		return err;
+	if (obj_type != GOT_OBJ_TYPE_COMMIT)
+		return got_error_fmt(GOT_ERR_OBJ_TYPE,
+		    "bad object type on server for %s", refname);
+
+	err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
+	    my_id, their_id, repo, cancel_cb, cancel_arg);
+	if (err)
+		return err;
+	if (yca_id == NULL)
+		return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
+
+	/*
+	 * Require a straight line of history between the two commits,
+	 * with their commit being older than my commit.
+	 *
+	 * Non-linear situations such as this require a rebase:
+	 *
+	 * (theirs) D       F (mine)
+	 *           \     /
+	 *            C   E
+	 *             \ /
+	 *              B (yca)
+	 *              |
+	 *              A
+	 */
+	if (got_object_id_cmp(their_id, yca_id) != 0)
+		err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
+
+	free(yca_id);
+	return err;
+}
+
+static const struct got_error *
+realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
+{
+	struct got_object_id **new;
+	const size_t alloc_chunksz = 256;
+
+	if (*nalloc >= n + alloc_chunksz)
+		return NULL;
+
+	new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
+	    sizeof(struct got_object_id));
+	if (new == NULL)
+		return got_error_from_errno("recallocarray");
+
+	*ids = new;
+	*nalloc += alloc_chunksz;
+	return NULL;
+}
+
+static struct got_reference *
+find_ref(struct got_reflist_head *refs, const char *refname)
+{
+	struct got_reflist_entry *re;
+
+	TAILQ_FOREACH(re, refs, entry) {
+		if (got_path_cmp(got_ref_get_name(re->ref), refname,
+		    strlen(got_ref_get_name(re->ref)),
+		    strlen(refname)) == 0) {
+			return re->ref;
+		}
+	}
+
+	return NULL;
+}
+
+static struct got_pathlist_entry *
+find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
+{
+	struct got_pathlist_entry *pe;
+
+	TAILQ_FOREACH(pe, their_refs, entry) {
+		const char *their_refname = pe->path;
+		if (got_path_cmp(their_refname, refname,
+		    strlen(their_refname), strlen(refname)) == 0) {
+			return pe;
+		}
+	}
+
+	return NULL;
+}
+
+static const struct got_error *
+get_remote_refname(char **remote_refname, const char *remote_name,
+    const char *refname)
+{
+	if (strncmp(refname, "refs/", 5) == 0)
+		refname += 5;
+	if (strncmp(refname, "heads/", 6) == 0)
+		refname += 6;
+
+	if (asprintf(remote_refname, "refs/remotes/%s/%s",
+	    remote_name, refname) == -1)
+		return got_error_from_errno("asprintf");
+
+	return NULL;
+}
+
+static const struct got_error *
+update_remote_ref(struct got_reference *my_ref, const char *remote_name,
+    struct got_repository *repo)
+{
+	const struct got_error *err, *unlock_err;
+	struct got_object_id *my_id;
+	struct got_reference *ref = NULL;
+	char *remote_refname = NULL;
+	int ref_locked = 0;
+
+	err = got_ref_resolve(&my_id, repo, my_ref);
+	if (err)
+		return err;
+
+	err = get_remote_refname(&remote_refname, remote_name,
+	    got_ref_get_name(my_ref));
+	if (err)
+		goto done;
+
+	err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
+	if (err) {
+		if (err->code != GOT_ERR_NOT_REF)
+			goto done;
+		err = got_ref_alloc(&ref, remote_refname, my_id);
+		if (err)
+			goto done;
+	} else {
+		ref_locked = 1;
+		err = got_ref_change_ref(ref, my_id);
+		if (err)
+			goto done;
+	}
+
+	err = got_ref_write(ref, repo);
+done:
+	if (ref) {
+		if (ref_locked) {
+			unlock_err = got_ref_unlock(ref);
+			if (unlock_err && err == NULL)
+				err = unlock_err;
+		}
+		got_ref_close(ref);
+	}
+	free(my_id);
+	free(remote_refname);
+	return err;
+}
+
+const struct got_error*
+got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
+    struct got_pathlist_head *tag_names,
+    struct got_pathlist_head *delete_branches,
+    int verbosity, int overwrite_refs, int sendfd,
+    struct got_repository *repo, got_send_progress_cb progress_cb,
+    void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
+{
+	int imsg_sendfds[2];
+	int npackfd = -1, nsendfd = -1;
+	int sendstatus, done = 0;
+	const struct got_error *err;
+	struct imsgbuf sendibuf;
+	pid_t sendpid = -1;
+	struct got_reflist_head refs;
+	struct got_pathlist_head have_refs;
+	struct got_pathlist_head their_refs;
+	struct got_pathlist_entry *pe;
+	struct got_reflist_entry *re;
+	struct got_object_id **our_ids = NULL;
+	struct got_object_id **their_ids = NULL;
+	struct got_object_id *my_id = NULL;
+	int i, nours = 0, ntheirs = 0;
+	size_t nalloc_ours = 0, nalloc_theirs = 0;
+	int refs_to_send = 0;
+	off_t bytes_sent = 0;
+	struct pack_progress_arg ppa;
+	uint8_t packsha1[SHA1_DIGEST_LENGTH];
+	FILE *packfile = NULL;
+
+	TAILQ_INIT(&refs);
+	TAILQ_INIT(&have_refs);
+	TAILQ_INIT(&their_refs);
+
+	TAILQ_FOREACH(pe, branch_names, entry) {
+		const char *branchname = pe->path;
+		if (strncmp(branchname, "refs/heads/", 11) != 0) {
+			char *s;
+			if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
+				err = got_error_from_errno("asprintf");
+				goto done;
+			}
+			err = insert_ref(&refs, s, repo);
+			free(s);
+		} else {
+			err = insert_ref(&refs, branchname, repo);
+		}
+		if (err)
+			goto done;
+	}
+
+	TAILQ_FOREACH(pe, delete_branches, entry) {
+		const char *branchname = pe->path;
+		struct got_reference *ref;
+		if (strncmp(branchname, "refs/heads/", 11) != 0) {
+			err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
+			    branchname);
+			goto done;
+		}
+		ref = find_ref(&refs, branchname);
+		if (ref) {
+			err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
+			    "changes on %s will be sent to server",
+			    branchname);
+			goto done;
+		}
+	}
+
+	TAILQ_FOREACH(pe, tag_names, entry) {
+		const char *tagname = pe->path;
+		if (strncmp(tagname, "refs/tags/", 10) != 0) {
+			char *s;
+			if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
+				err = got_error_from_errno("asprintf");
+				goto done;
+			}
+			err = insert_ref(&refs, s, repo);
+			free(s);
+		} else {
+			err = insert_ref(&refs, tagname, repo);
+		}
+		if (err)
+			goto done;
+	}
+
+	if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
+		err = got_error(GOT_ERR_SEND_EMPTY);
+		goto done;
+	}
+
+	TAILQ_FOREACH(re, &refs, entry) {
+		struct got_object_id *id;
+		int obj_type;
+
+		if (got_ref_is_symbolic(re->ref)) {
+			err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
+			    "cannot send symbolic reference %s",
+			    got_ref_get_name(re->ref));
+			goto done;
+		}
+
+		err = got_ref_resolve(&id, repo, re->ref);
+		if (err)
+			goto done;
+		err = got_object_get_type(&obj_type, repo, id);
+		free(id);
+		if (err)
+			goto done;
+		switch (obj_type) {
+		case GOT_OBJ_TYPE_COMMIT:
+		case GOT_OBJ_TYPE_TAG:
+			break;
+		default:
+			err = got_error_fmt(GOT_ERR_OBJ_TYPE,
+			    "cannot send %s", got_ref_get_name(re->ref));
+			goto done;
+		}
+	}
+
+	packfile = got_opentemp();
+	if (packfile == NULL) {
+		err = got_error_from_errno("got_opentemp");
+		goto done;
+	}
+
+	err = realloc_ids(&our_ids, &nalloc_ours, 0);
+	if (err)
+		goto done;
+	err = realloc_ids(&their_ids, &nalloc_ours, 0);
+	if (err)
+		goto done;
+
+	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
+		err = got_error_from_errno("socketpair");
+		goto done;
+	}
+
+	sendpid = fork();
+	if (sendpid == -1) {
+		err = got_error_from_errno("fork");
+		goto done;
+	} else if (sendpid == 0){
+		got_privsep_exec_child(imsg_sendfds,
+		    GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
+	}
+
+	if (close(imsg_sendfds[1]) == -1) {
+		err = got_error_from_errno("close");
+		goto done;
+	}
+	imsg_init(&sendibuf, imsg_sendfds[0]);
+	nsendfd = dup(sendfd);
+	if (nsendfd == -1) {
+		err = got_error_from_errno("dup");
+		goto done;
+	}
+
+	/*
+	 * Convert reflist to pathlist since the privsep layer
+	 * is linked into helper programs which lack reference.c.
+	 */
+	TAILQ_FOREACH(re, &refs, entry) {
+		struct got_object_id *id;
+		err = got_ref_resolve(&id, repo, re->ref);
+		if (err)
+			goto done;
+		err = got_pathlist_append(&have_refs,
+		    got_ref_get_name(re->ref), id);
+		if (err)
+			goto done;
+		/*
+		 * Also prepare the array of our object IDs which
+		 * will be needed for generating a pack file.
+		 */
+		err = realloc_ids(&our_ids, &nalloc_ours, nours);
+		if (err)
+			goto done;
+		our_ids[nours] = id;
+		nours++;
+	}
+
+	err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
+	    delete_branches, verbosity);
+	if (err)
+		goto done;
+	nsendfd = -1;
+
+	err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
+	if (err)
+		goto done;
+
+	/*
+	 * Process references reported by the server.
+	 * Push appropriate object IDs onto the "their IDs" array.
+	 * This array will be used to exclude objects which already
+	 * exist on the server from our pack file.
+	 */
+	TAILQ_FOREACH(pe, &their_refs, entry) {
+		const char *refname = pe->path;
+		struct got_object_id *their_id = pe->data;
+		int have_their_id;
+		struct got_object *obj;
+		struct got_reference *my_ref = NULL;
+		int is_tag = 0;
+
+		/* Don't blindly trust the server to send us valid names. */
+		if (!got_ref_name_is_valid(refname))
+			continue;
+
+		/*
+		 * Find out whether this is a reference we want to upload.
+		 * Otherwise we can still use this reference as a hint to
+		 * avoid uploading any objects the server already has.
+		 */
+		my_ref = find_ref(&refs, refname);
+		if (my_ref) {
+			err = got_ref_resolve(&my_id, repo, my_ref);
+			if (err)
+				goto done;
+			if (got_object_id_cmp(my_id, their_id) == 0) {
+				free(my_id);
+				my_id = NULL;
+				continue;
+			}
+			refs_to_send++;
+
+		}
+
+		if (strncmp(refname, "refs/tags/", 10) == 0)
+			is_tag = 1;
+
+		/* Prevent tags from being overwritten by default. */ 
+		if (!overwrite_refs && my_ref && is_tag) {
+			err = got_error_fmt(GOT_ERR_SEND_TAG_EXISTS,
+			    "%s", refname);
+			goto done;
+		}
+
+		/* Check if their object exists locally. */
+		err = got_object_open(&obj, repo, their_id);
+		if (err) {
+			if (err->code != GOT_ERR_NO_OBJ)
+				goto done;
+			if (!overwrite_refs && my_ref != NULL) {
+				err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
+				    "%s", refname);
+				goto done;
+			}
+			have_their_id = 0;
+		} else {
+			got_object_close(obj);
+			have_their_id = 1;
+		}
+
+		err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs);
+		if (err)
+			goto done;
+
+		if (have_their_id) {
+			/* Enforce linear ancestry if required. */
+			if (!overwrite_refs && my_ref && !is_tag) {
+				struct got_object_id *my_id;
+				err = got_ref_resolve(&my_id, repo, my_ref);
+				if (err)
+					goto done;
+				err = check_linear_ancestry(refname, my_id,
+				    their_id, repo, cancel_cb, cancel_arg);
+				free(my_id);
+				my_id = NULL;
+				if (err)
+					goto done;
+			}
+			/* Exclude any objects reachable via their ID. */
+			their_ids[ntheirs] = got_object_id_dup(their_id);
+			if (their_ids[ntheirs] == NULL) {
+				err = got_error_from_errno("got_object_id_dup");
+				goto done;
+			}
+			ntheirs++;
+		} else if (!is_tag) {
+			char *remote_refname;
+			struct got_reference *ref;
+			/*
+			 * Exclude any objects which exist on the server
+			 * according to a locally cached remote reference.
+			 */
+			err = get_remote_refname(&remote_refname,
+			    remote_name, refname);
+			if (err)
+				goto done;
+			err = got_ref_open(&ref, repo, remote_refname, 0);
+			free(remote_refname);
+			if (err) {
+				if (err->code != GOT_ERR_NOT_REF)
+					goto done;
+			} else {
+				err = got_ref_resolve(&their_ids[ntheirs],
+				    repo, ref);
+				got_ref_close(ref);
+				if (err)
+					goto done;
+				ntheirs++;
+			}
+		}
+	}
+
+	/* Account for any new references we are going to upload. */
+	TAILQ_FOREACH(re, &refs, entry) {
+		if (find_their_ref(&their_refs,
+		    got_ref_get_name(re->ref)) == NULL)
+			refs_to_send++;
+	}
+
+	if (refs_to_send == 0) {
+		got_privsep_send_stop(imsg_sendfds[0]);
+		goto done;
+	}
+
+	memset(&ppa, 0, sizeof(ppa));
+	ppa.progress_cb = progress_cb;
+	ppa.progress_arg = progress_arg;
+	err = got_pack_create(packsha1, packfile, their_ids, ntheirs,
+	    our_ids, nours, repo, 0, 1, pack_progress, &ppa,
+	    cancel_cb, cancel_arg);
+	if (err)
+		goto done;
+
+	if (fflush(packfile) == -1) {
+		err = got_error_from_errno("fflush");
+		goto done;
+	}
+
+	npackfd = dup(fileno(packfile));
+	if (npackfd == -1) {
+		err = got_error_from_errno("dup");
+		goto done;
+	}
+	err = got_privsep_send_packfd(&sendibuf, npackfd);
+	if (err != NULL)
+		goto done;
+	npackfd = -1;
+
+	while (!done) {
+		int success = 0;
+		char *refname = NULL;
+		off_t bytes_sent_cur = 0;
+		if (cancel_cb) {
+			err = (*cancel_cb)(cancel_arg);
+			if (err)
+				goto done;
+		}
+		err = got_privsep_recv_send_progress(&done, &bytes_sent,
+		    &success, &refname, &sendibuf);
+		if (err)
+			goto done;
+		if (refname && got_ref_name_is_valid(refname) && success &&
+		    strncmp(refname, "refs/tags/", 10) != 0) {
+			struct got_reference *my_ref;
+			/*
+			 * The server has accepted our changes.
+			 * Update our reference in refs/remotes/ accordingly.
+			 */
+			my_ref = find_ref(&refs, refname);
+			if (my_ref) {
+				err = update_remote_ref(my_ref, remote_name,
+				    repo);
+				if (err)
+					goto done;
+			}
+		}
+		if (refname != NULL ||
+		    bytes_sent_cur != bytes_sent) {
+			err = progress_cb(progress_arg, ppa.packfile_size,
+			    ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
+			    ppa.nobj_written, bytes_sent,
+			    refname, success);
+			if (err) {
+				free(refname);
+				goto done;
+			}
+			bytes_sent_cur = bytes_sent;
+		}
+		free(refname);
+	}
+done:
+	if (sendpid != -1) {
+		if (err)
+			got_privsep_send_stop(imsg_sendfds[0]);
+		if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
+			err = got_error_from_errno("waitpid");
+	}
+	if (packfile && fclose(packfile) == EOF && err == NULL)
+		err = got_error_from_errno("fclose");
+	if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
+		err = got_error_from_errno("close");
+	if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
+		err = got_error_from_errno("close");
+
+	got_ref_list_free(&refs);
+	got_pathlist_free(&have_refs);
+	got_pathlist_free(&their_refs);
+	for (i = 0; i < nours; i++)
+		free(our_ids[i]);
+	free(our_ids);
+	for (i = 0; i < ntheirs; i++)
+		free(their_ids[i]);
+	free(their_ids);
+	free(my_id);
+	return err;
+}
diff --git a/libexec/Makefile b/libexec/Makefile
index 1e55c98..3783b56 100644
--- a/libexec/Makefile
+++ b/libexec/Makefile
@@ -1,5 +1,5 @@
 SUBDIR = got-read-blob got-read-commit got-read-object got-read-tree \
 	got-read-tag got-fetch-pack got-index-pack got-read-pack \
-	got-read-gitconfig got-read-gotconfig
+	got-read-gitconfig got-read-gotconfig got-send-pack
 
 .include <bsd.subdir.mk>
diff --git a/libexec/got-send-pack/Makefile b/libexec/got-send-pack/Makefile
new file mode 100644
index 0000000..ae3ef0f
--- /dev/null
+++ b/libexec/got-send-pack/Makefile
@@ -0,0 +1,13 @@
+.PATH:${.CURDIR}/../../lib
+
+.include "../../got-version.mk"
+
+PROG=		got-send-pack
+SRCS=		got-send-pack.c error.c inflate.c object_parse.c \
+		path.c privsep.c sha1.c
+
+CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib
+LDADD = -lutil -lz
+DPADD = ${LIBZ} ${LIBUTIL}
+
+.include <bsd.prog.mk>
diff --git a/libexec/got-send-pack/got-send-pack.c b/libexec/got-send-pack/got-send-pack.c
new file mode 100644
index 0000000..d114153
--- /dev/null
+++ b/libexec/got-send-pack/got-send-pack.c
@@ -0,0 +1,1038 @@
+/*
+ * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
+ * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <sys/queue.h>
+#include <sys/uio.h>
+#include <sys/time.h>
+#include <sys/stat.h>
+
+#include <stdint.h>
+#include <errno.h>
+#include <imsg.h>
+#include <limits.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <sha1.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <zlib.h>
+#include <err.h>
+
+#include "got_error.h"
+#include "got_object.h"
+#include "got_path.h"
+#include "got_version.h"
+#include "got_fetch.h"
+#include "got_reference.h"
+
+#include "got_lib_sha1.h"
+#include "got_lib_delta.h"
+#include "got_lib_object.h"
+#include "got_lib_object_parse.h"
+#include "got_lib_privsep.h"
+#include "got_lib_pack.h"
+
+#ifndef nitems
+#define nitems(_a)	(sizeof((_a)) / sizeof((_a)[0]))
+#endif
+
+struct got_object *indexed;
+static int chattygot;
+
+static const struct got_error *
+readn(ssize_t *off, int fd, void *buf, size_t n)
+{
+	ssize_t r;
+
+	*off = 0;
+	while (*off != n) {
+		r = read(fd, buf + *off, n - *off);
+		if (r == -1)
+			return got_error_from_errno("read");
+		if (r == 0)
+			return NULL;
+		*off += r;
+	}
+	return NULL;
+}
+
+static const struct got_error *
+flushpkt(int fd)
+{
+	ssize_t w;
+
+	if (chattygot > 1)
+		fprintf(stderr, "%s: writepkt: 0000\n", getprogname());
+
+	w = write(fd, "0000", 4);
+	if (w == -1)
+		return got_error_from_errno("write");
+	if (w != 4)
+		return got_error(GOT_ERR_IO);
+	return NULL;
+}
+
+/*
+ * Packet header contains a 4-byte hexstring which specifies the length
+ * of data which follows.
+ */
+static const struct got_error *
+read_pkthdr(int *datalen, int fd)
+{
+	static const struct got_error *err = NULL;
+	char lenstr[5];
+	long len;
+	char *e;
+	int n, i;
+	ssize_t r;
+
+	*datalen = 0;
+
+	err = readn(&r, fd, lenstr, 4);
+	if (err)
+		return err;
+	if (r == 0) {
+		/* implicit "0000" */
+		if (chattygot > 1)
+			fprintf(stderr, "%s: readpkt: 0000\n", getprogname());
+		return NULL;
+	}
+	if (r != 4)
+		return got_error_msg(GOT_ERR_BAD_PACKET,
+		    "wrong packet header length");
+
+	lenstr[4] = '\0';
+	for (i = 0; i < 4; i++) {
+		if (!isprint((unsigned char)lenstr[i]))
+			return got_error_msg(GOT_ERR_BAD_PACKET,
+			    "unprintable character in packet length field");
+	}
+	for (i = 0; i < 4; i++) {
+		if (!isxdigit((unsigned char)lenstr[i])) {
+			if (chattygot)
+				fprintf(stderr, "%s: bad length: '%s'\n",
+				    getprogname(), lenstr);
+			return got_error_msg(GOT_ERR_BAD_PACKET,
+			    "packet length not specified in hex");
+		}
+	}
+	errno = 0;
+	len = strtol(lenstr, &e, 16);
+	if (lenstr[0] == '\0' || *e != '\0')
+		return got_error(GOT_ERR_BAD_PACKET);
+	if (errno == ERANGE && (len == LONG_MAX || len == LONG_MIN))
+		return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
+	if (len > INT_MAX || len < INT_MIN)
+		return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
+	n = len;
+	if (n == 0)
+		return NULL;
+	if (n <= 4)
+		return got_error_msg(GOT_ERR_BAD_PACKET, "packet too short");
+	n  -= 4;
+
+	*datalen = n;
+	return NULL;
+}
+
+static const struct got_error *
+readpkt(int *outlen, int fd, char *buf, int buflen)
+{
+	const struct got_error *err = NULL;
+	int datalen, i;
+	ssize_t n;
+
+	err = read_pkthdr(&datalen, fd);
+	if (err)
+		return err;
+
+	if (datalen > buflen)
+		return got_error(GOT_ERR_NO_SPACE);
+
+	err = readn(&n, fd, buf, datalen);
+	if (err)
+		return err;
+	if (n != datalen)
+		return got_error_msg(GOT_ERR_BAD_PACKET, "short packet");
+
+	if (chattygot > 1) {
+		fprintf(stderr, "%s: readpkt: %zd:\t", getprogname(), n);
+		for (i = 0; i < n; i++) {
+			if (isprint(buf[i]))
+				fputc(buf[i], stderr);
+			else
+				fprintf(stderr, "[0x%.2x]", buf[i]);
+		}
+		fputc('\n', stderr);
+	}
+
+	*outlen = n;
+	return NULL;
+}
+
+static const struct got_error *
+writepkt(int fd, char *buf, int nbuf)
+{
+	char len[5];
+	int i;
+	ssize_t w;
+
+	if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
+		return got_error(GOT_ERR_NO_SPACE);
+	w = write(fd, len, 4);
+	if (w == -1)
+		return got_error_from_errno("write");
+	if (w != 4)
+		return got_error(GOT_ERR_IO);
+	w = write(fd, buf, nbuf);
+	if (w == -1)
+		return got_error_from_errno("write");
+	if (w != nbuf)
+		return got_error(GOT_ERR_IO);
+	if (chattygot > 1) {
+		fprintf(stderr, "%s: writepkt: %s:\t", getprogname(), len);
+		for (i = 0; i < nbuf; i++) {
+			if (isprint(buf[i]))
+				fputc(buf[i], stderr);
+			else
+				fprintf(stderr, "[0x%.2x]", buf[i]);
+		}
+		fputc('\n', stderr);
+	}
+	return NULL;
+}
+
+static const struct got_error *
+tokenize_refline(char **tokens, char *line, int len, int maxtokens)
+{
+	const struct got_error *err = NULL;
+	char *p;
+	size_t i, n = 0;
+
+	for (i = 0; i < maxtokens; i++)
+		tokens[i] = NULL;
+
+	for (i = 0; n < len && i < maxtokens; i++) {
+		while (isspace(*line)) {
+			line++;
+			n++;
+		}
+		p = line;
+		while (*line != '\0' && n < len &&
+		    (!isspace(*line) || i == maxtokens - 1)) {
+			line++;
+			n++;
+		}
+		tokens[i] = strndup(p, line - p);
+		if (tokens[i] == NULL) {
+			err = got_error_from_errno("strndup");
+			goto done;
+		}
+		/* Skip \0 field-delimiter at end of token. */
+		while (line[0] == '\0' && n < len) {
+			line++;
+			n++;
+		}
+	}
+	if (i <= 2)
+		err = got_error(GOT_ERR_NOT_REF);
+done:
+	if (err) {
+		int j;
+		for (j = 0; j < i; j++) {
+			free(tokens[j]);
+			tokens[j] = NULL;
+		}
+	}
+	return err;
+}
+
+static const struct got_error *
+parse_refline(char **id_str, char **refname, char **server_capabilities,
+    char *line, int len)
+{
+	const struct got_error *err = NULL;
+	char *tokens[3];
+
+	err = tokenize_refline(tokens, line, len, nitems(tokens));
+	if (err)
+		return err;
+
+	if (tokens[0])
+		*id_str = tokens[0];
+	if (tokens[1])
+		*refname = tokens[1];
+	if (tokens[2]) {
+		char *p;
+		*server_capabilities = tokens[2];
+		p = strrchr(*server_capabilities, '\n');
+		if (p)
+			*p = '\0';
+	}
+
+	return NULL;
+}
+
+#define GOT_CAPA_AGENT			"agent"
+#define GOT_CAPA_OFS_DELTA		"ofs-delta"
+#define GOT_CAPA_SIDE_BAND_64K		"side-band-64k"
+#define GOT_CAPA_REPORT_STATUS		"report-status"
+#define GOT_CAPA_DELETE_REFS		"delete-refs"
+
+#define GOT_SIDEBAND_PACKFILE_DATA	1
+#define GOT_SIDEBAND_PROGRESS_INFO	2
+#define GOT_SIDEBAND_ERROR_INFO		3
+
+
+struct got_capability {
+	const char *key;
+	const char *value;
+};
+static const struct got_capability got_capabilities[] = {
+	{ GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
+	{ GOT_CAPA_OFS_DELTA, NULL },
+#if 0
+	{ GOT_CAPA_SIDE_BAND_64K, NULL },
+#endif
+	{ GOT_CAPA_REPORT_STATUS, NULL },
+	{ GOT_CAPA_DELETE_REFS, NULL },
+};
+
+static const struct got_error *
+match_capability(char **my_capabilities, const char *capa,
+    const struct got_capability *mycapa)
+{
+	char *equalsign;
+	char *s;
+
+	equalsign = strchr(capa, '=');
+	if (equalsign) {
+		if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
+			return NULL;
+	} else {
+		if (strcmp(capa, mycapa->key) != 0)
+			return NULL;
+	}
+
+	if (asprintf(&s, "%s %s%s%s",
+	    *my_capabilities != NULL ? *my_capabilities : "",
+	    mycapa->key,
+	    mycapa->value != NULL ? "=" : "",
+	    mycapa->value != NULL? mycapa->value : "") == -1)
+		return got_error_from_errno("asprintf");
+
+	free(*my_capabilities);
+	*my_capabilities = s;
+	return NULL;
+}
+
+static const struct got_error *
+match_capabilities(char **my_capabilities, char *server_capabilities)
+{
+	const struct got_error *err = NULL;
+	char *capa;
+	size_t i;
+
+	*my_capabilities = NULL;
+	do {
+		capa = strsep(&server_capabilities, " ");
+		for (i = 0; capa != NULL && i < nitems(got_capabilities); i++) {
+			err = match_capability(my_capabilities,
+			    capa, &got_capabilities[i]);
+			if (err)
+				goto done;
+		}
+	} while (capa);
+
+	if (*my_capabilities == NULL) {
+		*my_capabilities = strdup("");
+		if (*my_capabilities == NULL) {
+			err = got_error_from_errno("strdup");
+			goto done;
+		}
+	}
+
+	/*
+	 * Workaround for github.
+	 *
+	 * Github will accept the pack but fail to update the references
+	 * if we don't have capabilities advertised. Report-status seems
+	 * harmless to add, so we add it.
+	 *
+	 * Github doesn't advertise any capabilities, so we can't check
+	 * for compatibility. We just need to add it blindly.
+	 */
+	if (strstr(*my_capabilities, GOT_CAPA_REPORT_STATUS) == NULL) {
+		char *s;
+		if (asprintf(&s, "%s %s", *my_capabilities,
+		    GOT_CAPA_REPORT_STATUS) == -1) {
+			err = got_error_from_errno("asprintf");
+			goto done;
+		}
+		free(*my_capabilities);
+		*my_capabilities = s;
+	}
+done:
+	if (err) {
+		free(*my_capabilities);
+		*my_capabilities = NULL;
+	}
+	return err;
+}
+
+static const struct got_error *
+send_upload_progress(struct imsgbuf *ibuf, off_t bytes)
+{
+	if (imsg_compose(ibuf, GOT_IMSG_SEND_UPLOAD_PROGRESS, 0, 0, -1,
+	    &bytes, sizeof(bytes)) == -1)
+		return got_error_from_errno(
+		    "imsg_compose SEND_UPLOAD_PROGRESS");
+
+	return got_privsep_flush_imsg(ibuf);
+}
+
+static const struct got_error *
+send_pack_request(struct imsgbuf *ibuf)
+{
+	if (imsg_compose(ibuf, GOT_IMSG_SEND_PACK_REQUEST, 0, 0, -1,
+	    NULL, 0) == -1)
+		return got_error_from_errno("imsg_compose SEND_PACK_REQUEST");
+	return got_privsep_flush_imsg(ibuf);
+}
+
+static const struct got_error *
+send_done(struct imsgbuf *ibuf)
+{
+	if (imsg_compose(ibuf, GOT_IMSG_SEND_DONE, 0, 0, -1, NULL, 0) == -1)
+		return got_error_from_errno("imsg_compose SEND_DONE");
+	return got_privsep_flush_imsg(ibuf);
+}
+
+static const struct got_error *
+recv_packfd(int *packfd, struct imsgbuf *ibuf)
+{
+	const struct got_error *err;
+	struct imsg imsg;
+
+	*packfd = -1;
+
+	err = got_privsep_recv_imsg(&imsg, ibuf, 0);
+	if (err)
+		return err;
+		
+	if (imsg.hdr.type == GOT_IMSG_STOP) {
+		err = got_error(GOT_ERR_CANCELLED);
+		goto done;
+	}
+
+	if (imsg.hdr.type != GOT_IMSG_SEND_PACKFD) {
+		err = got_error(GOT_ERR_PRIVSEP_MSG);
+		goto done;
+	}
+
+	if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
+		err = got_error(GOT_ERR_PRIVSEP_LEN);
+		goto done;
+	}
+
+	*packfd = imsg.fd;
+done:
+	imsg_free(&imsg);
+	return err;
+}
+
+static const struct got_error *
+send_pack_file(int sendfd, int packfd, struct imsgbuf *ibuf)
+{
+	const struct got_error *err;
+	unsigned char buf[8192];
+	ssize_t r, w;
+	off_t wtotal = 0;
+
+	if (lseek(packfd, 0L, SEEK_SET) == -1)
+		return got_error_from_errno("lseek");
+
+	for (;;) {
+		r = read(packfd, buf, sizeof(buf));
+		if (r == -1)
+			return got_error_from_errno("read");
+		if (r == 0)
+			break;
+		w = write(sendfd, buf, r);
+		if (w == -1)
+			return got_error_from_errno("write");
+		if (w != r)
+			return got_error(GOT_ERR_IO);
+		wtotal += w;
+		err = send_upload_progress(ibuf, wtotal);
+		if (err)
+			return err;
+	}
+
+	return NULL;
+}
+
+static const struct got_error *
+send_error(const char *buf, size_t len)
+{
+	static char msg[1024];
+	size_t i;
+
+	for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
+		if (!isprint(buf[i]))
+			return got_error_msg(GOT_ERR_BAD_PACKET,
+			    "non-printable error message received from server");
+		msg[i] = buf[i];
+	}
+	msg[i] = '\0';
+	return got_error_msg(GOT_ERR_SEND_FAILED, msg);
+}
+
+static const struct got_error *
+send_their_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
+    const char *refname)
+{
+	const struct got_error *err = NULL;
+	struct ibuf *wbuf;
+	size_t len, reflen = strlen(refname);
+
+	len = sizeof(struct got_imsg_send_remote_ref) + reflen;
+	if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
+		return got_error(GOT_ERR_NO_SPACE);
+
+	wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REMOTE_REF, 0, 0, len);
+	if (wbuf == NULL)
+		return got_error_from_errno("imsg_create SEND_REMOTE_REF");
+
+	/* Keep in sync with struct got_imsg_send_remote_ref definition! */
+	if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
+		err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
+		ibuf_free(wbuf);
+		return err;
+	}
+	if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
+		err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
+		ibuf_free(wbuf);
+		return err;
+	}
+	if (imsg_add(wbuf, refname, reflen) == -1) {
+		err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
+		ibuf_free(wbuf);
+		return err;
+	}
+
+	wbuf->fd = -1;
+	imsg_close(ibuf, wbuf);
+	return got_privsep_flush_imsg(ibuf);
+}
+
+static const struct got_error *
+send_ref_status(struct imsgbuf *ibuf, const char *refname, int success,
+    struct got_pathlist_head *refs, struct got_pathlist_head *delete_refs)
+
+{
+	const struct got_error *err = NULL;
+	struct ibuf *wbuf;
+	size_t len, reflen = strlen(refname);
+	struct got_pathlist_entry *pe;
+	int ref_valid = 0;
+	char *eol;
+
+	eol = strchr(refname, '\n');
+	if (eol == NULL) {
+		return got_error_msg(GOT_ERR_BAD_PACKET,
+		    "unexpected message from server");
+	}
+	*eol = '\0';
+
+	TAILQ_FOREACH(pe, refs, entry) {
+		if (strcmp(refname, pe->path) == 0) {
+			ref_valid = 1;
+			break;
+		}
+	}
+	if (!ref_valid) {
+		TAILQ_FOREACH(pe, delete_refs, entry) {
+			if (strcmp(refname, pe->path) == 0) {
+				ref_valid = 1;
+				break;
+			}
+		}
+	}
+	if (!ref_valid) {
+		return got_error_msg(GOT_ERR_BAD_PACKET,
+		    "unexpected message from server");
+	}
+
+	len = sizeof(struct got_imsg_send_ref_status) + reflen;
+	if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
+		return got_error(GOT_ERR_NO_SPACE);
+
+	wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF_STATUS,
+	    0, 0, len);
+	if (wbuf == NULL)
+		return got_error_from_errno("imsg_create SEND_REF_STATUS");
+
+	/* Keep in sync with struct got_imsg_send_ref_status definition! */
+	if (imsg_add(wbuf, &success, sizeof(success)) == -1) {
+		err = got_error_from_errno("imsg_add SEND_REF_STATUS");
+		ibuf_free(wbuf);
+		return err;
+	}
+	if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
+		err = got_error_from_errno("imsg_add SEND_REF_STATUS");
+		ibuf_free(wbuf);
+		return err;
+	}
+	if (imsg_add(wbuf, refname, reflen) == -1) {
+		err = got_error_from_errno("imsg_add SEND_REF_STATUS");
+		ibuf_free(wbuf);
+		return err;
+	}
+
+	wbuf->fd = -1;
+	imsg_close(ibuf, wbuf);
+	return got_privsep_flush_imsg(ibuf);
+}
+
+static const struct got_error *
+describe_refchange(int *n, int *sent_my_capabilites,
+    const char *my_capabilities, char *buf, size_t bufsize,
+    const char *refname, const char *old_hashstr, const char *new_hashstr)
+{
+	*n = snprintf(buf, bufsize, "%s %s %s",
+	    old_hashstr, new_hashstr, refname);
+	if (*n >= bufsize)
+		return got_error(GOT_ERR_NO_SPACE);
+
+	/*
+	 * We must announce our capabilities along with the first
+	 * reference. Unfortunately, the protocol requires an embedded
+	 * NUL as a separator between reference name and capabilities,
+	 * which we have to deal with here.
+	 * It also requires a linefeed for terminating packet data.
+	 */
+	if (!*sent_my_capabilites && my_capabilities != NULL) {
+		int m;
+		if (*n >= bufsize - 1)
+			return got_error(GOT_ERR_NO_SPACE);
+		m = snprintf(buf + *n + 1, /* offset after '\0' */
+		    bufsize - (*n + 1), "%s\n", my_capabilities);
+		if (*n + m >= bufsize)
+			return got_error(GOT_ERR_NO_SPACE);
+		*n += m;
+		*sent_my_capabilites = 1;
+	} else {
+		*n = strlcat(buf, "\n", bufsize);
+		if (*n >= bufsize)
+			return got_error(GOT_ERR_NO_SPACE);
+	}
+
+	return NULL;
+}
+
+static const struct got_error *
+send_pack(int fd, struct got_pathlist_head *refs,
+    struct got_pathlist_head *delete_refs, struct imsgbuf *ibuf)
+{
+	const struct got_error *err = NULL;
+	char buf[GOT_FETCH_PKTMAX];
+	unsigned char zero_id[SHA1_DIGEST_LENGTH] = { 0 };
+	char old_hashstr[SHA1_DIGEST_STRING_LENGTH];
+	char new_hashstr[SHA1_DIGEST_STRING_LENGTH];
+	struct got_pathlist_head their_refs;
+	int is_firstpkt = 1;
+	int n, nsent = 0;
+	int packfd = -1;
+	char *id_str = NULL, *refname = NULL;
+	struct got_object_id *id = NULL;
+	char *server_capabilities = NULL, *my_capabilities = NULL;
+	struct got_pathlist_entry *pe;
+	int sent_my_capabilites = 0;
+
+	TAILQ_INIT(&their_refs);
+
+	if (TAILQ_EMPTY(refs) && TAILQ_EMPTY(delete_refs))
+		return got_error(GOT_ERR_SEND_EMPTY);
+
+	while (1) {
+		err = readpkt(&n, fd, buf, sizeof(buf));
+		if (err)
+			goto done;
+		if (n == 0)
+			break;
+		if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
+			err = send_error(&buf[4], n - 4);
+			goto done;
+		}
+		err = parse_refline(&id_str, &refname, &server_capabilities,
+		    buf, n);
+		if (err)
+			goto done;
+		if (is_firstpkt) {
+			if (chattygot && server_capabilities[0] != '\0')
+				fprintf(stderr, "%s: server capabilities: %s\n",
+				    getprogname(), server_capabilities);
+			err = match_capabilities(&my_capabilities,
+			    server_capabilities);
+			if (err)
+				goto done;
+			if (chattygot)
+				fprintf(stderr, "%s: my capabilities:%s\n",
+				    getprogname(), my_capabilities);
+			is_firstpkt = 0;
+		}
+		if (strstr(refname, "^{}")) {
+			if (chattygot) {
+				fprintf(stderr, "%s: ignoring %s\n",
+				    getprogname(), refname);
+			}
+			continue;
+		}
+
+		id = malloc(sizeof(*id));
+		if (id == NULL) {
+			err = got_error_from_errno("malloc");
+			goto done;
+		}
+		if (!got_parse_sha1_digest(id->sha1, id_str)) {
+			err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
+			goto done;
+		}
+		err = send_their_ref(ibuf, id, refname);
+		if (err)
+			goto done;
+
+		err = got_pathlist_append(&their_refs, refname, id);
+		if (chattygot)
+			fprintf(stderr, "%s: remote has %s %s\n",
+			    getprogname(), refname, id_str);
+		free(id_str);
+		id_str = NULL;
+		refname = NULL; /* do not free; owned by their_refs */
+		id = NULL; /* do not free; owned by their_refs */
+	}
+
+	if (!TAILQ_EMPTY(delete_refs)) {
+		if (my_capabilities == NULL ||
+		    strstr(my_capabilities, GOT_CAPA_DELETE_REFS) == NULL) {
+			err = got_error(GOT_ERR_CAPA_DELETE_REFS);
+			goto done;
+		}
+	}
+
+	TAILQ_FOREACH(pe, delete_refs, entry) {
+		const char *refname = pe->path;
+		struct got_pathlist_entry *their_pe;
+		struct got_object_id *their_id = NULL;
+
+		TAILQ_FOREACH(their_pe, &their_refs, entry) {
+			const char *their_refname = their_pe->path;
+			if (got_path_cmp(refname, their_refname,
+			    strlen(refname), strlen(their_refname)) == 0) {
+				their_id = their_pe->data;
+				break;
+			}
+		}
+		if (their_id == NULL) {
+			err = got_error_fmt(GOT_ERR_NOT_REF,
+			    "%s does not exist in remote repository",
+			    refname);
+			goto done;
+		}
+
+		got_sha1_digest_to_str(their_id->sha1, old_hashstr,
+		    sizeof(old_hashstr));
+		got_sha1_digest_to_str(zero_id, new_hashstr,
+		    sizeof(new_hashstr));
+		err = describe_refchange(&n, &sent_my_capabilites,
+		    my_capabilities, buf, sizeof(buf), refname,
+		    old_hashstr, new_hashstr);
+		if (err)
+			goto done;
+		err = writepkt(fd, buf, n);
+		if (err)
+			goto done;
+		if (chattygot) {
+			fprintf(stderr, "%s: deleting %s %s\n",
+			    getprogname(), refname, old_hashstr);
+		}
+		nsent++;
+	}
+
+	TAILQ_FOREACH(pe, refs, entry) {
+		const char *refname = pe->path;
+		struct got_object_id *id = pe->data;
+		struct got_object_id *their_id = NULL;
+		struct got_pathlist_entry *their_pe;
+
+		TAILQ_FOREACH(their_pe, &their_refs, entry) {
+			const char *their_refname = their_pe->path;
+			if (got_path_cmp(refname, their_refname,
+			    strlen(refname), strlen(their_refname)) == 0) {
+				their_id = their_pe->data;
+				break;
+			}
+		}
+		if (their_id) {
+			if (got_object_id_cmp(id, their_id) == 0) {
+				if (chattygot) {
+					fprintf(stderr,
+					    "%s: no change for %s\n",
+					    getprogname(), refname);
+				}
+				continue;
+			}
+			got_sha1_digest_to_str(their_id->sha1, old_hashstr,
+			    sizeof(old_hashstr));
+		} else {
+			got_sha1_digest_to_str(zero_id, old_hashstr,
+			    sizeof(old_hashstr));
+		}
+		got_sha1_digest_to_str(id->sha1, new_hashstr,
+		    sizeof(new_hashstr));
+		err = describe_refchange(&n, &sent_my_capabilites,
+		    my_capabilities, buf, sizeof(buf), refname,
+		    old_hashstr, new_hashstr);
+		if (err)
+			goto done;
+		err = writepkt(fd, buf, n);
+		if (err)
+			goto done;
+		if (chattygot) {
+			if (their_id) {
+				fprintf(stderr, "%s: updating %s %s -> %s\n",
+				    getprogname(), refname, old_hashstr,
+				    new_hashstr);
+			} else {
+				fprintf(stderr, "%s: creating %s %s\n",
+				    getprogname(), refname, new_hashstr);
+			}
+		}
+		nsent++;
+	}
+	err = flushpkt(fd);
+	if (err)
+		goto done;
+
+	err = send_pack_request(ibuf);
+	if (err)
+		goto done;
+
+	err = recv_packfd(&packfd, ibuf);
+	if (err)
+		goto done;
+
+	err = send_pack_file(fd, packfd, ibuf);
+	if (err)
+		goto done;
+
+	err = readpkt(&n, fd, buf, sizeof(buf));
+	if (err)
+		goto done;
+	if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
+		err = send_error(&buf[4], n - 4);
+		goto done;
+	} else if (n < 10 || strncmp(buf, "unpack ok\n", 10) != 0) {
+		err = got_error_msg(GOT_ERR_BAD_PACKET,
+		    "unexpected message from server");
+		goto done;
+	}
+
+	while (nsent > 0) {
+		err = readpkt(&n, fd, buf, sizeof(buf));
+		if (err)
+			goto done;
+		if (n < 3) {
+			err = got_error_msg(GOT_ERR_BAD_PACKET,
+			    "unexpected message from server");
+			goto done;
+		} else if (strncmp(buf, "ok ", 3) == 0) {
+			err = send_ref_status(ibuf, buf + 3, 1,
+			   refs, delete_refs);
+			if (err)
+				goto done;
+		} else if (strncmp(buf, "ng ", 3) == 0) {
+			err = send_ref_status(ibuf, buf + 3, 0,
+			    refs, delete_refs);
+			if (err)
+				goto done;
+		} else {
+			err = got_error_msg(GOT_ERR_BAD_PACKET,
+			    "unexpected message from server");
+			goto done;
+		}
+		nsent--;
+	}
+
+	err = send_done(ibuf);
+done:
+	TAILQ_FOREACH(pe, &their_refs, entry) {
+		free((void *)pe->path);
+		free(pe->data);
+	}
+	got_pathlist_free(&their_refs);
+	free(id_str);
+	free(id);
+	free(refname);
+	free(server_capabilities);
+	return err;
+}
+
+int
+main(int argc, char **argv)
+{
+	const struct got_error *err = NULL;
+	int sendfd, i;
+	struct imsgbuf ibuf;
+	struct imsg imsg;
+	struct got_pathlist_head refs;
+	struct got_pathlist_head delete_refs;
+	struct got_pathlist_entry *pe;
+	struct got_imsg_send_request send_req;
+	struct got_imsg_send_ref href;
+	size_t datalen;
+#if 0
+	static int attached;
+	while (!attached)
+		sleep (1);
+#endif
+
+	TAILQ_INIT(&refs);
+	TAILQ_INIT(&delete_refs);
+
+	imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
+#ifndef PROFILE
+	/* revoke access to most system calls */
+	if (pledge("stdio recvfd", NULL) == -1) {
+		err = got_error_from_errno("pledge");
+		got_privsep_send_error(&ibuf, err);
+		return 1;
+	}
+#endif
+	if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
+		if (err->code == GOT_ERR_PRIVSEP_PIPE)
+			err = NULL;
+		goto done;
+	}
+	if (imsg.hdr.type == GOT_IMSG_STOP)
+		goto done;
+	if (imsg.hdr.type != GOT_IMSG_SEND_REQUEST) {
+		err = got_error(GOT_ERR_PRIVSEP_MSG);
+		goto done;
+	}
+	datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
+	if (datalen < sizeof(send_req)) {
+		err = got_error(GOT_ERR_PRIVSEP_LEN);
+		goto done;
+	}
+	memcpy(&send_req, imsg.data, sizeof(send_req));
+	sendfd = imsg.fd;
+	imsg_free(&imsg);
+
+	if (send_req.verbosity > 0)
+		chattygot += send_req.verbosity;
+
+	for (i = 0; i < send_req.nrefs; i++) {
+		struct got_object_id *id;
+		char *refname;
+
+		if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
+			if (err->code == GOT_ERR_PRIVSEP_PIPE)
+				err = NULL;
+			goto done;
+		}
+		if (imsg.hdr.type == GOT_IMSG_STOP)
+			goto done;
+		if (imsg.hdr.type != GOT_IMSG_SEND_REF) {
+			err = got_error(GOT_ERR_PRIVSEP_MSG);
+			goto done;
+		}
+		datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
+		if (datalen < sizeof(href)) {
+			err = got_error(GOT_ERR_PRIVSEP_LEN);
+			goto done;
+		}
+		memcpy(&href, imsg.data, sizeof(href));
+		if (datalen - sizeof(href) < href.name_len) {
+			err = got_error(GOT_ERR_PRIVSEP_LEN);
+			goto done;
+		}
+		refname = malloc(href.name_len + 1);
+		if (refname == NULL) {
+			err = got_error_from_errno("malloc");
+			goto done;
+		}
+		memcpy(refname, imsg.data + sizeof(href), href.name_len);
+		refname[href.name_len] = '\0';
+
+		/*
+		 * Prevent sending of references that won't make any
+		 * sense outside the local repository's context.
+		 */
+		if (strncmp(refname, "refs/got/", 9) == 0 ||
+		    strncmp(refname, "refs/remotes/", 13) == 0) {
+			err = got_error_fmt(GOT_ERR_SEND_BAD_REF,
+			    "%s", refname);
+			goto done;
+		}
+
+		id = malloc(sizeof(*id));
+		if (id == NULL) {
+			free(refname);
+			err = got_error_from_errno("malloc");
+			goto done;
+		}
+		memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
+		if (href.delete)
+			err = got_pathlist_append(&delete_refs, refname, id);
+		else
+			err = got_pathlist_append(&refs, refname, id);
+		if (err) {
+			free(refname);
+			free(id);
+			goto done;
+		}
+
+		imsg_free(&imsg);
+	}
+
+	err = send_pack(sendfd, &refs, &delete_refs, &ibuf);
+done:
+	TAILQ_FOREACH(pe, &refs, entry) {
+		free((char *)pe->path);
+		free(pe->data);
+	}
+	got_pathlist_free(&refs);
+	TAILQ_FOREACH(pe, &delete_refs, entry) {
+		free((char *)pe->path);
+		free(pe->data);
+	}
+	got_pathlist_free(&delete_refs);
+	if (sendfd != -1 && close(sendfd) == -1 && err == NULL)
+		err = got_error_from_errno("close");
+	if (err != NULL && err->code != GOT_ERR_CANCELLED)  {
+		fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
+		got_privsep_send_error(&ibuf, err);
+	}
+
+	exit(0);
+}
diff --git a/regress/cmdline/Makefile b/regress/cmdline/Makefile
index ef0efe1..68314f1 100644
--- a/regress/cmdline/Makefile
+++ b/regress/cmdline/Makefile
@@ -77,6 +77,9 @@ clone:
 fetch:
 	./fetch.sh -q -r "$(GOT_TEST_ROOT)"
 
+send:
+	./send.sh -q -r "$(GOT_TEST_ROOT)"
+
 tree:
 	./tree.sh -q -r "$(GOT_TEST_ROOT)"
 
diff --git a/regress/cmdline/send.sh b/regress/cmdline/send.sh
new file mode 100755
index 0000000..9ac2cfc
--- /dev/null
+++ b/regress/cmdline/send.sh
@@ -0,0 +1,1042 @@
+#!/bin/sh
+#
+# Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
+#
+# Permission to use, copy, modify, and distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+. ./common.sh
+
+test_send_basic() {
+	local testroot=`test_init send_basic`
+	local testurl=ssh://127.0.0.1/$testroot
+	local commit_id=`git_show_head $testroot/repo`
+
+	got clone -q $testurl/repo $testroot/repo-clone
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got clone command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	cat > $testroot/repo/.git/got.conf <<EOF
+remote "origin" {
+	protocol ssh
+	server 127.0.0.1
+	repository "$testroot/repo-clone"
+}
+EOF
+	got tag -r $testroot/repo -m '1.0' 1.0 >/dev/null
+	tag_id=`got ref -r $testroot/repo -l | grep "^refs/tags/1.0" \
+		| tr -d ' ' | cut -d: -f2`
+
+	echo "modified alpha" > $testroot/repo/alpha
+	git_commit $testroot/repo -m "modified alpha"
+	local commit_id2=`git_show_head $testroot/repo`
+
+	got send -q -r $testroot/repo > $testroot/stdout 2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got send command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	
+	echo -n > $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got ref -l -r $testroot/repo > $testroot/stdout
+	if [ "$ret" != "0" ]; then
+		echo "got ref command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id2" >> $testroot/stdout.expected
+	echo "refs/remotes/origin/master: $commit_id2" \
+		>> $testroot/stdout.expected
+	echo "refs/tags/1.0: $tag_id" >> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got ref -l -r $testroot/repo-clone > $testroot/stdout
+	if [ "$ret" != "0" ]; then
+		echo "got ref command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id2" >> $testroot/stdout.expected
+	echo "refs/remotes/origin/HEAD: refs/remotes/origin/master" \
+		>> $testroot/stdout.expected
+	echo "refs/remotes/origin/master: $commit_id" \
+		>> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got send -r $testroot/repo > $testroot/stdout 2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got send command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	
+	echo 'Connecting to "origin" 127.0.0.1' > $testroot/stdout.expected
+	echo "Already up-to-date" >> $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+	fi
+	test_done "$testroot" "$ret"
+}
+
+test_send_rebase_required() {
+	local testroot=`test_init send_rebase_required`
+	local testurl=ssh://127.0.0.1/$testroot
+	local commit_id=`git_show_head $testroot/repo`
+
+	got clone -q $testurl/repo $testroot/repo-clone
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got clone command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	cat > $testroot/repo/.git/got.conf <<EOF
+remote "origin" {
+	protocol ssh
+	server 127.0.0.1
+	repository "$testroot/repo-clone"
+}
+EOF
+	echo "modified alpha" > $testroot/repo/alpha
+	git_commit $testroot/repo -m "modified alpha"
+	local commit_id2=`git_show_head $testroot/repo`
+
+	got checkout $testroot/repo-clone $testroot/wt-clone >/dev/null
+	echo "modified alpha, too" > $testroot/wt-clone/alpha
+	(cd $testroot/wt-clone && got commit -m 'change alpha' >/dev/null)
+
+	got send -q -r $testroot/repo > $testroot/stdout 2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "got send command succeeded unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	
+	echo -n > $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "got: refs/heads/master: fetch and rebase required" \
+		> $testroot/stderr.expected
+	cmp -s $testroot/stderr $testroot/stderr.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+	fi
+	test_done "$testroot" "$ret"
+}
+
+test_send_rebase_required_overwrite() {
+	local testroot=`test_init send_rebase_required_overwrite`
+	local testurl=ssh://127.0.0.1/$testroot
+	local commit_id=`git_show_head $testroot/repo`
+
+	got clone -q $testurl/repo $testroot/repo-clone
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got clone command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	cat > $testroot/repo/.git/got.conf <<EOF
+remote "foobar" {
+	protocol ssh
+	server 127.0.0.1
+	repository "$testroot/repo-clone"
+}
+EOF
+	echo "modified alpha" > $testroot/repo/alpha
+	git_commit $testroot/repo -m "modified alpha"
+	local commit_id2=`git_show_head $testroot/repo`
+
+	got checkout $testroot/repo-clone $testroot/wt-clone >/dev/null
+	echo "modified alpha, too" > $testroot/wt-clone/alpha
+	(cd $testroot/wt-clone && got commit -m 'change alpha' >/dev/null)
+	local commit_id3=`git_show_head $testroot/repo-clone`
+
+	# non-default remote requires an explicit argument
+	got send -q -r $testroot/repo -f > $testroot/stdout \
+		2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "got send command succeeded unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	echo "got: origin: remote repository not found" \
+		> $testroot/stderr.expected
+	cmp -s $testroot/stderr $testroot/stderr.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got send -q -r $testroot/repo -f foobar > $testroot/stdout \
+		2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got send command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	
+	echo -n > $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got ref -l -r $testroot/repo > $testroot/stdout
+	if [ "$ret" != "0" ]; then
+		echo "got ref command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id2" >> $testroot/stdout.expected
+	echo "refs/remotes/foobar/master: $commit_id2" \
+		>> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got ref -l -r $testroot/repo-clone > $testroot/stdout
+	if [ "$ret" != "0" ]; then
+		echo "got ref command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	wt_uuid=`(cd $testroot/wt-clone && got info | grep 'UUID:' | \
+		cut -d ':' -f 2 | tr -d ' ')`
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/got/worktree/base-$wt_uuid: $commit_id3" \
+		>> $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id2" >> $testroot/stdout.expected
+	echo "refs/remotes/origin/HEAD: refs/remotes/origin/master" \
+		>> $testroot/stdout.expected
+	echo "refs/remotes/origin/master: $commit_id" \
+		>> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+	fi
+	test_done "$testroot" "$ret"
+}
+
+test_send_delete() {
+	local testroot=`test_init send_delete`
+	local testurl=ssh://127.0.0.1/$testroot
+	local commit_id=`git_show_head $testroot/repo`
+
+	# branch1 exists in both repositories
+	got branch -r $testroot/repo branch1
+
+	got clone -a -q $testurl/repo $testroot/repo-clone
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got clone command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	cat > $testroot/repo/.git/got.conf <<EOF
+remote "origin" {
+	protocol ssh
+	server 127.0.0.1
+	repository "$testroot/repo-clone"
+}
+EOF
+	# branch2 exists only in the remote repository
+	got branch -r $testroot/repo-clone branch2
+
+	got ref -l -r $testroot/repo-clone > $testroot/stdout
+	if [ "$ret" != "0" ]; then
+		echo "got ref command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
+	echo "refs/heads/branch2: $commit_id" >> $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+
+	# Sending changes for a branch and deleting it at the same
+	# time is not allowed.
+	got send -q -r $testroot/repo -d branch1 -b branch1 \
+		> $testroot/stdout 2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "got send command succeeded unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	echo -n "got: changes on refs/heads/branch1 will be sent to server" \
+		> $testroot/stderr.expected
+	echo ": reference cannot be deleted" >> $testroot/stderr.expected
+	cmp -s $testroot/stderr $testroot/stderr.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got send -q -r $testroot/repo -d refs/heads/branch1 origin \
+		> $testroot/stdout 2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got send command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got send -q -r $testroot/repo -d refs/heads/branch2 origin \
+		> $testroot/stdout 2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got send command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	# branchX exists in neither repository
+	got send -q -r $testroot/repo -d refs/heads/branchX origin \
+		> $testroot/stdout 2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "got send command succeeded unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	echo -n "got-send-pack: refs/heads/branchX does not exist in remote " \
+		> $testroot/stderr.expected
+	echo "repository: no such reference found" >> $testroot/stderr.expected
+	echo "got: no such reference found" >> $testroot/stderr.expected
+	cmp -s $testroot/stderr $testroot/stderr.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	# References outside of refs/heads/ cannot be deleted with 'got send'.
+	got send -q -r $testroot/repo -d refs/tags/1.0 origin \
+		> $testroot/stdout 2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "got send command succeeded unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	echo -n "got-send-pack: refs/heads/refs/tags/1.0 does not exist " \
+		> $testroot/stderr.expected
+	echo "in remote repository: no such reference found" \
+		>> $testroot/stderr.expected
+	echo "got: no such reference found" >> $testroot/stderr.expected
+	cmp -s $testroot/stderr $testroot/stderr.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	
+	got ref -l -r $testroot/repo > $testroot/stdout
+	if [ "$ret" != "0" ]; then
+		echo "got ref command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got ref -l -r $testroot/repo-clone > $testroot/stdout
+	if [ "$ret" != "0" ]; then
+		echo "got ref command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+	echo "refs/remotes/origin/HEAD: refs/remotes/origin/master" \
+		>> $testroot/stdout.expected
+	echo "refs/remotes/origin/branch1: $commit_id" \
+		>> $testroot/stdout.expected
+	echo "refs/remotes/origin/master: $commit_id" \
+		>> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+	fi
+	test_done "$testroot" "$ret"
+}
+
+test_send_clone_and_send() {
+	local testroot=`test_init send_clone_and_send`
+	local testurl=ssh://127.0.0.1/$testroot
+	local commit_id=`git_show_head $testroot/repo`
+
+	(cd $testroot/repo && git config receive.denyCurrentBranch ignore)
+
+	got clone -q $testurl/repo $testroot/repo-clone
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got clone command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got checkout $testroot/repo-clone $testroot/wt >/dev/null
+	echo "modified alpha" > $testroot/wt/alpha
+	(cd $testroot/wt && got commit -m "modified alpha" >/dev/null)
+	local commit_id2=`git_show_head $testroot/repo-clone`
+
+	(cd $testroot/wt && got send -q > $testroot/stdout 2> $testroot/stderr)
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got send command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	
+	echo -n > $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got ref -l -r $testroot/repo > $testroot/stdout
+	if [ "$ret" != "0" ]; then
+		echo "got ref command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id2" >> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got ref -l -r $testroot/repo-clone > $testroot/stdout
+	if [ "$ret" != "0" ]; then
+		echo "got ref command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	wt_uuid=`(cd $testroot/wt && got info | grep 'UUID:' | \
+		cut -d ':' -f 2 | tr -d ' ')`
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/got/worktree/base-$wt_uuid: $commit_id2" \
+		>> $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id2" >> $testroot/stdout.expected
+	echo "refs/remotes/origin/HEAD: refs/remotes/origin/master" \
+		>> $testroot/stdout.expected
+	echo "refs/remotes/origin/master: $commit_id2" \
+		>> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+	fi
+	test_done "$testroot" "$ret"
+}
+
+test_send_tags() {
+	local testroot=`test_init send_tags`
+	local testurl=ssh://127.0.0.1/$testroot
+	local commit_id=`git_show_head $testroot/repo`
+
+	got clone -q $testurl/repo $testroot/repo-clone
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got clone command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	cat > $testroot/repo/.git/got.conf <<EOF
+remote "origin" {
+	protocol ssh
+	server 127.0.0.1
+	repository "$testroot/repo-clone"
+}
+EOF
+	got tag -r $testroot/repo -m '1.0' 1.0 >/dev/null
+	tag_id=`got ref -r $testroot/repo -l | grep "^refs/tags/1.0" \
+		| tr -d ' ' | cut -d: -f2`
+
+	echo "modified alpha" > $testroot/repo/alpha
+	git_commit $testroot/repo -m "modified alpha"
+	local commit_id2=`git_show_head $testroot/repo`
+
+	got tag -r $testroot/repo -m '2.0' 2.0 >/dev/null
+	tag_id2=`got ref -r $testroot/repo -l | grep "^refs/tags/2.0" \
+		| tr -d ' ' | cut -d: -f2`
+
+	got send -q -r $testroot/repo -T > $testroot/stdout 2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got send command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	
+	echo -n > $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got ref -l -r $testroot/repo > $testroot/stdout
+	if [ "$ret" != "0" ]; then
+		echo "got ref command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id2" >> $testroot/stdout.expected
+	echo "refs/remotes/origin/master: $commit_id2" \
+		>> $testroot/stdout.expected
+	echo "refs/tags/1.0: $tag_id" >> $testroot/stdout.expected
+	echo "refs/tags/2.0: $tag_id2" >> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got ref -l -r $testroot/repo-clone > $testroot/stdout
+	if [ "$ret" != "0" ]; then
+		echo "got ref command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id2" >> $testroot/stdout.expected
+	echo "refs/remotes/origin/HEAD: refs/remotes/origin/master" \
+		>> $testroot/stdout.expected
+	echo "refs/remotes/origin/master: $commit_id" \
+		>> $testroot/stdout.expected
+	echo "refs/tags/1.0: $tag_id" >> $testroot/stdout.expected
+	echo "refs/tags/2.0: $tag_id2" >> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got tag -l -r $testroot/repo-clone | grep ^tag | sort > $testroot/stdout
+	echo "tag 1.0 $tag_id" > $testroot/stdout.expected
+	echo "tag 2.0 $tag_id2" >> $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	# Overwriting an existing tag 'got send -f'.
+	got ref -r $testroot/repo -d refs/tags/1.0 >/dev/null
+	got tag -r $testroot/repo -m '1.0' 1.0 >/dev/null
+	tag_id3=`got ref -r $testroot/repo -l | grep "^refs/tags/1.0" \
+		| tr -d ' ' | cut -d: -f2`
+
+	got send -q -r $testroot/repo -t 1.0 > $testroot/stdout \
+		2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "got send command succeeded unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "got: refs/tags/1.0: tag already exists on server" \
+		> $testroot/stderr.expected
+	cmp -s $testroot/stderr $testroot/stderr.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	# attempting the same with -T should fail, too
+	got send -q -r $testroot/repo -T > $testroot/stdout \
+		2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "got send command succeeded unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "got: refs/tags/1.0: tag already exists on server" \
+		> $testroot/stderr.expected
+	cmp -s $testroot/stderr $testroot/stderr.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got tag -l -r $testroot/repo-clone | grep ^tag | sort > $testroot/stdout
+	echo "tag 1.0 $tag_id" > $testroot/stdout.expected
+	echo "tag 2.0 $tag_id2" >> $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	
+	# overwrite the 1.0 tag only
+	got send -q -r $testroot/repo -t 1.0 -f > $testroot/stdout \
+		2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got send command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	
+	got tag -l -r $testroot/repo-clone | grep ^tag | sort > $testroot/stdout
+	echo "tag 1.0 $tag_id3" > $testroot/stdout.expected
+	echo "tag 2.0 $tag_id2" >> $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+	fi
+	test_done "$testroot" "$ret"
+}
+
+test_send_new_branch() {
+	local testroot=`test_init send_new_branch`
+	local testurl=ssh://127.0.0.1/$testroot
+	local commit_id=`git_show_head $testroot/repo`
+
+	(cd $testroot/repo && git config receive.denyCurrentBranch ignore)
+
+	got clone -q $testurl/repo $testroot/repo-clone
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got clone command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got branch -r $testroot/repo-clone foo >/dev/null
+	got checkout -b foo $testroot/repo-clone $testroot/wt >/dev/null
+	echo "modified alpha" > $testroot/wt/alpha
+	(cd $testroot/wt && got commit -m "modified alpha" >/dev/null)
+	local commit_id2=`git_show_branch_head $testroot/repo-clone foo`
+
+	(cd $testroot/wt && got send -q > $testroot/stdout 2> $testroot/stderr)
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got send command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	
+	echo -n > $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got ref -l -r $testroot/repo > $testroot/stdout
+	if [ "$ret" != "0" ]; then
+		echo "got ref command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/heads/foo: $commit_id2" >> $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got ref -l -r $testroot/repo-clone > $testroot/stdout
+	if [ "$ret" != "0" ]; then
+		echo "got ref command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	wt_uuid=`(cd $testroot/wt && got info | grep 'UUID:' | \
+		cut -d ':' -f 2 | tr -d ' ')`
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/got/worktree/base-$wt_uuid: $commit_id2" \
+		>> $testroot/stdout.expected
+	echo "refs/heads/foo: $commit_id2" >> $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+	echo "refs/remotes/origin/HEAD: refs/remotes/origin/master" \
+		>> $testroot/stdout.expected
+	echo "refs/remotes/origin/foo: $commit_id2" \
+		>> $testroot/stdout.expected
+	echo "refs/remotes/origin/master: $commit_id" \
+		>> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+	fi
+	test_done "$testroot" "$ret"
+}
+
+test_send_all_branches() {
+	local testroot=`test_init send_all_branches`
+	local testurl=ssh://127.0.0.1/$testroot
+	local commit_id=`git_show_head $testroot/repo`
+
+	(cd $testroot/repo && git config receive.denyCurrentBranch ignore)
+
+	got clone -q $testurl/repo $testroot/repo-clone
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got clone command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got checkout $testroot/repo-clone $testroot/wt >/dev/null
+	echo "modified alpha" > $testroot/wt/alpha
+	(cd $testroot/wt && got commit -m "modified alpha" >/dev/null)
+	local commit_id2=`git_show_head $testroot/repo-clone`
+
+	got branch -r $testroot/repo-clone foo >/dev/null
+	(cd $testroot/wt && got update -b foo >/dev/null)
+	echo "modified beta on new branch foo" > $testroot/wt/beta
+	(cd $testroot/wt && got commit -m "modified beta" >/dev/null)
+	local commit_id3=`git_show_branch_head $testroot/repo-clone foo`
+
+	got branch -r $testroot/repo-clone bar >/dev/null
+	(cd $testroot/wt && got update -b bar >/dev/null)
+	echo "modified beta again on new branch bar" > $testroot/wt/beta
+	(cd $testroot/wt && got commit -m "modified beta" >/dev/null)
+	local commit_id4=`git_show_branch_head $testroot/repo-clone bar`
+
+	got ref -l -r $testroot/repo-clone > $testroot/stdout
+	if [ "$ret" != "0" ]; then
+		echo "got ref command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/heads/bar: $commit_id4" >> $testroot/stdout.expected
+	echo "refs/heads/foo: $commit_id3" >> $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id2" >> $testroot/stdout.expected
+
+	got send -a -q -r $testroot/repo-clone -b master > $testroot/stdout \
+		2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "got send command succeeded unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	echo "got: -a and -b options are mutually exclusive" \
+		> $testroot/stderr.expected
+	cmp -s $testroot/stderr $testroot/stderr.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got send -a -q -r $testroot/repo-clone > $testroot/stdout \
+		2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got send command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo -n > $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got ref -l -r $testroot/repo > $testroot/stdout
+	if [ "$ret" != "0" ]; then
+		echo "got ref command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/heads/bar: $commit_id4" >> $testroot/stdout.expected
+	echo "refs/heads/foo: $commit_id3" >> $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id2" >> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got ref -l -r $testroot/repo-clone > $testroot/stdout
+	if [ "$ret" != "0" ]; then
+		echo "got ref command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	wt_uuid=`(cd $testroot/wt && got info | grep 'UUID:' | \
+		cut -d ':' -f 2 | tr -d ' ')`
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/got/worktree/base-$wt_uuid: $commit_id4" \
+		>> $testroot/stdout.expected
+	echo "refs/heads/bar: $commit_id4" >> $testroot/stdout.expected
+	echo "refs/heads/foo: $commit_id3" >> $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id2" >> $testroot/stdout.expected
+	echo "refs/remotes/origin/HEAD: refs/remotes/origin/master" \
+		>> $testroot/stdout.expected
+	echo "refs/remotes/origin/bar: $commit_id4" \
+		>> $testroot/stdout.expected
+	echo "refs/remotes/origin/foo: $commit_id3" \
+		>> $testroot/stdout.expected
+	echo "refs/remotes/origin/master: $commit_id2" \
+		>> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+	fi
+	test_done "$testroot" "$ret"
+}
+
+test_send_to_empty_repo() {
+	local testroot=`test_init send_to_empty_repo`
+	local testurl=ssh://127.0.0.1/$testroot
+	local commit_id=`git_show_head $testroot/repo`
+
+	got init $testroot/repo2
+
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got clone command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	cat > $testroot/repo/.git/got.conf <<EOF
+remote "origin" {
+	protocol ssh
+	server 127.0.0.1
+	repository "$testroot/repo2"
+}
+EOF
+	echo "modified alpha" > $testroot/repo/alpha
+	git_commit $testroot/repo -m "modified alpha"
+	local commit_id2=`git_show_head $testroot/repo`
+
+	got send -q -r $testroot/repo > $testroot/stdout 2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got send command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	
+	echo -n > $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	# XXX Workaround: We cannot give the target for HEAD to 'got init'
+	got ref -r $testroot/repo2 -s refs/heads/master HEAD
+
+	got ref -l -r $testroot/repo > $testroot/stdout
+	if [ "$ret" != "0" ]; then
+		echo "got ref command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id2" >> $testroot/stdout.expected
+	echo "refs/remotes/origin/master: $commit_id2" \
+		>> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got ref -l -r $testroot/repo2 > $testroot/stdout
+	if [ "$ret" != "0" ]; then
+		echo "got ref command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id2" >> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got send -r $testroot/repo > $testroot/stdout 2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got send command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	
+	echo 'Connecting to "origin" 127.0.0.1' > $testroot/stdout.expected
+	echo "Already up-to-date" >> $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+	fi
+	test_done "$testroot" "$ret"
+}
+
+
+test_parseargs "$@"
+run_test test_send_basic
+run_test test_send_rebase_required
+run_test test_send_rebase_required_overwrite
+run_test test_send_delete
+run_test test_send_clone_and_send
+run_test test_send_tags
+run_test test_send_new_branch
+run_test test_send_all_branches
+run_test test_send_to_empty_repo