3
_Af~P                 @   s   d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
mZ d dlmZ d dlmZ d dlmZ e s|eZneZd	ZdddZdd Zdd ZG dd deZdS )    )print_functionN)InteractiveCommand)Editor)	HookErrorUploadError)
GitCommand)RepoHook)
is_python3   Fc             C   s2   | rt d nt d t d tdj }|dkS )NzHATTENTION: One or more branches has an unusually high number of commits.zAATTENTION: You are uploading an unusually high number of commits.zFYOU PROBABLY DO NOT MEAN TO DO THIS. (Did you rebase across branches?)z3If you are sure you intend to do this, type 'yes': yes)printinputstrip)multiple_branchesanswer r   @/home/qytech/356xLinux510-101server/.repo/repo/subcmds/upload.py_ConfirmManyUploads%   s    
r   c             G   s(   | | }t d| tjd tjd d S )Nz	error: %s)file   )r   sysstderrexit)fmtargsmsgr   r   r   _die1   s    r   c             C   s0   g }x&| D ]}|j dd |jdD  q
W |S )Nc             S   s   g | ]}|j  qS r   )r   ).0sr   r   r   
<listcomp>:   s    z _SplitEmails.<locals>.<listcomp>,)extendsplit)valuesresultvaluer   r   r   _SplitEmails7   s    
r&   c               @   s\   e Zd ZdZdZdZdZdd Zdd Zd	d
 Z	dd Z
dd Zdd Zdd Zdd ZdS )UploadTzUpload changes for code reviewz"
%prog [--re --cc] [<project>]...
a
  
The '%prog' command is used to send changes to the Gerrit Code
Review system.  It searches for topic branches in local projects
that have not yet been published for review.  If multiple topic
branches are found, '%prog' opens an editor to allow the user to
select which branches to upload.

'%prog' searches for uploadable changes in all projects listed at
the command line.  Projects can be specified either by name, or by
a relative or absolute path to the project's local directory. If no
projects are specified, '%prog' will search for uploadable changes
in all projects listed in the manifest.

If the --reviewers or --cc options are passed, those emails are
added to the respective list of users, and emails are sent to any
new users.  Users passed as --reviewers must already be registered
with the code review system, or the upload will fail.

# Configuration

review.URL.autoupload:

To disable the "Upload ... (y/N)?" prompt, you can set a per-project
or global Git configuration option.  If review.URL.autoupload is set
to "true" then repo will assume you always answer "y" at the prompt,
and will not prompt you further.  If it is set to "false" then repo
will assume you always answer "n", and will abort.

review.URL.autoreviewer:

To automatically append a user or mailing list to reviews, you can set
a per-project or global Git option to do so.

review.URL.autocopy:

To automatically copy a user or mailing list to all uploaded reviews,
you can set a per-project or global Git option to do so. Specifically,
review.URL.autocopy can be set to a comma separated list of reviewers
who you always want copied on all uploads with a non-empty --re
argument.

review.URL.username:

Override the username used to connect to Gerrit Code Review.
By default the local part of the email address is used.

The URL must match the review URL listed in the manifest XML file,
or in the .git/config within the project.  For example:

  [remote "origin"]
    url = git://git.example.com/project.git
    review = http://review.example.com/

  [review "http://review.example.com/"]
    autoupload = true
    autocopy = johndoe@company.com,my-team-alias@company.com

review.URL.uploadtopic:

To add a topic branch whenever uploading a commit, you can set a
per-project or global Git option to do so. If review.URL.uploadtopic
is set to "true" then repo will assume you always want the equivalent
of the -t option to the repo command. If unset or set to "false" then
repo will make use of only the command line option.

review.URL.uploadhashtags:

To add hashtags whenever uploading a commit, you can set a per-project
or global Git option to do so. The value of review.URL.uploadhashtags
will be used as comma delimited hashtags like the --hashtags option.

# References

Gerrit Code Review:  https://www.gerritcodereview.com/

c          
   C   s  |j ddddd |j dddd	g d
d |j ddddd |j dddd	ddd |j ddd	ddd |j dddddd |j ddddd d |j d!d"dd#d$d%d& |j d'd(d)d*d+d,d& |j d-d.dd/d$d0d& |j d1d2dd3d$d4d& |j d5d6dd	d7g d8d9 |j d:d;d<ddd=d>d?d@ |j dAdBdCd$ddDdE |j dFdGd$ddHdI |j dJdKd)d+dLd |jdM}|j dNdOddPd |j dQdRddSd |j dTdUddVd d S )WNz-t
auto_topic
store_truez,Send local branch name to Gerrit Code Review)destactionhelpz	--hashtagz--hthashtagsappendz-Add hashtags (comma delimited) to the review.)r*   r+   defaultr,   z--hashtag-branchz--htbz#Add local branch name as a hashtag.)r+   r,   z--rez--reviewersstring	reviewersz"Request reviews from these people.)typer+   r*   r,   z--ccccz)Also send email to these email addresses.z--brstorebranchzBranch to upload.z--cbrz--current-branchcurrent_branchzUpload current git branch.z-dz--draftdraftFz If specified, upload as a draft.)r+   r*   r/   r,   z--nez--no-emailsstore_falsenotifyTz+If specified, do not send emails on upload.z-pz	--privateprivatez)If specified, upload as a private change.z-wz--wipwipz2If specified, upload as a work-in-progress change.z-oz--push-optionpush_optionsz#Additional push options to transmit)r2   r+   r*   r/   r,   z-Dz--destinationz--destdest_branchBRANCHz(Submit for review on this target branch.)r2   r+   r*   metavarr,   z-nz	--dry-rundryrunz,Do everything except actually upload the CL.)r*   r/   r+   r,   z-yz--yeszAnswer yes to all safe prompts.)r/   r+   r,   z--no-cert-checksvalidate_certsz%Disable verifying ssl certs (unsafe).zUpload hooksz--no-verifybypass_hookszDo not run the upload hook.z--verifyallow_all_hooksz&Run the upload hook without prompting.z--ignore-hooksignore_hooksz,Do not abort uploading if upload hooks fail.)
add_optionadd_option_group)selfpgr   r   r   _Options   sx    

zUpload._Optionsc             C   sT  |j }|j}|j|j}d|j }|jj|}|dkrBtd|  |d kr|j}	|j	}
|j
ph|j
ph|j}td|j||jr~dndf  td|t|
t|
dkrd	pd|	f  x|
D ]}td
|  qW td|j dd tjj  |jrtd d}ntjj j j }|dk}|r0t|j	tkr0t }|rH| j||g| ntd d S )Nzreview.%s.autouploadFzupload blocked by %s = falsez)Upload project %s/ to remote branch %s%s:z (draft) z  branch %s (%2d commit%s, %s):r   r   z         %szto %s (y/N)? )endz<--yes>Tyr   1truetzupload aborted by user)rM   r   rN   rO   rP   )projectname	GetBranchremotereviewconfig
GetBooleanr   datecommitsr=   revisionExprr   relpathr7   lenr   stdoutflushr   stdinreadliner   lowerUNUSUAL_COMMIT_THRESHOLDr   _UploadAndReport)rG   optr5   peoplerQ   rR   rT   keyr   rX   commit_listdestinationcommitr   r   r   _SingleBranch   sB    




zUpload._SingleBranchc          	   C   s4  i }i }g }|j d x|D ]\}}|j d |j d|j  i }	x|D ]}
|
d krVqH|
j}|
j}|
j}|	rv|j d |jp|jp|j}|j d|t|t|dkrdpd||f  x|D ]}|j d|  qW |
|	|< qHW |||j< |	||j< qW |j d tj	d	j
|jd	}tjd
}tjd}d }g }x|D ]}|j|}|rr|jd}|j|}|s6td| q6|j|}|r6|jd}|std| ||j j|}
|
std||j |j |
 q6W |std d}x$|D ]}
t|
jtkrd}P qW |r"tdds"td | j||| d S )Nz## Uncomment the branches to upload:#z# project %s/:z4#  branch %s (%2d commit%s, %s) to remote branch %s:r   r   rK   z#         %s
z^#?\s*project\s*([^\s]+)/:$z^\s*branch\s*([^\s(]+)\s*\(.*z#project %s not available for uploadz#project for branch %s not in scriptzbranch %s not in %sznothing uncommented for uploadFT)r   zupload aborted by user)r.   r[   rR   rX   rY   r=   rZ   r\   r   Z
EditStringjoinr"   recompilematchgroupgetr   rb   r   rc   )rG   rd   pendingre   projectsbranchesscriptrQ   availbr5   rR   rX   rg   rh   ri   Z
project_reZ	branch_retodolinemZmany_commitsr   r   r   _MultipleBranches  s|    


















zUpload._MultipleBranchesc             C   s   |j }|j}d|j|jj }|jj|}|dk	rP|d jdd |jdD  d|j|jj }|jj|}|dk	rt	|d dkr|d jd	d |jdD  dS )
z
    Appends the list of reviewers in the git project's config.
    Appends the list of users in the CC list in the git project's config if a
    non-empty reviewer list was found.
    zreview.%s.autoreviewerNr   c             S   s   g | ]}|j  qS r   )r   )r   entryr   r   r   r   d  s    z*Upload._AppendAutoList.<locals>.<listcomp>r    zreview.%s.autocopyr   c             S   s   g | ]}|j  qS r   )r   )r   r}   r   r   r   r   i  s    )
rR   rQ   rS   rT   rU   rV   	GetStringr!   r"   r\   )rG   r5   re   rR   rQ   rf   Zraw_listr   r   r   _AppendAutoListX  s    zUpload._AppendAutoListc             C   sT   |j j|j}|d krdS |j }y|j|jdd S  ttfk
rN   dS X d S )NrK   /   )rQ   WasPublishedrR   GetPublishedRefsrr   r"   AttributeError
IndexError)rG   r5   Zlast_pubrefsr   r   r   _FindGerritChangek  s    zUpload._FindGerritChangec             C   s,  d}xP|D ]F}y
t j|}| j|| |jj }|rd|jjj }|jjj|}	|	d krt	  t	d|jj
  t	dj| t	ddd tjj  |jrt	d d	}
ntjj j j }
|
d$krt	dtjd d|_d|_w|jdk	rd|jjj }|jjj||_dd }d|jjj }t||jjj|}x|jD ]}|j|| q@W |jrl|j|j
 |jpz|jj}| j|j}|rd| }|j r|r||krt	d||f  t	d t	d|  d|_w|j ||j!|j||j"|j#|j$r d nd|j%||j&|j'd d|_W q t(k
rR } z||_d|_d}W Y d d }~X qX qW t	tjd t	dtjd |rx`|D ]X}|js~t)t*|jdkrd}nd}t	d | |jj+d! |j
t*|jf tjd q~W t	  x6|D ].}|jrt	d"|jj+d! |j
f tjd qW |r(tj,d# d S )%NFzreview.%s.autouploadz5Uncommitted changes in %s (did you forget to amend?):rl   zContinue uploading? (y/N) rK   )rL   z<--yes>r   rM   rP   rO   onzskipping upload)r   zUser abortedTzreview.%s.uploadtopicc             s   s2   | sdS x$| j dD ]}|j }|r|V  qW dS )z+Split |value| up into comma delimited tags.Nr    )r"   r   )r%   tagr   r   r   _ExpandHashtag  s    z/Upload._UploadAndReport.<locals>._ExpandHashtagzreview.%s.uploadhashtagszrefs/heads/%sz4merge branch %s does not match destination branch %szskipping upload.z4Please use `--destination %s` if this is intentionalNONE)
r@   r(   r-   r7   r:   r9   r;   r=   rA   r<   zF----------------------------------------------------------------------   z (%s)z
       (%s)z[FAILED] %-15s %-15sr   z[OK    ] %-15s %sr   )rM   r   rP   rO   r   )-copydeepcopyr   rQ   UncommitedFilesrT   rU   rV   rW   r   rR   rm   r   r]   r^   r   r_   r`   r   ra   r   Zuploadederrorr(   setr~   r-   updateZhashtag_branchaddr=   _GetMergeBranchUploadForReviewr@   r7   r:   r9   r;   rA   r<   r   r\   strr[   r   )rG   rd   ry   Zoriginal_peopleZhave_errorsr5   re   changesrf   r   ar   r-   r   rh   merge_branchZ	full_dester   r   r   r   rc   w  s    








zUpload._UploadAndReportc             C   sX   t |dddgddd}|j  |jj }t |ddd| gddd}|j  |jj }|S )	Nz	rev-parsez--abbrev-refHEADT)capture_stdoutcapture_stderrrV   z--getzbranch.%s.merge)r   Waitr]   r   )rG   rQ   rH   Zlocal_branchr   r   r   r   r     s    

zUpload._GetMergeBranchc             C   s(  | j |}g }g }g }d }|jr&|j}xf|D ]^}|jrn|j}	|j|	}
|
rR|
g}qxd }tdt|	 tjd n
|j	|}|r,|j
||f q,W |s|d krtdtjd ntd|f tjd dS |jstd| jj| jj| jjjdjdd	}d
d |D }dd |D }d}y|j|j||d W n` tk
rJ   d}|jsF Y n> tk
r } z d}tdt| tjd W Y d d }~X nX |s|jrtdtjd nd S |jrt|j}|jrt|j}||f}t|dkrt|d d dkr| j||d d d | n| j||| d S )Nz~ERROR: Current branch (%s) not uploadable. You may be able to type "git branch --set-upstream-to m/master" to fix your branch.)r   z)repo: error: no branches ready for uploadz4repo: error: no branches named "%s" ready for uploadr   z
pre-uploadoriginT)abort_if_user_deniesc             S   s   g | ]\}}|j qS r   )rR   )r   rQ   	availabler   r   r   r   !  s    z"Upload.Execute.<locals>.<listcomp>c             S   s   g | ]\}}|j qS r   )worktree)r   rQ   r   r   r   r   r   "  s    )project_listZworktree_listFz	ERROR: %sz9
WARNING: pre-upload hooks failed, but uploading anyways.r   )GetProjectsr5   r6   CurrentBranchGetUploadableBranchr   r   r   r   GetUploadableBranchesr.   rB   r   manifestZrepo_hooks_projecttopdirmanifestProject	GetRemoteurlRunrC   
SystemExitrD   r   r1   r&   r3   r\   rj   r|   )rG   rd   r   r   rs   r1   r3   r5   rQ   ZcbrZ	up_branchrw   hookZpending_proj_namesZpending_worktreesZpassedr   re   r   r   r   Execute  sr    





(

$zUpload.ExecuteN)__name__
__module____qualname__commonhelpSummary	helpUsagehelpDescriptionrJ   rj   r|   r   r   rc   r   r   r   r   r   r   r'   >   s   LO-Krr'   )F)
__future__r   r   rn   r   commandr   editorr   r   r   r   git_commandr   rQ   r   	pyversionr	   	raw_inputr   r   unicoderb   r   r   r&   r'   r   r   r   r   <module>   s"   
