git p4: add submit --dry-run option
A new option, "git p4 submit --dry-run" can be used to verify what commits and labels would be moved into p4. Signed-off-by: Pete Wyckoff <pw@padd.com> Acked-by: Luke Diamand <luke@diamand.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Pete Wyckoff committed
Sep 9, 2012 at 16:16 UTC
ef739f0829f866b7e70e072a6744dc9cd1e12822
3 files changed
+75
-13
Documentation/git-p4.txt
+4
@@ -269,6 +269,10 @@ These options can be used to modify 'git p4 submit' behavior.
269
Export tags from git as p4 labels. Tags found in git are applied
270
to the perforce working directory.
271
272
+--dry-run, -n::
273
+ Show just what commits would be submitted to p4; do not change
274
+ state in git or p4.
275
+
276
Rebase options
277
~~~~~~~~~~~~~~
278
These options can be used to modify 'git p4 rebase' behavior.
git-p4.py
+30
-13
@@ -853,12 +853,14 @@ class P4Submit(Command, P4UserMap):
853
# preserve the user, requires relevant p4 permissions
854
optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"),
855
optparse.make_option("--export-labels", dest="exportLabels", action="store_true"),
856
+ optparse.make_option("--dry-run", "-n", dest="dry_run", action="store_true"),
857
]
858
self.description = "Submit changes from git to the perforce depot."
859
self.usage += " [name of git branch to submit into perforce depot]"
860
self.origin = ""
861
self.detectRenames = False
862
self.preserveUser = gitConfig("git-p4.preserveUser").lower() == "true"
863
+ self.dry_run = False
864
self.isWindows = (platform.system() == "Windows")
865
self.exportLabels = False
866
self.p4HasMoveCommand = p4_has_command("move")
@@ -1366,14 +1368,17 @@ class P4Submit(Command, P4UserMap):
1368
for mapping in clientSpec.mappings:
1369
labelTemplate += "\t%s\n" % mapping.depot_side.path
1370
1369
- p4_write_pipe(["label", "-i"], labelTemplate)
1371
+ if self.dry_run:
1372
+ print "Would create p4 label %s for tag" % name
1373
+ else:
1374
+ p4_write_pipe(["label", "-i"], labelTemplate)
1375
1371
- # Use the label
1372
- p4_system(["tag", "-l", name] +
1373
- ["%s@%s" % (mapping.depot_side.path, changelist) for mapping in clientSpec.mappings])
1376
+ # Use the label
1377
+ p4_system(["tag", "-l", name] +
1378
+ ["%s@%s" % (mapping.depot_side.path, changelist) for mapping in clientSpec.mappings])
1379
1375
- if verbose:
1376
- print "created p4 label for tag %s" % name
1380
+ if verbose:
1381
+ print "created p4 label for tag %s" % name
1382
1383
def run(self, args):
1384
if len(args) == 0:
@@ -1432,12 +1437,15 @@ class P4Submit(Command, P4UserMap):
1437
os.makedirs(self.clientPath)
1438
1439
chdir(self.clientPath)
1435
- print "Synchronizing p4 checkout..."
1436
- if new_client_dir:
1437
- # old one was destroyed, and maybe nobody told p4
1438
- p4_sync("...", "-f")
1440
+ if self.dry_run:
1441
+ print "Would synchronize p4 checkout in %s" % self.clientPath
1442
else:
1440
- p4_sync("...")
1443
+ print "Synchronizing p4 checkout..."
1444
+ if new_client_dir:
1445
+ # old one was destroyed, and maybe nobody told p4
1446
+ p4_sync("...", "-f")
1447
+ else:
1448
+ p4_sync("...")
1449
self.check()
1450
1451
commits = []
@@ -1488,10 +1496,17 @@ class P4Submit(Command, P4UserMap):
1496
# Apply the commits, one at a time. On failure, ask if should
1497
# continue to try the rest of the patches, or quit.
1498
#
1499
+ if self.dry_run:
1500
+ print "Would apply"
1501
applied = []
1502
last = len(commits) - 1
1503
for i, commit in enumerate(commits):
1494
- ok = self.applyCommit(commit)
1504
+ if self.dry_run:
1505
+ print " ", read_pipe(["git", "show", "-s",
1506
+ "--format=format:%h %s", commit])
1507
+ ok = True
1508
+ else:
1509
+ ok = self.applyCommit(commit)
1510
if ok:
1511
applied.append(commit)
1512
else:
@@ -1515,7 +1530,9 @@ class P4Submit(Command, P4UserMap):
1530
1531
chdir(self.oldWorkingDirectory)
1532
1518
- if len(commits) == len(applied):
1533
+ if self.dry_run:
1534
+ pass
1535
+ elif len(commits) == len(applied):
1536
print "All commits applied!"
1537
1538
sync = P4Sync()
t/t9807-git-p4-submit.sh
+41
@@ -54,6 +54,47 @@ test_expect_success 'submit --origin' '
54
)
55
'
56
57
+test_expect_success 'submit --dry-run' '
58
+ test_when_finished cleanup_git &&
59
+ git p4 clone --dest="$git" //depot &&
60
+ (
61
+ cd "$git" &&
62
+ test_commit "dry-run1" &&
63
+ test_commit "dry-run2" &&
64
+ git p4 submit --dry-run >out &&
65
+ test_i18ngrep "Would apply" out
66
+ ) &&
67
+ (
68
+ cd "$cli" &&
69
+ test_path_is_missing "dry-run1.t" &&
70
+ test_path_is_missing "dry-run2.t"
71
+ )
72
+'
73
+
74
+test_expect_success 'submit --dry-run --export-labels' '
75
+ test_when_finished cleanup_git &&
76
+ git p4 clone --dest="$git" //depot &&
77
+ (
78
+ cd "$git" &&
79
+ echo dry-run1 >dry-run1 &&
80
+ git add dry-run1 &&
81
+ git commit -m "dry-run1" dry-run1 &&
82
+ git config git-p4.skipSubmitEdit true &&
83
+ git p4 submit &&
84
+ echo dry-run2 >dry-run2 &&
85
+ git add dry-run2 &&
86
+ git commit -m "dry-run2" dry-run2 &&
87
+ git tag -m "dry-run-tag1" dry-run-tag1 HEAD^ &&
88
+ git p4 submit --dry-run --export-labels >out &&
89
+ test_i18ngrep "Would create p4 label" out
90
+ ) &&
91
+ (
92
+ cd "$cli" &&
93
+ test_path_is_file "dry-run1" &&
94
+ test_path_is_missing "dry-run2"
95
+ )
96
+'
97
+
98
test_expect_success 'submit with allowSubmit' '
99
test_when_finished cleanup_git &&
100
git p4 clone --dest="$git" //depot &&