2014-10-05 4 views
1

git의 성가심은 일부 업스트림 저장소 (예 : Github 포크)에서 분기 된 저장소의 모든 복제본에 git remote add upstream URL을 사용해야합니다. 이상적으로는 모든 복제본이 자동으로 설정되도록 포크 된 저장소 자체에 업스트림 위치를 저장할 수 있어야합니다. 거기에 자식을 사용하여 이것을 달성하는 방법이 있습니까?git repo에 업스트림 위치를 영구적으로 추가하는 방법은 무엇입니까?

+0

잘 모르겠습니다. 기본적으로 Andrew가 지적한 것처럼 복제 된 Repo는 복제 된 원격을 ("origin"이라는 이름으로) 알고 있습니다. 그 이름을 대신 "업스트림"으로 지정 하시겠습니까 (이 경우 앤드류의 대답을 봐야합니다), 아니면 복제시 두 번째 리모컨을 추가하려고합니까? – Jubobs

+1

원점이 분기 된 원본 Repo에 두 번째 리모컨을 추가합니다. 나는이 질문에 이것을 분명히했다. 모호함을 지적 해 주셔서 감사합니다! – holocronweaver

답변

2

다른 답변 상태로, 자식은 현재 repos를 함께 연결하기위한 기본 제공 기능이 없습니다.

따라서 나는 BitBucket 및 GitHub에 대한 업스트림 저장소를 구문 분석하고 upstream이라는 원격 저장소로 추가하기 위해 BeatifulSoup 4를 사용하는 clone이라는 작은 Python 스크립트를 작성했습니다. 이것은 git와 Mercurial 모두에서 작동합니다. hosted on GitHub이지만 완료하려면이 답변에 전체 스크립트를 포함합니다.

#! /usr/bin/env python3 
description=''' 
A wrapper for repo cloning commands for various DVCS. 
Automatically adds the upstream URL when cloning a forked repo from 
popular DVCS web hosts. 
Currently supports: 
(DVCS: git, Mercurial) 
(hosts: GitHub, Bitbucket) 
''' 
# Created by Jesse Johnson a.k.a. holocronweaver (2014-10-08). 
import argparse 
import re 
import subprocess 
import urllib.request 

from bs4 import BeautifulSoup 

def find(regex, string, errorMessage): 
    '''Return value at regex, or else print error message and exit program.''' 
    m = re.search(regex, string) 
    if (m): 
     return m.group(1) 
    else: 
     print('Error:', errorMessage) 
     exit() 

parser = argparse.ArgumentParser(description=description) 
parser.add_argument('origin', metavar='O', type=str, 
        help='the SSH URL of origin repo, optionally' 
        'followed by its destination folder') 
args = parser.parse_args() 

# Parse destination. 
splitArgs = re.split('\s+', args.origin) 
if len(splitArgs) > 1: 
    origin = splitArgs[0] 
    destination = splitArgs[1] 
else: 
    origin = args.origin 
    destination = find('@.*\.\w+[:|/].*/(.*)[.git]?$', origin, 
          'Error: Could not parse destination folder from origin URL.') 
    destination = re.sub('\.git', '', destination) 
print('destination folder:', destination) 

# Unfortunately HTTPS URLs do not contain enough info to clarify which 
# DVCS is being used, so SSH is easiest to support. 
vcs = find('^[ssh://]?(.*)@', origin, 
      'URL does not contain SSH user (e.g., [email protected] or [email protected]).') 
print('version control system:', vcs) 

domain = find('@(.*\.\w+)[:|/]', origin, 
       'Error: Could not parse domain from origin URL.') 
print('domain:', domain) 

path = find('@.*\.\w+([:|/].*)[.git]?$', origin, 
     'Error: Could not parse repo path from origin URL.') 
path = re.sub(':', '/', path) 
print('repo path:', path) 

homepage = 'https://' + domain + path 
print(homepage) 

data = urllib.request.urlopen(homepage).read() 
soup = BeautifulSoup(data) 

# Version control system specifics. 
if ('[email protected]' in origin): 
    ext = '.git' 
    clone = 'git clone %s %s' 
    setUpstream = 'git remote add upstream %s' 
elif ('[email protected]' in origin): 
    ext = '' 
    clone = 'hg clone %s %s' 
    setUpstream = 'echo "upstream = %s \n" >> .hg/hgrc' 
else: 
    print('Error: Version control system not supported.') 
    exit() 

upstream = None 
if ('github.com' in origin): 
    element = soup.find('span', class_='fork-flag') 
    if element: 
     upstreamBase = element.span.a['href'] 
     upstream = 'https://github.com' + upstreamBase + ext 
elif ('bitbucket.org' in origin): 
    element = soup.find('a', class_='fork-of') 
    if element: 
     upstreamBase = element['href'] 
     upstream = 'https://bitbucket.org' + upstreamBase + ext 
else: 
    print('Warning: Web host not supported.') 
    print('Warning: Continuing to clone without adding upstream repo.') 

print(upstream) 

subprocess.Popen(clone % (origin, destination), shell=True).wait() 
if upstream: 
    subprocess.Popen(setUpstream % upstream, cwd=destination, shell=True).wait() 
1

기본적으로 git clone은 원격 "출발지"를 호출합니다. 당신이 원하는 경우 다른 것을 사용

git clone -o upstream <path_to_repo> 

Git Clone

--origin <name> 
-o <name> 
Instead of using the remote name origin to keep track of the upstream repository, use <name>. 

편집라는 : 원격 원격을 추적 할 필요의 사건을 위해 ... 내가 할 수있는 좋은 방법을 발견 한 적이 이. git clone 주위에 래퍼를 작성할 수 있지만 각 저장소마다 하드 코딩해야합니다.

2

Git 자체와 관련하여 원래의 저장소 (즉, 사용자가 분기 한 저장소)와 포크의 복제본 간에는 아무런 관련이 없습니다. 이전 URL에 대한 정보는 후자에 포함되어 있지 않습니다. 따라서 어떤 방법으로 복제하려면 복제 후 복제본의 리모컨으로 원래 repo를 추가해야합니다 ().

, 당신은 당신이에서 갈래의 각 프로젝트에 대한 별명 정의 할 수 있습니다 것들을 작은 비트를 자동화하고 URL을 매번 입력 할 필요가 없도록하려면 포크를 복제 한 후

git config alias.remaddfoo "remote add upstream <URL-of-original-project-foo>" 

을, 당신은 내부 cd 할 것 새로운 복제본을 만들고 해당 별칭을 호출하는 것을 잊지 마십시오. 더

촬영 자동화 한 단계는 복제 내부 git clone 것 자동으로 cd 주위에 래퍼의 어떤 종류를 작성 포함하고 git remote add...을 실행하지만, git clone의 인수를 구문 분석 올바르게 조금 너무 복잡 할 수있다.

관련 문제