Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F179695
find_taskless_commits.php
No One
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
joshuaspence
Jul 22 2014, 1:31 AM
2014-07-22 01:31:33 (UTC+0)
Size
3 KB
Referenced Files
None
Subscribers
None
find_taskless_commits.php
View Options
#!/usr/bin/env php
<?php
/**
* Examines commits which //appear// to reference a task but for which no edge
* actually exists. In some cases, it is safe to just create an edge
* automatically. In other cases, the commit identifier is printed out so that
* further action can be taken.
*/
require_once
(
'/usr/src/phabricator/scripts/__init_script__.php'
);
$console
=
PhutilConsole
::
getConsole
();
id
(
new
PhutilArgumentParser
(
$argv
))
->
parseStandardArguments
();
$table
=
new
PhabricatorRepositoryCommit
();
$commits
=
new
LiskMigrationIterator
(
$table
);
$data_table
=
new
PhabricatorRepositoryCommitData
();
// Obtain a mapping of repository ID to repository callsign.
$repos
=
id
(
new
PhabricatorRepositoryQuery
())
->
setViewer
(
PhabricatorUser
::
getOmnipotentUser
())
->
execute
();
$repos
=
mpull
(
$repos
,
'getCallsign'
,
'getID'
);
// Obtain a mapping of task ID to task PHID.
$task
=
new
ManiphestTask
();
$tasks
=
queryfx_all
(
$task
->
establishConnection
(
'r'
),
'SELECT id, phid FROM %T'
,
$task
->
getTableName
());
$tasks
=
ipull
(
$tasks
,
'phid'
,
'id'
);
// Create an editor for creating new edges.
$edge_editor
=
new
PhabricatorEdgeEditor
();
foreach
(
$commits
as
$commit
)
{
// Load commit data.
$data
=
$commit
->
loadOneRelative
(
$data_table
,
'commitID'
);
if
(!
$data
)
{
continue
;
}
$commit_hash
=
$commit
->
getCommitIdentifier
();
$commit_message
=
$data
->
getCommitMessage
();
$commit_repo
=
idx
(
$repos
,
$commit
->
getRepositoryID
(),
'???'
);
// If the commit already has edges to tasks, don't bother doing anything.
$commit_tasks
=
PhabricatorEdgeQuery
::
loadDestinationPHIDs
(
$commit
->
getPHID
(),
DiffusionCommitHasTaskEdgeType
::
EDGECONST
);
if
(
$commit_tasks
)
{
continue
;
}
// Commit appears to reference a task... create an edge automatically.
$matches
=
null
;
if
(
preg_match
(
'/^Maniphest Tasks: T(
\d
+)$/m'
,
$commit_message
,
$matches
)
||
preg_match
(
'/^
\[
T(
\d
+)
\]\s
/'
,
$commit_message
,
$matches
)
||
preg_match
(
'/^T(
\d
+)
\b
/'
,
$commit_message
,
$matches
))
{
$task_id
=
$matches
[
1
];
$task_phid
=
idx
(
$tasks
,
$task_id
);
if
(!
$task_phid
)
{
$console
->
writeErr
(
"Commit r%s%s references T%d, but no such task exists.
\n
"
,
$commit_repo
,
$commit_hash
,
$task_id
);
continue
;
}
$edge_editor
->
addEdge
(
$commit
->
getPHID
(),
DiffusionCommitHasTaskEdgeType
::
EDGECONST
,
$task_phid
);
$console
->
writeOut
(
"Created an edge from commit r%s%s to T%d.
\n
"
,
$commit_repo
,
$commit_hash
,
$task_id
);
continue
;
}
if
(
preg_match
(
"@^Merge (remote-tracking )?branch '(
\w
+/)?master' of git
\.
freelancer
\.
com:(
\w
+/)?[
\w
_-]+( into [
\w
_-]+)?$@"
,
trim
(
$commit_message
))
||
preg_match
(
"@^Merge branch 'master' into [
\w
_-]+$@"
,
trim
(
$commit_message
)))
{
// Skip what appears to be a merge commit.
continue
;
}
// Do a really quick (but really dumb) check to see if the commit message contains a task ID.
$matches
=
null
;
if
(
preg_match_all
(
'/
\b
T
\d
+
\b
/'
,
$commit_message
,
$matches
))
{
$console
->
writeOut
(
"Commit r%s%s appears to reference tasks: %s
\n
"
,
$commit_repo
,
$commit_hash
,
implode
(
', '
,
array_unique
(
$matches
[
0
])));
}
}
// Save
$edge_editor
->
save
();
$console
->
writeOut
(
"Done.
\n
"
);
File Metadata
Details
Attached
Mime Type
text/plain; charset=utf-8
Storage Engine
amazon-s3
Storage Format
Raw Data
Storage Handle
phabricator/32/mh/tbalepboaub5vnz4
Default Alt Text
find_taskless_commits.php (3 KB)
Attached To
Mode
P1206 find_taskless_commits.php
Attached
Detach File
Event Timeline
Log In to Comment