git-p4: add options --commit and --disable-rebase

On a daily work with multiple local git branches, the usual way to submit only a specified commit was to cherry-pick the commit on master then run git-p4 submit. It can be very annoying to switch between local branches and master, only to submit one commit. The proposed new way is to select directly the commit you want to submit. Add option --commit to command 'git-p4 submit' in order to submit only specified commit(s) in p4. On a daily work developping software with big compilation time, one may not want to rebase on his local git tree, in order to avoid long recompilation. Add option --disable-rebase to command 'git-p4 submit' in order to disable rebase after submission. Thanks-to: Cedric Borgese <cedric.borgese@gmail.com> Reviewed-by: Luke Diamand <luke@diamand.org> Signed-off-by: Romain Merland <merlorom@yahoo.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Romain Merland committed Jun 1, 2018 at 09:46 UTC f55b87c1c748cdec7ce1631e6296e3edfd7cfc7d
3 files changed +77 -6
Documentation/git-p4.txt
+14
@@ -149,6 +149,12 @@ To specify a branch other than the current one, use:
149 $ git p4 submit topicbranch
150 ------------
151
152 +To specify a single commit or a range of commits, use:
153 +------------
154 +$ git p4 submit --commit <sha1>
155 +$ git p4 submit --commit <sha1..sha1>
156 +------------
157 +
158 The upstream reference is generally 'refs/remotes/p4/master', but can
159 be overridden using the `--origin=` command-line option.
160
@@ -330,6 +336,14 @@ These options can be used to modify 'git p4 submit' behavior.
336 p4/master. See the "Sync options" section above for more
337 information.
338
339 +--commit <sha1>|<sha1..sha1>::
340 + Submit only the specified commit or range of commits, instead of the full
341 + list of changes that are in the current Git branch.
342 +
343 +--disable-rebase::
344 + Disable the automatic rebase after all commits have been successfully
345 + submitted.
346 +
347 Rebase options
348 ~~~~~~~~~~~~~~
349 These options can be used to modify 'git p4 rebase' behavior.
git-p4.py
+23 -6
@@ -1352,7 +1352,12 @@ class P4Submit(Command, P4UserMap):
1352 optparse.make_option("--update-shelve", dest="update_shelve", action="append", type="int",
1353 metavar="CHANGELIST",
1354 help="update an existing shelved changelist, implies --shelve, "
1355 - "repeat in-order for multiple shelved changelists")
1355 + "repeat in-order for multiple shelved changelists"),
1356 + optparse.make_option("--commit", dest="commit", metavar="COMMIT",
1357 + help="submit only the specified commit(s), one commit or xxx..xxx"),
1358 + optparse.make_option("--disable-rebase", dest="disable_rebase", action="store_true",
1359 + help="Disable rebase after submit is completed. Can be useful if you "
1360 + "work from a local git branch that is not master")
1361 ]
1362 self.description = "Submit changes from git to the perforce depot."
1363 self.usage += " [name of git branch to submit into perforce depot]"
@@ -1362,6 +1367,8 @@ class P4Submit(Command, P4UserMap):
1367 self.dry_run = False
1368 self.shelve = False
1369 self.update_shelve = list()
1370 + self.commit = ""
1371 + self.disable_rebase = False
1372 self.prepare_p4_only = False
1373 self.conflict_behavior = None
1374 self.isWindows = (platform.system() == "Windows")
@@ -2103,9 +2110,18 @@ class P4Submit(Command, P4UserMap):
2110 else:
2111 commitish = 'HEAD'
2112
2106 - for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (self.origin, commitish)]):
2107 - commits.append(line.strip())
2108 - commits.reverse()
2113 + if self.commit != "":
2114 + if self.commit.find("..") != -1:
2115 + limits_ish = self.commit.split("..")
2116 + for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (limits_ish[0], limits_ish[1])]):
2117 + commits.append(line.strip())
2118 + commits.reverse()
2119 + else:
2120 + commits.append(self.commit)
2121 + else:
2122 + for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (self.origin, commitish)]):
2123 + commits.append(line.strip())
2124 + commits.reverse()
2125
2126 if self.preserveUser or gitConfigBool("git-p4.skipUserNameCheck"):
2127 self.checkAuthorship = False
@@ -2215,8 +2231,9 @@ class P4Submit(Command, P4UserMap):
2231 sync.branch = self.branch
2232 sync.run([])
2233
2218 - rebase = P4Rebase()
2219 - rebase.rebase()
2234 + if self.disable_rebase is False:
2235 + rebase = P4Rebase()
2236 + rebase.rebase()
2237
2238 else:
2239 if len(applied) == 0:
t/t9807-git-p4-submit.sh
+40
@@ -155,6 +155,46 @@ test_expect_success 'allow submit from branch with same revision but different n
155 )
156 '
157
158 +# make two commits, but tell it to apply only one
159 +
160 +test_expect_success 'submit --commit one' '
161 + test_when_finished cleanup_git &&
162 + git p4 clone --dest="$git" //depot &&
163 + (
164 + cd "$git" &&
165 + test_commit "file9" &&
166 + test_commit "file10" &&
167 + git config git-p4.skipSubmitEdit true &&
168 + git p4 submit --commit HEAD
169 + ) &&
170 + (
171 + cd "$cli" &&
172 + test_path_is_missing "file9.t" &&
173 + test_path_is_file "file10.t"
174 + )
175 +'
176 +
177 +# make three commits, but tell it to apply only range
178 +
179 +test_expect_success 'submit --commit range' '
180 + test_when_finished cleanup_git &&
181 + git p4 clone --dest="$git" //depot &&
182 + (
183 + cd "$git" &&
184 + test_commit "file11" &&
185 + test_commit "file12" &&
186 + test_commit "file13" &&
187 + git config git-p4.skipSubmitEdit true &&
188 + git p4 submit --commit HEAD~2..HEAD
189 + ) &&
190 + (
191 + cd "$cli" &&
192 + test_path_is_missing "file11.t" &&
193 + test_path_is_file "file12.t" &&
194 + test_path_is_file "file13.t"
195 + )
196 +'
197 +
198 #
199 # Basic submit tests, the five handled cases
200 #