Skip to content

Commit e68e14a

Browse files
authored
Merge pull request #930 from intersystems/take-ownership-button
Added Take Ownership option to source control menu
2 parents b1b301c + 71d10ff commit e68e14a

5 files changed

Lines changed: 70 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111
- Baseline Export now includes decomposed production items when production decomposition is enabled (#680)
12+
- Take Ownership option in source control menu allows you to take ownership of an item edited by another user (#926)
1213

1314
## Fixed
1415
- Fixed issue where Generated Files Read-only option didn't entirely work for files not in source control (#712)

cls/SourceControl/Git/Extension.cls

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ XData Menu
3535
<MenuItem Name="ExportForce" Save="101" />
3636
<MenuItem Name="Import" />
3737
<MenuItem Name="ImportForce" />
38+
<MenuItem Name="TakeOwnership" />
3839
</Menu>
3940
<Menu Name="%SourceContext" Type="1">
4041
<MenuItem Name="AddToSC" Save="100" />
4142
<MenuItem Name="RemoveFromSC"/>
4243
<MenuItem Name="Revert" Save="100" />
4344
<MenuItem Name="Commit" Save="100" />
45+
<MenuItem Name="TakeOwnership" />
4446
</Menu>
4547
</MenuBase>
4648
}
@@ -150,6 +152,7 @@ Method LocalizeName(name As %String) As %String
150152
"SwitchBranch":$$$Text("@SwitchBranch@Check out an existing branch"),
151153
"Revert":$$$Text("@Revert@Discard changes to file"),
152154
"Commit":$$$Text("@Commit@Commit changes to file"),
155+
"TakeOwnership":$$$Text("@TakeOwnership@Take ownership of changes to file"),
153156
"Sync":$$$Text("@Sync@Sync"),
154157
"Push":$$$Text("@Push@Push to remote branch"),
155158
"PushForce":$$$Text("@PushForce@Push to remote branch (Force)"),
@@ -172,7 +175,7 @@ Method OnSourceMenuItem(name As %String, ByRef Enabled As %String, ByRef Display
172175
}
173176
if ##class(SourceControl.Git.Utils).IsNamespaceInGit() {
174177

175-
if $listfind($listbuild("AddToSC", "RemoveFromSC", "Revert", "Commit", "ExportProduction"), name) {
178+
if $listfind($listbuild("AddToSC", "RemoveFromSC", "Revert", "Commit", "ExportProduction", "TakeOwnership"), name) {
176179
quit ..OnSourceMenuContextItem(InternalName,name,.Enabled,.DisplayName)
177180
}
178181

@@ -260,6 +263,13 @@ Method OnSourceMenuContextItem(itemName As %String, menuItemName As %String, ByR
260263
if '(##class(SourceControl.Git.Change).IsUncommitted(##class(SourceControl.Git.Utils).FullExternalName(itemName))) || ($username '= userCheckedOut) {
261264
set Enabled = 0
262265
}
266+
} elseif menuItemName = "TakeOwnership" {
267+
set Enabled = -1 // Hide by default
268+
do ..GetStatus(.itemName, .isInSourceControl, .isEditable,.isCheckedOut,.userCheckedOut)
269+
// Show only if file is checked out by a different user
270+
if isCheckedOut && (userCheckedOut '= $username) {
271+
set Enabled = 1
272+
}
263273
} elseif menuItemName = "ExportProduction" {
264274
set itemNameNoExt = $piece(itemName,".",1,*-1)
265275
set Enabled = (##class(SourceControl.Git.Production).IsProductionClass(itemNameNoExt,"FullExternalName"))

cls/SourceControl/Git/Utils.cls

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,14 @@ ClassMethod UserAction(InternalName As %String, MenuName As %String, ByRef Targe
339339
set ec = ..AddToSourceControl(InternalName)
340340
} elseif (menuItemName = "RemoveFromSC") {
341341
set ec = ..RemoveFromSourceControl(InternalName)
342+
} elseif (menuItemName = "TakeOwnership") {
343+
set filename = ..FullExternalName(InternalName)
344+
set sc = ##class(SourceControl.Git.Change).GetUncommitted(filename, .tAction, .tInternalName, .UncommittedUser, .tSource, .UncommittedLastUpdated)
345+
$$$QuitOnError(sc)
346+
if $get(UncommittedUser) = "" set UncomittedUser = "unknown user"
347+
set Target = "File '"_InternalName_"' has uncommitted changes by '"_UncommittedUser_"'. Do you want to take ownership of these changes?"
348+
set Action = 1 // Show Yes/No confirmation dialog
349+
quit $$$OK
342350
} elseif (menuItemName = "Status") {
343351
do ..RunGitCommand("status", .errStream, .outStream)
344352
write !, !, "Git Status: "
@@ -382,6 +390,11 @@ ClassMethod AfterUserAction(Type As %Integer, Name As %String, InternalName As %
382390
do ..Push(,1)
383391
set Reload = 1
384392
}
393+
} elseif (menuItemName = "TakeOwnership") {
394+
if (Answer = 1) {
395+
set status = ..TakeOwnership(InternalName)
396+
set Reload = 1
397+
}
385398
} elseif (menuItemName = "GitWebUI") {
386399
// Always force reload as many things could have possibly changed.
387400
set Reload = 1
@@ -424,6 +437,20 @@ ClassMethod Commit(InternalName As %String, Message As %String = "example commit
424437
quit $$$OK
425438
}
426439

440+
ClassMethod TakeOwnership(InternalName As %String) As %Status
441+
{
442+
set sc = $$$OK
443+
try {
444+
&sql(update SourceControl_Git.Change set ChangedBy = $username where InternalName = :InternalName)
445+
$$$ThrowSQLIfError(SQLCODE,.%msg)
446+
write !, "Ownership of '"_InternalName_"' transferred to '"_$username_"'"
447+
} catch ex {
448+
set sc = ex.AsStatus()
449+
do $System.OBJ.DisplayError(sc)
450+
}
451+
quit sc
452+
}
453+
427454
/// <p>This function performs <code>git checkout -b [newBranchName]</code> from the current commit.</p>
428455
/// <p>If the user is in "Basic Mode" AND there is a Default Merge Branch defined,
429456
/// then this method first checks out and pulls that default merge branch before creating the new branch. This is

docs/menu-items.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,6 @@ This option imports the versions of the files that are found in the configured d
8484

8585
## Import All (Force)
8686
This menu option behaves similarly to the regular import but forces the files to be imported regardless of whether the on-disk version is the same or older.
87+
88+
## Take Ownership of Changes to File
89+
This option is enabled if the current item has uncommitted changes owned by a different user. Take Ownership will transfer ownership of those uncommitted changes to the current user, allowing the current user to make their own edits.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Class UnitTest.SourceControl.Git.Utils.TakeOwnership Extends UnitTest.SourceControl.Git.AbstractTest
2+
{
3+
4+
Method TestTakeOwnership()
5+
{
6+
set internalName = "Test.Package.TestClass.cls"
7+
8+
&sql(INSERT INTO SourceControl_Git.Change (InternalName, ChangedBy, ItemFile, Action, Committed)
9+
VALUES (:internalName, 'OtherUser', 'cls/Test/Package/TestClass.cls', 'edit', 0))
10+
$$$ThrowSQLIfError(SQLCODE,.%msg)
11+
12+
try {
13+
// Call TakeOwnership and verify it succeeds
14+
do $$$AssertStatusOK(##class(SourceControl.Git.Utils).TakeOwnership(internalName))
15+
16+
// Verify ChangedBy was updated
17+
&sql(SELECT ChangedBy INTO :changedBy FROM SourceControl_Git.Change WHERE InternalName = :internalName)
18+
$$$ThrowSQLIfError(SQLCODE,.%msg)
19+
do $$$AssertEquals(changedBy, $username)
20+
} catch ex {
21+
do $$$AssertStatusOK(ex.AsStatus())
22+
}
23+
24+
// Clean up
25+
&sql(DELETE FROM SourceControl_Git.Change WHERE InternalName = :internalName)
26+
}
27+
28+
}

0 commit comments

Comments
 (0)