Skip to content

Commit ef739f0

Browse files
Pete Wyckoffgitster
authored andcommitted
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 <[email protected]> Acked-by: Luke Diamand <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b0ccc80 commit ef739f0

File tree

3 files changed

+75
-13
lines changed

3 files changed

+75
-13
lines changed

Documentation/git-p4.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ These options can be used to modify 'git p4 submit' behavior.
269269
Export tags from git as p4 labels. Tags found in git are applied
270270
to the perforce working directory.
271271

272+
--dry-run, -n::
273+
Show just what commits would be submitted to p4; do not change
274+
state in git or p4.
275+
272276
Rebase options
273277
~~~~~~~~~~~~~~
274278
These options can be used to modify 'git p4 rebase' behavior.

git-p4.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -853,12 +853,14 @@ def __init__(self):
853853
# preserve the user, requires relevant p4 permissions
854854
optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"),
855855
optparse.make_option("--export-labels", dest="exportLabels", action="store_true"),
856+
optparse.make_option("--dry-run", "-n", dest="dry_run", action="store_true"),
856857
]
857858
self.description = "Submit changes from git to the perforce depot."
858859
self.usage += " [name of git branch to submit into perforce depot]"
859860
self.origin = ""
860861
self.detectRenames = False
861862
self.preserveUser = gitConfig("git-p4.preserveUser").lower() == "true"
863+
self.dry_run = False
862864
self.isWindows = (platform.system() == "Windows")
863865
self.exportLabels = False
864866
self.p4HasMoveCommand = p4_has_command("move")
@@ -1366,14 +1368,17 @@ def exportGitTags(self, gitTags):
13661368
for mapping in clientSpec.mappings:
13671369
labelTemplate += "\t%s\n" % mapping.depot_side.path
13681370

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)
13701375

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])
13741379

1375-
if verbose:
1376-
print "created p4 label for tag %s" % name
1380+
if verbose:
1381+
print "created p4 label for tag %s" % name
13771382

13781383
def run(self, args):
13791384
if len(args) == 0:
@@ -1432,12 +1437,15 @@ def run(self, args):
14321437
os.makedirs(self.clientPath)
14331438

14341439
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
14391442
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("...")
14411449
self.check()
14421450

14431451
commits = []
@@ -1488,10 +1496,17 @@ def run(self, args):
14881496
# Apply the commits, one at a time. On failure, ask if should
14891497
# continue to try the rest of the patches, or quit.
14901498
#
1499+
if self.dry_run:
1500+
print "Would apply"
14911501
applied = []
14921502
last = len(commits) - 1
14931503
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)
14951510
if ok:
14961511
applied.append(commit)
14971512
else:
@@ -1515,7 +1530,9 @@ def run(self, args):
15151530

15161531
chdir(self.oldWorkingDirectory)
15171532

1518-
if len(commits) == len(applied):
1533+
if self.dry_run:
1534+
pass
1535+
elif len(commits) == len(applied):
15191536
print "All commits applied!"
15201537

15211538
sync = P4Sync()

t/t9807-git-p4-submit.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,47 @@ test_expect_success 'submit --origin' '
5454
)
5555
'
5656

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+
5798
test_expect_success 'submit with allowSubmit' '
5899
test_when_finished cleanup_git &&
59100
git p4 clone --dest="$git" //depot &&

0 commit comments

Comments
 (0)