git-p4: add P4 jobs to git commit message
When migrating from Perforce to git the information about P4 jobs associated with P4 changelists is lost. Having these jobs listed on messages of related git commits enables smooth migration for projects that take advantage of e.g. JIRA integration (which uses jobs on Perforce side and parses commit messages on git side). The jobs are added to the message in the same format as is expected when migrating in the reverse direction. Signed-off-by: Jan Durovec <jan.durovec@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jan Durovec committed
Apr 19, 2016 at 19:49 UTC
26e6a27d6965c9c5ee4f59ae9a97cef893b52686
3 files changed
+120
git-p4.py
+12
@@ -2320,6 +2320,15 @@ class P4Sync(Command, P4UserMap):
2320
fnum = fnum + 1
2321
return files
2322
2323
+ def extractJobsFromCommit(self, commit):
2324
+ jobs = []
2325
+ jnum = 0
2326
+ while commit.has_key("job%s" % jnum):
2327
+ job = commit["job%s" % jnum]
2328
+ jobs.append(job)
2329
+ jnum = jnum + 1
2330
+ return jobs
2331
+
2332
def stripRepoPath(self, path, prefixes):
2333
"""When streaming files, this is called to map a p4 depot path
2334
to where it should go in git. The prefixes are either
@@ -2665,6 +2674,7 @@ class P4Sync(Command, P4UserMap):
2674
def commit(self, details, files, branch, parent = ""):
2675
epoch = details["time"]
2676
author = details["user"]
2677
+ jobs = self.extractJobsFromCommit(details)
2678
2679
if self.verbose:
2680
print('commit into {0}'.format(branch))
@@ -2692,6 +2702,8 @@ class P4Sync(Command, P4UserMap):
2702
2703
self.gitStream.write("data <<EOT\n")
2704
self.gitStream.write(details["desc"])
2705
+ if len(jobs) > 0:
2706
+ self.gitStream.write("\nJobs: %s" % (' '.join(jobs)))
2707
self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s" %
2708
(','.join(self.branchPrefixes), details["change"]))
2709
if len(details['options']) > 0:
t/lib-git-p4.sh
+9
@@ -160,6 +160,15 @@ p4_add_user () {
160
EOF
161
}
162
163
+p4_add_job () {
164
+ p4 job -f -i <<-EOF
165
+ Job: $1
166
+ Status: open
167
+ User: dummy
168
+ Description:
169
+ EOF
170
+}
171
+
172
retry_until_success () {
173
timeout=$(($(time_in_seconds) + $RETRY_TIMEOUT))
174
until "$@" 2>/dev/null || test $(time_in_seconds) -gt $timeout
t/t9829-git-p4-jobs.sh
new
+99
@@ -0,0 +1,99 @@
1
+#!/bin/sh
2
+
3
+test_description='git p4 retrieve job info'
4
+
5
+. ./lib-git-p4.sh
6
+
7
+test_expect_success 'start p4d' '
8
+ start_p4d
9
+'
10
+
11
+test_expect_success 'add p4 jobs' '
12
+ (
13
+ p4_add_job TESTJOB-A &&
14
+ p4_add_job TESTJOB-B
15
+ )
16
+'
17
+
18
+test_expect_success 'add p4 files' '
19
+ client_view "//depot/... //client/..." &&
20
+ (
21
+ cd "$cli" &&
22
+ >file1 &&
23
+ p4 add file1 &&
24
+ p4 submit -d "Add file 1"
25
+ )
26
+'
27
+
28
+test_expect_success 'check log message of changelist with no jobs' '
29
+ client_view "//depot/... //client/..." &&
30
+ test_when_finished cleanup_git &&
31
+ (
32
+ cd "$git" &&
33
+ git init . &&
34
+ git p4 clone --use-client-spec --destination="$git" //depot@all &&
35
+ cat >expect <<-\EOF &&
36
+ Add file 1
37
+ [git-p4: depot-paths = "//depot/": change = 1]
38
+
39
+ EOF
40
+ git log --format=%B >actual &&
41
+ test_cmp expect actual
42
+ )
43
+'
44
+
45
+test_expect_success 'add TESTJOB-A to change 1' '
46
+ (
47
+ cd "$cli" &&
48
+ p4 fix -c 1 TESTJOB-A
49
+ )
50
+'
51
+
52
+test_expect_success 'check log message of changelist with one job' '
53
+ client_view "//depot/... //client/..." &&
54
+ test_when_finished cleanup_git &&
55
+ (
56
+ cd "$git" &&
57
+ git init . &&
58
+ git p4 clone --use-client-spec --destination="$git" //depot@all &&
59
+ cat >expect <<-\EOF &&
60
+ Add file 1
61
+ Jobs: TESTJOB-A
62
+ [git-p4: depot-paths = "//depot/": change = 1]
63
+
64
+ EOF
65
+ git log --format=%B >actual &&
66
+ test_cmp expect actual
67
+ )
68
+'
69
+
70
+test_expect_success 'add TESTJOB-B to change 1' '
71
+ (
72
+ cd "$cli" &&
73
+ p4 fix -c 1 TESTJOB-B
74
+ )
75
+'
76
+
77
+test_expect_success 'check log message of changelist with more jobs' '
78
+ client_view "//depot/... //client/..." &&
79
+ test_when_finished cleanup_git &&
80
+ (
81
+ cd "$git" &&
82
+ git init . &&
83
+ git p4 clone --use-client-spec --destination="$git" //depot@all &&
84
+ cat >expect <<-\EOF &&
85
+ Add file 1
86
+ Jobs: TESTJOB-A TESTJOB-B
87
+ [git-p4: depot-paths = "//depot/": change = 1]
88
+
89
+ EOF
90
+ git log --format=%B >actual &&
91
+ test_cmp expect actual
92
+ )
93
+'
94
+
95
+test_expect_success 'kill p4d' '
96
+ kill_p4d
97
+'
98
+
99
+test_done