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 <luke@diamand.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Luke Diamand committed Oct 15, 2018 at 12:14 UTC 89143ac28a61578487af7324c653a9efa5e40034
3 files changed +106 -51
Documentation/git-p4.txt
+2 -2
@@ -177,8 +177,8 @@ Unshelving will take a shelved P4 changelist, and produce the equivalent git com
177 in the branch refs/remotes/p4-unshelved/<changelist>.
178
179 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.
182
183 The origin revision can be changed with the "--origin" option.
184
git-p4.py
+48 -36
@@ -1306,6 +1306,9 @@ class GitLFS(LargeFileSystem):
1306 return LargeFileSystem.processContent(self, git_mode, relPath, contents)
1307
1308 class Command:
1309 + delete_actions = ( "delete", "move/delete", "purge" )
1310 + add_actions = ( "add", "move/add" )
1311 +
1312 def __init__(self):
1313 self.usage = "usage: %prog [options]"
1314 self.needsGit = True
@@ -2524,7 +2527,6 @@ class View(object):
2527 return ""
2528
2529 class P4Sync(Command, P4UserMap):
2527 - delete_actions = ( "delete", "move/delete", "purge" )
2530
2531 def __init__(self):
2532 Command.__init__(self)
@@ -2612,20 +2614,7 @@ class P4Sync(Command, P4UserMap):
2614 if self.verbose:
2615 print("checkpoint finished: " + out)
2616
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):
2618 self.cloneExclude = [re.sub(r"\.\.\.$", "", path)
2619 for path in self.cloneExclude]
2620 files = []
@@ -2650,17 +2639,6 @@ class P4Sync(Command, P4UserMap):
2639 file["type"] = commit["type%s" % fnum]
2640 if shelved:
2641 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 -
2642 files.append(file)
2643 fnum = fnum + 1
2644 return files
@@ -3032,7 +3010,7 @@ class P4Sync(Command, P4UserMap):
3010 print('Ignoring file outside of prefix: {0}'.format(path))
3011 return hasPrefix
3012
3035 - def commit(self, details, files, branch, parent = ""):
3013 + def commit(self, details, files, branch, parent = "", allow_empty=False):
3014 epoch = details["time"]
3015 author = details["user"]
3016 jobs = self.extractJobsFromCommit(details)
@@ -3046,7 +3024,10 @@ class P4Sync(Command, P4UserMap):
3024 files = [f for f in files
3025 if self.inClientSpec(f['path']) and self.hasBranchPrefix(f['path'])]
3026
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:
3031 print('Ignoring revision {0} as it would produce an empty commit.'
3032 .format(details['change']))
3033 return
@@ -3387,10 +3368,10 @@ class P4Sync(Command, P4UserMap):
3368 else:
3369 return None
3370
3390 - def importChanges(self, changes, shelved=False, origin_revision=0):
3371 + def importChanges(self, changes, origin_revision=0):
3372 cnt = 1
3373 for change in changes:
3393 - description = p4_describe(change, shelved)
3374 + description = p4_describe(change)
3375 self.updateOptionDict(description)
3376
3377 if not self.silent:
@@ -3460,7 +3441,7 @@ class P4Sync(Command, P4UserMap):
3441 print("Parent of %s not found. Committing into head of %s" % (branch, parent))
3442 self.commit(description, filesForCommit, branch, parent)
3443 else:
3463 - files = self.extractFilesFromCommit(description, shelved, change, origin_revision)
3444 + files = self.extractFilesFromCommit(description)
3445 self.commit(description, files, self.branch,
3446 self.initialParent)
3447 # only needed once, to connect to the previous commit
@@ -3957,7 +3938,6 @@ class P4Unshelve(Command):
3938 self.verbose = False
3939 self.noCommit = False
3940 self.destbranch = "refs/remotes/p4-unshelved"
3960 - self.origin = "p4/master"
3941
3942 def renameBranch(self, branch_name):
3943 """ Rename the existing branch to branch_name.N
@@ -3989,6 +3969,32 @@ class P4Unshelve(Command):
3969
3970 sys.exit("could not find git-p4 commits in {0}".format(self.origin))
3971
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 +
3998 def run(self, args):
3999 if len(args) != 1:
4000 return False
@@ -3998,9 +4004,8 @@ class P4Unshelve(Command):
4004
4005 sync = P4Sync()
4006 changes = args
4001 - sync.initialParent = self.origin
4007
4003 - # use the first change in the list to construct the branch to unshelve into
4008 + # only one change at a time
4009 change = changes[0]
4010
4011 # if the target branch already exists, rename it
@@ -4013,14 +4018,21 @@ class P4Unshelve(Command):
4018 sync.suppress_meta_comment = True
4019
4020 settings = self.findLastP4Revision(self.origin)
4016 - origin_revision = settings['change']
4021 sync.depotPaths = settings['depot-paths']
4022 sync.branchPrefixes = sync.depotPaths
4023
4024 sync.openStreams()
4025 sync.loadUserMapFromCache()
4026 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, "")
4036 sync.closeStreams()
4037
4038 print("unshelved changelist {0} into {1}".format(change, branch_name))
t/t9832-unshelve.sh
+56 -13
@@ -19,8 +19,10 @@ test_expect_success 'init depot' '
19 p4 add file1 &&
20 p4 submit -d "change 1" &&
21 : >file_to_delete &&
22 + : >file_to_move &&
23 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"
26 )
27 '
28
@@ -36,6 +38,8 @@ test_expect_success 'create shelved changelist' '
38 echo "new file" >file2 &&
39 p4 add file2 &&
40 p4 delete file_to_delete &&
41 + p4 edit file_to_move &&
42 + p4 move file_to_move moved_file &&
43 p4 opened &&
44 p4 shelve -i <<EOF
45 Change: new
@@ -47,6 +51,8 @@ Files:
51 //depot/file1
52 //depot/file2
53 //depot/file_to_delete
54 + //depot/file_to_move
55 + //depot/moved_file
56 EOF
57
58 ) &&
@@ -59,7 +65,9 @@ EOF
65 test_path_is_file file2 &&
66 test_cmp file1 "$cli"/file1 &&
67 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
71 )
72 '
73
@@ -92,6 +100,18 @@ EOF
100 )
101 '
102
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 +
115 # This is the tricky case where the shelved changelist base revision doesn't
116 # match git-p4's idea of the base revision
117 #
@@ -108,29 +128,52 @@ test_expect_success 'create shelved changelist based on p4 change ahead of p4/ma
128 p4 submit -d "change:foo" &&
129 p4 edit file1 &&
130 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 &&
132 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
135 )
136 '
137
123 -# Now try to unshelve it. git-p4 should refuse to do so.
138 +# Now try to unshelve it.
139 test_expect_success 'try to unshelve the change' '
140 test_when_finished cleanup_git &&
141 (
142 change=$(last_shelved_change) &&
143 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
146 )
147 '
148
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 +'
177 test_expect_success 'kill p4d' '
178 kill_p4d
179 '