Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UNC path prefix before adding to safe directory list #14368

Merged
merged 2 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/src/lib/git/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ export async function addGlobalConfigValue(
)
}

/**
* Adds a path to the `safe.directories` configuration variable if it's not
* already present. Adding a path to `safe.directory` will cause Git to ignore
* if the path is owner by a different user than the current.
*/
export async function addSafeDirectory(path: string) {
// UNC-paths on Windows need to be prefixed with `%(prefix)/`, see
// https://github.com/git-for-windows/git/commit/e394a16023cbb62784e380f70ad8a833fb960d68
if (__WIN32__ && path[0] === '/') {
path = `%(prefix)/${path}`
}

addGlobalConfigValueIfMissing('safe.directory', path)
}
Comment on lines +169 to +177

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chiming in from Git's perspective to say that this looks right to me. Thanks!


/** Set the global config value by name. */
export async function addGlobalConfigValueIfMissing(
name: string,
Expand Down
6 changes: 3 additions & 3 deletions app/src/ui/add-repository/add-existing-repository.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react'
import * as Path from 'path'
import { Dispatcher } from '../dispatcher'
import { addGlobalConfigValueIfMissing, getRepositoryType } from '../../lib/git'
import { addSafeDirectory, getRepositoryType } from '../../lib/git'
import { Button } from '../lib/button'
import { TextBox } from '../lib/text-box'
import { Row } from '../lib/row'
Expand Down Expand Up @@ -81,10 +81,10 @@ export class AddExistingRepository extends React.Component<
}
}

private onTrustDirectory = () => {
private onTrustDirectory = async () => {
const { repositoryUnsafePath, path } = this.state
if (repositoryUnsafePath) {
addGlobalConfigValueIfMissing('safe.directory', repositoryUnsafePath)
await addSafeDirectory(repositoryUnsafePath)
}
this.validatePath(path)
}
Expand Down
16 changes: 8 additions & 8 deletions app/src/ui/missing-repository.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Repository } from '../models/repository'
import { Button } from './lib/button'
import { Row } from './lib/row'
import { LinkButton } from './lib/link-button'
import { addGlobalConfigValueIfMissing, getRepositoryType } from '../lib/git'
import { addSafeDirectory, getRepositoryType } from '../lib/git'
import { Ref } from './lib/ref'

interface IMissingRepositoryProps {
Expand All @@ -31,12 +31,12 @@ export class MissingRepository extends React.Component<
}

private onTrustDirectory = async () => {
if (this.state.unsafePath) {
await addGlobalConfigValueIfMissing(
'safe.directory',
this.state.unsafePath
)
const type = await getRepositoryType(this.props.repository.path)
const { unsafePath } = this.state
const { repository } = this.props

if (unsafePath) {
await addSafeDirectory(unsafePath)
const type = await getRepositoryType(repository.path)

if (type.kind !== 'unsafe') {
this.checkAgain()
Expand Down Expand Up @@ -90,7 +90,7 @@ export class MissingRepository extends React.Component<
onClick={this.onTrustDirectory}
type="submit"
>
{__DARWIN__ ? 'Trust Repository' : 'Trust repository'}
{__DARWIN__ ? 'Trust Repository' : 'Trust repository'}
</Button>
)
}
Expand Down