
    7j                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddZ	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zd ZddZd Zy)z)Functions for working with the QMK repo.
    )DEVNULL)Path)cli)QMK_FIRMWAREc                    g d}| dk7  rt        d      | z  } |dk7  r| |z  }t        |      j                         rt        j                  |t        |       }|j
                  dk(  r|j                  j                         S t        j                  j                  ddj                  |       d|j
                          t        |j                         y	y	)
z9Returns the current git version for a repo, or None.
    )gitdescribez
--abbrev=6z--dirtyz--always--tags.lib)stdincwdr   " z" returned error code N)r   existsr   runr   
returncodestdoutstriplogwarningjoinprintstderr)repo_dir	check_dirgit_describe_cmdgit_describes       @/home/hanthebot/chat/keyboard/qmk_firmware/lib/python/qmk/git.pygit_get_versionr       s     Z3;)Cy(	Iww/wHM""a'&&,,.. GGOOa)9 :;;QR^RiRiQjkl,%%&    c                      t        j                  g d      } | j                  dk(  r'| j                  r| j                  j	                         S yy)z7Retrieves user's username from Git config, if set.
    )r   configz--getz	user.namer   Nr   r   r   r   r   )git_usernames    r   git_get_usernamer&   $   sF     77BCL!#(;(;""((** )<#r!   c                      t        j                  g d      } | j                  dk7  r| j                  st        j                  g d      } | j                  dk(  r| j                  j	                         S y)z4Returns the current branch for a repo, or None.
    )r   branchz--show-currentr   )r   	rev-parsez--abbrev-refHEADNr$   )
git_branchs    r   git_get_branchr,   -   s`     <=J  A%Z->->WWIJ
!  &&(( "r!   c                      t        j                  g d      } | j                  dk(  r| j                  j	                         S y)z1Returns the current tag for a repo, or None.
    )r   r	   z
--abbrev=0r
   r   Nr$   )git_tags    r   git_get_tagr/   9   s9     ggABGQ~~##%% r!   c           	          t        j                  dddddd| g      }|j                  dk(  r'|j                  r|j                  j	                         S yy)	zARetrieves the last log entry for the branch being worked on.
    r   
--no-pagerr   --pretty=format:%ad (%h) -- %s
--date=iso-n1r   Nr$   )branch_namegit_lastlogs     r   git_get_last_log_entryr7   A   sX     ''5,7WYeglnyz{K"{'9'9!!'')) (:"r!   c           
      
   t        j                  dd| |g      }t        j                  dddddd|j                  j                         g      }|j                  dk(  r'|j                  r|j                  j                         S y	y	)
zIRetrieves the common ancestor between for the two supplied branches.
    r   z
merge-baser1   r   r2   r3   r4   r   N)r   r   r   r   r   )branch_abranch_bgit_merge_basegit_branchpoint_logs       r   git_get_common_ancestorr=   J   s     WWe\8XFGN''5,?_amot  wE  wL  wL  wR  wR  wT  #U  V%%*/B/I/I"))//11 0J*r!   c                  ,   i } g d}g d}t        j                  |      }|j                  dk(  re|j                  j	                         D ]H  }t        j                  g ||      }d|j                  j                         i| |j                         <   J | S )z,Returns the current remotes for a repo.
    )r   remoteshow)r   r?   zget-urlr   url)r   r   r   r   
splitlinesr   )remotesgit_remote_show_cmdgit_remote_get_cmdgit_remote_shownamegit_remote_names         r   git_get_remotesrI   T   s     G35gg12O!!Q&#**557 	LD!gg&A(:&AD&ABO%*O,B,B,H,H,J$KGDJJL!	L Nr!   c                      g d} g | d}t        j                  |       }t        j                  |      }|j                  dk7  xs |j                  dk7  S )z.Returns 1 if repo is dirty, or 0 if clean
    )r   diffz--quietz--cachedr   )r   r   r   )git_diff_staged_cmdgit_diff_unstaged_cmdunstagedstageds       r   git_is_dirtyrP   e   sZ     5>1>:>ww*+HWW*+F!#=v'8'8A'==r!   c                  4    t         dz  } | j                         S )zChecks that the .git directory exists inside QMK_HOME.

    This is a decent enough indicator that the qmk_firmware directory is a
    proper Git repository, rather than a .zip download from GitHub.
    z.git)r   is_dir)dot_git_dirs    r   git_check_reporT   q   s     'Kr!   c           	          t        j                  ddd| g       t        j                  dddd|  d|  g      }t        |j                        S )z-Return True if branch has custom commits
    r   fetchupstreamr1   r   z	upstream/z...)r   r   boolr   )active_branch
deviationss     r   git_check_deviationr[   |   sQ     GGUGZ78%u	-PSTaSb6cdeJ
%%&&r!   c           	          t        j                  dddddd| g      }|j                  dk7  rg S |j                  j	                         j                         S )zLReturn a list of files that would be captured by the current .gitignore
    r   zls-filesz-cz-oz-iz--exclude-from=.gitignorer   )r   r   r   r   r   rB   )r   invalids     r   git_get_ignored_filesr^      sR     gguj$d<WYbcdGQ	>>!,,..r!   c                      t        j                  g d      } | j                  dk7  ry | j                  j	                         S )N)r   r)   z--shortr*   r   r$   )outputs    r   git_get_qmk_hashra      s5    WW<=FA==  r!   N)r   r   )r   )__doc__
subprocessr   pathlibr   milcr   qmk.constantsr   r    r&   r,   r/   r7   r=   rI   rP   rT   r[   r^   ra    r!   r   <module>rh      sR       &2+	)&*2"	> '/!r!   