Skip to content

Commit 89143ac

Browse files
luked99gitster
authored andcommitted
git-p4: fully support unshelving changelists
The previous git-p4 unshelve support would check for changes in Perforce to the files being unshelved since the original shelve, and would complain if any were found. This was to ensure that the user wouldn't end up with both the shelved change delta, and some deltas from other changes in their git commit. e.g. given fileA: the quick brown fox change1: s/the/The/ <- p4 shelve this change change2: s/fox/Fox/ <- p4 submit this change git p4 unshelve 1 <- FAIL This change teaches the P4Unshelve class to always create a parent commit which matches the P4 tree (for the files being unshelved) at the point prior to the P4 shelve being created (which is reported in the p4 description for a shelved changelist). That then means git-p4 can always create a git commit matching the P4 shelve that was originally created, without any extra deltas. The user might still need to use the --origin option though - there is no way for git-p4 to work out the versions of all of the other *unchanged* files in the shelve, since this information is not recorded by Perforce. Additionally this fixes handling of shelved 'move' operations. Signed-off-by: Luke Diamand <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0881312 commit 89143ac

File tree

3 files changed

+106
-51
lines changed

3 files changed

+106
-51
lines changed

Documentation/git-p4.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ Unshelving will take a shelved P4 changelist, and produce the equivalent git com
177177
in the branch refs/remotes/p4-unshelved/<changelist>.
178178

179179
The git commit is created relative to the current origin revision (HEAD by default).
180-
If the shelved changelist's parent revisions differ, git-p4 will refuse to unshelve;
181-
you need to be unshelving onto an equivalent tree.
180+
A parent commit is created based on the origin, and then the unshelve commit is
181+
created based on that.
182182

183183
The origin revision can be changed with the "--origin" option.
184184

git-p4.py

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,9 @@ def processContent(self, git_mode, relPath, contents):
13061306
return LargeFileSystem.processContent(self, git_mode, relPath, contents)
13071307

13081308
class Command:
1309+
delete_actions = ( "delete", "move/delete", "purge" )
1310+
add_actions = ( "add", "move/add" )
1311+
13091312
def __init__(self):
13101313
self.usage = "usage: %prog [options]"
13111314
self.needsGit = True
@@ -2524,7 +2527,6 @@ def map_in_client(self, depot_path):
25242527
return ""
25252528

25262529
class P4Sync(Command, P4UserMap):
2527-
delete_actions = ( "delete", "move/delete", "purge" )
25282530

25292531
def __init__(self):
25302532
Command.__init__(self)
@@ -2612,20 +2614,7 @@ def checkpoint(self):
26122614
if self.verbose:
26132615
print("checkpoint finished: " + out)
26142616

2615-
def cmp_shelved(self, path, filerev, revision):
2616-
""" Determine if a path at revision #filerev is the same as the file
2617-
at revision @revision for a shelved changelist. If they don't match,
2618-
unshelving won't be safe (we will get other changes mixed in).
2619-
2620-
This is comparing the revision that the shelved changelist is *based* on, not
2621-
the shelved changelist itself.
2622-
"""
2623-
ret = p4Cmd(["diff2", "{0}#{1}".format(path, filerev), "{0}@{1}".format(path, revision)])
2624-
if verbose:
2625-
print("p4 diff2 path %s filerev %s revision %s => %s" % (path, filerev, revision, ret))
2626-
return ret["status"] == "identical"
2627-
2628-
def extractFilesFromCommit(self, commit, shelved=False, shelved_cl = 0, origin_revision = 0):
2617+
def extractFilesFromCommit(self, commit, shelved=False, shelved_cl = 0):
26292618
self.cloneExclude = [re.sub(r"\.\.\.$", "", path)
26302619
for path in self.cloneExclude]
26312620
files = []
@@ -2650,17 +2639,6 @@ def extractFilesFromCommit(self, commit, shelved=False, shelved_cl = 0, origin_r
26502639
file["type"] = commit["type%s" % fnum]
26512640
if shelved:
26522641
file["shelved_cl"] = int(shelved_cl)
2653-
2654-
# For shelved changelists, check that the revision of each file that the
2655-
# shelve was based on matches the revision that we are using for the
2656-
# starting point for git-fast-import (self.initialParent). Otherwise
2657-
# the resulting diff will contain deltas from multiple commits.
2658-
2659-
if file["action"] != "add" and \
2660-
not self.cmp_shelved(path, file["rev"], origin_revision):
2661-
sys.exit("change {0} not based on {1} for {2}, cannot unshelve".format(
2662-
commit["change"], self.initialParent, path))
2663-
26642642
files.append(file)
26652643
fnum = fnum + 1
26662644
return files
@@ -3032,7 +3010,7 @@ def hasBranchPrefix(self, path):
30323010
print('Ignoring file outside of prefix: {0}'.format(path))
30333011
return hasPrefix
30343012

3035-
def commit(self, details, files, branch, parent = ""):
3013+
def commit(self, details, files, branch, parent = "", allow_empty=False):
30363014
epoch = details["time"]
30373015
author = details["user"]
30383016
jobs = self.extractJobsFromCommit(details)
@@ -3046,7 +3024,10 @@ def commit(self, details, files, branch, parent = ""):
30463024
files = [f for f in files
30473025
if self.inClientSpec(f['path']) and self.hasBranchPrefix(f['path'])]
30483026

3049-
if not files and not gitConfigBool('git-p4.keepEmptyCommits'):
3027+
if gitConfigBool('git-p4.keepEmptyCommits'):
3028+
allow_empty = True
3029+
3030+
if not files and not allow_empty:
30503031
print('Ignoring revision {0} as it would produce an empty commit.'
30513032
.format(details['change']))
30523033
return
@@ -3387,10 +3368,10 @@ def searchParent(self, parent, branch, target):
33873368
else:
33883369
return None
33893370

3390-
def importChanges(self, changes, shelved=False, origin_revision=0):
3371+
def importChanges(self, changes, origin_revision=0):
33913372
cnt = 1
33923373
for change in changes:
3393-
description = p4_describe(change, shelved)
3374+
description = p4_describe(change)
33943375
self.updateOptionDict(description)
33953376

33963377
if not self.silent:
@@ -3460,7 +3441,7 @@ def importChanges(self, changes, shelved=False, origin_revision=0):
34603441
print("Parent of %s not found. Committing into head of %s" % (branch, parent))
34613442
self.commit(description, filesForCommit, branch, parent)
34623443
else:
3463-
files = self.extractFilesFromCommit(description, shelved, change, origin_revision)
3444+
files = self.extractFilesFromCommit(description)
34643445
self.commit(description, files, self.branch,
34653446
self.initialParent)
34663447
# only needed once, to connect to the previous commit
@@ -3957,7 +3938,6 @@ def __init__(self):
39573938
self.verbose = False
39583939
self.noCommit = False
39593940
self.destbranch = "refs/remotes/p4-unshelved"
3960-
self.origin = "p4/master"
39613941

39623942
def renameBranch(self, branch_name):
39633943
""" Rename the existing branch to branch_name.N
@@ -3989,6 +3969,32 @@ def findLastP4Revision(self, starting_point):
39893969

39903970
sys.exit("could not find git-p4 commits in {0}".format(self.origin))
39913971

3972+
def createShelveParent(self, change, branch_name, sync, origin):
3973+
""" Create a commit matching the parent of the shelved changelist 'change'
3974+
"""
3975+
parent_description = p4_describe(change, shelved=True)
3976+
parent_description['desc'] = 'parent for shelved changelist {}\n'.format(change)
3977+
files = sync.extractFilesFromCommit(parent_description, shelved=False, shelved_cl=change)
3978+
3979+
parent_files = []
3980+
for f in files:
3981+
# if it was added in the shelved changelist, it won't exist in the parent
3982+
if f['action'] in self.add_actions:
3983+
continue
3984+
3985+
# if it was deleted in the shelved changelist it must not be deleted
3986+
# in the parent - we might even need to create it if the origin branch
3987+
# does not have it
3988+
if f['action'] in self.delete_actions:
3989+
f['action'] = 'add'
3990+
3991+
parent_files.append(f)
3992+
3993+
sync.commit(parent_description, parent_files, branch_name,
3994+
parent=origin, allow_empty=True)
3995+
print("created parent commit for {0} based on {1} in {2}".format(
3996+
change, self.origin, branch_name))
3997+
39923998
def run(self, args):
39933999
if len(args) != 1:
39944000
return False
@@ -3998,9 +4004,8 @@ def run(self, args):
39984004

39994005
sync = P4Sync()
40004006
changes = args
4001-
sync.initialParent = self.origin
40024007

4003-
# use the first change in the list to construct the branch to unshelve into
4008+
# only one change at a time
40044009
change = changes[0]
40054010

40064011
# if the target branch already exists, rename it
@@ -4013,14 +4018,21 @@ def run(self, args):
40134018
sync.suppress_meta_comment = True
40144019

40154020
settings = self.findLastP4Revision(self.origin)
4016-
origin_revision = settings['change']
40174021
sync.depotPaths = settings['depot-paths']
40184022
sync.branchPrefixes = sync.depotPaths
40194023

40204024
sync.openStreams()
40214025
sync.loadUserMapFromCache()
40224026
sync.silent = True
4023-
sync.importChanges(changes, shelved=True, origin_revision=origin_revision)
4027+
4028+
# create a commit for the parent of the shelved changelist
4029+
self.createShelveParent(change, branch_name, sync, self.origin)
4030+
4031+
# create the commit for the shelved changelist itself
4032+
description = p4_describe(change, True)
4033+
files = sync.extractFilesFromCommit(description, True, change)
4034+
4035+
sync.commit(description, files, branch_name, "")
40244036
sync.closeStreams()
40254037

40264038
print("unshelved changelist {0} into {1}".format(change, branch_name))

t/t9832-unshelve.sh

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ test_expect_success 'init depot' '
1919
p4 add file1 &&
2020
p4 submit -d "change 1" &&
2121
: >file_to_delete &&
22+
: >file_to_move &&
2223
p4 add file_to_delete &&
23-
p4 submit -d "file to delete"
24+
p4 add file_to_move &&
25+
p4 submit -d "add files to delete"
2426
)
2527
'
2628

@@ -36,6 +38,8 @@ test_expect_success 'create shelved changelist' '
3638
echo "new file" >file2 &&
3739
p4 add file2 &&
3840
p4 delete file_to_delete &&
41+
p4 edit file_to_move &&
42+
p4 move file_to_move moved_file &&
3943
p4 opened &&
4044
p4 shelve -i <<EOF
4145
Change: new
@@ -47,6 +51,8 @@ Files:
4751
//depot/file1
4852
//depot/file2
4953
//depot/file_to_delete
54+
//depot/file_to_move
55+
//depot/moved_file
5056
EOF
5157
5258
) &&
@@ -59,7 +65,9 @@ EOF
5965
test_path_is_file file2 &&
6066
test_cmp file1 "$cli"/file1 &&
6167
test_cmp file2 "$cli"/file2 &&
62-
test_path_is_missing file_to_delete
68+
test_path_is_missing file_to_delete &&
69+
test_path_is_missing file_to_move &&
70+
test_path_is_file moved_file
6371
)
6472
'
6573

@@ -92,6 +100,18 @@ EOF
92100
)
93101
'
94102

103+
shelve_one_file () {
104+
description="Change to be unshelved" &&
105+
file="$1" &&
106+
p4 shelve -i <<EOF
107+
Change: new
108+
Description:
109+
$description
110+
Files:
111+
$file
112+
EOF
113+
}
114+
95115
# This is the tricky case where the shelved changelist base revision doesn't
96116
# match git-p4's idea of the base revision
97117
#
@@ -108,29 +128,52 @@ test_expect_success 'create shelved changelist based on p4 change ahead of p4/ma
108128
p4 submit -d "change:foo" &&
109129
p4 edit file1 &&
110130
echo "bar" >>file1 &&
111-
p4 shelve -i <<EOF &&
112-
Change: new
113-
Description:
114-
Change to be unshelved
115-
Files:
116-
//depot/file1
117-
EOF
131+
shelve_one_file //depot/file1 &&
118132
change=$(last_shelved_change) &&
119-
p4 describe -S $change | grep -q "Change to be unshelved"
133+
p4 describe -S $change >out.txt &&
134+
grep -q "Change to be unshelved" out.txt
120135
)
121136
'
122137

123-
# Now try to unshelve it. git-p4 should refuse to do so.
138+
# Now try to unshelve it.
124139
test_expect_success 'try to unshelve the change' '
125140
test_when_finished cleanup_git &&
126141
(
127142
change=$(last_shelved_change) &&
128143
cd "$git" &&
129-
test_must_fail git p4 unshelve $change 2>out.txt &&
130-
grep -q "cannot unshelve" out.txt
144+
git p4 unshelve $change >out.txt &&
145+
grep -q "unshelved changelist $change" out.txt
131146
)
132147
'
133148

149+
# Specify the origin. Create 2 unrelated files, and check that
150+
# we only get the one in HEAD~, not the one in HEAD.
151+
152+
test_expect_success 'unshelve specifying the origin' '
153+
(
154+
cd "$cli" &&
155+
: >unrelated_file0 &&
156+
p4 add unrelated_file0 &&
157+
p4 submit -d "unrelated" &&
158+
: >unrelated_file1 &&
159+
p4 add unrelated_file1 &&
160+
p4 submit -d "unrelated" &&
161+
: >file_to_shelve &&
162+
p4 add file_to_shelve &&
163+
shelve_one_file //depot/file_to_shelve
164+
) &&
165+
test_when_finished cleanup_git &&
166+
git p4 clone --dest="$git" //depot/@all &&
167+
(
168+
cd "$git" &&
169+
change=$(last_shelved_change) &&
170+
git p4 unshelve --origin HEAD~ $change &&
171+
git checkout refs/remotes/p4-unshelved/$change &&
172+
test_path_is_file unrelated_file0 &&
173+
test_path_is_missing unrelated_file1 &&
174+
test_path_is_file file_to_shelve
175+
)
176+
'
134177
test_expect_success 'kill p4d' '
135178
kill_p4d
136179
'

0 commit comments

Comments
 (0)