πŸ“œ ⬆️ ⬇️

Draw commits on Github

[Friday]


Always wanted to make my own user profile activity graph on GitHub. For example, spread commits every day so that in a year this chart would turn into some kind of picture, albeit with a size limit of 52 Γ— 7 squares-pixels (52 weeks a year Γ— 7 days a week).


The problem was that even with the full automation of the process, it would still be to wait a whole year. And then I read the Gitkhab documentation and realized that the problem is solved easier and moreover - at one time. So, it is necessary to do without delay. Usually it is difficult to invent titles for projects, but here it has come by itself. Kai painted with ice and Gerd draws with commits !


Graph of commits on Gitkhab as a picture


How it works


The currently existing (intentional?) Behavior of GitHub is used when constructing the activity graph, taking into account the local commit dates provided by the client. You can learn more about this topic in the service help .


And since Gitkhab trusts the local dates specified in the commit, you can send him as many commits with any dates you like:


echo "   ,   +3!" >> gerda.md git add gerda.md git commit -m "   " --date="0001-01-01T00:00:00+0300" 

Thus, our task is to automate the very process of creating commits with the necessary dates. Simply put, you need to write a script that generates a script that creates the necessary commits. Along the way, you still need to remember that the profile schedule uses weeks on Sunday, not on Monday, the first day.


It is best for scripts to feed images in a more or less human-readable format, for example, an array of strings or, in general, real image files. I made lines for simplicity of parsing. The githab displays five different shades of color in a graphic; the darker the shade, the more active the user is on a given day. Take for example three:



For example, let's write the word β€œPRIVET” (use an asterisk as anti-aliasing for β€œsmooth” transitions):


 $commits = [ /* columns ' 10β†’| 20β†’| 30β†’| 40β†’| 50β†’| ' ← exactly 52 characters */ ' ####### #####* # # ##### ###### ####### ', // Sun ' # # # # # ## # # # # ', // Mon ' # # # # # # # # * # # ', // Tue ' # # #####* # # # ##### ##### # ', // Wed ' # # # # # # # # # # ', // Thu ' # # # ## # # # # # ', // Fri ' # # # # # #####* ###### # ', // Sat ]; 

It is better to leave a couple of weeks (spaces) to the left and to the right - so that the current activity is less interfered with.


Now the most difficult thing is to parse this array, correlating each character in it with some particular day. I did this:


 /** *   . * * @param string[] $map       7   52   * @param \DateTime $firstSunday      * @return array */ function generateCommits($map, $firstSunday) { $commits = []; $count = 7 * 52; $date = clone $firstSunday; //      ,      for ($day = 0, $weekDay = 0; $day < $count; $day++) { $week = intval($day / 7); $char = substr($map[$weekDay], $week, 1); if ($char !== ' ') { $commits[$date->format('Ym-d')] = $char === '#' ? 20 : 10; } //     $date->add(new DateInterval('P1D')); $weekDay = ($weekDay + 1) % 7; } return $commits; } 

As a result, we obtain an associative array (dictionary), where the key is the day of the week, and the value is the number of commits needed on that day:


 $commits = [ // ... '2016-01-31' => 10, '2016-02-01' => 20, '2016-02-02' => 20, '2016-02-03' => 10, // ... ] 

Then everything is quite simple: we go through this dictionary and write the required number of commits for each day in the resulting shell script (for the time zone I used Moscow time). As a test file, we use a marcaun document, in the text of which with each commit we add a day or some other information (I have a bulleted list with the number of the commit on that day):


 git init echo "# Gerda" > gerda.md echo "\n## 2016-01-31" >> gerda.md echo "* Gerda β„–1" >> gerda.md git add gerda.md git commit -m "Gerda β„–1" --date="2016-01-31T12:00:00+0300" # ...  10  ... echo "\n## 2016-02-01" >> gerda.md echo "* Gerda β„–1" >> gerda.md git add gerda.md git commit -m "Gerda β„–1" --date="2016-02-01T12:00:00+0300" # ...  20  ... echo "\n## 2016-02-02" >> gerda.md echo "* Gerda β„–1" >> gerda.md git add gerda.md git commit -m "Gerda β„–1" --date="2016-02-02T12:00:00+0300" # ...  20  ... echo "\n## 2016-02-03" >> gerda.md echo "* Gerda β„–1" >> gerda.md git add gerda.md git commit -m "Gerda β„–1" --date="2016-02-03T12:00:00+0300" # ...  10  ... 

Programming logic is ready. We'll take out all the necessary settings in one settings.php file to separate the data from the logic:


 // Streak graph picture $commits = [ ... ]; // Repository origin $origin = 'https://github.com/maximal/gerda.git'; // Output shell file $commandFile = 'repo' . DIRECTORY_SEPARATOR . 'make-commits.sh'; 

Of course, in order for Githab to understand from which user commits were made, in the Gita settings you should have your login and mailbox:


 # ~/.gitconfig [user] name = my_github_username email = my_github_email@ya.ru ### ... 

We come up with the activity schedule we need. We write the desired picture in the settings. Run what happened:


Validation


Oh, Friday for joy jumped out for a limit of 52 characters. Let's correct:


Generating a script with commits


The commit script has been created, run it:


 cd repo ./make-commits.sh 

At the end of the script (as a rule, there are a couple of thousand commits: you have to wait a minute), Github will ask for your password. Entering passwords into other people's (and sometimes into your own) scripts is not cool, so if you want, you can complete the script in this step and then git push yourself (in the generated script, honestly, honestly, only the git push command):


 git remote add origin https://github.com/<MY>/<REPO>.git git push -u origin master -f 

We are waiting for a couple of minutes (it is reasonable to assume that this data is cached by Github), we are updating the profile page.


Is done.


Links


All source code and its description are on (surprise!) Github: https://github.com/sijeko/gerda
There will be pullwalks - go to the light.


Live picture of activity on the example of my profile: https://github.com/maximal


Minuses


What the program does not know how, and what can be improved:



')

Source: https://habr.com/ru/post/319298/


All Articles